1 package JSci.maths.vectors; 2 3 import JSci.GlobalSettings; 4 import JSci.maths.ExtraMath; 5 import JSci.maths.Mapping; 6 import JSci.maths.algebras.*; 7 import JSci.maths.fields.*; 8 import JSci.maths.groups.AbelianGroup; 9 10 15 public abstract class AbstractDoubleVector extends MathVector implements BanachSpace.Member { 16 protected AbstractDoubleVector(final int dim) { 17 super(dim); 18 } 19 24 public final boolean equals(Object obj) { 25 return equals(obj, GlobalSettings.ZERO_TOL); 26 } 27 public boolean equals(Object obj, double tol) { 28 if(obj != null && (obj instanceof AbstractDoubleVector)) { 29 final AbstractDoubleVector vec = (AbstractDoubleVector) obj; 30 return (this.dimension() == vec.dimension() && this.subtract(vec).norm() <= tol); 31 } else 32 return false; 33 } 34 37 public String toString() { 38 final StringBuffer buf = new StringBuffer (8*N); 39 int i; 40 for(i=0; i<N-1; i++) { 41 buf.append(getComponent(i)); 42 buf.append(','); 43 } 44 buf.append(getComponent(i)); 45 return buf.toString(); 46 } 47 50 public int hashCode() { 51 return (int)Math.exp(norm()); 52 } 53 58 public abstract double getComponent(int n); 59 65 public abstract void setComponent(int n, double x); 66 public Object getSet() { 67 throw new RuntimeException ("Not implemented: file bug"); 68 } 69 73 public double norm(int n) { 74 double answer = Math.pow(Math.abs(getComponent(0)), n); 75 for(int i=1; i<N; i++) 76 answer += Math.pow(Math.abs(getComponent(i)), n); 77 return Math.pow(answer, 1.0/n); 78 } 79 83 public double norm() { 84 double answer = getComponent(0); 85 for(int i=1; i<N; i++) 86 answer = ExtraMath.hypot(answer, getComponent(i)); 87 return answer; 88 } 89 94 public double infNorm() { 95 double infNorm = Math.abs(getComponent(0)); 96 for(int i=1; i<N; i++) { 97 final double abs = Math.abs(getComponent(i)); 98 if(abs > infNorm) 99 infNorm = abs; 100 } 101 return infNorm; 102 } 103 106 public double mass() { 107 double mass=0.0; 108 for(int i=1; i<N; i++) 109 mass+=getComponent(i); 110 return mass; 111 } 112 113 117 122 public abstract AbstractDoubleVector add(AbstractDoubleVector v); 123 128 public abstract AbstractDoubleVector subtract(AbstractDoubleVector v); 129 133 public abstract AbstractDoubleVector scalarMultiply(double x); 134 139 public abstract AbstractDoubleVector scalarDivide(double x); 140 143 public AbstractDoubleVector normalize() { 144 return this.scalarDivide(norm()); 145 } 146 151 public abstract double scalarProduct(AbstractDoubleVector v); 152 157 public abstract AbstractDoubleVector mapComponents(final Mapping f); 158 } 159 160 | Popular Tags |