Changeset 16249 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs
- Timestamp:
- 10/22/18 10:04:28 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs
r16248 r16249 40 40 namespace HeuristicLab.Problems.DynamicalSystemsModelling { 41 41 42 public class Vector { 43 public readonly static Vector Zero = new Vector(new double[0]); 44 45 public static Vector operator +(Vector a, Vector b) { 46 if (a == Zero) return b; 47 if (b == Zero) return a; 48 Debug.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); 53 } 54 public static Vector operator -(Vector a, Vector b) { 55 if (b == Zero) return a; 56 if (a == Zero) return -b; 57 Debug.Assert(a.arr.Length == b.arr.Length); 58 var res = new double[a.arr.Length]; 59 for (int i = 0; i < res.Length; i++) 60 res[i] = a.arr[i] - b.arr[i]; 61 return new Vector(res); 62 } 63 public static Vector operator -(Vector v) { 64 if (v == Zero) return Zero; 65 for (int i = 0; i < v.arr.Length; i++) 66 v.arr[i] = -v.arr[i]; 67 return v; 68 } 69 70 public static Vector operator *(double s, Vector v) { 71 if (v == Zero) return Zero; 72 if (s == 0.0) return Zero; 73 var res = new double[v.arr.Length]; 74 for (int i = 0; i < res.Length; i++) 75 res[i] = s * v.arr[i]; 76 return new Vector(res); 77 } 78 79 public static Vector operator *(Vector v, double s) { 80 return s * v; 81 } 82 public static Vector operator *(Vector u, Vector v) { 83 if (v == Zero) return Zero; 84 if (u == Zero) return Zero; 85 var res = new double[v.arr.Length]; 86 for (int i = 0; i < res.Length; i++) 87 res[i] = u.arr[i] * v.arr[i]; 88 return new Vector(res); 89 } 90 public static Vector operator /(double s, Vector v) { 91 if (s == 0.0) return Zero; 92 if (v == Zero) throw new ArgumentException("Division by zero vector"); 93 var res = new double[v.arr.Length]; 94 for (int i = 0; i < res.Length; i++) 95 res[i] = 1.0 / v.arr[i]; 96 return new Vector(res); 97 } 98 public static Vector operator /(Vector v, double s) { 99 return v * 1.0 / s; 100 } 101 102 public static Vector Sin(Vector s) { 103 var res = new double[s.arr.Length]; 104 for (int i = 0; i < res.Length; i++) res[i] = Math.Sin(s.arr[i]); 105 return new Vector(res); 106 } 107 public static Vector Cos(Vector s) { 108 var res = new double[s.arr.Length]; 109 for (int i = 0; i < res.Length; i++) res[i] = Math.Cos(s.arr[i]); 110 return new Vector(res); 111 } 112 113 private readonly double[] arr; // backing array; 114 115 public Vector(double[] v) { 116 this.arr = v; 117 } 118 119 public void CopyTo(double[] target) { 120 Debug.Assert(arr.Length <= target.Length); 121 Array.Copy(arr, target, arr.Length); 122 } 123 } 42 124 43 125 44
Note: See TracChangeset
for help on using the changeset viewer.