1 package JSci.maths.vectors; 2 3 import JSci.maths.Mapping; 4 import JSci.maths.MathInteger; 5 import JSci.maths.MathDouble; 6 import JSci.maths.groups.AbelianGroup; 7 import JSci.maths.algebras.Module; 8 import JSci.maths.fields.Ring; 9 10 15 public final class Integer3Vector extends AbstractIntegerVector { 16 protected int x; 17 protected int y; 18 protected int z; 19 22 public Integer3Vector() { 23 super(3); 24 } 25 31 public Integer3Vector(final int x, final int y, final int z) { 32 this(); 33 this.x = x; 34 this.y = y; 35 this.z = z; 36 } 37 40 public Integer3Vector(int[] array) { 41 this(); 42 x = array[0]; 43 y = array[1]; 44 z = array[2]; 45 } 46 50 public boolean equals(Object obj, double tol) { 51 if(obj != null && (obj instanceof Integer3Vector)) { 52 final Integer3Vector vec = (Integer3Vector) obj; 53 int dx = x - vec.x; 54 int dy = y - vec.y; 55 int dz = z - vec.z; 56 return (dx*dx 57 + dy*dy 58 + dz*dz == 0); 59 } else 60 return false; 61 } 62 65 public String toString() { 66 final StringBuffer buf = new StringBuffer (15); 67 buf.append(x).append(',').append(y).append(',').append(z); 68 return buf.toString(); 69 } 70 74 public AbstractDoubleVector toDoubleVector() { 75 return new Double3Vector( 76 x, 77 y, 78 z 79 ); 80 } 81 85 public AbstractComplexVector toComplexVector() { 86 return new Complex3Vector( 87 x, 0.0, 88 y, 0.0, 89 z, 0.0 90 ); 91 } 92 97 public int getComponent(final int n) { 98 switch(n) { 99 case 0 : return x; 100 case 1 : return y; 101 case 2 : return z; 102 default : throw new VectorDimensionException("Invalid component."); 103 } 104 } 105 112 public void setComponent(final int n, final int value) { 113 switch(n) { 114 case 0 : x = value; break; 115 case 1 : y = value; break; 116 case 2 : z = value; break; 117 default : throw new VectorDimensionException("Invalid component."); 118 } 119 } 120 123 public double norm(final int n) { 124 final double answer = Math.pow(Math.abs(x), n) 125 +Math.pow(Math.abs(y), n) 126 +Math.pow(Math.abs(z), n); 127 return Math.pow(answer, 1.0/n); 128 } 129 132 public double norm() { 133 return Math.sqrt( 134 x*x 135 +y*y 136 +z*z 137 ); 138 } 139 143 public double infNorm() { 144 int infNorm = 0; 145 int abs; 146 abs = Math.abs(x); 147 if(abs > infNorm) 148 infNorm = abs; 149 abs = Math.abs(y); 150 if(abs > infNorm) 151 infNorm = abs; 152 abs = Math.abs(z); 153 if(abs > infNorm) 154 infNorm = abs; 155 return infNorm; 156 } 157 158 162 165 public AbelianGroup.Member negate() { 166 return new Integer3Vector( 167 -x, 168 -y, 169 -z 170 ); 171 } 172 173 175 178 public AbelianGroup.Member add(final AbelianGroup.Member vec) { 179 if(vec instanceof AbstractIntegerVector) 180 return add((AbstractIntegerVector)vec); 181 else 182 throw new IllegalArgumentException ("Member class not recognised by this method."); 183 } 184 188 public AbstractIntegerVector add(final AbstractIntegerVector vec) { 189 if(vec.N == 3) { 190 return new Integer3Vector( 191 x+vec.getComponent(0), 192 y+vec.getComponent(1), 193 z+vec.getComponent(2) 194 ); 195 } else 196 throw new VectorDimensionException("Vectors are different sizes."); 197 } 198 199 201 204 public AbelianGroup.Member subtract(final AbelianGroup.Member vec) { 205 if(vec instanceof AbstractIntegerVector) 206 return subtract((AbstractIntegerVector)vec); 207 else 208 throw new IllegalArgumentException ("Member class not recognised by this method."); 209 } 210 214 public AbstractIntegerVector subtract(final AbstractIntegerVector vec) { 215 if(vec.N == 3) { 216 return new Integer3Vector( 217 x-vec.getComponent(0), 218 y-vec.getComponent(1), 219 z-vec.getComponent(2) 220 ); 221 } else 222 throw new VectorDimensionException("Vectors are different sizes."); 223 } 224 225 227 230 public Module.Member scalarMultiply(Ring.Member x) { 231 if(x instanceof MathInteger) 232 return scalarMultiply(((MathInteger)x).value()); 233 else 234 throw new IllegalArgumentException ("Member class not recognised by this method."); 235 } 236 241 public AbstractIntegerVector scalarMultiply(final int k) { 242 return new Integer3Vector( 243 k*x, 244 k*y, 245 k*z 246 ); 247 } 248 249 250 252 256 public int scalarProduct(final AbstractIntegerVector vec) { 257 if(vec.N == 3) { 258 return x*vec.getComponent(0) 259 +y*vec.getComponent(1) 260 +z*vec.getComponent(2); 261 } else 262 throw new VectorDimensionException("Vectors are different sizes."); 263 } 264 265 267 271 public Integer3Vector multiply(final Integer3Vector vec) { 272 return new Integer3Vector( 273 y*vec.z - vec.y*z, 274 z*vec.x - vec.z*x, 275 x*vec.y - vec.x*y 276 ); 277 } 278 279 } 280 281 | Popular Tags |