1 package JSci.maths.vectors; 2 3 import JSci.GlobalSettings; 4 import JSci.maths.Mapping; 5 import JSci.maths.MathInteger; 6 import JSci.maths.MathDouble; 7 import JSci.maths.groups.AbelianGroup; 8 import JSci.maths.algebras.Module; 9 import JSci.maths.fields.Ring; 10 import JSci.maths.algebras.VectorSpace; 11 import JSci.maths.fields.Field; 12 13 18 public final class Double3Vector extends AbstractDoubleVector { 19 protected double x; 20 protected double y; 21 protected double z; 22 25 public Double3Vector() { 26 super(3); 27 } 28 34 public Double3Vector(final double x, final double y, final double z) { 35 this(); 36 this.x = x; 37 this.y = y; 38 this.z = z; 39 } 40 43 public Double3Vector(double[] array) { 44 this(); 45 x = array[0]; 46 y = array[1]; 47 z = array[2]; 48 } 49 53 public boolean equals(Object obj, double tol) { 54 if(obj != null && (obj instanceof Double3Vector)) { 55 final Double3Vector vec = (Double3Vector) obj; 56 double dx = x - vec.x; 57 double dy = y - vec.y; 58 double dz = z - vec.z; 59 return (dx*dx 60 + dy*dy 61 + dz*dz <= tol*tol); 62 } else 63 return false; 64 } 65 68 public String toString() { 69 final StringBuffer buf = new StringBuffer (15); 70 buf.append(x).append(',').append(y).append(',').append(z); 71 return buf.toString(); 72 } 73 77 public AbstractIntegerVector toIntegerVector() { 78 return new Integer3Vector( 79 Math.round((float)x), 80 Math.round((float)y), 81 Math.round((float)z) 82 ); 83 } 84 88 public AbstractComplexVector toComplexVector() { 89 return new Complex3Vector( 90 x, 0.0, 91 y, 0.0, 92 z, 0.0 93 ); 94 } 95 100 public double getComponent(final int n) { 101 switch(n) { 102 case 0 : return x; 103 case 1 : return y; 104 case 2 : return z; 105 default : throw new VectorDimensionException("Invalid component."); 106 } 107 } 108 115 public void setComponent(final int n, final double value) { 116 switch(n) { 117 case 0 : x = value; break; 118 case 1 : y = value; break; 119 case 2 : z = value; break; 120 default : throw new VectorDimensionException("Invalid component."); 121 } 122 } 123 126 public double norm(final int n) { 127 final double answer = Math.pow(Math.abs(x), n) 128 +Math.pow(Math.abs(y), n) 129 +Math.pow(Math.abs(z), n); 130 return Math.pow(answer, 1.0/n); 131 } 132 135 public double norm() { 136 return Math.sqrt( 137 x*x 138 +y*y 139 +z*z 140 ); 141 } 142 146 public double infNorm() { 147 double infNorm = 0; 148 double abs; 149 abs = Math.abs(x); 150 if(abs > infNorm) 151 infNorm = abs; 152 abs = Math.abs(y); 153 if(abs > infNorm) 154 infNorm = abs; 155 abs = Math.abs(z); 156 if(abs > infNorm) 157 infNorm = abs; 158 return infNorm; 159 } 160 161 165 168 public AbelianGroup.Member negate() { 169 return new Double3Vector( 170 -x, 171 -y, 172 -z 173 ); 174 } 175 176 178 181 public AbelianGroup.Member add(final AbelianGroup.Member vec) { 182 if(vec instanceof AbstractDoubleVector) 183 return add((AbstractDoubleVector)vec); 184 else 185 throw new IllegalArgumentException ("Member class not recognised by this method."); 186 } 187 191 public AbstractDoubleVector add(final AbstractDoubleVector vec) { 192 if(vec.N == 3) { 193 return new Double3Vector( 194 x+vec.getComponent(0), 195 y+vec.getComponent(1), 196 z+vec.getComponent(2) 197 ); 198 } else 199 throw new VectorDimensionException("Vectors are different sizes."); 200 } 201 202 204 207 public AbelianGroup.Member subtract(final AbelianGroup.Member vec) { 208 if(vec instanceof AbstractDoubleVector) 209 return subtract((AbstractDoubleVector)vec); 210 else 211 throw new IllegalArgumentException ("Member class not recognised by this method."); 212 } 213 217 public AbstractDoubleVector subtract(final AbstractDoubleVector vec) { 218 if(vec.N == 3) { 219 return new Double3Vector( 220 x-vec.getComponent(0), 221 y-vec.getComponent(1), 222 z-vec.getComponent(2) 223 ); 224 } else 225 throw new VectorDimensionException("Vectors are different sizes."); 226 } 227 228 230 233 public Module.Member scalarMultiply(Ring.Member x) { 234 if(x instanceof MathInteger) 235 return scalarMultiply(((MathInteger)x).value()); 236 else if(x instanceof MathDouble) 237 return scalarMultiply(((MathDouble)x).value()); 238 else 239 throw new IllegalArgumentException ("Member class not recognised by this method."); 240 } 241 246 public AbstractDoubleVector scalarMultiply(final double k) { 247 return new Double3Vector( 248 k*x, 249 k*y, 250 k*z 251 ); 252 } 253 254 256 259 public VectorSpace.Member scalarDivide(Field.Member x) { 260 if(x instanceof MathDouble) 261 return scalarDivide(((MathDouble)x).value()); 262 else 263 throw new IllegalArgumentException ("Member class not recognised by this method."); 264 } 265 271 public AbstractDoubleVector scalarDivide(final double k) { 272 return new Double3Vector( 273 x/k, 274 y/k, 275 z/k 276 ); 277 } 278 279 281 285 public double scalarProduct(final AbstractDoubleVector vec) { 286 if(vec.N == 3) { 287 return x*vec.getComponent(0) 288 +y*vec.getComponent(1) 289 +z*vec.getComponent(2); 290 } else 291 throw new VectorDimensionException("Vectors are different sizes."); 292 } 293 294 296 300 public Double3Vector multiply(final Double3Vector vec) { 301 return new Double3Vector( 302 y*vec.z - vec.y*z, 303 z*vec.x - vec.z*x, 304 x*vec.y - vec.x*y 305 ); 306 } 307 308 310 315 public AbstractDoubleVector mapComponents(final Mapping mapping) { 316 return new Double3Vector( 317 mapping.map(x), 318 mapping.map(y), 319 mapping.map(z) 320 ); 321 } 322 } 323 324 | Popular Tags |