1 9 package org.jscience.mathematics.matrices; 10 11 import java.util.Collection ; 12 import java.util.Iterator ; 13 14 import javolution.lang.Text; 15 import javolution.lang.TextBuilder; 16 import javolution.xml.XmlElement; 17 import javolution.xml.XmlFormat; 18 19 28 public class Vector<O extends Operable<O>> extends Matrix<O> { 29 30 39 protected static final XmlFormat<Vector> XML = new XmlFormat<Vector>( 40 Vector.class) { 41 public void format(Vector vector, XmlElement xml) { 42 for (int i = 0; i < vector.m; i++) { 43 xml.getContent().add(vector.o[i]); 44 } 45 } 46 47 public Vector parse(XmlElement xml) { 48 Collection <Operable> elements = xml.getContent(); 49 return Vector.valueOf(elements); 50 } 51 }; 52 53 58 Vector(int size) { 59 super(size); 60 } 61 62 67 public Vector(O... coordinates) { 68 super(coordinates.length); 69 this.n = 1; 70 this.m = coordinates.length; 71 for (int i = 0; i < m;) { 72 this.o[i] = coordinates[i++]; 73 } 74 } 75 76 82 public static <O extends Operable<O>> Vector<O> valueOf(O... coordinates) { 83 Vector<O> V = newInstance(coordinates.length); 84 for (int i = 0; i < V.m;) { 85 V.o[i] = coordinates[i++]; 86 } 87 return V; 88 } 89 90 98 public static <O extends Operable<O>> Vector<O> valueOf( 99 Collection <O> coordinates) { 100 Vector<O> V = newInstance(coordinates.size()); 101 Iterator <O> iterator = coordinates.iterator(); 102 for (int i = 0; i < V.m;) { 103 V.o[i++] = iterator.next(); 104 } 105 return V; 106 } 107 108 113 public final int getDimension() { 114 return m; 115 } 116 117 124 public final O get(int i) { 125 if (i >= m) 126 throw new IndexOutOfBoundsException ("i: " + i + " (Vector[" + m 127 + "])"); 128 return o[i]; 129 } 130 131 140 public final O dot(Vector<O> that) { 141 if (this.m != that.m) 142 throw new MatrixException(this.m + " different from " + that.m); 143 O sum = this.o[0].times(that.o[0]); 144 for (int i = 1; i < m; i++) { 145 sum = sum.plus(this.o[i].times(that.o[i])); 146 } 147 return sum; 148 } 149 150 160 public final Vector<O> cross(Vector<O> that) { 161 if ((this.m != 3) || (that.m != 3)) 162 throw new MatrixException( 163 "The cross product of two vectors requires " 164 + "3-dimensional vectors"); 165 Vector<O> V = newInstance(3); 166 V.o[0] = this.o[1].times(that.o[2]).plus( 167 this.o[2].times(that.o[1]).opposite()); 168 V.o[1] = this.o[2].times(that.o[0]).plus( 169 this.o[0].times(that.o[2]).opposite()); 170 V.o[2] = this.o[0].times(that.o[1]).plus( 171 this.o[1].times(that.o[0]).opposite()); 172 return V; 173 } 174 175 public Vector<O> opposite() { 177 return (Vector<O>) super.opposite(); 178 } 179 180 public Vector<O> plus(Vector<O> that) { 182 return (Vector<O>) super.plus(that); 183 } 184 185 public Vector<O> minus(Vector<O> that) { 187 return (Vector<O>) super.minus(that); 188 } 189 190 public Vector<O> times(O k) { 192 return (Vector<O>) super.times(k); 193 } 194 195 200 public Text toText() { 201 TextBuilder cb = TextBuilder.newInstance(); 202 cb.append("{"); 203 for (int i = 0; i < m; i++) { 204 if (i != 0) { 205 cb.append(", "); 206 } 207 cb.append(o[i]); 208 } 209 cb.append("}"); 210 return cb.toText(); 211 } 212 213 219 static <O extends Operable<O>> Vector<O> newInstance(int n) { 220 Vector V; 221 if (n <= 1 << 3) { 222 V = FACTORY_3.object(); 223 } else if (n <= 1 << 6) { 224 V = FACTORY_6.object(); 225 } else if (n <= 1 << 9) { 226 V = FACTORY_9.object(); 227 } else if (n <= 1 << 12) { 228 V = FACTORY_12.object(); 229 } else if (n <= 1 << 15) { 230 V = FACTORY_15.object(); 231 } else if (n <= 1 << 18) { 232 V = FACTORY_18.object(); 233 } else if (n <= 1 << 21) { 234 V = FACTORY_21.object(); 235 } else if (n <= 1 << 24) { 236 V = FACTORY_24.object(); 237 } else if (n <= 1 << 27) { 238 V = FACTORY_27.object(); 239 } else if (n <= 1 << 30) { 240 V = FACTORY_30.object(); 241 } else { 242 throw new UnsupportedOperationException ("Vector too large"); 243 } 244 V.m = n; 245 V.n = 1; 246 return V; 247 } 248 249 252 private static final Factory<Vector> FACTORY_3 = new Factory<Vector>() { 253 protected Vector create() { 254 return new Vector(1 << 3); 255 } 256 }; 257 258 private static final Factory<Vector> FACTORY_6 = new Factory<Vector>() { 259 protected Vector create() { 260 return new Vector(1 << 6); 261 } 262 }; 263 264 private static final Factory<Vector> FACTORY_9 = new Factory<Vector>() { 265 protected Vector create() { 266 return new Vector(1 << 9); 267 } 268 }; 269 270 private static final Factory<Vector> FACTORY_12 = new Factory<Vector>() { 271 protected Vector create() { 272 return new Vector(1 << 12); 273 } 274 }; 275 276 private static final Factory<Vector> FACTORY_15 = new Factory<Vector>() { 277 protected Vector create() { 278 return new Vector(1 << 15); 279 } 280 }; 281 282 private static final Factory<Vector> FACTORY_18 = new Factory<Vector>() { 283 protected Vector create() { 284 return new Vector(1 << 18); 285 } 286 }; 287 288 private static final Factory<Vector> FACTORY_21 = new Factory<Vector>() { 289 protected Vector create() { 290 return new Vector(1 << 21); 291 } 292 }; 293 294 private static final Factory<Vector> FACTORY_24 = new Factory<Vector>() { 295 protected Vector create() { 296 return new Vector(1 << 24); 297 } 298 }; 299 300 private static final Factory<Vector> FACTORY_27 = new Factory<Vector>() { 301 protected Vector create() { 302 return new Vector(1 << 27); 303 } 304 }; 305 306 private static final Factory<Vector> FACTORY_30 = new Factory<Vector>() { 307 protected Vector create() { 308 return new Vector(1 << 30); 309 } 310 }; 311 312 private static final long serialVersionUID = 1L; 313 } | Popular Tags |