1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Problems.DynamicalSystemsModelling {
|
---|
6 | public class Vector {
|
---|
7 | public readonly static Vector Zero = new Vector(new double[0]);
|
---|
8 |
|
---|
9 | public static Vector operator +(Vector a, Vector b) {
|
---|
10 | if (a == Zero) return b;
|
---|
11 | if (b == Zero) return a;
|
---|
12 | Trace.Assert(a.arr.Length == b.arr.Length);
|
---|
13 | var res = new double[a.arr.Length];
|
---|
14 | for (int i = 0; i < res.Length; i++)
|
---|
15 | res[i] = a.arr[i] + b.arr[i];
|
---|
16 | return new Vector(res);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public Vector Add(Vector b) {
|
---|
20 | var a = this;
|
---|
21 | if (b == Zero) return a;
|
---|
22 | if (a == Zero) {
|
---|
23 | var vArr = new double[b.Length];
|
---|
24 | b.CopyTo(vArr);
|
---|
25 | return new Vector(vArr);
|
---|
26 | } else {
|
---|
27 | Trace.Assert(a.arr.Length == b.arr.Length);
|
---|
28 | for (int i = 0; i < a.arr.Length; i++)
|
---|
29 | a.arr[i] += b.arr[i];
|
---|
30 | return a;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | public Vector Subtract(Vector b) {
|
---|
36 | var a = this;
|
---|
37 | if (b == Zero) return a;
|
---|
38 | if (a == Zero) {
|
---|
39 | var vArr = new double[b.Length];
|
---|
40 | a = new Vector(vArr);
|
---|
41 | }
|
---|
42 | Trace.Assert(a.arr.Length == b.arr.Length);
|
---|
43 | for (int i = 0; i < a.arr.Length; i++)
|
---|
44 | a.arr[i] -= b.arr[i];
|
---|
45 | return a;
|
---|
46 | }
|
---|
47 |
|
---|
48 | // public static Vector operator -(Vector a, Vector b) {
|
---|
49 | // return Subtract(a, b);
|
---|
50 | // }
|
---|
51 | // public static Vector operator -(Vector v) {
|
---|
52 | // return v.Scale(-1.0);
|
---|
53 | // }
|
---|
54 |
|
---|
55 | //public static Vector operator *(double s, Vector v) {
|
---|
56 | // return v.Scale(s);
|
---|
57 | //}
|
---|
58 |
|
---|
59 | public Vector Scale(double s) {
|
---|
60 | if (this == Zero) return Zero;
|
---|
61 |
|
---|
62 | for (int i = 0; i < arr.Length; i++) {
|
---|
63 | arr[i] *= s;
|
---|
64 | }
|
---|
65 | return this;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // public static Vector operator *(Vector v, double s) {
|
---|
69 | // return s * v;
|
---|
70 | // }
|
---|
71 |
|
---|
72 | public static Vector operator *(Vector u, Vector v) {
|
---|
73 | if (v == Zero) return Zero;
|
---|
74 | if (u == Zero) return Zero;
|
---|
75 | var res = new double[v.arr.Length];
|
---|
76 | for (int i = 0; i < res.Length; i++)
|
---|
77 | res[i] = u.arr[i] * v.arr[i];
|
---|
78 | return new Vector(res);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static Vector operator /(double s, Vector v) {
|
---|
82 | if (s == 0.0) return Zero;
|
---|
83 | if (v == Zero) throw new ArgumentException("Division by zero vector");
|
---|
84 | var res = new double[v.arr.Length];
|
---|
85 | for (int i = 0; i < res.Length; i++)
|
---|
86 | res[i] = s / v.arr[i];
|
---|
87 | return new Vector(res);
|
---|
88 | }
|
---|
89 |
|
---|
90 | // public static Vector operator /(Vector v, double s) {
|
---|
91 | // return v.Scale(1.0 / s);
|
---|
92 | // }
|
---|
93 |
|
---|
94 | public Vector Sin() {
|
---|
95 | for (int i = 0; i < arr.Length; i++) arr[i] = Math.Sin(arr[i]);
|
---|
96 | return this;
|
---|
97 | }
|
---|
98 | public Vector Cos() {
|
---|
99 | for (int i = 0; i < arr.Length; i++) arr[i] = Math.Cos(arr[i]);
|
---|
100 | return this;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public double this[int idx] {
|
---|
104 | get {
|
---|
105 | if (this == Vector.Zero) return 0.0;
|
---|
106 | else return arr[idx];
|
---|
107 | }
|
---|
108 | set {
|
---|
109 | if (this == Vector.Zero) throw new InvalidOperationException();
|
---|
110 | else arr[idx] = value;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public int Length {
|
---|
115 | get {
|
---|
116 | if (this == Vector.Zero) throw new InvalidOperationException();
|
---|
117 | else return arr.Length;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private readonly double[] arr; // backing array;
|
---|
122 |
|
---|
123 | /// <summary>
|
---|
124 | ///
|
---|
125 | /// </summary>
|
---|
126 | /// <param name="v">Is not cloned!</param>
|
---|
127 | public Vector(double[] v) {
|
---|
128 | this.arr = v;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public void CopyTo(double[] target) {
|
---|
132 | Trace.Assert(arr.Length <= target.Length);
|
---|
133 | Array.Copy(arr, target, arr.Length);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void CopyTo(double[,] target, int rowIdx) {
|
---|
137 | if (target == null) return;
|
---|
138 | if (this == Zero) {
|
---|
139 | for (int j = 0; j < target.GetLength(1); j++) target[rowIdx, j] = 0.0;
|
---|
140 | } else {
|
---|
141 | for (int j = 0; j < target.GetLength(1); j++) target[rowIdx, j] = arr[j];
|
---|
142 | }
|
---|
143 | }
|
---|
144 | /// <summary>
|
---|
145 | /// Creates a new vector
|
---|
146 | /// </summary>
|
---|
147 | /// <param name="a"> is cloned</param>
|
---|
148 | /// <returns></returns>
|
---|
149 | public static Vector CreateNew(double[] a) {
|
---|
150 | var arr = new double[a.Length];
|
---|
151 | Array.Copy(a, arr, arr.Length);
|
---|
152 | return new Vector(arr);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /// <summary>
|
---|
156 | /// Creates a new vector
|
---|
157 | /// </summary>
|
---|
158 | /// <param name="v"> is cloned</param>
|
---|
159 | /// <returns></returns>
|
---|
160 | public static Vector CreateNew(Vector v) {
|
---|
161 | if (v == Zero) return Zero;
|
---|
162 | var arr = new double[v.arr.Length];
|
---|
163 | Array.Copy(v.arr, arr, arr.Length);
|
---|
164 | return new Vector(arr);
|
---|
165 | }
|
---|
166 |
|
---|
167 | internal static Vector CreateIndicator(int length, int idx) {
|
---|
168 | var gArr = new double[length]; // backing array
|
---|
169 | gArr[idx] = 1.0;
|
---|
170 | return new Vector(gArr);
|
---|
171 | }
|
---|
172 |
|
---|
173 | internal static Vector CreateFromMatrixRow(double[,] jac, int rowIdx) {
|
---|
174 | var arr = new double[jac.GetLength(1)];
|
---|
175 | for (int i = 0; i < arr.Length; i++) arr[i] = jac[rowIdx, i];
|
---|
176 | return new Vector(arr);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|