Changeset 16600 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Vector.cs
 Timestamp:
 02/12/19 13:59:15 (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Vector.cs
r16599 r16600 16 16 } 17 17 18 public static Vector AddTo(Vector a, Vector b) {19 if (b == Zero) return a;20 if (a == Zero) {21 var vArr = new double[b.Length];22 b.CopyTo(vArr);23 return new Vector(vArr);24 } else {25 Trace.Assert(a.arr.Length == b.arr.Length);26 for (int i = 0; i < a.arr.Length; i++)27 a.arr[i] += b.arr[i];28 return a;29 }30 }18 // public static Vector AddTo(Vector a, Vector b) { 19 // if (b == Zero) return a; 20 // if (a == Zero) { 21 // var vArr = new double[b.Length]; 22 // b.CopyTo(vArr); 23 // return new Vector(vArr); 24 // } else { 25 // Trace.Assert(a.arr.Length == b.arr.Length); 26 // for (int i = 0; i < a.arr.Length; i++) 27 // a.arr[i] += b.arr[i]; 28 // return a; 29 // } 30 // } 31 31 32 32 … … 38 38 } 39 39 Trace.Assert(a.arr.Length == b.arr.Length); 40 var res = new double[a.arr.Length]; 40 41 for (int i = 0; i < a.arr.Length; i++) 41 a.arr[i] =b.arr[i];42 return a;42 res[i] = a.arr[i]  b.arr[i]; 43 return new Vector(res); 43 44 } 44 45 45 46 public static Vector operator (Vector a, Vector b) { 46 if (b == Zero) return a; 47 if (a == Zero) return b; 48 Trace.Assert(a.arr.Length == b.arr.Length); 49 var res = new double[a.arr.Length]; 50 for (int i = 0; i < res.Length; i++) 51 res[i] = a.arr[i]  b.arr[i]; 52 return new Vector(res); 47 return Subtract(a, b); 53 48 } 54 49 public static Vector operator (Vector v) { 55 if (v == Zero) return Zero; 56 for (int i = 0; i < v.arr.Length; i++) 57 v.arr[i] = v.arr[i]; 58 return v; 50 return v.Scale(1.0); 59 51 } 60 52 61 53 public static Vector operator *(double s, Vector v) { 62 if (v == Zero) return Zero; 63 if (s == 0.0) return Zero; 64 var res = new double[v.arr.Length]; 65 for (int i = 0; i < res.Length; i++) 66 res[i] = s * v.arr[i]; 67 return new Vector(res); 54 return v.Scale(s); 68 55 } 69 56 70 57 public Vector Scale(double s) { 71 if (this != Zero) { 72 for (int i = 0; i < arr.Length; i++) { 73 arr[i] *= s; 74 } 58 if (this == Zero) return Zero; 59 60 var res = new double[arr.Length]; 61 for (int i = 0; i < arr.Length; i++) { 62 res[i] = arr[i] * s; 75 63 } 64 return new Vector(res); 76 65 77 return this;78 66 } 79 67 … … 81 69 return s * v; 82 70 } 71 83 72 public static Vector operator *(Vector u, Vector v) { 84 73 if (v == Zero) return Zero; … … 89 78 return new Vector(res); 90 79 } 80 91 81 public static Vector operator /(double s, Vector v) { 92 82 if (s == 0.0) return Zero; … … 94 84 var res = new double[v.arr.Length]; 95 85 for (int i = 0; i < res.Length; i++) 96 res[i] = 1.0/ v.arr[i];86 res[i] = s / v.arr[i]; 97 87 return new Vector(res); 98 88 } 89 99 90 public static Vector operator /(Vector v, double s) { 100 return v * 1.0 / s;91 return v.Scale(1.0 / s); 101 92 } 102 93
Note: See TracChangeset
for help on using the changeset viewer.