-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathVectorBasicOperations.ts
243 lines (223 loc) · 5.66 KB
/
MathVectorBasicOperations.ts
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { SquareMatrix } from './SquareMatrix';
import { DenseMatrix } from './DenseMatrix';
/**
* Multiply a vector by a scalar
* @param vector vector
* @param value scalar
*/
export function multiplyVectorByScalar(vector: number[], value: number) {
let result: number[] = []
for (let i = 0; i < vector.length; i += 1) {
result.push(vector[i] * value)
}
return result
}
/**
* Divide a vector by a scalar
* @param vector Vector
* @param value Scalar
* @throws If the scalar value is zero
*/
export function divideVectorByScalar(vector: number[], value: number) {
if (value === 0) {
throw new Error("Division by zero")
}
let result: number[] = []
for (let i = 0; i < vector.length; i += 1) {
result.push(vector[i] / value)
}
return result
}
/**
* A standard function in basic linear algebra : y = ax + y
* @param a Scalar
* @param x Vector
* @param y Vector
* @throws If x and y have different length
*/
export function saxpy(a: number, x: number[], y: number[]) {
if (x.length !== y.length) {
throw new Error("Adding two vectors of different length")
}
for (let i = 0; i < x.length; i += 1) {
y[i] += a * x[i]
}
}
/**
* A standard function in basic linear algebra : z = ax + y
* @param a Scalar
* @param x Vector
* @param y Vector
* @returns ax + y
* @throws If x and y have different length
*/
export function saxpy2(a: number, x: number[], y: number[]) {
if (x.length !== y.length) {
throw new Error("Adding two vectors of different length")
}
let result: number[] = []
for (let i = 0; i < x.length; i += 1) {
result.push(a * x[i] + y[i])
}
return result
}
/**
* Compute the dot product of two vectors
* @param x Vector
* @param y Vector
* @return The scalar result
* @throws If x and y have different length
*/
export function dotProduct(x: number[], y: number[]) {
if (x.length !== y.length) {
throw new Error("Making the dot product of two vectors of different length");
}
let result = 0
for (let i = 0; i < x.length; i += 1) {
result += x[i] * y[i];
}
return result;
}
/**
* Add two vectors
* @param x Vector
* @param y Vector
* @return Vector
* @throws If x and y have different length
*/
export function addTwoVectors(x: number[], y: number[]) {
if (x.length !== y.length) {
throw new Error("Adding two vectors of different length");
}
let result: number[] = []
for (let i = 0; i < x.length; i += 1) {
result.push(x[i] + y[i]);
}
return result;
}
/**
* Add the second vector to the first vector
* @param x Vector
* @param y Vector
* @throws If x and y have different length
*/
export function addSecondVectorToFirst(x: number[], y: number[]) {
if (x.length !== y.length) {
throw new Error("Adding two vectors of different length");
}
for (let i = 0; i < x.length; i += 1) {
x[i] += y[i]
}
}
/**
* Compute the square of the norm
* @param v Vector
* @return Non negative scalar
*/
export function squaredNorm(v: number[]) {
let result = 0
for (let i = 0; i < v.length; i += 1) {
result += v[i] * v[i];
}
return result;
}
/**
* Compute the norm
* @param v Vector
* @return Non negative scalar
*/
export function norm(v: number[]) {
return Math.sqrt(squaredNorm(v))
}
/**
* Compute the norm p = 1
* @param v Vector
* @return Non negative scalar
*/
export function norm1(v: number[]) {
let result = 0
for (let i = 0; i < v.length; i += 1) {
result += Math.abs(v[i]);
}
return result;
}
/**
* Create a zero vector of size n
* @param n Size
*/
export function zeroVector(n: number) {
let result: number[] = []
for (let i = 0; i < n; i += 1) {
result.push(0);
}
return result;
};
/**
* Compute the product of a vector and its transpose
* @param v Vector
*/
export function product_v_vt(v: number[]) {
const n = v.length
let result = new SquareMatrix(n)
for (let i = 0; i < n; i += 1) {
for (let j = 0; j < n; j += 1) {
result.set(i, j, v[i] * v[j]);
}
}
return result;
}
/**
* Compute the product of a first vector with the transpose of a second vector
* @param v1 The first vector taken as a column vector
* @param v2 The second vector taken after transposition as a row vector
*/
export function product_v1_v2t(v1: number[], v2: number[]) {
const m = v1.length
const n = v2.length
let result = new DenseMatrix(m, n)
for (let i = 0; i < m; i += 1) {
for (let j = 0; j < n; j += 1) {
result.set(i, j, v1[i] * v2[j]);
}
}
return result;
}
export function isZeroVector(v: number[]) {
const n = v.length
for (let i = 0; i < v.length; i += 1) {
if (v[i] !== 0) {
return false;
}
}
return true;
}
/**
* Returns a vector filled with random values between 0 and 1
* @param n The size of the random vector
*/
export function randomVector(n: number){
let result: number[] = []
for (let i = 0; i < n; i += 1) {
result.push((Math.random()-0.5)*10e8)
//result.push((Math.random())*10e8)
}
return result
}
export function containsNaN(v: number[]) {
const n = v.length
for (let i = 0; i < v.length; i += 1) {
if (isNaN(v[i])) {
return true;
}
}
return false;
}
/**
* Return the sign of a number.
* It returns 1 if the number is positive, -1 if the number is negative and 0 if it is zero or minus zero
* The standard Math.sign() function doesn't work with Windows Internet Explorer
* @param x Number
*/
export function sign(x: number) {
return x ? x < 0 ? -1 : 1 : 0
}