Changeset 16604 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Vector.cs
- Timestamp:
- 02/13/19 17:29:49 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Vector.cs
r16601 r16604 17 17 } 18 18 19 // public static Vector AddTo(Vector a, Vector b) { 20 // if (b == Zero) return a; 21 // if (a == Zero) { 22 // var vArr = new double[b.Length]; 23 // b.CopyTo(vArr); 24 // return new Vector(vArr); 25 // } else { 26 // Trace.Assert(a.arr.Length == b.arr.Length); 27 // for (int i = 0; i < a.arr.Length; i++) 28 // a.arr[i] += b.arr[i]; 29 // return a; 30 // } 31 // } 19 public Vector Add(Vector b) { 20 var a = this; 21 if (b == Zero) return a; 22 if (a == Zero) { 23 var vArr = new double[b.Length]; 24 b.CopyTo(vArr); 25 return new Vector(vArr); 26 } else { 27 Trace.Assert(a.arr.Length == b.arr.Length); 28 for (int i = 0; i < a.arr.Length; i++) 29 a.arr[i] += b.arr[i]; 30 return a; 31 } 32 } 32 33 33 34 34 public static Vector Subtract(Vector a, Vector b) { 35 public Vector Subtract(Vector b) { 36 var a = this; 35 37 if (b == Zero) return a; 36 38 if (a == Zero) { … … 39 41 } 40 42 Trace.Assert(a.arr.Length == b.arr.Length); 41 var res = new double[a.arr.Length];42 43 for (int i = 0; i < a.arr.Length; i++) 43 res[i] = a.arr[i] -b.arr[i];44 return new Vector(res);44 a.arr[i] -= b.arr[i]; 45 return a; 45 46 } 46 47 47 public static Vector operator -(Vector a, Vector b) {48 return Subtract(a, b);49 }50 public static Vector operator -(Vector v) {51 return v.Scale(-1.0);52 }48 // public static Vector operator -(Vector a, Vector b) { 49 // return Subtract(a, b); 50 // } 51 // public static Vector operator -(Vector v) { 52 // return v.Scale(-1.0); 53 // } 53 54 54 public static Vector operator *(double s, Vector v) {55 return v.Scale(s);56 }55 //public static Vector operator *(double s, Vector v) { 56 // return v.Scale(s); 57 //} 57 58 58 59 public Vector Scale(double s) { 59 60 if (this == Zero) return Zero; 60 61 61 var res = new double[arr.Length];62 62 for (int i = 0; i < arr.Length; i++) { 63 res[i] = arr[i] *s;63 arr[i] *= s; 64 64 } 65 return new Vector(res); 66 65 return this; 67 66 } 68 67 69 public static Vector operator *(Vector v, double s) {70 return s * v;71 }68 // public static Vector operator *(Vector v, double s) { 69 // return s * v; 70 // } 72 71 73 72 public static Vector operator *(Vector u, Vector v) { … … 89 88 } 90 89 91 public static Vector operator /(Vector v, double s) { 92 return v.Scale(1.0 / s); 90 // public static Vector operator /(Vector v, double s) { 91 // return v.Scale(1.0 / s); 92 // } 93 94 public Vector Sin() { 95 for (int i = 0; i < arr.Length; i++) arr[i] = Math.Sin(arr[i]); 96 return this; 93 97 } 94 95 public static Vector Sin(Vector s) { 96 var res = new double[s.arr.Length]; 97 for (int i = 0; i < res.Length; i++) res[i] = Math.Sin(s.arr[i]); 98 return new Vector(res); 99 } 100 public static Vector Cos(Vector s) { 101 var res = new double[s.arr.Length]; 102 for (int i = 0; i < res.Length; i++) res[i] = Math.Cos(s.arr[i]); 103 return new Vector(res); 98 public Vector Cos() { 99 for (int i = 0; i < arr.Length; i++) arr[i] = Math.Cos(arr[i]); 100 return this; 104 101 } 105 102 … … 137 134 } 138 135 136 public void CopyTo(double[,] target, int rowIdx) { 137 if (target == null) return; 138 if (this == Zero) { 139 for (int j = 0; j < target.GetLength(1); j++) target[rowIdx, j] = 0.0; 140 } else { 141 for (int j = 0; j < target.GetLength(1); j++) target[rowIdx, j] = arr[j]; 142 } 143 } 139 144 /// <summary> 140 145 /// Creates a new vector … … 165 170 return new Vector(gArr); 166 171 } 172 173 internal static Vector CreateFromMatrixRow(double[,] jac, int rowIdx) { 174 var arr = new double[jac.GetLength(1)]; 175 for (int i = 0; i < arr.Length; i++) arr[i] = jac[rowIdx, i]; 176 return new Vector(arr); 177 } 167 178 } 168 179 }
Note: See TracChangeset
for help on using the changeset viewer.