Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Vector.cs

Last change on this file was 17984, checked in by gkronber, 3 years ago

#3106 updated implementation based on the reply by Moscato

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