source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Vector.cs @ 16600

Last change on this file since 16600 was 16600, checked in by gkronber, 3 years ago

#2925: made some simplifications (Vector) to aid debugging.

File size: 4.5 KB
Line 
1using System;
2using System.Diagnostics;
3
4namespace HeuristicLab.Problems.DynamicalSystemsModelling {
5  public class Vector {
6    public readonly static Vector Zero = new Vector(new double[0]);
7
8    public static Vector operator +(Vector a, Vector b) {
9      if (a == Zero) return b;
10      if (b == Zero) return a;
11      Trace.Assert(a.arr.Length == b.arr.Length);
12      var res = new double[a.arr.Length];
13      for (int i = 0; i < res.Length; i++)
14        res[i] = a.arr[i] + b.arr[i];
15      return new Vector(res);
16    }
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    // }
31
32
33    public static Vector Subtract(Vector a, Vector b) {
34      if (b == Zero) return a;
35      if (a == Zero) {
36        var vArr = new double[b.Length];
37        a = new Vector(vArr);
38      }
39      Trace.Assert(a.arr.Length == b.arr.Length);
40      var res = new double[a.arr.Length];
41      for (int i = 0; i < a.arr.Length; i++)
42        res[i] = a.arr[i] - b.arr[i];
43      return new Vector(res);
44    }
45
46    public static Vector operator -(Vector a, Vector b) {
47      return Subtract(a, b);
48    }
49    public static Vector operator -(Vector v) {
50      return v.Scale(-1.0);
51    }
52
53    public static Vector operator *(double s, Vector v) {
54      return v.Scale(s);
55    }
56
57    public Vector Scale(double s) {
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;
63      }
64      return new Vector(res);
65
66    }
67
68    public static Vector operator *(Vector v, double s) {
69      return s * v;
70    }
71
72    public static Vector operator *(Vector u, Vector v) {
73      if (v == Zero) return Zero;
74      if (u == Zero) return Zero;
75      var res = new double[v.arr.Length];
76      for (int i = 0; i < res.Length; i++)
77        res[i] = u.arr[i] * v.arr[i];
78      return new Vector(res);
79    }
80
81    public static Vector operator /(double s, Vector v) {
82      if (s == 0.0) return Zero;
83      if (v == Zero) throw new ArgumentException("Division by zero vector");
84      var res = new double[v.arr.Length];
85      for (int i = 0; i < res.Length; i++)
86        res[i] = s / v.arr[i];
87      return new Vector(res);
88    }
89
90    public static Vector operator /(Vector v, double s) {
91      return v.Scale(1.0 / s);
92    }
93
94    public static Vector Sin(Vector s) {
95      var res = new double[s.arr.Length];
96      for (int i = 0; i < res.Length; i++) res[i] = Math.Sin(s.arr[i]);
97      return new Vector(res);
98    }
99    public static Vector Cos(Vector s) {
100      var res = new double[s.arr.Length];
101      for (int i = 0; i < res.Length; i++) res[i] = Math.Cos(s.arr[i]);
102      return new Vector(res);
103    }
104
105    public double this[int idx] {
106      get {
107        if (this == Vector.Zero) return 0.0;
108        else return arr[idx];
109      }
110      set {
111        if (this == Vector.Zero) throw new InvalidOperationException();
112        else arr[idx] = value;
113      }
114    }
115
116    public int Length {
117      get {
118        if (this == Vector.Zero) throw new InvalidOperationException();
119        else return arr.Length;
120      }
121    }
122
123    private readonly double[] arr; // backing array;
124
125    /// <summary>
126    ///
127    /// </summary>
128    /// <param name="v">Is not cloned!</param>
129    public Vector(double[] v) {
130      this.arr = v;
131    }
132
133    public void CopyTo(double[] target) {
134      Trace.Assert(arr.Length <= target.Length);
135      Array.Copy(arr, target, arr.Length);
136    }
137
138    /// <summary>
139    /// Creates a new vector
140    /// </summary>
141    /// <param name="a"> is cloned</param>
142    /// <returns></returns>
143    public static Vector CreateNew(double[] a) {
144      var arr = new double[a.Length];
145      Array.Copy(a, arr, arr.Length);
146      return new Vector(arr);
147    }
148
149    /// <summary>
150    /// Creates a new vector
151    /// </summary>
152    /// <param name="v"> is cloned</param>
153    /// <returns></returns>
154    public static Vector CreateNew(Vector v) {
155      if (v == Zero) return Zero;
156      var arr = new double[v.arr.Length];
157      Array.Copy(v.arr, arr, arr.Length);
158      return new Vector(arr);
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.