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 Double2Vector extends AbstractDoubleVector { 19 protected double x; 20 protected double y; 21 24 public Double2Vector() { 25 super(2); 26 } 27 32 public Double2Vector(final double x, final double y) { 33 this(); 34 this.x = x; 35 this.y = y; 36 } 37 40 public Double2Vector(double[] array) { 41 this(); 42 x = array[0]; 43 y = array[1]; 44 } 45 49 public boolean equals(Object obj, double tol) { 50 if(obj != null && (obj instanceof Double2Vector)) { 51 final Double2Vector vec = (Double2Vector) obj; 52 double dx = x - vec.x; 53 double dy = y - vec.y; 54 return (dx*dx 55 + dy*dy <= tol*tol); 56 } else 57 return false; 58 } 59 62 public String toString() { 63 final StringBuffer buf = new StringBuffer (15); 64 buf.append(x).append(',').append(y); 65 return buf.toString(); 66 } 67 71 public AbstractIntegerVector toIntegerVector() { 72 return new Integer2Vector( 73 Math.round((float)x), 74 Math.round((float)y) 75 ); 76 } 77 81 public AbstractComplexVector toComplexVector() { 82 return new Complex2Vector( 83 x, 0.0, 84 y, 0.0 85 ); 86 } 87 92 public double getComponent(final int n) { 93 switch(n) { 94 case 0 : return x; 95 case 1 : return y; 96 default : throw new VectorDimensionException("Invalid component."); 97 } 98 } 99 106 public void setComponent(final int n, final double value) { 107 switch(n) { 108 case 0 : x = value; break; 109 case 1 : y = value; break; 110 default : throw new VectorDimensionException("Invalid component."); 111 } 112 } 113 116 public double norm(final int n) { 117 final double answer = Math.pow(Math.abs(x), n) 118 +Math.pow(Math.abs(y), n); 119 return Math.pow(answer, 1.0/n); 120 } 121 124 public double norm() { 125 return Math.sqrt( 126 x*x 127 +y*y 128 ); 129 } 130 134 public double infNorm() { 135 double infNorm = 0; 136 double abs; 137 abs = Math.abs(x); 138 if(abs > infNorm) 139 infNorm = abs; 140 abs = Math.abs(y); 141 if(abs > infNorm) 142 infNorm = abs; 143 return infNorm; 144 } 145 146 150 153 public AbelianGroup.Member negate() { 154 return new Double2Vector( 155 -x, 156 -y 157 ); 158 } 159 160 162 165 public AbelianGroup.Member add(final AbelianGroup.Member vec) { 166 if(vec instanceof AbstractDoubleVector) 167 return add((AbstractDoubleVector)vec); 168 else 169 throw new IllegalArgumentException ("Member class not recognised by this method."); 170 } 171 175 public AbstractDoubleVector add(final AbstractDoubleVector vec) { 176 if(vec.N == 2) { 177 return new Double2Vector( 178 x+vec.getComponent(0), 179 y+vec.getComponent(1) 180 ); 181 } else 182 throw new VectorDimensionException("Vectors are different sizes."); 183 } 184 185 187 190 public AbelianGroup.Member subtract(final AbelianGroup.Member vec) { 191 if(vec instanceof AbstractDoubleVector) 192 return subtract((AbstractDoubleVector)vec); 193 else 194 throw new IllegalArgumentException ("Member class not recognised by this method."); 195 } 196 200 public AbstractDoubleVector subtract(final AbstractDoubleVector vec) { 201 if(vec.N == 2) { 202 return new Double2Vector( 203 x-vec.getComponent(0), 204 y-vec.getComponent(1) 205 ); 206 } else 207 throw new VectorDimensionException("Vectors are different sizes."); 208 } 209 210 212 215 public Module.Member scalarMultiply(Ring.Member x) { 216 if(x instanceof MathInteger) 217 return scalarMultiply(((MathInteger)x).value()); 218 else if(x instanceof MathDouble) 219 return scalarMultiply(((MathDouble)x).value()); 220 else 221 throw new IllegalArgumentException ("Member class not recognised by this method."); 222 } 223 228 public AbstractDoubleVector scalarMultiply(final double k) { 229 return new Double2Vector( 230 k*x, 231 k*y 232 ); 233 } 234 235 237 240 public VectorSpace.Member scalarDivide(Field.Member x) { 241 if(x instanceof MathDouble) 242 return scalarDivide(((MathDouble)x).value()); 243 else 244 throw new IllegalArgumentException ("Member class not recognised by this method."); 245 } 246 252 public AbstractDoubleVector scalarDivide(final double k) { 253 return new Double2Vector( 254 x/k, 255 y/k 256 ); 257 } 258 259 261 265 public double scalarProduct(final AbstractDoubleVector vec) { 266 if(vec.N == 2) { 267 return x*vec.getComponent(0) 268 +y*vec.getComponent(1); 269 } else 270 throw new VectorDimensionException("Vectors are different sizes."); 271 } 272 273 274 276 281 public AbstractDoubleVector mapComponents(final Mapping mapping) { 282 return new Double2Vector( 283 mapping.map(x), 284 mapping.map(y) 285 ); 286 } 287 } 288 289 | Popular Tags |