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