using System; using System.Diagnostics; using System.Linq; namespace HeuristicLab.Problems.DataAnalysis.Symbolic { // A vector where each element is an algebraic // Operations on the vector are broadcast to each element [DebuggerDisplay("VectorOfAlgebraic(len={Length}): {string}")] public class VectorOfAlgebraic : IAlgebraicType> where T : IAlgebraicType, new() { private T[] arr; public T this[int idx] { get { return arr[idx]; } set { arr[idx] = value; } } public int Length => arr.Length; public VectorOfAlgebraic(int length) { arr = new T[length]; } public VectorOfAlgebraic() { } /// /// /// /// array is not copied public VectorOfAlgebraic(T[] arr) { this.arr = arr; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] public VectorOfAlgebraic Zero { get { var zero = new T[this.Length]; for (int i = 0; i < zero.Length; i++) { zero[i] = new T(); // assumed to be initialized to zero } return new VectorOfAlgebraic(zero); } } [DebuggerBrowsable(DebuggerBrowsableState.Never)] public VectorOfAlgebraic One { get { var one = new T[this.Length]; for (int i = 0; i < one.Length; i++) { one[i] = new T().One; } return new VectorOfAlgebraic(one); } } public VectorOfAlgebraic Assign(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Assign(a.arr[i]); } return this; } public VectorOfAlgebraic Add(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Add(a.arr[i]); } return this; } public VectorOfAlgebraic Sub(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Sub(a.arr[i]); } return this; } public VectorOfAlgebraic Mul(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Mul(a.arr[i]); } return this; } public VectorOfAlgebraic Div(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].Div(a.arr[i]); } return this; } public VectorOfAlgebraic AssignNeg(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignNeg(a.arr[i]); } return this; } public VectorOfAlgebraic AssignInv(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignInv(a.arr[i]); } return this; } public VectorOfAlgebraic Scale(double s) { for (int i = 0; i < arr.Length; ++i) { arr[i].Scale(s); } return this; } public VectorOfAlgebraic AssignLog(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignLog(a.arr[i]); } return this; } public VectorOfAlgebraic AssignSin(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignSin(a.arr[i]); } return this; } public VectorOfAlgebraic AssignExp(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignExp(a.arr[i]); } return this; } public VectorOfAlgebraic AssignCos(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignCos(a.arr[i]); } return this; } public VectorOfAlgebraic AssignTanh(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignTanh(a.arr[i]); } return this; } public VectorOfAlgebraic AssignIntPower(VectorOfAlgebraic a, int p) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignIntPower(a.arr[i], p); } return this; } public VectorOfAlgebraic AssignIntRoot(VectorOfAlgebraic a, int r) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignIntRoot(a.arr[i], r); } return this; } public VectorOfAlgebraic AssignAbs(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignAbs(a.arr[i]); } return this; } public VectorOfAlgebraic AssignSgn(VectorOfAlgebraic a) { for (int i = 0; i < arr.Length; ++i) { arr[i].AssignSgn(a.arr[i]); } return this; } public VectorOfAlgebraic Clone() { var v = Zero; // create new zero vector v.Assign(this); // copy each element return v; } public VectorOfAlgebraic AssignConstant(T constantValue) { for (int i = 0; i < arr.Length; ++i) { arr[i].Assign(constantValue); } return this; } public void CopyTo(T[] dest, int idx, int length) { throw new NotImplementedException(); // TODO: check if we need to copy all elements // Array.Copy(arr, 0, dest, idx, length); } public void CopyFrom(T[] data, int rowIndex) { throw new NotImplementedException(); // TODO: check if we need to copy all elements // Array.Copy(data, rowIndex, arr, 0, Math.Min(arr.Length, data.Length - rowIndex)); } public void CopyRowTo(T[,] dest, int row) { throw new NotImplementedException(); // TODO: check if we need to copy all elements // for (int j = 0; j < arr.Length; ++j) dest[row, j] = arr[j]; } internal void CopyColumnTo(T[,] dest, int column, int row, int len) { throw new NotImplementedException(); // TODO: check if we need to copy all elements // for (int j = 0; j < len; ++j) dest[row + j, column] = arr[j]; } public override string ToString() { return "{" + string.Join(", ", arr.Take(Math.Max(5, arr.Length))) + (arr.Length > 5 ? "..." : string.Empty) + "}"; } } }