Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16604 was 16604, checked in by gkronber, 5 years ago

#2925: efficiency improvements

File size: 5.0 KB
RevLine 
[16249]1using System;
2using System.Diagnostics;
[16601]3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[16249]4
5namespace HeuristicLab.Problems.DynamicalSystemsModelling {
6  public class Vector {
7    public readonly static Vector Zero = new Vector(new double[0]);
8
9    public static Vector operator +(Vector a, Vector b) {
10      if (a == Zero) return b;
11      if (b == Zero) return a;
[16597]12      Trace.Assert(a.arr.Length == b.arr.Length);
[16249]13      var res = new double[a.arr.Length];
14      for (int i = 0; i < res.Length; i++)
15        res[i] = a.arr[i] + b.arr[i];
16      return new Vector(res);
17    }
[16398]18
[16604]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    }
[16597]33
34
[16604]35    public Vector Subtract(Vector b) {
36      var a = this;
[16597]37      if (b == Zero) return a;
38      if (a == Zero) {
39        var vArr = new double[b.Length];
40        a = new Vector(vArr);
41      }
42      Trace.Assert(a.arr.Length == b.arr.Length);
43      for (int i = 0; i < a.arr.Length; i++)
[16604]44        a.arr[i] -= b.arr[i];
45      return a;
[16597]46    }
47
[16604]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    // }
[16249]54
[16604]55    //public static Vector operator *(double s, Vector v) {
56    //  return v.Scale(s);
57    //}
[16249]58
[16597]59    public Vector Scale(double s) {
[16600]60      if (this == Zero) return Zero;
61
62      for (int i = 0; i < arr.Length; i++) {
[16604]63        arr[i] *= s;
[16597]64      }
[16604]65      return this;
[16597]66    }
67
[16604]68    // public static Vector operator *(Vector v, double s) {
69    //   return s * v;
70    // }
[16600]71
[16249]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    }
[16600]80
[16249]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++)
[16600]86        res[i] = s / v.arr[i];
[16249]87      return new Vector(res);
88    }
[16600]89
[16604]90    // public static Vector operator /(Vector v, double s) {
91    //   return v.Scale(1.0 / s);
92    // }
[16249]93
[16604]94    public Vector Sin() {
95      for (int i = 0; i < arr.Length; i++) arr[i] = Math.Sin(arr[i]);
96      return this;
[16249]97    }
[16604]98    public Vector Cos() {
99      for (int i = 0; i < arr.Length; i++) arr[i] = Math.Cos(arr[i]);
100      return this;
[16249]101    }
102
[16251]103    public double this[int idx] {
104      get {
105        if (this == Vector.Zero) return 0.0;
106        else return arr[idx];
107      }
108      set {
109        if (this == Vector.Zero) throw new InvalidOperationException();
110        else arr[idx] = value;
111      }
112    }
113
114    public int Length {
115      get {
116        if (this == Vector.Zero) throw new InvalidOperationException();
117        else return arr.Length;
118      }
119    }
120
[16249]121    private readonly double[] arr; // backing array;
122
[16597]123    /// <summary>
124    ///
125    /// </summary>
126    /// <param name="v">Is not cloned!</param>
[16249]127    public Vector(double[] v) {
128      this.arr = v;
129    }
130
131    public void CopyTo(double[] target) {
[16597]132      Trace.Assert(arr.Length <= target.Length);
[16249]133      Array.Copy(arr, target, arr.Length);
134    }
[16597]135
[16604]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    }
[16597]144    /// <summary>
145    /// Creates a new vector
146    /// </summary>
147    /// <param name="a"> is cloned</param>
148    /// <returns></returns>
149    public static Vector CreateNew(double[] a) {
150      var arr = new double[a.Length];
151      Array.Copy(a, arr, arr.Length);
152      return new Vector(arr);
153    }
154
155    /// <summary>
156    /// Creates a new vector
157    /// </summary>
158    /// <param name="v"> is cloned</param>
159    /// <returns></returns>
160    public static Vector CreateNew(Vector v) {
161      if (v == Zero) return Zero;
162      var arr = new double[v.arr.Length];
163      Array.Copy(v.arr, arr, arr.Length);
164      return new Vector(arr);
165    }
[16601]166
167    internal static Vector CreateIndicator(int length, int idx) {
168      var gArr = new double[length]; // backing array
169      gArr[idx] = 1.0;
170      return new Vector(gArr);
171    }
[16604]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    }
[16249]178  }
179}
Note: See TracBrowser for help on using the repository browser.