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 pathmissing.hhs
146 lines (131 loc) · 4.31 KB
/
missing.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
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
/**
* @Author Jianan Lin (林家南)
* @param input: a 1-2d JS array / matrix / tensor
* @param dim - 0 means row and 1 means column
* @param times - how many rows / columns created
* @returns - input with extra blank (missing) rows / columns
* According to the previous value, number - NaN, string - "",
* boolean - false, others - undefined (maybe null is also OK)
*
*/
function missing(input, dim = 1, times = 1) {
*import math: ndim
*import math: deep_copy
// argument check
if (arguments.length === 0) {
throw new Error('Exception occurred in missing - no argument given');
}
if (arguments.length > 3) {
throw new Error('Exception occurred in missing - wrong argument number');
}
// type check
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in missing - argument[0] must be JS array, matrix or tensor');
}
if (!(typeof times === 'number') || times <= 0 || parseInt(times) !== times) {
throw new Error('Exception occurred in missing - argument[2] must be a positive integer');
}
if (dim !== 0 && dim !== 1) {
throw new Error('Exception occurred in missing - argument[1] must be 0 or 1');
}
let in_type = input instanceof Mat || input instanceof Tensor;
let raw_in = in_type ? input.clone().val : deep_copy(input);
let size = ndim(raw_in);
if (size > 2) {
throw new Error('Exception occurred in missing - argument[0] must be no more than 2-dimensional');
}
if (size === 1) {
if (dim === 1) {
if (raw_in.length === 0) {
for (let i = 0; i < times; i++) {
raw_in.push(NaN);
}
}
else {
let temp = raw_in[raw_in.length - 1];
for (let i = 0; i < times; i++) {
let result = getMiss(temp);
raw_in.push(result);
}
}
}
else {
if (raw_in.length === 0) {
for (let i = 0; i < times; i++) {
raw_in.push([NaN]);
}
}
else {
let temp = deep_copy(raw_in);
for (let i = 0; i < raw_in.length; i++) {
temp[i] = getMiss(raw_in[i]);
}
raw_in = [raw_in];
for (let i = 0; i < times; i++) {
raw_in.push(temp);
}
}
}
}
// size == 2
else {
if (dim === 1) {
if (raw_in[0].length === 0) {
for (let i = 0; i < raw_in.length; i++) {
for (let j = 0; j < times; j++) {
raw_in[i].push(NaN);
}
}
}
else {
for (let i = 0; i < raw_in.length; i++) {
let temp = raw_in[i][raw_in[i].length - 1];
let result = getMiss(temp);
for (let j = 0; j < times; j++) {
raw_in[i].push(result);
}
}
}
}
// dim === 0
else {
if (raw_in[0].length === 0) {
for (let j = 0; j < times; j++) {
raw_in.push([]);
}
}
else {
let temp = [];
let result = [];
for (let j = 0; j < raw_in[raw_in.length - 1].length; j++) {
temp.push(raw_in[raw_in.length - 1][j]);
result.push(getMiss(temp[j]));
}
for (let i = 0; i < times; i++) {
raw_in.push(result);
}
}
}
}
return mat(raw_in);
function getMiss(x) {
if (x || x === 0) {
if (typeof x === 'number') {
return NaN;
}
else if (typeof x === 'string') {
return "";
}
else if (typeof x === 'boolean') {
return false;
}
else {
return undefined;
}
}
// there is a bug, typeof NaN will case an error
else {
return x;
}
}
}