Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > JSci > maths > vectors > DoubleSparseVector

 `1 package JSci.maths.vectors;2 3 import JSci.GlobalSettings;4 import JSci.maths.MathDouble;5 import JSci.maths.MathInteger;6 import JSci.maths.Mapping;7 import JSci.maths.matrices.DoubleSparseMatrix;8 import JSci.maths.groups.AbelianGroup;9 import JSci.maths.algebras.Module;10 import JSci.maths.algebras.VectorSpace;11 import JSci.maths.fields.Ring;12 import JSci.maths.fields.Field;13 14 /**15 * The DoubleSparseVector class encapsulates sparse vectors.16 * Uses Morse-coding.17 * @author Daniel Lemire18 * @author Alain Beliveau19 */20 public final class DoubleSparseVector extends AbstractDoubleVector {21         private double vector[];22         /**23         * Sparse indexing data.24         * Contains the component positions of each element,25         * e.g. pos[n] is the component position26         * of the nth element27         * (the pos[n]th component is stored at index n).28         */29         private int pos[];30         /**31         * Constructs an empty vector.32         * @param dim the dimension of the vector.33         */34         public DoubleSparseVector(final int dim) {35                 super(dim);36                 vector=new double[0];37                 pos=new int[0];38         }39         /**40         * Constructs a vector from an array.41         */42         public DoubleSparseVector(double array[]) {43                 super(array.length);44                 int n=0;45                 for(int i=0;i=N)87                         throw new VectorDimensionException(getInvalidComponentMsg(n));88                 for(int k=0;k=N)102                         throw new VectorDimensionException(getInvalidComponentMsg(n));103                 if(Math.abs(x)<=GlobalSettings.ZERO_TOL)104                         return;105                 for(int k=0;k2-norm (magnitude).122         */123         public double norm() {124                 return Math.sqrt(sumSquares());125         }126         /**127         * Returns the sum of the squares of the components.128         */129         public double sumSquares() {130                 double norm=0.0;131         for(int k=0;k