|
| 1 | +/** |
| 2 | + * @Author Jianan Lin (林家南) |
| 3 | + * @param input: a 1-2d JS array / matrix / tensor |
| 4 | + * @param dim - 0 means row and 1 means column |
| 5 | + * @param times - how many rows / columns created |
| 6 | + * @returns - input with extra blank (missing) rows / columns |
| 7 | + * According to the previous value, number - NaN, string - "", |
| 8 | + * boolean - false, others - undefined (maybe null is also OK) |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +function missing(input, dim = 1, times = 1) { |
| 15 | + |
| 16 | + *import math: ndim |
| 17 | + *import math: deep_copy |
| 18 | + |
| 19 | + // argument check |
| 20 | + if (arguments.length === 0) { |
| 21 | + throw new Error('Exception occurred in missing - no argument given'); |
| 22 | + } |
| 23 | + if (arguments.length > 3) { |
| 24 | + throw new Error('Exception occurred in missing - wrong argument number'); |
| 25 | + } |
| 26 | + |
| 27 | + // type check |
| 28 | + if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) { |
| 29 | + throw new Error('Exception occurred in missing - argument[0] must be JS array, matrix or tensor'); |
| 30 | + } |
| 31 | + if (!(typeof times === 'number') || times <= 0 || parseInt(times) !== times) { |
| 32 | + throw new Error('Exception occurred in missing - argument[2] must be a positive integer'); |
| 33 | + } |
| 34 | + if (dim !== 0 && dim !== 1) { |
| 35 | + throw new Error('Exception occurred in missing - argument[1] must be 0 or 1'); |
| 36 | + } |
| 37 | + |
| 38 | + let in_type = input instanceof Mat || input instanceof Tensor; |
| 39 | + let raw_in = in_type ? input.clone().val : deep_copy(input); |
| 40 | + let size = ndim(raw_in); |
| 41 | + if (size > 2) { |
| 42 | + throw new Error('Exception occurred in missing - argument[0] must be no more than 2-dimensional'); |
| 43 | + } |
| 44 | + |
| 45 | + if (size === 1) { |
| 46 | + if (dim === 1) { |
| 47 | + if (raw_in.length === 0) { |
| 48 | + for (let i = 0; i < times; i++) { |
| 49 | + raw_in.push(NaN); |
| 50 | + } |
| 51 | + } |
| 52 | + else { |
| 53 | + let temp = raw_in[raw_in.length - 1]; |
| 54 | + for (let i = 0; i < times; i++) { |
| 55 | + let result = getMiss(temp); |
| 56 | + raw_in.push(result); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + else { |
| 62 | + if (raw_in.length === 0) { |
| 63 | + for (let i = 0; i < times; i++) { |
| 64 | + raw_in.push([NaN]); |
| 65 | + } |
| 66 | + } |
| 67 | + else { |
| 68 | + let temp = deep_copy(raw_in); |
| 69 | + for (let i = 0; i < raw_in.length; i++) { |
| 70 | + temp[i] = getMiss(raw_in[i]); |
| 71 | + } |
| 72 | + raw_in = [raw_in]; |
| 73 | + for (let i = 0; i < times; i++) { |
| 74 | + raw_in.push(temp); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // size == 2 |
| 81 | + else { |
| 82 | + if (dim === 1) { |
| 83 | + if (raw_in[0].length === 0) { |
| 84 | + for (let i = 0; i < raw_in.length; i++) { |
| 85 | + for (let j = 0; j < times; j++) { |
| 86 | + raw_in[i].push(NaN); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + else { |
| 92 | + for (let i = 0; i < raw_in.length; i++) { |
| 93 | + let temp = raw_in[i][raw_in[i].length - 1]; |
| 94 | + let result = getMiss(temp); |
| 95 | + for (let j = 0; j < times; j++) { |
| 96 | + raw_in[i].push(result); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // dim === 0 |
| 103 | + else { |
| 104 | + if (raw_in[0].length === 0) { |
| 105 | + for (let j = 0; j < times; j++) { |
| 106 | + raw_in.push([]); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + else { |
| 111 | + let temp = []; |
| 112 | + let result = []; |
| 113 | + for (let j = 0; j < raw_in[raw_in.length - 1].length; j++) { |
| 114 | + temp.push(raw_in[raw_in.length - 1][j]); |
| 115 | + result.push(getMiss(temp[j])); |
| 116 | + } |
| 117 | + for (let i = 0; i < times; i++) { |
| 118 | + raw_in.push(result); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return mat(raw_in); |
| 124 | + |
| 125 | + function getMiss(x) { |
| 126 | + if (x || x === 0) { |
| 127 | + if (typeof x === 'number') { |
| 128 | + return NaN; |
| 129 | + } |
| 130 | + else if (typeof x === 'string') { |
| 131 | + return ""; |
| 132 | + } |
| 133 | + else if (typeof x === 'boolean') { |
| 134 | + return false; |
| 135 | + } |
| 136 | + else { |
| 137 | + return undefined; |
| 138 | + } |
| 139 | + } |
| 140 | + // there is a bug, typeof NaN will case an error |
| 141 | + else { |
| 142 | + return x; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments