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

📄 testmatrix.java

📁 美国标准化组织和马里兰大学共同开发.实现java matrix包. JAMA由六个java类组成:Matrix, CholeskyDecomposition , LUDecomposition QR
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package Jama.test;
import Jama.*;
import java.io.*;
import java.util.zip.GZIPInputStream;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/** TestMatrix tests the functionality of the Jama Matrix class and associated decompositions.
<P>
Run the test from the command line using
<BLOCKQUOTE><PRE><CODE>
 java Jama.test.TestMatrix 
</CODE></PRE></BLOCKQUOTE>
Detailed output is provided indicating the functionality being tested
and whether the functionality is correctly implemented.   Exception handling
is also tested.  
<P>
The test is designed to run to completion and give a summary of any implementation errors
encountered. The final output should be:
<BLOCKQUOTE><PRE><CODE>
      TestMatrix completed.
      Total errors reported: n1
      Total warning reported: n2
</CODE></PRE></BLOCKQUOTE>
If the test does not run to completion, this indicates that there is a 
substantial problem within the implementation that was not anticipated in the test design.  
The stopping point should give an indication of where the problem exists.
**/
public class TestMatrix {
   public static void main (String argv[]) {
      Matrix A,B,C,Z,O,I,R,S,X,SUB,M,T,SQ,DEF,SOL;
      // Uncomment this to test IO in a different locale.
      // Locale.setDefault(Locale.GERMAN);
      int errorCount=0;
      int warningCount=0;
      double tmp, s;
      double[] columnwise = {1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.};
      double[] rowwise = {1.,4.,7.,10.,2.,5.,8.,11.,3.,6.,9.,12.};
      double[][] avals = {{1.,4.,7.,10.},{2.,5.,8.,11.},{3.,6.,9.,12.}};
      double[][] rankdef = avals;
      double[][] tvals =  {{1.,2.,3.},{4.,5.,6.},{7.,8.,9.},{10.,11.,12.}};
      double[][] subavals = {{5.,8.,11.},{6.,9.,12.}};
      double[][] rvals = {{1.,4.,7.},{2.,5.,8.,11.},{3.,6.,9.,12.}};
      double[][] pvals = {{4.,1.,1.},{1.,2.,3.},{1.,3.,6.}};
      double[][] ivals = {{1.,0.,0.,0.},{0.,1.,0.,0.},{0.,0.,1.,0.}};
      double[][] evals = 
         {{0.,1.,0.,0.},{1.,0.,2.e-7,0.},{0.,-2.e-7,0.,1.},{0.,0.,1.,0.}};
      double[][] square = {{166.,188.,210.},{188.,214.,240.},{210.,240.,270.}};
      double[][] sqSolution = {{13.},{15.}};
      double[][] condmat = {{1.,3.},{7.,9.}};
      int rows=3,cols=4;
      int invalidld=5;/* should trigger bad shape for construction with val */
      int raggedr=0; /* (raggedr,raggedc) should be out of bounds in ragged array */
      int raggedc=4; 
      int validld=3; /* leading dimension of intended test Matrices */
      int nonconformld=4; /* leading dimension which is valid, but nonconforming */
      int ib=1,ie=2,jb=1,je=3; /* index ranges for sub Matrix */
      int[] rowindexset = {1,2}; 
      int[] badrowindexset = {1,3}; 
      int[] columnindexset = {1,2,3};
      int[] badcolumnindexset = {1,2,4};
      double columnsummax = 33.;
      double rowsummax = 30.;
      double sumofdiagonals = 15;
      double sumofsquares = 650;

/** 
      Constructors and constructor-like methods:
         double[], int
         double[][]  
         int, int
         int, int, double
         int, int, double[][]
         constructWithCopy(double[][])
         random(int,int)
         identity(int)
**/

      print("\nTesting constructors and constructor-like methods...\n");
      try{  
         /** check that exception is thrown in packed constructor with invalid length **/
         A = new Matrix(columnwise,invalidld);
         errorCount = try_failure(errorCount,"Catch invalid length in packed constructor... ",
                     "exception not thrown for invalid input");
      } catch ( IllegalArgumentException e ) {
         try_success("Catch invalid length in packed constructor... ",
                     e.getMessage());
      }
      try{ 
         /** check that exception is thrown in default constructor
             if input array is 'ragged' **/
         A = new Matrix(rvals);
         tmp = A.get(raggedr,raggedc);
      } catch ( IllegalArgumentException e ) {
         try_success("Catch ragged input to default constructor... ",
                      e.getMessage());
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         errorCount = try_failure(errorCount,"Catch ragged input to constructor... ",
                     "exception not thrown in construction...ArrayIndexOutOfBoundsException thrown later");
      }
      try{ 
         /** check that exception is thrown in constructWithCopy
             if input array is 'ragged' **/
         A = Matrix.constructWithCopy(rvals);
         tmp = A.get(raggedr,raggedc);
      } catch ( IllegalArgumentException e ) {
         try_success("Catch ragged input to constructWithCopy... ",e.getMessage());
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         errorCount = try_failure(errorCount,"Catch ragged input to constructWithCopy... ","exception not thrown in construction...ArrayIndexOutOfBoundsException thrown later");
      }

      A = new Matrix(columnwise,validld);
      B = new Matrix(avals);
      tmp = B.get(0,0);
      avals[0][0] = 0.0;
      C = B.minus(A);
      avals[0][0] = tmp;
      B = Matrix.constructWithCopy(avals);
      tmp = B.get(0,0);
      avals[0][0] = 0.0;
      if ( ( tmp - B.get(0,0) ) != 0.0 ) {
        /** check that constructWithCopy behaves properly **/
        errorCount = try_failure(errorCount,"constructWithCopy... ","copy not effected... data visible outside");
      } else {
        try_success("constructWithCopy... ","");
      }
      avals[0][0] = columnwise[0]; 
      I = new Matrix(ivals);
      try {
        check(I,Matrix.identity(3,4));
        try_success("identity... ","");
      } catch ( java.lang.RuntimeException e ) {
        errorCount = try_failure(errorCount,"identity... ","identity Matrix not successfully created");
      }   

/**   
      Access Methods:
         getColumnDimension()
         getRowDimension()
         getArray()
         getArrayCopy()
         getColumnPackedCopy()
         getRowPackedCopy()
         get(int,int)
         getMatrix(int,int,int,int)
         getMatrix(int,int,int[])
         getMatrix(int[],int,int)
         getMatrix(int[],int[])
         set(int,int,double)
         setMatrix(int,int,int,int,Matrix)
         setMatrix(int,int,int[],Matrix)
         setMatrix(int[],int,int,Matrix)
         setMatrix(int[],int[],Matrix)
**/

      print("\nTesting access methods...\n");

/**
      Various get methods:
**/

      B = new Matrix(avals);
      if (B.getRowDimension() != rows) {
         errorCount = try_failure(errorCount,"getRowDimension... ","");
      } else {
         try_success("getRowDimension... ","");
      }
      if (B.getColumnDimension() != cols) {
         errorCount = try_failure(errorCount,"getColumnDimension... ","");
      } else {
         try_success("getColumnDimension... ","");
      }
      B = new Matrix(avals);
      double[][] barray = B.getArray();
      if ( barray != avals ) {
         errorCount = try_failure(errorCount,"getArray... ","");
      } else {
         try_success("getArray... ","");
      }
      barray = B.getArrayCopy();
      if ( barray == avals ) {
         errorCount = try_failure(errorCount,"getArrayCopy... ","data not (deep) copied");
      }
      try {
         check(barray,avals);
         try_success("getArrayCopy... ","");
      } catch ( java.lang.RuntimeException e ) {
         errorCount = try_failure(errorCount,"getArrayCopy... ","data not successfully (deep) copied");
      }
      double[] bpacked = B.getColumnPackedCopy();
      try {
         check(bpacked,columnwise);
         try_success("getColumnPackedCopy... ","");
      } catch ( java.lang.RuntimeException e ) {
         errorCount = try_failure(errorCount,"getColumnPackedCopy... ","data not successfully (deep) copied by columns");
      }
      bpacked = B.getRowPackedCopy();
      try {
         check(bpacked,rowwise);
         try_success("getRowPackedCopy... ","");
      } catch ( java.lang.RuntimeException e ) {
         errorCount = try_failure(errorCount,"getRowPackedCopy... ","data not successfully (deep) copied by rows");
      }
      try {
         tmp = B.get(B.getRowDimension(),B.getColumnDimension()-1);
         errorCount = try_failure(errorCount,"get(int,int)... ","OutOfBoundsException expected but not thrown");
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         try {
            tmp = B.get(B.getRowDimension()-1,B.getColumnDimension());
            errorCount = try_failure(errorCount,"get(int,int)... ","OutOfBoundsException expected but not thrown");
         } catch ( java.lang.ArrayIndexOutOfBoundsException e1 ) {
            try_success("get(int,int)... OutofBoundsException... ","");
         }
      } catch ( java.lang.IllegalArgumentException e1 ) {
         errorCount = try_failure(errorCount,"get(int,int)... ","OutOfBoundsException expected but not thrown");
      }
      try {
         if (B.get(B.getRowDimension()-1,B.getColumnDimension()-1) != 
             avals[B.getRowDimension()-1][B.getColumnDimension()-1] ) {
            errorCount = try_failure(errorCount,"get(int,int)... ","Matrix entry (i,j) not successfully retreived");
         } else {
            try_success("get(int,int)... ","");
         }
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         errorCount = try_failure(errorCount,"get(int,int)... ","Unexpected ArrayIndexOutOfBoundsException");
      }
      SUB = new Matrix(subavals);
      try {
         M = B.getMatrix(ib,ie+B.getRowDimension()+1,jb,je);
         errorCount = try_failure(errorCount,"getMatrix(int,int,int,int)... ","ArrayIndexOutOfBoundsException expected but not thrown");
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         try {
            M = B.getMatrix(ib,ie,jb,je+B.getColumnDimension()+1);
            errorCount = try_failure(errorCount,"getMatrix(int,int,int,int)... ","ArrayIndexOutOfBoundsException expected but not thrown");
         } catch ( java.lang.ArrayIndexOutOfBoundsException e1 ) {
            try_success("getMatrix(int,int,int,int)... ArrayIndexOutOfBoundsException... ","");
         }
      } catch ( java.lang.IllegalArgumentException e1 ) {
         errorCount = try_failure(errorCount,"getMatrix(int,int,int,int)... ","ArrayIndexOutOfBoundsException expected but not thrown");
      }
      try {
         M = B.getMatrix(ib,ie,jb,je);
         try {
            check(SUB,M);
            try_success("getMatrix(int,int,int,int)... ","");
         } catch ( java.lang.RuntimeException e ) {
            errorCount = try_failure(errorCount,"getMatrix(int,int,int,int)... ","submatrix not successfully retreived");
         }
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         errorCount = try_failure(errorCount,"getMatrix(int,int,int,int)... ","Unexpected ArrayIndexOutOfBoundsException");
      }
      
      try {
         M = B.getMatrix(ib,ie,badcolumnindexset);
         errorCount = try_failure(errorCount,"getMatrix(int,int,int[])... ","ArrayIndexOutOfBoundsException expected but not thrown");
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         try {
            M = B.getMatrix(ib,ie+B.getRowDimension()+1,columnindexset);
            errorCount = try_failure(errorCount,"getMatrix(int,int,int[])... ","ArrayIndexOutOfBoundsException expected but not thrown");
         } catch ( java.lang.ArrayIndexOutOfBoundsException e1 ) {
            try_success("getMatrix(int,int,int[])... ArrayIndexOutOfBoundsException... ","");
         }
      } catch ( java.lang.IllegalArgumentException e1 ) {
         errorCount = try_failure(errorCount,"getMatrix(int,int,int[])... ","ArrayIndexOutOfBoundsException expected but not thrown");
      } 
      try {
         M = B.getMatrix(ib,ie,columnindexset);
         try {
            check(SUB,M);
            try_success("getMatrix(int,int,int[])... ","");
         } catch ( java.lang.RuntimeException e ) {
            errorCount = try_failure(errorCount,"getMatrix(int,int,int[])... ","submatrix not successfully retreived");
         }
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         errorCount = try_failure(errorCount,"getMatrix(int,int,int[])... ","Unexpected ArrayIndexOutOfBoundsException");
      }
      try {
         M = B.getMatrix(badrowindexset,jb,je);
         errorCount = try_failure(errorCount,"getMatrix(int[],int,int)... ","ArrayIndexOutOfBoundsException expected but not thrown");
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         try {
            M = B.getMatrix(rowindexset,jb,je+B.getColumnDimension()+1);
            errorCount = try_failure(errorCount,"getMatrix(int[],int,int)... ","ArrayIndexOutOfBoundsException expected but not thrown");
         } catch ( java.lang.ArrayIndexOutOfBoundsException e1 ) {
            try_success("getMatrix(int[],int,int)... ArrayIndexOutOfBoundsException... ","");
         }
      } catch ( java.lang.IllegalArgumentException e1 ) {
         errorCount = try_failure(errorCount,"getMatrix(int[],int,int)... ","ArrayIndexOutOfBoundsException expected but not thrown");
      } 
      try {
         M = B.getMatrix(rowindexset,jb,je);
         try {
            check(SUB,M);
            try_success("getMatrix(int[],int,int)... ","");
         } catch ( java.lang.RuntimeException e ) {
            errorCount = try_failure(errorCount,"getMatrix(int[],int,int)... ","submatrix not successfully retreived");
         }
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         errorCount = try_failure(errorCount,"getMatrix(int[],int,int)... ","Unexpected ArrayIndexOutOfBoundsException");
      }
      try {
         M = B.getMatrix(badrowindexset,columnindexset);
         errorCount = try_failure(errorCount,"getMatrix(int[],int[])... ","ArrayIndexOutOfBoundsException expected but not thrown");
      } catch ( java.lang.ArrayIndexOutOfBoundsException e ) {
         try {
            M = B.getMatrix(rowindexset,badcolumnindexset);
            errorCount = try_failure(errorCount,"getMatrix(int[],int[])... ","ArrayIndexOutOfBoundsException expected but not thrown");
         } catch ( java.lang.ArrayIndexOutOfBoundsException e1 ) {
            try_success("getMatrix(int[],int[])... ArrayIndexOutOfBoundsException... ","");
         }
      } catch ( java.lang.IllegalArgumentException e1 ) {
         errorCount = try_failure(errorCount,"getMatrix(int[],int[])... ","ArrayIndexOutOfBoundsException expected but not thrown");
      } 
      try {

⌨️ 快捷键说明

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