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