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

JSci.maths.vectors.DoubleSparseVector 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.vectors;

import JSci.GlobalSettings;
import JSci.maths.MathDouble;
import JSci.maths.MathInteger;
import JSci.maths.Mapping;
import JSci.maths.matrices.DoubleSparseMatrix;
import JSci.maths.groups.AbelianGroup;
import JSci.maths.algebras.Module;
import JSci.maths.algebras.VectorSpace;
import JSci.maths.fields.Ring;
import JSci.maths.fields.Field;

/**
* The DoubleSparseVector class encapsulates sparse vectors.
* Uses Morse-coding.
* @author Daniel Lemire
* @author Alain Beliveau
*/
public final class DoubleSparseVector extends AbstractDoubleVector {
        private double vector[];
        /**
        * Sparse indexing data.
        * Contains the component positions of each element,
        * e.g. pos[n] is the component position
        * of the nth element
        * (the pos[n]th component is stored at index n).
        */
        private int pos[];
        private int count = 0;
        /**
         * Amount by which to increase the capacity.
         */
        private int capacityIncrement = 1;
        /**
        * Constructs an empty vector.
        * @param dim the dimension of the vector.
        */
        public DoubleSparseVector(final int dim) {
                super(dim);
                vector=new double[0];
                pos=new int[0];
        }
        public DoubleSparseVector(final int dim, int capacityIncrement) {
            this(dim);
            this.capacityIncrement = capacityIncrement;
        }
        /**
        * Constructs a vector from an array.
        */
        public DoubleSparseVector(double array[]) {
                super(array.length);
                int n=0;
                for(int i=0;iGlobalSettings.ZERO_TOL)
                                n++;
                }
                count = n;
                vector=new double[n];
                pos=new int[n];
                n=0;
                for(int i=0;iGlobalSettings.ZERO_TOL) {
                                vector[n]=array[i];
                                pos[n]=i;
                                n++;
                        }
                }
        }
        /**
        * Compares two vectors for equality.
        * @param obj a double sparse vector
        */
	public boolean equals(Object obj, double tol) {
                if(obj!=null && (obj instanceof DoubleSparseVector) && N==((DoubleSparseVector)obj).N) {
                        DoubleSparseVector v=(DoubleSparseVector)obj;
                        double sumSqr = 0.0;
                        for(int i=0; i=N)
                        throw new VectorDimensionException(getInvalidComponentMsg(n));
                int k = 0;
                while(k=N)
                        throw new VectorDimensionException(getInvalidComponentMsg(n));
                int k = 0;
                while(k GlobalSettings.ZERO_TOL) {
                    // expand
                    if(count == vector.length) {
                        // increase capacity
                        double oldVector[] = vector;
                        int oldPos[] = pos;
                        vector=new double[oldVector.length+capacityIncrement];
                        pos=new int[oldPos.length+capacityIncrement];
                        System.arraycopy(oldVector,0,vector,0,k);
                        System.arraycopy(oldPos,0,pos,0,k);
                        System.arraycopy(oldVector,k,vector,k+1,oldVector.length-k);
                        System.arraycopy(oldPos,k,pos,k+1,oldPos.length-k);
                    } else {
                        System.arraycopy(vector,k,vector,k+1,count-k);
                        System.arraycopy(pos,k,pos,k+1,count-k);
                    }
                    vector[k] = x;
                    pos[k] = n;
                    count++;
                }
        }
        /**
         * Returns the number of non-zero components.
         */
        public int componentCount() {
            return count;
        }
        /**
        * Returns the l2-norm (magnitude).
        */
        public double norm() {
                return Math.sqrt(sumSquares());
        }
        /**
        * Returns the sum of the squares of the components.
        */
        public double sumSquares() {
                double norm=0.0;
  		for(int k=0;k




© 2015 - 2024 Weber Informatics LLC | Privacy Policy