1 9 package org.jscience.mathematics.vectors; 10 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import javax.realtime.MemoryArea; 15 16 import javolution.xml.XMLFormat; 17 import javolution.xml.stream.XMLStreamException; 18 19 import org.jscience.mathematics.numbers.Complex; 20 import org.jscience.mathematics.structures.VectorSpaceNormed; 21 22 29 public final class ComplexVector extends Vector<Complex> implements 30 VectorSpaceNormed<Vector<Complex>, Complex> { 31 32 40 protected static final XMLFormat<ComplexVector> XML = new XMLFormat<ComplexVector>( 41 ComplexVector.class) { 42 43 @Override 44 public ComplexVector newInstance(Class <ComplexVector> cls, 45 InputElement xml) throws XMLStreamException { 46 int dimension = xml.getAttribute("dimension", 0); 47 ComplexVector V = ComplexVector.newInstance(dimension); 48 V._dimension = dimension; 49 return V; 50 } 51 52 @SuppressWarnings ("unchecked") 53 @Override 54 public void read(InputElement xml, ComplexVector V) 55 throws XMLStreamException { 56 for (int i = 0, n = V._dimension; i < n;) { 57 V._reals[i++] = ((Complex) xml.getNext()).doubleValue(); 58 } 59 if (xml.hasNext()) 60 throw new XMLStreamException("Too many elements"); 61 } 62 63 @Override 64 public void write(ComplexVector V, OutputElement xml) 65 throws XMLStreamException { 66 xml.setAttribute("dimension", V._dimension); 67 for (int i = 0, n = V._dimension; i < n;) { 68 xml.add(V.get(i++)); 69 } 70 } 71 }; 72 73 76 int _dimension; 77 78 81 double[] _reals; 82 83 86 double[] _imags; 87 88 94 public static ComplexVector valueOf(Complex... elements) { 95 int n = elements.length; 96 ComplexVector V = ComplexVector.newInstance(n); 97 for (int i = 0; i < n; i++) { 98 Complex complex = elements[i]; 99 V._reals[i] = complex.getReal(); 100 V._imags[i] = complex.getImaginary(); 101 } 102 return V; 103 } 104 105 112 public static ComplexVector valueOf(List <Complex> elements) { 113 int n = elements.size(); 114 ComplexVector V = ComplexVector.newInstance(n); 115 Iterator <Complex> iterator = elements.iterator(); 116 for (int i = 0; i < n; i++) { 117 Complex complex = iterator.next(); 118 V._reals[i] = complex.getReal(); 119 V._imags[i] = complex.getImaginary(); 120 } 121 return V; 122 } 123 124 131 public static ComplexVector valueOf(Vector<Complex> that) { 132 if (that instanceof ComplexVector) 133 return (ComplexVector) that; 134 int n = that.getDimension(); 135 ComplexVector V = ComplexVector.newInstance(n); 136 for (int i = 0; i < n; i++) { 137 Complex complex = that.get(i); 138 V._reals[i] = complex.getReal(); 139 V._imags[i] = complex.getImaginary(); 140 } 141 return V; 142 } 143 144 151 public double getReal(int i) { 152 if (i >= _dimension) 153 throw new ArrayIndexOutOfBoundsException (); 154 return _reals[i]; 155 } 156 157 164 public double getImaginary(int i) { 165 if (i >= _dimension) 166 throw new ArrayIndexOutOfBoundsException (); 167 return _imags[i]; 168 } 169 170 176 public Complex norm() { 177 double normSquaredReal = 0; 178 double normSquaredImag = 0; 179 for (int i = _dimension; --i >= 0;) { 180 double real = _reals[i]; 181 double imag = _imags[i]; 182 normSquaredReal += real * real - imag * imag; 183 normSquaredImag += real * imag * 2.0; 184 } 185 return Complex.valueOf(normSquaredReal, normSquaredImag).sqrt(); 186 } 187 188 @Override 189 public int getDimension() { 190 return _dimension; 191 } 192 193 @Override 194 public Complex get(int i) { 195 if (i >= _dimension) 196 throw new IndexOutOfBoundsException (); 197 return Complex.valueOf(_reals[i], _imags[i]); 198 } 199 200 @Override 201 public ComplexVector opposite() { 202 ComplexVector V = ComplexVector.newInstance(_dimension); 203 for (int i = 0; i < _dimension; i++) { 204 V._reals[i] = -_reals[i]; 205 V._imags[i] = -_imags[i]; 206 } 207 return V; 208 } 209 210 @Override 211 public ComplexVector plus(Vector<Complex> that) { 212 ComplexVector T = ComplexVector.valueOf(that); 213 if (T._dimension != _dimension) 214 throw new DimensionException(); 215 ComplexVector V = ComplexVector.newInstance(_dimension); 216 for (int i = 0; i < _dimension; i++) { 217 V._reals[i] = _reals[i] + T._reals[i]; 218 V._imags[i] = _imags[i] + T._imags[i]; 219 } 220 return V; 221 } 222 223 @Override 224 public ComplexVector minus(Vector<Complex> that) { 225 ComplexVector T = ComplexVector.valueOf(that); 226 if (T._dimension != _dimension) 227 throw new DimensionException(); 228 ComplexVector V = ComplexVector.newInstance(_dimension); 229 for (int i = 0; i < _dimension; i++) { 230 V._reals[i] = _reals[i] - T._reals[i]; 231 V._imags[i] = _imags[i] - T._imags[i]; 232 } 233 return V; 234 } 235 236 @Override 237 public ComplexVector times(Complex k) { 238 ComplexVector V = ComplexVector.newInstance(_dimension); 239 for (int i = 0; i < _dimension; i++) { 240 double real = _reals[i]; 241 double imag = _imags[i]; 242 V._reals[i] = real * k.getReal() - imag * k.getImaginary(); 243 V._imags[i] = real * k.getImaginary() + imag * k.getReal(); 244 } 245 return V; 246 } 247 248 @Override 249 public Complex times(Vector<Complex> that) { 250 ComplexVector T = ComplexVector.valueOf(that); 251 if (T._dimension != _dimension) 252 throw new DimensionException(); 253 double sumReal = _reals[0] * T._reals[0] - _imags[0] * T._imags[0]; 254 double sumImag = _reals[0] * T._imags[0] + _imags[0] * T._reals[0]; 255 for (int i = 1; i < _dimension; i++) { 256 sumReal += _reals[i] * T._reals[i] - _imags[i] * T._imags[i]; 257 sumImag += _reals[i] * T._imags[i] + _imags[i] * T._reals[i]; 258 } 259 return Complex.valueOf(sumReal, sumImag); 260 } 261 262 266 void set(int i, Complex c) { 267 _reals[i] = c.getReal(); 268 _imags[i] = c.getImaginary(); 269 } 270 271 275 static ComplexVector newInstance(final int dimension) { 276 final ComplexVector V = FACTORY.object(); 277 V._dimension = dimension; 278 if ((V._reals == null) || (V._reals.length < dimension)) { 279 MemoryArea.getMemoryArea(V).executeInArea(new Runnable () { 280 public void run() { 281 V._reals = new double[dimension]; 282 V._imags = new double[dimension]; 283 } 284 }); 285 } 286 return V; 287 } 288 289 private static Factory<ComplexVector> FACTORY = new Factory<ComplexVector>() { 290 protected ComplexVector create() { 291 return new ComplexVector(); 292 } 293 }; 294 295 private ComplexVector() { 296 } 297 298 private static final long serialVersionUID = 1L; 299 } | Popular Tags |