source: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/AlgebraicDoubleVector.cs @ 17298

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

#2994: small changes after refactoring

File size: 5.3 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Linq;
4
5namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
6  // a simple vector as an algebraic type
7  [DebuggerDisplay("DoubleVector(len={Length}): {string.}")]
8  public class AlgebraicDoubleVector : IAlgebraicType<AlgebraicDoubleVector> {
9    private double[] arr;
10    public double this[int idx] { get { return arr[idx]; } set { arr[idx] = value; } }
11    public int Length => arr.Length;
12
13    public AlgebraicDoubleVector(int length) { arr = new double[length]; }
14
15    public AlgebraicDoubleVector() { }
16
17    /// <summary>
18    ///
19    /// </summary>
20    /// <param name="arr">array is not copied</param>
21    public AlgebraicDoubleVector(double[] arr) { this.arr = arr; }
22
23    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
24    public AlgebraicDoubleVector Zero => new AlgebraicDoubleVector(new double[this.Length]); // must return vector of same length as this (therefore Zero is not static)
25    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
26    public AlgebraicDoubleVector One => new AlgebraicDoubleVector(new double[this.Length]).AssignConstant(1.0); // must return vector of same length as this (therefore Zero is not static)
27    public AlgebraicDoubleVector Assign(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = a.arr[i]; } return this; }
28    public AlgebraicDoubleVector Add(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] += a.arr[i]; } return this; }
29    public AlgebraicDoubleVector Sub(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] -= a.arr[i]; } return this; }
30    public AlgebraicDoubleVector Mul(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] *= a.arr[i]; } return this; }
31    public AlgebraicDoubleVector Div(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] /= a.arr[i]; } return this; }
32    public AlgebraicDoubleVector AssignNeg(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = -a.arr[i]; } return this; }
33    public AlgebraicDoubleVector AssignInv(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = 1.0 / a.arr[i]; } return this; }
34    public AlgebraicDoubleVector Scale(double s) { for (int i = 0; i < arr.Length; ++i) { arr[i] *= s; } return this; }
35    public AlgebraicDoubleVector AssignLog(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Log(a.arr[i]); } return this; }
36    public AlgebraicDoubleVector AssignSin(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Sin(a.arr[i]); } return this; }
37    public AlgebraicDoubleVector AssignExp(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Exp(a.arr[i]); } return this; }
38    public AlgebraicDoubleVector AssignCos(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Cos(a.arr[i]); } return this; }
39    public AlgebraicDoubleVector AssignTanh(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Tanh(a.arr[i]); } return this; }
40    public AlgebraicDoubleVector AssignIntPower(AlgebraicDoubleVector a, int p) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Pow(a.arr[i], p); } return this; }
41    public AlgebraicDoubleVector AssignIntRoot(AlgebraicDoubleVector a, int r) { for (int i = 0; i < arr.Length; ++i) { arr[i] = IntRoot(a.arr[i], r); } return this; }
42    public AlgebraicDoubleVector AssignMin(AlgebraicDoubleVector other) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Min(arr[i], other.arr[i]); } return this; }
43    public AlgebraicDoubleVector AssignMax(AlgebraicDoubleVector other) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Max(arr[i], other.arr[i]); } return this; }
44    public AlgebraicDoubleVector AssignAbs(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Abs(a.arr[i]); } return this; }
45    public AlgebraicDoubleVector AssignSgn(AlgebraicDoubleVector a) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Sign(a.arr[i]); } return this; }
46
47
48    // helper
49    private double IntRoot(double v, int r) {
50      if (r % 2 == 0) return Math.Pow(v, 1.0 / r);
51      else return v < 0 ? -Math.Pow(-v, 1.0 / r) : Math.Pow(v, 1.0 / r);
52    }
53
54    public AlgebraicDoubleVector Clone() {
55      var v = new AlgebraicDoubleVector(this.arr.Length);
56      Array.Copy(arr, v.arr, v.arr.Length);
57      return v;
58    }
59
60    public AlgebraicDoubleVector AssignConstant(double constantValue) {
61      for (int i = 0; i < arr.Length; ++i) {
62        arr[i] = constantValue;
63      }
64      return this;
65    }
66
67    public void CopyTo(double[] dest, int idx, int length) {
68      Array.Copy(arr, 0, dest, idx, length);
69    }
70
71    public void CopyFrom(double[] data, int rowIndex) {
72      Array.Copy(data, rowIndex, arr, 0, Math.Min(arr.Length, data.Length - rowIndex));
73    }
74    public void CopyRowTo(double[,] dest, int row) {
75      for (int j = 0; j < arr.Length; ++j) dest[row, j] = arr[j];
76    }
77
78    internal void CopyColumnTo(double[,] dest, int column, int row, int len) {
79      for (int j = 0; j < len; ++j) dest[row + j, column] = arr[j];
80    }
81
82    public override string ToString() {
83      return "{" + string.Join(", ", arr.Take(Math.Max(5, arr.Length))) + (arr.Length > 5 ? "..." : string.Empty) + "}";
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.