All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JSci.maths.matrices.DoubleSparseSquareMatrix Maven / Gradle / Ivy

Go to download

JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software. It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ... Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).

The newest version!
package JSci.maths.matrices;

import JSci.GlobalSettings;
import JSci.maths.Mapping;
import JSci.maths.DimensionException;
import JSci.maths.vectors.AbstractDoubleVector;
import JSci.maths.vectors.DoubleVector;
import JSci.maths.vectors.DoubleSparseVector;

/**
* The DoubleSparseSquareMatrix class provides an object for encapsulating sparse square matrices.
* Uses compressed row storage (Yale sparse matrix format).
* @version 1.4
* @author Mark Hale
*/
public final class DoubleSparseSquareMatrix extends AbstractDoubleSquareMatrix {
        /**
        * Matrix elements.
        */
        private double elements[];
        /**
        * Sparse indexing data.
        * Contains the column positions of each element,
        * e.g. colPos[n] is the column position
        * of the nth element.
        */
        private int colPos[];
        /**
        * Sparse indexing data.
        * Contains the indices of the start of each row,
        * e.g. rows[i] is the index
        * where the ith row starts.
        */
        private int rows[];
        /**
         * Amount by which to increase the capacity.
         */
        private int capacityIncrement = 1;
        /**
        * Constructs an empty matrix.
        * @param size the number of rows/columns
        */
        public DoubleSparseSquareMatrix(final int size) {
                super(size);
                elements=new double[0];
                colPos=new int[0];
                rows=new int[numRows+1];
        }
        public DoubleSparseSquareMatrix(final int size, int capacityIncrement) {
            this(size);
            this.capacityIncrement = capacityIncrement;
        }
        /**
        * Constructs a matrix from an array.
        * @param array an assigned value
        * @exception MatrixDimensionException If the array is not square.
        */
        public DoubleSparseSquareMatrix(final double array[][]) {
                super(array.length);
                rows=new int[numRows+1];
                int n=0;
                for(int i=0;iGlobalSettings.ZERO_TOL)
                                        n++;
                        }
                }
                elements=new double[n];
                colPos=new int[n];
                n=0;
                for(int i=0;iGlobalSettings.ZERO_TOL) {
                                        elements[n]=array[i][j];
                                        colPos[n]=j;
                                        n++;
                                }
                        }
                }
                rows[numRows]=n;
        }
        /**
        * Compares two double sparse square matrices for equality.
        * @param m a double matrix
        */
        public boolean equals(AbstractDoubleSquareMatrix m, double tol) {
                if(numRows==m.numRows && numCols==m.numCols) {
                        if(m instanceof DoubleSparseSquareMatrix) {
                                return this.equals((DoubleSparseSquareMatrix)m);
                        } else {
        			double sumSqr = 0;
                                for(int i=0;i=0 && i=0 && j=0 && i=0 && j GlobalSettings.ZERO_TOL) {
                            // expand
                            if(rows[numRows] == elements.length) {
                                // increase capacity
                                final double oldMatrix[]=elements;
                                final int oldColPos[]=colPos;
                                elements=new double[oldMatrix.length+capacityIncrement];
                                colPos=new int[oldColPos.length+capacityIncrement];
                                System.arraycopy(oldMatrix,0,elements,0,p);
                                System.arraycopy(oldColPos,0,colPos,0,p);
                                System.arraycopy(oldMatrix,p,elements,p+1,oldMatrix.length-p);
                                System.arraycopy(oldColPos,p,colPos,p+1,oldColPos.length-p);
                            } else {
                                System.arraycopy(elements,p,elements,p+1,rows[numRows]-p);
                                System.arraycopy(colPos,p,colPos,p+1,rows[numRows]-p);
                            }
                            elements[p]=x;
                            colPos[p]=j;
                            for(int k=i+1;kinfinity-norm.
        */
        public double infNorm() {
                double result=0.0,tmpResult;
                for(int j,i=0;iresult)
                                result=tmpResult;
                }
                return result;
        }
        /**
        * Returns the Frobenius (l2) norm.
        */
        public double frobeniusNorm() {
                double result=0.0;
                for(int i=0;irows()+1
        * to hold the pivot information (null if not interested)
        * @return an array with [0] containing the L-matrix
        * and [1] containing the U-matrix.
        */
        public AbstractDoubleSquareMatrix[] luDecompose(int pivot[]) {
                AbstractDoubleSquareMatrix[] LU = luDecompose_cache(pivot);
                if(LU != null)
                    return LU;
                final double arrayL[][]=new double[numRows][numCols];
                final double arrayU[][]=new double[numRows][numCols];
                if(pivot==null)
                        pivot=new int[numRows+1];
                for(int i=0;imax) {
                                        max=tmp;
                                        pivotrow=i;
                                }
                        }
                // swap row j with pivotrow
                        if(pivotrow!=j) {
                                double[] tmprow = arrayU[j];
                                arrayU[j] = arrayU[pivotrow];
                                arrayU[pivotrow] = tmprow;
                                int k=pivot[j];
                                pivot[j]=pivot[pivotrow];
                                pivot[pivotrow]=k;
                                // update parity
                                pivot[numRows]=-pivot[numRows];
                        }
                // divide by pivot
                        for(int i=j+1;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy