⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.java

📁 Java实现的各种数学算法
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// matrix.java jplewis 99// modified// jan03	minor// dec01	scruff	// nov01	scruff	convenience methods// feb01	scruff  printFull// dec00	scruff	floatClone/doubleClone methods// nov00	scruff// sep00	switch from cstdio.printf to numberformat// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Library General Public// License as published by the Free Software Foundation; either// version 2 of the License, or (at your option) any later version.// // This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU// Library General Public License for more details.// // You should have received a copy of the GNU Library General Public// License along with this library; if not, write to the// Free Software Foundation, Inc., 59 Temple Place - Suite 330,// Boston, MA  02111-1307, USA.//// contact info:  zilla@computer.orgpackage ZS;import java.io.*;import java.text.NumberFormat;import zlib.*;final public class matrix{  // beware _stdout does not get flushed the same way/time as system.out.  public static PrintWriter  _stdout = new PrintWriter(System.out);  public static NumberFormat _nf;  public static NumberFormat _if;	// for ints  static {    _nf = NumberFormat.getInstance();    _nf.setMinimumFractionDigits(3);    _nf.setMaximumFractionDigits(3);    _nf.setGroupingUsed ( false );	// do not do 15,000  (no comma)    // for integers    _if = NumberFormat.getInstance();    _if.setMinimumFractionDigits(0);    _if.setMaximumFractionDigits(0);  }  /**   */  public static void setFractionDigits(int n)  {    _nf.setMinimumFractionDigits(n);    _nf.setMaximumFractionDigits(n);  }  //----------------------------------------------------------------  // matrix.  int,float,double  //----------------------------------------------------------------  /**   */  public static void print(String msg, int[][] M)  {    print(_stdout, msg, M);    _stdout.flush();  }  /**   * TODO: does printing int with _if work?   */  public static void print(PrintWriter f, String msg, int[][] M)  {    int nr = M.length;    int nc = M[0].length;    f.println(msg);    for( int ir=0; ir < nr; ir++ ) {      f.print("[ ");      if (nc <= 10)   	for(int ic=0; ic<nc; ic++) {	  f.print(_if.format(M[ir][ic]) + "  ");	}	else { /* elide */	  for( int ic=0; ic < 4; ic++ ) 	    f.print(_if.format(M[ir][ic]) + "  ");	  f.print(" ... ");	  for( int ic=nc-2; ic < nc; ic++ ) 	    f.print(_if.format(M[ir][ic]) + "  ");	}      f.println("]");    }    f.flush();  } //print(int)  //----------------------------------------------------------------  /**   */  public static void print(String msg, float[][] M)  {    print(_stdout, msg, M);    _stdout.flush();  }  /**   */  public static void print(PrintWriter f, String msg, float[][] M)  {    int nr = M.length;    int nc = M[0].length;    f.println(msg);    for( int ir=0; ir < nr; ir++ ) {      f.print("[ ");      if (nc <= 10)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(M[ir][ic]) + "  ");	}	else { /* elide */	  for( int ic=0; ic < 4; ic++ ) 	    f.print(_nf.format(M[ir][ic]) + "  ");	  f.print(" ... ");	  for( int ic=nc-2; ic < nc; ic++ ) 	    f.print(_nf.format(M[ir][ic]) + "  ");	}      f.println("]");    }    f.flush();  } //print  //----------------------------------------------------------------  /**   */  public static void print(String msg, double[][] M)  {    print(_stdout, msg, M);    _stdout.flush();  }  /**   */  public static void print(PrintWriter f, String msg, double[][] M)  {    int nr = M.length;    int nc = M[0].length;    f.println(msg);    for( int ir=0; ir < nr; ir++ ) {      f.print("[ ");      if (nc <= 10)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(M[ir][ic]) + "  ");	}      else { /* elide */	for( int ic=0; ic < 4; ic++ ) 	  f.print(_nf.format(M[ir][ic]) + "  ");	f.print(" ... ");	for( int ic=nc-2; ic < nc; ic++ ) 	  f.print(_nf.format(M[ir][ic]) + "  ");      }      f.println("]");    }    f.flush();  } //print  //----------------------------------------------------------------  // vector.  short,int,float,double  //----------------------------------------------------------------  /**   */  public static void print(String msg, short[] v)  {    print(_stdout, msg, v);    _stdout.flush();  }  /**   */  public static void print(PrintWriter f, String msg, short[] v)  {    int n = v.length;    f.println(msg);    f.print("[ ");    if (n <= 10)         for(int ic=0; ic<n; ic++) {	f.print(_if.format(v[ic]) + "  ");      }    else { /* elide */      for( int ic=0; ic < 4; ic++ ) 	f.print(_if.format(v[ic]) + "  ");      f.print(" ... ");      for( int ic=n-2; ic < n; ic++ ) 	f.print(_if.format(v[ic]) + "  ");    }    f.println("]");    f.flush();  } //print(short vector)  //----------------------------------------------------------------  /**   */  public static void print(String msg, int[] v)  {    print(_stdout, msg, v);    _stdout.flush();  }  /**   */  public static void print(PrintWriter f, String msg, int[] v)  {    int n = v.length;    f.println(msg);    f.print("[ ");    if (n <= 10)         for(int ic=0; ic<n; ic++) {	f.print(_if.format(v[ic]) + "  ");      }    else { /* elide */      for( int ic=0; ic < 4; ic++ ) 	f.print(_if.format(v[ic]) + "  ");      f.print(" ... ");      for( int ic=n-2; ic < n; ic++ ) 	f.print(_if.format(v[ic]) + "  ");    }    f.println("]");    f.flush();  } //print(int vector)  //----------------------------------------------------------------  /**   */  public static void print(String msg, float[] v)  {    print(_stdout, msg, v);    _stdout.flush();  }  /**   */  public static void print(PrintWriter f, String msg, float[] v)  {    int n = v.length;    f.println(msg);    f.print("[ ");    if (n <= 10)         for(int ic=0; ic<n; ic++) {	f.print(_nf.format(v[ic]) + "  ");      }    else { /* elide */      for( int ic=0; ic < 4; ic++ ) 	f.print(_nf.format(v[ic]) + "  ");      f.print(" ... ");      for( int ic=n-2; ic < n; ic++ ) 	f.print(_nf.format(v[ic]) + "  ");    }    f.println("]");    f.flush();  } //print(float vector)  //----------------------------------------------------------------  /**   */  public static void print(String msg, double[] v)  {    print(_stdout, msg, v);    _stdout.flush();  }  /**   */  public static void print(PrintWriter f, String msg, double[] v)  {    int n = v.length;    f.println(msg);    f.print("[ ");    if (n <= 10)         for(int ic=0; ic<n; ic++) {	f.print(_nf.format(v[ic]) + "  ");      }    else { /* elide */      for( int ic=0; ic < 4; ic++ ) 	f.print(_nf.format(v[ic]) + "  ");      f.print(" ... ");      for( int ic=n-2; ic < n; ic++ ) 	f.print(_nf.format(v[ic]) + "  ");    }    f.println("]");    f.flush();  } //print(double vector)  //----------------------------------------------------------------  /**   * print rows of A, B side by side for comparison   */  public static void printCompare(PrintWriter f, String msg,				  double[][] A, double[][] B)  {    int nr = A.length;    int nc = A[0].length;    zliberror._assert(B.length == A.length);    zliberror._assert(B[0].length == A[0].length);    f.println(msg);    for( int ir=0; ir < nr; ir++ ) {      f.print("[ ");      if (nc <= 5)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(A[ir][ic]) + "  ");	}      else { /* elide */	for( int ic=0; ic < 4; ic++ ) 	  f.print(_nf.format(A[ir][ic]) + "  ");	f.print(" ... ");	for( int ic=nc-2; ic < nc; ic++ ) 	  f.print(_nf.format(A[ir][ic]) + "  ");      }      f.print("] vs. [");      if (nc <= 5)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(B[ir][ic]) + "  ");	}      else { /* elide */	for( int ic=0; ic < 4; ic++ ) 	  f.print(_nf.format(B[ir][ic]) + "  ");	f.print(" ... ");	for( int ic=nc-2; ic < nc; ic++ ) 	  f.print(_nf.format(B[ir][ic]) + "  ");      }      f.println("]");    }    f.flush();  } //printCompare  //----------------------------------------------------------------  /**   * print rows of A, B side by side for comparison   */  public static void printCompare(PrintWriter f, String msg,				  float[][] A, float[][] B)  {    int nr = A.length;    int nc = A[0].length;    zliberror._assert(B.length == A.length);    zliberror._assert(B[0].length == A[0].length);    f.println(msg);    for( int ir=0; ir < nr; ir++ ) {      f.print("[ ");      if (nc <= 5)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(A[ir][ic]) + "  ");	}      else { /* elide */	for( int ic=0; ic < 4; ic++ ) 	  f.print(_nf.format(A[ir][ic]) + "  ");	f.print(" ... ");	for( int ic=nc-2; ic < nc; ic++ ) 	  f.print(_nf.format(A[ir][ic]) + "  ");      }      f.print("] vs. [");      if (nc <= 5)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(B[ir][ic]) + "  ");	}      else { /* elide */	for( int ic=0; ic < 4; ic++ ) 	  f.print(_nf.format(B[ir][ic]) + "  ");	f.print(" ... ");	for( int ic=nc-2; ic < nc; ic++ ) 	  f.print(_nf.format(B[ir][ic]) + "  ");      }      f.println("]");    }    f.flush();  } //printCompare  //----------------------------------------------------------------  /**   * print rows of A, B side by side for comparison.   * float vs. double version.   */  public static void printCompare(PrintWriter f, String msg,				  float[][] A, double[][] B)  {    int nr = A.length;    int nc = A[0].length;    zliberror._assert(B.length == A.length);    zliberror._assert(B[0].length == A[0].length);    f.println(msg);    for( int ir=0; ir < nr; ir++ ) {      f.print("[ ");      if (nc <= 5)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(A[ir][ic]) + "  ");	}      else { /* elide */	for( int ic=0; ic < 4; ic++ ) 	  f.print(_nf.format(A[ir][ic]) + "  ");	f.print(" ... ");	for( int ic=nc-2; ic < nc; ic++ ) 	  f.print(_nf.format(A[ir][ic]) + "  ");      }      f.print("] vs. [");      if (nc <= 5)   	for(int ic=0; ic<nc; ic++) {	  f.print(_nf.format(B[ir][ic]) + "  ");	}      else { /* elide */	for( int ic=0; ic < 4; ic++ ) 	  f.print(_nf.format(B[ir][ic]) + "  ");	f.print(" ... ");	for( int ic=nc-2; ic < nc; ic++ ) 	  f.print(_nf.format(B[ir][ic]) + "  ");      }      f.println("]");    }    f.flush();  } //printCompare float vs double  //----------------------------------------------------------------  /**   * print the whole thing to a file, for debugging.   */  public static void printFull(String msg, float[][] A, String path)    throws IOException  {    PrintWriter f      = (path.equals("stdout")	 ? _stdout	 : new PrintWriter(new BufferedWriter(new FileWriter(path))));    f.print(msg);    int yres = A.length;    int xres = A[0].length;    for( int y=0; y < yres; y++ ) {            for( int x=0; x < xres; x++ ) {	if (x%7 == 0) {	  f.println("");	  int end = ((x+6) >= xres) ? (xres-1) : (x+6);	  f.print("row "+y+" "+x+".."+end+": ");	}	f.print(_nf.format(A[y][x]) + "  ");	      }      f.println("");    } //y        if (f == _stdout)      f.flush();    else       f.close();  } //printFull  /**   * print the whole thing to a file, for debugging.   */  public static void printFull(String msg, double[][] A, String path)    throws IOException  {    PrintWriter f      = (path.equals("stdout")	 ? _stdout	 : new PrintWriter(new BufferedWriter(new FileWriter(path))));    f.print(msg);    int yres = A.length;    int xres = A[0].length;    for( int y=0; y < yres; y++ ) {            for( int x=0; x < xres; x++ ) {	if (x%7 == 0) {	  f.println("");	  int end = ((x+6) >= xres) ? (xres-1) : (x+6);	  f.print("row "+y+" "+x+".."+end+": ");	}	f.print(_nf.format(A[y][x]) + "  ");	      }      f.println("");    } //y        if (f == _stdout)      f.flush();    else       f.close();  } //printFull  //----------------------------------------------------------------  /**   * print every element of a 1d float array, for detailed debugging.   */  public static void printFull(String msg, float[] v, String path)    throws IOException  {    PrintWriter f      = (path.equals("stdout")	 ? _stdout	 : new PrintWriter(new BufferedWriter(new FileWriter(path))));    f.print(msg);    int len = v.length;    for( int x=0; x < len; x++ ) {      if (x%7 == 0) {	f.println("");	int end = ((x+6) >= len) ? (len-1) : (x+6);	f.print(x+".."+end+": ");      }      f.print(_nf.format(v[x]) + "  ");	    } //x    f.println("");    if (f == _stdout)      f.flush();    else       f.close();  } //printFull  /**   * print every element of a 1d double array, for detailed debugging.   */  public static void printFull(String msg, double[] v, String path)    throws IOException  {    PrintWriter f      = (path.equals("stdout")	 ? _stdout	 : new PrintWriter(new BufferedWriter(new FileWriter(path))));    f.print(msg);    int len = v.length;f.println("--> v[0] = " + v[0]);    for( int x=0; x < len; x++ ) {      if (x%7 == 0) {	f.println("");	int end = ((x+6) >= len) ? (len-1) : (x+6);	f.print(x+".."+end+": ");      }      f.print(_nf.format(v[x]) + "  ");	    } //x    f.println("");    if (f == _stdout)      f.flush();    else       f.close();  } //printFull  //----------------------------------------------------------------  // dimension compare  //----------------------------------------------------------------  /** @deprecated moved to zlib.array */  public static void assertCongruent(int[] v1, int[] v2)  {    zliberror._assert(v1.length == v2.length,		     "vector length mismatch");  } //assertCongruent  /** @deprecated moved to zlib.array */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -