1 package JSci.maths.vectors; 2 3 import JSci.maths.ExtraMath; 4 import JSci.maths.MathDouble; 5 import JSci.maths.MathInteger; 6 import JSci.maths.Mapping; 7 import JSci.maths.algebras.Module; 8 import JSci.maths.algebras.VectorSpace; 9 import JSci.maths.fields.Ring; 10 import JSci.maths.fields.Field; 11 import JSci.maths.groups.AbelianGroup; 12 13 18 public class DoubleVector extends AbstractDoubleVector { 19 22 protected double vector[]; 23 27 public DoubleVector(final int dim) { 28 super(dim); 29 vector=new double[dim]; 30 } 31 35 public DoubleVector(final double array[]) { 36 super(array.length); 37 vector=array; 38 } 39 43 public boolean equals(Object a, double tol) { 44 if(a!=null && (a instanceof AbstractDoubleVector) && N==((AbstractDoubleVector)a).N) { 45 final AbstractDoubleVector dv=(AbstractDoubleVector)a; 46 double sumSqr = 0.0; 47 for(int i=0; i<N; i++) { 48 double delta = vector[i] - dv.getComponent(i); 49 sumSqr += delta*delta; 50 } 51 return (sumSqr <= tol*tol); 52 } else 53 return false; 54 } 55 58 public String toString() { 59 final StringBuffer buf=new StringBuffer (8*N); 60 int i; 61 for(i=0;i<N-1;i++) { 62 buf.append(vector[i]); 63 buf.append(','); 64 } 65 buf.append(vector[i]); 66 return buf.toString(); 67 } 68 72 public AbstractIntegerVector toIntegerVector() { 73 final int array[]=new int[N]; 74 for(int i=0;i<N;i++) 75 array[i]=Math.round((float)vector[i]); 76 return new IntegerVector(array); 77 } 78 82 public AbstractComplexVector toComplexVector() { 83 return new ComplexVector(vector,new double[N]); 84 } 85 90 public double getComponent(final int n) { 91 if(n>=0 && n<N) 92 return vector[n]; 93 else 94 throw new VectorDimensionException(getInvalidComponentMsg(n)); 95 } 96 102 public void setComponent(final int n, final double x) { 103 if(n>=0 && n<N) 104 vector[n]=x; 105 else 106 throw new VectorDimensionException(getInvalidComponentMsg(n)); 107 } 108 112 public double norm(int n) { 113 double answer=Math.pow(Math.abs(vector[0]), n); 114 for(int i=1; i<N; i++) 115 answer+=Math.pow(Math.abs(vector[i]), n); 116 return Math.pow(answer, 1.0/n); 117 } 118 122 public double norm() { 123 double answer=vector[0]; 124 for(int i=1;i<N;i++) 125 answer=ExtraMath.hypot(answer,vector[i]); 126 return answer; 127 } 128 133 public double infNorm() { 134 double infNorm = Math.abs(vector[0]); 135 for(int i=1; i<N; i++) { 136 final double abs = Math.abs(vector[i]); 137 if(abs > infNorm) 138 infNorm = abs; 139 } 140 return infNorm; 141 } 142 143 147 150 public AbelianGroup.Member negate() { 151 final double array[]=new double[N]; 152 array[0]=-vector[0]; 153 for(int i=1;i<N;i++) 154 array[i]=-vector[i]; 155 return new DoubleVector(array); 156 } 157 158 160 163 public AbelianGroup.Member add(final AbelianGroup.Member v) { 164 if(v instanceof AbstractDoubleVector) 165 return add((AbstractDoubleVector)v); 166 else 167 throw new IllegalArgumentException ("Member class not recognised by this method."); 168 } 169 174 public AbstractDoubleVector add(AbstractDoubleVector v) { 175 if(v instanceof DoubleVector) 176 return add((DoubleVector)v); 177 else { 178 if(N==v.N) { 179 final double array[]=new double[N]; 180 array[0]=vector[0]+v.getComponent(0); 181 for(int i=1;i<N;i++) 182 array[i]=vector[i]+v.getComponent(i); 183 return new DoubleVector(array); 184 } else 185 throw new VectorDimensionException("Vectors are different sizes."); 186 } 187 } 188 public DoubleVector add(final DoubleVector v) { 189 if(N==v.N) { 190 final double array[]=new double[N]; 191 array[0]=vector[0]+v.vector[0]; 192 for(int i=1;i<N;i++) 193 array[i]=vector[i]+v.vector[i]; 194 return new DoubleVector(array); 195 } else 196 throw new VectorDimensionException("Vectors are different sizes."); 197 } 198 199 201 204 public AbelianGroup.Member subtract(final AbelianGroup.Member v) { 205 if(v instanceof AbstractDoubleVector) 206 return subtract((AbstractDoubleVector)v); 207 else 208 throw new IllegalArgumentException ("Member class not recognised by this method."); 209 } 210 215 public AbstractDoubleVector subtract(final AbstractDoubleVector v) { 216 if(v instanceof DoubleVector) 217 return subtract((DoubleVector)v); 218 else { 219 if(N==v.N) { 220 final double array[]=new double[N]; 221 array[0]=vector[0]-v.getComponent(0); 222 for(int i=1;i<N;i++) 223 array[i]=vector[i]-v.getComponent(i); 224 return new DoubleVector(array); 225 } else 226 throw new VectorDimensionException("Vectors are different sizes."); 227 } 228 } 229 public DoubleVector subtract(final DoubleVector v) { 230 if(N==v.N) { 231 final double array[]=new double[N]; 232 array[0]=vector[0]-v.vector[0]; 233 for(int i=1;i<N;i++) 234 array[i]=vector[i]-v.vector[i]; 235 return new DoubleVector(array); 236 } else 237 throw new VectorDimensionException("Vectors are different sizes."); 238 } 239 240 242 245 public Module.Member scalarMultiply(Ring.Member x) { 246 if(x instanceof MathDouble) 247 return scalarMultiply(((MathDouble)x).value()); 248 else if(x instanceof MathInteger) 249 return scalarMultiply(((MathInteger)x).value()); 250 else 251 throw new IllegalArgumentException ("Member class not recognised by this method."); 252 } 253 257 public AbstractDoubleVector scalarMultiply(final double x) { 258 final double array[]=new double[N]; 259 array[0]=x*vector[0]; 260 for(int i=1;i<N;i++) 261 array[i]=x*vector[i]; 262 return new DoubleVector(array); 263 } 264 265 267 270 public VectorSpace.Member scalarDivide(Field.Member x) { 271 if(x instanceof MathDouble) 272 return scalarDivide(((MathDouble)x).value()); 273 else 274 throw new IllegalArgumentException ("Member class not recognised by this method."); 275 } 276 281 public AbstractDoubleVector scalarDivide(final double x) { 282 final double array[]=new double[N]; 283 array[0]=vector[0]/x; 284 for(int i=1;i<N;i++) 285 array[i]=vector[i]/x; 286 return new DoubleVector(array); 287 } 288 289 291 296 public double scalarProduct(final AbstractDoubleVector v) { 297 if(v instanceof DoubleVector) 298 return scalarProduct((DoubleVector)v); 299 else { 300 if(N==v.N) { 301 double answer=vector[0]*v.getComponent(0); 302 for(int i=1;i<N;i++) 303 answer+=vector[i]*v.getComponent(i); 304 return answer; 305 } else 306 throw new VectorDimensionException("Vectors are different sizes."); 307 } 308 } 309 public double scalarProduct(final DoubleVector v) { 310 if(N==v.N) { 311 double answer=vector[0]*v.vector[0]; 312 for(int i=1;i<N;i++) 313 answer+=vector[i]*v.vector[i]; 314 return answer; 315 } else 316 throw new VectorDimensionException("Vectors are different sizes."); 317 } 318 319 321 326 public AbstractDoubleVector mapComponents(final Mapping f) { 327 final double array[]=new double[N]; 328 array[0]=f.map(vector[0]); 329 for(int i=1;i<N;i++) 330 array[i]=f.map(vector[i]); 331 return new DoubleVector(array); 332 } 333 } 334 335 | Popular Tags |