Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > JSci > maths > matrices > DoubleSparseMatrix

 `1 package JSci.maths.matrices;2 3 import JSci.GlobalSettings;4 import JSci.maths.Mapping;5 import JSci.maths.DimensionException;6 import JSci.maths.vectors.AbstractDoubleVector;7 import JSci.maths.vectors.DoubleVector;8 9 /**10 * The DoubleSparseMatrix class provides an object for encapsulating sparse matrices.11 * Uses compressed row storage.12 * @version 1.213 * @author Mark Hale14 */15 public final class DoubleSparseMatrix extends AbstractDoubleMatrix {16         /**17         * Matrix elements.18         */19         private double elements[];20         /**21         * Sparse indexing data.22         * Contains the column positions of each element,23         * e.g. colPos[n] is the column position24         * of the nth element.25         */26         private int colPos[];27         /**28         * Sparse indexing data.29         * Contains the indices of the start of each row,30         * e.g. rows[i] is the index31         * where the ith row starts.32         */33         private int rows[];34         /**35         * Constructs an empty matrix.36         * @param rowCount the number of rows37         * @param colCount the number of columns38         */39         public DoubleSparseMatrix(final int rowCount, final int colCount) {40                 super(rowCount, colCount);41                 elements=new double[0];42                 colPos=new int[0];43                 rows=new int[numRows+1];44         }45         /**46         * Constructs a matrix from an array.47         * @param array an assigned value48         */49         public DoubleSparseMatrix(final double array[][]) {50                 super(array.length,array[0].length);51                 rows=new int[numRows+1];52                 int n=0;53                 for(int i=0;iGlobalSettings.ZERO_TOL)58                                         n++;59                         }60                 }61                 elements=new double[n];62                 colPos=new int[n];63                 n=0;64                 for(int i=0;iGlobalSettings.ZERO_TOL) {68                                         elements[n]=array[i][j];69                                         colPos[n]=j;70                                         n++;71                                 }72                         }73                 }74                 rows[numRows]=n;75         }76         /**77         * Compares two double sparse matrices for equality.78         * @param m a double matrix79         */80         public boolean equals(AbstractDoubleMatrix m, double tol) {81                 if(numRows==m.numRows && numCols==m.numCols) {82                         if(m instanceof DoubleSparseMatrix) {83                                 return this.equals((DoubleSparseMatrix)m);84                         } else {85                     double sumSqr = 0;86                                 for(int i=0;i=0 && i=0 && j=0 && i=0 && j-norm.212         */213         public double infNorm() {214                 double result=0.0,tmpResult;215                 for(int i=0;iresult)220                                 result=tmpResult;221                 }222                 return result;223         }224         /**225         * Returns the Frobenius (l2) norm.226         */227         public double frobeniusNorm() {228                 double result=0.0;229                 for(int i=0;i