-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (85 loc) · 2 KB
/
Copy pathProgram.cs
File metadata and controls
100 lines (85 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class Vector
{
private double[] _elements;
public Vector()
{
_elements = new double[3];
}
public Vector(int dimension)
{
_elements = new double[dimension];
}
public Vector(double[] elements)
{
_elements = elements;
}
public double this[int index]
{
get { return _elements[index]; }
set { _elements[index] = value; }
}
public double[] Elements
{
get { return _elements; }
}
public int Dimension
{
get { return _elements.Length; }
}
public Vector Add(Vector other)
{
var result = new Vector(Dimension);
for (int i = 0; i < Dimension; i++)
{
result[i] = this[i] + other[i];
}
return result;
}
public Vector Subtract(Vector other)
{
var result = new Vector(Dimension);
for (int i = 0; i < Dimension; i++)
{
result[i] = this[i] - other[i];
}
return result;
}
public double Dot(Vector other)
{
double result = 0;
for (int i = 0; i < Dimension; i++)
{
result += this[i] * other[i];
}
return result;
}
public override string ToString()
{
return string.Join(", ", _elements);
}
public static Vector operator +(Vector a, Vector b)
{
return a.Add(b);
}
public static Vector operator -(Vector a, Vector b)
{
return a.Subtract(b);
}
public static double operator *(Vector a, Vector b)
{
return a.Dot(b);
}
public static Vector operator *(double c, Vector a)
{
var result = new Vector(a.Dimension);
for (int i = 0; i < a.Dimension; i++)
{
result[i] = c * a[i];
}
return result;
}
public static Vector operator *(Vector a, double c)
{
return c * a;
}
}