This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhankel.hhs
70 lines (63 loc) · 1.99 KB
/
hankel.hhs
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
/**
* @Author Jianan Lin (林家南)
* @param input: an array or some numbers, must be odd length
* @returns - a hankel matrix
* definition: https://en.wikipedia.org/wiki/Hankel_matrix
*
*/
function hankel(input) {
*import math: ndim
*import math: deep_copy
*import math: flatten
// argument check
if (arguments.length === 0) {
throw new Error('Exception occurred in hankel - no argument given');
}
// hankel(1, 2, 3)
if (typeof input === 'number') {
if (arguments.length % 2 === 0) {
throw new Error('Exception occurred in hankel - length of input must be odd');
}
let raw_in = [];
for (let i = 0; i < arguments.length; i++) {
raw_in.push(arguments[i]);
}
let mid = parseInt(raw_in.length / 2);
let result = new Array();
for (let i = 0; i <= mid; i++) {
result[i] = new Array();
for (let j = 0; j <= mid; j++) {
result[i][j] = 0;
}
}
for (let i = 0; i <= mid; i++) {
for (j = 0; j <= i; j++) {
result[i - j][j] = raw_in[i];
result[mid - i + j][mid - j] = raw_in[raw_in.length - i - 1];
}
}
return mat(result);
}
// hankel([1, 2, 3])
else {
let raw_in = flatten(input);
if (raw_in.length % 2 === 0) {
throw new Error('Exception occurred in hankel - length of input must be odd');
}
let mid = parseInt(raw_in.length / 2);
let result = new Array();
for (let i = 0; i <= mid; i++) {
result[i] = new Array();
for (let j = 0; j <= mid; j++) {
result[i][j] = 0;
}
}
for (let i = 0; i <= mid; i++) {
for (j = 0; j <= i; j++) {
result[i - j][j] = raw_in[i];
result[mid - i + j][mid - j] = raw_in[raw_in.length - i - 1];
}
}
return mat(result);
}
}