1 9 package org.jscience.mathematics.vectors; 10 11 import java.util.Comparator ; 12 import javolution.lang.Immutable; 13 import javolution.text.Text; 14 import javolution.text.TextBuilder; 15 import javolution.context.RealtimeObject; 16 import javolution.util.FastTable; 17 import org.jscience.mathematics.structures.Field; 18 import org.jscience.mathematics.structures.VectorSpace; 19 20 28 public abstract class Vector<F extends Field<F>> extends RealtimeObject 29 implements VectorSpace<Vector<F>, F>, Immutable { 30 31 40 public static <F extends Field<F>> Vector<F> valueOf(F... elements) { 41 return DenseVector.valueOf(elements); 42 } 43 44 53 public static Float64Vector valueOf(double... values) { 54 return Float64Vector.valueOf(values); 55 } 56 57 60 protected Vector() { 61 } 62 63 68 public abstract int getDimension(); 69 70 77 public abstract F get(int i); 78 79 84 public abstract Vector<F> opposite(); 85 86 93 public abstract Vector<F> plus(Vector<F> that); 94 95 101 public Vector<F> minus(Vector<F> that) { 102 return this.plus(that.opposite()); 103 } 104 105 111 public abstract Vector<F> times(F k); 112 113 122 public abstract F times(Vector<F> that); 123 124 132 public DenseVector<F> cross(Vector<F> that) { 133 if ((this.getDimension() != 3) || (that.getDimension() != 3)) 134 throw new DimensionException( 135 "The cross product of two vectors requires " 136 + "3-dimensional vectors"); 137 FastTable<F> elements = FastTable.newInstance(); 138 elements.add((this.get(1).times(that.get(2))).plus((this.get(2).times(that 139 .get(1))).opposite())); 140 elements.add((this.get(2).times(that.get(0))).plus((this.get(0).times(that 141 .get(2))).opposite())); 142 elements.add((this.get(0).times(that.get(1))).plus((this.get(1).times(that 143 .get(0))).opposite())); 144 DenseVector<F> V = DenseVector.valueOf(elements); 145 FastTable.recycle(elements); 146 return V; 147 } 148 149 154 public Text toText() { 155 final int dimension = this.getDimension(); 156 TextBuilder tmp = TextBuilder.newInstance(); 157 tmp.append('{'); 158 for (int i = 0; i < dimension; i++) { 159 tmp.append(get(i).toText()); 160 if (i != dimension - 1) { 161 tmp.append(", "); 162 } 163 } 164 tmp.append('}'); 165 Text txt = tmp.toText(); 166 TextBuilder.recycle(tmp); 167 return txt; 168 } 169 170 182 public boolean equals(Vector<F> that, Comparator <F> cmp) { 183 if (this == that) 184 return true; 185 final int dimension = this.getDimension(); 186 if (that.getDimension() != dimension) 187 return false; 188 for (int i = dimension; --i >= 0;) { 189 if (cmp.compare(this.get(i), that.get(i)) != 0) 190 return false; 191 } 192 return true; 193 } 194 195 202 public boolean equals(Object that) { 203 if (this == that) 204 return true; 205 if (!(that instanceof Vector)) 206 return false; 207 final int dimension = this.getDimension(); 208 Vector v = (Vector) that; 209 if (v.getDimension() != dimension) 210 return false; 211 for (int i = dimension; --i >= 0;) { 212 if (!this.get(i).equals(v.get(i))) 213 return false; 214 } 215 return true; 216 } 217 218 225 public int hashCode() { 226 final int dimension = this.getDimension(); 227 int code = 0; 228 for (int i = dimension; --i >= 0;) { 229 code += get(i).hashCode(); 230 } 231 return code; 232 } 233 } | Popular Tags |