1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Algorithms.DataAnalysis.ContinuedFractionRegression {
|
---|
8 | public class Vector {
|
---|
9 | private double[] _components;
|
---|
10 | private int _nDimensions;
|
---|
11 | public Vector(int dimensions) {
|
---|
12 | _components = new double[dimensions];
|
---|
13 | _nDimensions = dimensions;
|
---|
14 | }
|
---|
15 |
|
---|
16 | public int NDimensions {
|
---|
17 | get { return _nDimensions; }
|
---|
18 | }
|
---|
19 |
|
---|
20 | public double this[int index] {
|
---|
21 | get { return _components[index]; }
|
---|
22 | set { _components[index] = value; }
|
---|
23 | }
|
---|
24 |
|
---|
25 | public double[] Components {
|
---|
26 | get { return _components; }
|
---|
27 | }
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// Add another vector to this one
|
---|
31 | /// </summary>
|
---|
32 | /// <param name="v"></param>
|
---|
33 | /// <returns></returns>
|
---|
34 | public Vector Add(Vector v) {
|
---|
35 | if (v.NDimensions != this.NDimensions)
|
---|
36 | throw new ArgumentException("Can only add vectors of the same dimensionality");
|
---|
37 |
|
---|
38 | Vector vector = new Vector(v.NDimensions);
|
---|
39 | for (int i = 0; i < v.NDimensions; i++) {
|
---|
40 | vector[i] = this[i] + v[i];
|
---|
41 | }
|
---|
42 | return vector;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Subtract another vector from this one
|
---|
47 | /// </summary>
|
---|
48 | /// <param name="v"></param>
|
---|
49 | /// <returns></returns>
|
---|
50 | public Vector Subtract(Vector v) {
|
---|
51 | if (v.NDimensions != this.NDimensions)
|
---|
52 | throw new ArgumentException("Can only subtract vectors of the same dimensionality");
|
---|
53 |
|
---|
54 | Vector vector = new Vector(v.NDimensions);
|
---|
55 | for (int i = 0; i < v.NDimensions; i++) {
|
---|
56 | vector[i] = this[i] - v[i];
|
---|
57 | }
|
---|
58 | return vector;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Multiply this vector by a scalar value
|
---|
63 | /// </summary>
|
---|
64 | /// <param name="scalar"></param>
|
---|
65 | /// <returns></returns>
|
---|
66 | public Vector Multiply(double scalar) {
|
---|
67 | Vector scaledVector = new Vector(this.NDimensions);
|
---|
68 | for (int i = 0; i < this.NDimensions; i++) {
|
---|
69 | scaledVector[i] = this[i] * scalar;
|
---|
70 | }
|
---|
71 | return scaledVector;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Compute the dot product of this vector and the given vector
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="v"></param>
|
---|
78 | /// <returns></returns>
|
---|
79 | public double DotProduct(Vector v) {
|
---|
80 | if (v.NDimensions != this.NDimensions)
|
---|
81 | throw new ArgumentException("Can only compute dot product for vectors of the same dimensionality");
|
---|
82 |
|
---|
83 | double sum = 0;
|
---|
84 | for (int i = 0; i < v.NDimensions; i++) {
|
---|
85 | sum += this[i] * v[i];
|
---|
86 | }
|
---|
87 | return sum;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override string ToString() {
|
---|
91 | string[] components = new string[_components.Length];
|
---|
92 | for (int i = 0; i < components.Length; i++) {
|
---|
93 | components[i] = _components[i].ToString();
|
---|
94 | }
|
---|
95 | return "[ " + string.Join(", ", components) + " ]";
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|