-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.cs
203 lines (181 loc) · 5.4 KB
/
Vector.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Linq;
namespace autocorrelation
{
class Vector
{
private double[] x;
public Vector(double[] data)
{
if (data.Length == 0) { throw new Exception("Dimension must be at least one"); }
x = data;
}
// Define the indexer to allow client code to use [] notation.
public double this[int i]
{
get { return x[i]; }
set { x[i] = value; }
}
public int Length { get => x.Length; }
public double Max => this.x.Aggregate(Double.MinValue, (x, y) => Math.Max(x, y));
public double Min => this.x.Aggregate(Double.MaxValue, (x, y) => Math.Min(x, y));
public double Sum => this.x.Aggregate(0.0, (x, y) => x + y);
public double Mean => this.Sum / this.Length;
public double Norm => Math.Sqrt(this.Dot(this));
public double Dot(Vector y)
{
if (this.Length != y.Length) { throw new Exception("Dimensions in dot product have to be equal"); }
var ret = 0.0;
for (int i = 0; i < x.Length; i++)
{
ret += x[i] * y[i];
}
return ret;
}
public static Vector operator *(Vector x, Vector y)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] * y[i];
}
return new Vector(ret);
}
public static Vector operator /(Vector x, Vector y)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] / y[i];
}
return new Vector(ret);
}
public static Vector operator +(Vector x, Vector y)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] + y[i];
}
return new Vector(ret);
}
public static Vector operator -(Vector x, Vector y)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] - y[i];
}
return new Vector(ret);
}
public static Vector operator *(Vector x, double a)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] * a;
}
return new Vector(ret);
}
public static Vector operator /(Vector x, double a)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] / a;
}
return new Vector(ret);
}
public static Vector operator +(Vector x, double a)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] + a;
}
return new Vector(ret);
}
public static Vector operator -(Vector x, double a)
{
var ret = new double[x.Length];
for (int i = 0; i < x.Length; i++)
{
ret[i] = x[i] - a;
}
return new Vector(ret);
}
public Vector SliceTo(int index)
{
int len;
if (index >= 0)
{
len = Math.Min(x.Length, index);
}
else
{
len = Math.Max(0, x.Length + index);
}
var ret = new double[len];
for (int i = 0; i < len; i++)
{
ret[i] = x[i];
}
return new Vector(ret);
}
public Vector SliceFrom(int index)
{
int start;
int len;
if (index >= 0)
{
start = index;
len = x.Length - Math.Min(x.Length, index);
}
else
{
start = x.Length + index;
len = -index;
}
var ret = new double[len];
for (int i = start; i < x.Length; i++)
{
ret[i - start] = x[i];
}
return new Vector(ret);
}
//c_{av}[k] = sum_n a[n+k] * conj(v[n])
public Vector Autocorrelate()
{
var c_av = new double[2 * x.Length - 1];
for (int k = 0; k < x.Length - 1; k++)
{
c_av[k] = 0;
for (int n = 0; n <= k; n++)
{
c_av[k] += x[n] * x[x.Length - k + n - 1];
}
}
for (int k = 1; k < x.Length; k++)
{
c_av[k + x.Length - 1] = 0;
for (int n = 0; n < x.Length - k; n++)
{
c_av[k + x.Length - 1] += x[k + n] * x[n];
}
}
c_av[x.Length - 1] = 0;
for (int n = 0; n < x.Length; n++)
{
c_av[x.Length - 1] += x[n] * x[n];
}
return new Vector(c_av);
}
override public string ToString()
{
return "["
+ String.Join(", ", x.Select(
xx => string.Format("{0}", xx)))
+ "]";
}
}
}