Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2994 continued refactoring and extended unit tests. Interval calculation still fails for some edge cases (mainly for undefined behaviour). VectorEvaluator and VectorAutoDiffEvaluator produce the same results as the LinearInterpreter. TODO: check gradient calculation

File size: 5.3 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Linq;
4
5namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
6  // A vector where each element is an algebraic
7  // Operations on the vector are broadcast to each element
8  [DebuggerDisplay("VectorOfAlgebraic(len={Length}): {string}")]
9  public class VectorOfAlgebraic<T> : IAlgebraicType<VectorOfAlgebraic<T>> where T : IAlgebraicType<T>, new() {
10    private T[] arr;
11    public T this[int idx] { get { return arr[idx]; } set { arr[idx] = value; } }
12    public int Length => arr.Length;
13
14    public VectorOfAlgebraic(int length) { arr = new T[length]; }
15
16    public VectorOfAlgebraic() { }
17
18    /// <summary>
19    ///
20    /// </summary>
21    /// <param name="arr">array is not copied</param>
22    public VectorOfAlgebraic(T[] arr) { this.arr = arr; }
23
24    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
25    public VectorOfAlgebraic<T> Zero {
26      get {
27        var zero = new T[this.Length];
28        for (int i = 0; i < zero.Length; i++) {
29          zero[i] = new T(); // assumed to be initialized to zero
30        }
31        return new VectorOfAlgebraic<T>(zero);
32      }
33    }
34    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
35    public VectorOfAlgebraic<T> One {
36      get {
37        var one = new T[this.Length];
38        for (int i = 0; i < one.Length; i++) {
39          one[i] = new T().One;
40        }
41        return new VectorOfAlgebraic<T>(one);
42      }
43    }
44
45    public VectorOfAlgebraic<T> Assign(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Assign(a.arr[i]); } return this; }
46    public VectorOfAlgebraic<T> Add(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Add(a.arr[i]); } return this; }
47    public VectorOfAlgebraic<T> Sub(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Sub(a.arr[i]); } return this; }
48    public VectorOfAlgebraic<T> Mul(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Mul(a.arr[i]); } return this; }
49    public VectorOfAlgebraic<T> Div(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Div(a.arr[i]); } return this; }
50    public VectorOfAlgebraic<T> AssignNeg(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignNeg(a.arr[i]); } return this; }
51    public VectorOfAlgebraic<T> AssignInv(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignInv(a.arr[i]); } return this; }
52    public VectorOfAlgebraic<T> Scale(double s) { for (int i = 0; i < arr.Length; ++i) { arr[i].Scale(s); } return this; }
53    public VectorOfAlgebraic<T> AssignLog(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignLog(a.arr[i]); } return this; }
54    public VectorOfAlgebraic<T> AssignSin(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignSin(a.arr[i]); } return this; }
55    public VectorOfAlgebraic<T> AssignExp(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignExp(a.arr[i]); } return this; }
56    public VectorOfAlgebraic<T> AssignCos(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignCos(a.arr[i]); } return this; }
57    public VectorOfAlgebraic<T> AssignTanh(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignTanh(a.arr[i]); } return this; }
58    public VectorOfAlgebraic<T> AssignIntPower(VectorOfAlgebraic<T> a, int p) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignIntPower(a.arr[i], p); } return this; }
59    public VectorOfAlgebraic<T> AssignIntRoot(VectorOfAlgebraic<T> a, int r) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignIntRoot(a.arr[i], r); } return this; }
60    public VectorOfAlgebraic<T> AssignAbs(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignAbs(a.arr[i]); } return this; }
61    public VectorOfAlgebraic<T> AssignSgn(VectorOfAlgebraic<T> a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignSgn(a.arr[i]); } return this; }
62
63    public VectorOfAlgebraic<T> Clone() {
64      var v = Zero; // create new zero vector
65      v.Assign(this); // copy each element
66      return v;
67    }
68
69    public VectorOfAlgebraic<T> AssignConstant(T constantValue) {
70      for (int i = 0; i < arr.Length; ++i) {
71        arr[i].Assign(constantValue);
72      }
73      return this;
74    }
75
76    public void CopyTo(T[] dest, int idx, int length) {
77      throw new NotImplementedException(); // TODO: check if we need to copy all elements
78      // Array.Copy(arr, 0, dest, idx, length);
79    }
80
81    public void CopyFrom(T[] data, int rowIndex) {
82      throw new NotImplementedException(); // TODO: check if we need to copy all elements
83      // Array.Copy(data, rowIndex, arr, 0, Math.Min(arr.Length, data.Length - rowIndex));
84    }
85    public void CopyRowTo(T[,] dest, int row) {
86      throw new NotImplementedException(); // TODO: check if we need to copy all elements
87      // for (int j = 0; j < arr.Length; ++j) dest[row, j] = arr[j];
88    }
89
90    internal void CopyColumnTo(T[,] dest, int column, int row, int len) {
91      throw new NotImplementedException(); // TODO: check if we need to copy all elements
92      // for (int j = 0; j < len; ++j) dest[row + j, column] = arr[j];
93    }
94
95    public override string ToString() {
96      return "{" + string.Join(", ", arr.Take(Math.Max(5, arr.Length))) + (arr.Length > 5 ? "..." : string.Empty) + "}";
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.