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 Integer2Vector extends AbstractIntegerVector { 16 protected int x; 17 protected int y; 18 21 public Integer2Vector() { 22 super(2); 23 } 24 29 public Integer2Vector(final int x, final int y) { 30 this(); 31 this.x = x; 32 this.y = y; 33 } 34 37 public Integer2Vector(int[] array) { 38 this(); 39 x = array[0]; 40 y = array[1]; 41 } 42 46 public boolean equals(Object obj, double tol) { 47 if(obj != null && (obj instanceof Integer2Vector)) { 48 final Integer2Vector vec = (Integer2Vector) obj; 49 int dx = x - vec.x; 50 int dy = y - vec.y; 51 return (dx*dx 52 + dy*dy == 0); 53 } else 54 return false; 55 } 56 59 public String toString() { 60 final StringBuffer buf = new StringBuffer (15); 61 buf.append(x).append(',').append(y); 62 return buf.toString(); 63 } 64 68 public AbstractDoubleVector toDoubleVector() { 69 return new Double2Vector( 70 x, 71 y 72 ); 73 } 74 78 public AbstractComplexVector toComplexVector() { 79 return new Complex2Vector( 80 x, 0.0, 81 y, 0.0 82 ); 83 } 84 89 public int getComponent(final int n) { 90 switch(n) { 91 case 0 : return x; 92 case 1 : return y; 93 default : throw new VectorDimensionException("Invalid component."); 94 } 95 } 96 103 public void setComponent(final int n, final int value) { 104 switch(n) { 105 case 0 : x = value; break; 106 case 1 : y = value; break; 107 default : throw new VectorDimensionException("Invalid component."); 108 } 109 } 110 113 public double norm(final int n) { 114 final double answer = Math.pow(Math.abs(x), n) 115 +Math.pow(Math.abs(y), n); 116 return Math.pow(answer, 1.0/n); 117 } 118 121 public double norm() { 122 return Math.sqrt( 123 x*x 124 +y*y 125 ); 126 } 127 131 public double infNorm() { 132 int infNorm = 0; 133 int abs; 134 abs = Math.abs(x); 135 if(abs > infNorm) 136 infNorm = abs; 137 abs = Math.abs(y); 138 if(abs > infNorm) 139 infNorm = abs; 140 return infNorm; 141 } 142 143 147 150 public AbelianGroup.Member negate() { 151 return new Integer2Vector( 152 -x, 153 -y 154 ); 155 } 156 157 159 162 public AbelianGroup.Member add(final AbelianGroup.Member vec) { 163 if(vec instanceof AbstractIntegerVector) 164 return add((AbstractIntegerVector)vec); 165 else 166 throw new IllegalArgumentException ("Member class not recognised by this method."); 167 } 168 172 public AbstractIntegerVector add(final AbstractIntegerVector vec) { 173 if(vec.N == 2) { 174 return new Integer2Vector( 175 x+vec.getComponent(0), 176 y+vec.getComponent(1) 177 ); 178 } else 179 throw new VectorDimensionException("Vectors are different sizes."); 180 } 181 182 184 187 public AbelianGroup.Member subtract(final AbelianGroup.Member vec) { 188 if(vec instanceof AbstractIntegerVector) 189 return subtract((AbstractIntegerVector)vec); 190 else 191 throw new IllegalArgumentException ("Member class not recognised by this method."); 192 } 193 197 public AbstractIntegerVector subtract(final AbstractIntegerVector vec) { 198 if(vec.N == 2) { 199 return new Integer2Vector( 200 x-vec.getComponent(0), 201 y-vec.getComponent(1) 202 ); 203 } else 204 throw new VectorDimensionException("Vectors are different sizes."); 205 } 206 207 209 212 public Module.Member scalarMultiply(Ring.Member x) { 213 if(x instanceof MathInteger) 214 return scalarMultiply(((MathInteger)x).value()); 215 else 216 throw new IllegalArgumentException ("Member class not recognised by this method."); 217 } 218 223 public AbstractIntegerVector scalarMultiply(final int k) { 224 return new Integer2Vector( 225 k*x, 226 k*y 227 ); 228 } 229 230 231 233 237 public int scalarProduct(final AbstractIntegerVector vec) { 238 if(vec.N == 2) { 239 return x*vec.getComponent(0) 240 +y*vec.getComponent(1); 241 } else 242 throw new VectorDimensionException("Vectors are different sizes."); 243 } 244 245 246 } 247 248 | Popular Tags |