@mirascript/mirascript 0.1.76 → 0.1.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-ZRLBJK6A.js → chunk-BPFOJ4Q6.js} +499 -470
- package/dist/chunk-BPFOJ4Q6.js.map +6 -0
- package/dist/{chunk-VI74OKFU.js → chunk-LSPCAN32.js} +291 -226
- package/dist/chunk-LSPCAN32.js.map +6 -0
- package/dist/compiler/emit/constants.d.ts +1 -1
- package/dist/compiler/emit/constants.d.ts.map +1 -1
- package/dist/compiler/emit/consts.d.ts.map +1 -1
- package/dist/compiler/emit/index.d.ts.map +1 -1
- package/dist/compiler/emit-script.d.ts.map +1 -1
- package/dist/compiler/worker.js +1 -1
- package/dist/helpers/convert/{to-boolean.d.ts → boolean.d.ts} +1 -1
- package/dist/helpers/convert/boolean.d.ts.map +1 -0
- package/dist/helpers/convert/{to-format.d.ts → format.d.ts} +1 -1
- package/dist/helpers/convert/format.d.ts.map +1 -0
- package/dist/helpers/convert/index.d.ts +4 -4
- package/dist/helpers/convert/index.d.ts.map +1 -1
- package/dist/helpers/convert/{to-number.d.ts → number.d.ts} +1 -1
- package/dist/helpers/convert/number.d.ts.map +1 -0
- package/dist/helpers/convert/{to-string.d.ts → string.d.ts} +3 -1
- package/dist/helpers/convert/string.d.ts.map +1 -0
- package/dist/helpers/error.d.ts +1 -0
- package/dist/helpers/error.d.ts.map +1 -1
- package/dist/helpers/serialize.d.ts.map +1 -1
- package/dist/helpers/types/array-like.d.ts +8 -0
- package/dist/helpers/types/array-like.d.ts.map +1 -0
- package/dist/helpers/types/basic.d.ts +26 -0
- package/dist/helpers/types/basic.d.ts.map +1 -0
- package/dist/helpers/types/composed.d.ts +12 -0
- package/dist/helpers/types/composed.d.ts.map +1 -0
- package/dist/helpers/types/const.d.ts +6 -0
- package/dist/helpers/types/const.d.ts.map +1 -0
- package/dist/helpers/types/get-type.d.ts +6 -0
- package/dist/helpers/types/get-type.d.ts.map +1 -0
- package/dist/helpers/types/index.d.ts +6 -0
- package/dist/helpers/types/index.d.ts.map +1 -0
- package/dist/helpers/types/proto.d.ts +5 -0
- package/dist/helpers/types/proto.d.ts.map +1 -0
- package/dist/helpers/utils.d.ts +9 -0
- package/dist/helpers/utils.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/subtle.js +3 -3
- package/dist/vm/lib/global/debug.d.ts.map +1 -1
- package/dist/vm/lib/global/json.d.ts +3 -2
- package/dist/vm/lib/global/json.d.ts.map +1 -1
- package/dist/vm/lib/global/math/tgamma.d.ts.map +1 -1
- package/dist/vm/lib/global/sequence/entries.d.ts +1 -1
- package/dist/vm/lib/global/sequence/entries.d.ts.map +1 -1
- package/dist/vm/lib/global/sequence/fold.d.ts +3 -0
- package/dist/vm/lib/global/sequence/fold.d.ts.map +1 -0
- package/dist/vm/lib/global/sequence/group.d.ts +3 -0
- package/dist/vm/lib/global/sequence/group.d.ts.map +1 -0
- package/dist/vm/lib/global/sequence/index.d.ts +2 -0
- package/dist/vm/lib/global/sequence/index.d.ts.map +1 -1
- package/dist/vm/lib/global/sequence/new.d.ts.map +1 -1
- package/dist/vm/lib/global/sequence/zip.d.ts +2 -2
- package/dist/vm/lib/global/sequence/zip.d.ts.map +1 -1
- package/dist/vm/lib/helpers.d.ts +2 -2
- package/dist/vm/lib/helpers.d.ts.map +1 -1
- package/dist/vm/lib/mod/index.d.ts +1 -1
- package/dist/vm/lib/mod/index.d.ts.map +1 -1
- package/dist/vm/lib/mod/matrix/bin-ops.d.ts +8 -0
- package/dist/vm/lib/mod/matrix/bin-ops.d.ts.map +1 -0
- package/dist/vm/lib/mod/matrix/fill.d.ts +6 -0
- package/dist/vm/lib/mod/matrix/fill.d.ts.map +1 -0
- package/dist/vm/lib/mod/matrix/helpers.d.ts +6 -0
- package/dist/vm/lib/mod/matrix/helpers.d.ts.map +1 -0
- package/dist/vm/lib/mod/matrix/index.d.ts +6 -0
- package/dist/vm/lib/mod/matrix/index.d.ts.map +1 -0
- package/dist/vm/lib/mod/matrix/invert.d.ts +3 -0
- package/dist/vm/lib/mod/matrix/invert.d.ts.map +1 -0
- package/dist/vm/lib/mod/matrix/size.d.ts +2 -0
- package/dist/vm/lib/mod/matrix/size.d.ts.map +1 -0
- package/dist/vm/lib/mod/matrix/transpose.d.ts +2 -0
- package/dist/vm/lib/mod/matrix/transpose.d.ts.map +1 -0
- package/dist/vm/operations/array-range.d.ts +3 -3
- package/dist/vm/operations/array-range.d.ts.map +1 -1
- package/dist/vm/operations/call.d.ts +1 -1
- package/dist/vm/operations/call.d.ts.map +1 -1
- package/dist/vm/operations/compound.d.ts +1 -13
- package/dist/vm/operations/compound.d.ts.map +1 -1
- package/dist/vm/operations/index.d.ts +3 -0
- package/dist/vm/operations/index.d.ts.map +1 -1
- package/dist/vm/operations/iterable.d.ts +6 -0
- package/dist/vm/operations/iterable.d.ts.map +1 -0
- package/dist/vm/operations/record.d.ts +6 -0
- package/dist/vm/operations/record.d.ts.map +1 -0
- package/dist/vm/operations/slice.d.ts.map +1 -1
- package/dist/vm/operations/spread.d.ts +6 -0
- package/dist/vm/operations/spread.d.ts.map +1 -0
- package/dist/vm/operations/utils.d.ts.map +1 -1
- package/dist/vm/types/extern/access.d.ts +5 -0
- package/dist/vm/types/extern/access.d.ts.map +1 -0
- package/dist/vm/types/{extern.d.ts → extern/index.d.ts} +3 -3
- package/dist/vm/types/extern/index.d.ts.map +1 -0
- package/dist/vm/types/extern/tag.d.ts +3 -0
- package/dist/vm/types/extern/tag.d.ts.map +1 -0
- package/dist/vm/types/extern/utils.d.ts +13 -0
- package/dist/vm/types/extern/utils.d.ts.map +1 -0
- package/dist/vm/types/index.d.ts +3 -3
- package/dist/vm/types/index.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/compiler/compile-fast.ts +1 -1
- package/src/compiler/diagnostic.ts +2 -2
- package/src/compiler/emit/constants.ts +1 -1
- package/src/compiler/emit/consts.ts +0 -1
- package/src/compiler/emit/index.ts +1 -7
- package/src/compiler/emit/sourcemap.ts +1 -1
- package/src/compiler/emit-script.ts +1 -0
- package/src/compiler/load-module.ts +5 -1
- package/src/helpers/convert/{to-boolean.ts → boolean.ts} +1 -1
- package/src/helpers/convert/{to-format.ts → format.ts} +1 -1
- package/src/helpers/convert/index.ts +4 -4
- package/src/helpers/convert/{to-number.ts → number.ts} +16 -32
- package/src/helpers/convert/{to-string.ts → string.ts} +5 -4
- package/src/helpers/error.ts +3 -1
- package/src/helpers/serialize.ts +45 -44
- package/src/helpers/types/array-like.ts +32 -0
- package/src/helpers/types/basic.ts +82 -0
- package/src/helpers/types/composed.ts +27 -0
- package/src/helpers/types/const.ts +44 -0
- package/src/helpers/types/get-type.ts +16 -0
- package/src/helpers/types/index.ts +5 -0
- package/src/helpers/types/proto.ts +21 -0
- package/src/helpers/utils.ts +33 -0
- package/src/vm/lib/global/debug.ts +4 -8
- package/src/vm/lib/global/json.ts +21 -9
- package/src/vm/lib/global/math/tgamma.ts +8 -14
- package/src/vm/lib/global/sequence/all-any.ts +1 -1
- package/src/vm/lib/global/sequence/entries.ts +6 -1
- package/src/vm/lib/global/sequence/find.ts +1 -1
- package/src/vm/lib/global/sequence/fold.ts +30 -0
- package/src/vm/lib/global/sequence/group.ts +37 -0
- package/src/vm/lib/global/sequence/index.ts +2 -0
- package/src/vm/lib/global/sequence/map-filter.ts +3 -3
- package/src/vm/lib/global/sequence/new.ts +9 -3
- package/src/vm/lib/global/sequence/sort.ts +1 -1
- package/src/vm/lib/global/sequence/unique.ts +1 -1
- package/src/vm/lib/global/sequence/with.ts +5 -5
- package/src/vm/lib/global/sequence/zip.ts +21 -10
- package/src/vm/lib/global/time.ts +1 -1
- package/src/vm/lib/helpers.ts +12 -5
- package/src/vm/lib/mod/index.ts +1 -1
- package/src/vm/lib/mod/matrix/bin-ops.ts +291 -0
- package/src/vm/lib/mod/matrix/fill.ts +109 -0
- package/src/vm/lib/mod/matrix/helpers.ts +26 -0
- package/src/vm/lib/mod/matrix/index.ts +5 -0
- package/src/vm/lib/mod/matrix/invert.ts +122 -0
- package/src/vm/lib/mod/matrix/size.ts +15 -0
- package/src/vm/lib/mod/matrix/transpose.ts +32 -0
- package/src/vm/operations/array-range.ts +15 -27
- package/src/vm/operations/call.ts +2 -2
- package/src/vm/operations/compound.ts +5 -125
- package/src/vm/operations/helpers.ts +1 -1
- package/src/vm/operations/index.ts +3 -0
- package/src/vm/operations/iterable.ts +40 -0
- package/src/vm/operations/record.ts +33 -0
- package/src/vm/operations/slice.ts +23 -22
- package/src/vm/operations/spread.ts +57 -0
- package/src/vm/operations/type-check.ts +1 -1
- package/src/vm/operations/utils.ts +45 -37
- package/src/vm/types/boundary.ts +2 -2
- package/src/vm/types/extern/access.ts +24 -0
- package/src/vm/types/extern/index.ts +124 -0
- package/src/vm/types/extern/tag.ts +58 -0
- package/src/vm/types/extern/utils.ts +55 -0
- package/src/vm/types/function.ts +1 -1
- package/src/vm/types/index.ts +3 -3
- package/dist/chunk-VI74OKFU.js.map +0 -6
- package/dist/chunk-ZRLBJK6A.js.map +0 -6
- package/dist/helpers/convert/to-boolean.d.ts.map +0 -1
- package/dist/helpers/convert/to-format.d.ts.map +0 -1
- package/dist/helpers/convert/to-number.d.ts.map +0 -1
- package/dist/helpers/convert/to-string.d.ts.map +0 -1
- package/dist/helpers/types.d.ts +0 -58
- package/dist/helpers/types.d.ts.map +0 -1
- package/dist/vm/lib/mod/matrix.d.ts +0 -15
- package/dist/vm/lib/mod/matrix.d.ts.map +0 -1
- package/dist/vm/types/extern.d.ts.map +0 -1
- package/src/helpers/types.ts +0 -283
- package/src/vm/lib/mod/matrix.ts +0 -590
- package/src/vm/types/extern.ts +0 -197
package/src/vm/lib/mod/matrix.ts
DELETED
|
@@ -1,590 +0,0 @@
|
|
|
1
|
-
import { toNumber } from '../../../helpers/convert/to-number.js';
|
|
2
|
-
import { isArray } from '../../../helpers/utils.js';
|
|
3
|
-
import { Cp } from '../../checkpoint.js';
|
|
4
|
-
import { $Add, $Call, $Div, $Mul, $Sub } from '../../operations/index.js';
|
|
5
|
-
import { isVmArray, isVmConst, type VmAny, type VmArray, type VmConst, type VmValue } from '../../types/index.js';
|
|
6
|
-
import {
|
|
7
|
-
VmLib,
|
|
8
|
-
expectArray,
|
|
9
|
-
expectCallable,
|
|
10
|
-
expectConst,
|
|
11
|
-
required,
|
|
12
|
-
throwError,
|
|
13
|
-
getNumbers,
|
|
14
|
-
arrayLen,
|
|
15
|
-
map,
|
|
16
|
-
expectInteger,
|
|
17
|
-
} from '../helpers.js';
|
|
18
|
-
|
|
19
|
-
/** 计算尺寸 */
|
|
20
|
-
function sizeImpl(matrix: VmValue): [] | [number] | [number, number] {
|
|
21
|
-
if (!isVmArray(matrix)) return [];
|
|
22
|
-
if (matrix.length === 0) return [0];
|
|
23
|
-
|
|
24
|
-
const numRows = matrix.length;
|
|
25
|
-
let numCols = 0;
|
|
26
|
-
|
|
27
|
-
for (const row of matrix) {
|
|
28
|
-
if (isVmArray(row)) {
|
|
29
|
-
numCols = Math.max(numCols, row.length);
|
|
30
|
-
} else {
|
|
31
|
-
return [numRows];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return [numRows, numCols];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** 数组元素转为 number */
|
|
39
|
-
function num(v: VmConst | undefined): number {
|
|
40
|
-
return toNumber(v, undefined);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const size = VmLib(
|
|
44
|
-
(matrix) => {
|
|
45
|
-
required('matrix', matrix, []);
|
|
46
|
-
return sizeImpl(matrix);
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
summary: '获取矩阵尺寸',
|
|
50
|
-
params: { matrix: { type: 'any[][]', description: '要获取尺寸的矩阵' } },
|
|
51
|
-
returns: { type: '[number, number]' },
|
|
52
|
-
examples: ['matrix.size([[1, 2], [3, 4]]) // [2, 2]'],
|
|
53
|
-
},
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
export const transpose = VmLib(
|
|
57
|
-
(matrix) => {
|
|
58
|
-
required('matrix', matrix, []);
|
|
59
|
-
const [numRows, numCols] = sizeImpl(matrix);
|
|
60
|
-
if (numRows == null || numCols == null) return matrix; // 一维数组或空数组无需转置
|
|
61
|
-
|
|
62
|
-
const transposed: VmConst[][] = [];
|
|
63
|
-
for (let j = 0; j < numCols; j++) {
|
|
64
|
-
Cp();
|
|
65
|
-
const tj = [];
|
|
66
|
-
for (let i = 0; i < numRows; i++) {
|
|
67
|
-
const row = (matrix as VmConst[][])[i] ?? null;
|
|
68
|
-
const item = row?.[j] ?? null;
|
|
69
|
-
tj[i] = item;
|
|
70
|
-
}
|
|
71
|
-
transposed[j] = tj;
|
|
72
|
-
}
|
|
73
|
-
return transposed;
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
summary: '转置矩阵',
|
|
77
|
-
params: { matrix: { type: 'any[][]', description: '要转置的矩阵' } },
|
|
78
|
-
returns: { type: 'any[][]' },
|
|
79
|
-
examples: ['matrix.transpose([[1, 2], [3, 4]]) // [[1, 3], [2, 4]]'],
|
|
80
|
-
},
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
/** 逐项操作 */
|
|
84
|
-
function entrywiseImpl(
|
|
85
|
-
a: VmConst,
|
|
86
|
-
b: VmConst,
|
|
87
|
-
f: (a: VmConst, b: VmConst) => VmConst,
|
|
88
|
-
vvf?: (va: readonly VmConst[], vb: readonly VmConst[], ar: number, br: number) => VmConst,
|
|
89
|
-
mmf?: (
|
|
90
|
-
ma: readonly VmConst[][],
|
|
91
|
-
mb: readonly VmConst[][],
|
|
92
|
-
ar: number,
|
|
93
|
-
ac: number,
|
|
94
|
-
br: number,
|
|
95
|
-
bc: number,
|
|
96
|
-
) => VmConst,
|
|
97
|
-
vmf?: (va: readonly VmConst[], mb: readonly VmConst[][], al: number, br: number, bc: number) => VmConst,
|
|
98
|
-
mvf?: (ma: readonly VmConst[][], vb: readonly VmConst[], ar: number, ac: number, bl: number) => VmConst,
|
|
99
|
-
): VmConst {
|
|
100
|
-
let [ar, ac] = sizeImpl(a);
|
|
101
|
-
let [br, bc] = sizeImpl(b);
|
|
102
|
-
|
|
103
|
-
if (ar == null) {
|
|
104
|
-
if (br == null) {
|
|
105
|
-
// s/s
|
|
106
|
-
return f(a, b);
|
|
107
|
-
} else if (bc == null) {
|
|
108
|
-
// s/v
|
|
109
|
-
const result: VmConst[] = [];
|
|
110
|
-
for (let r = 0; r < br; r++) {
|
|
111
|
-
const bItem = (b as VmConst[])[r] ?? null;
|
|
112
|
-
result[r] = f(a, bItem);
|
|
113
|
-
}
|
|
114
|
-
return result;
|
|
115
|
-
} else {
|
|
116
|
-
// s/m
|
|
117
|
-
const result: VmConst[][] = [];
|
|
118
|
-
for (let r = 0; r < br; r++) {
|
|
119
|
-
const bRow = (b as VmConst[][])[r] ?? [];
|
|
120
|
-
const rRow: VmConst[] = [];
|
|
121
|
-
result[r] = rRow;
|
|
122
|
-
for (let c = 0; c < bc; c++) {
|
|
123
|
-
const bItem = bRow[c] ?? null;
|
|
124
|
-
rRow[c] = f(a, bItem);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return result;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (br == null) {
|
|
131
|
-
if (ac == null) {
|
|
132
|
-
// v/s
|
|
133
|
-
const result: VmConst[] = [];
|
|
134
|
-
for (let r = 0; r < ar; r++) {
|
|
135
|
-
const aItem = (a as VmConst[])[r] ?? null;
|
|
136
|
-
result[r] = f(aItem, b);
|
|
137
|
-
}
|
|
138
|
-
return result;
|
|
139
|
-
} else {
|
|
140
|
-
// m/s
|
|
141
|
-
const result: VmConst[][] = [];
|
|
142
|
-
for (let r = 0; r < ar; r++) {
|
|
143
|
-
const aRow = (a as VmConst[][])[r] ?? [];
|
|
144
|
-
const rRow: VmConst[] = [];
|
|
145
|
-
result[r] = rRow;
|
|
146
|
-
for (let c = 0; c < ac; c++) {
|
|
147
|
-
const aItem = aRow[c] ?? null;
|
|
148
|
-
rRow[c] = f(aItem, b);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return result;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
if (ac == null && bc == null) {
|
|
155
|
-
// v/v
|
|
156
|
-
if (vvf != null) {
|
|
157
|
-
return vvf(a as VmConst[], b as VmConst[], ar, br);
|
|
158
|
-
}
|
|
159
|
-
const rr = Math.max(ar, br);
|
|
160
|
-
const result: VmConst[] = [];
|
|
161
|
-
for (let r = 0; r < rr; r++) {
|
|
162
|
-
const aItem = (a as VmConst[])[r] ?? null;
|
|
163
|
-
const bItem = (b as VmConst[])[r] ?? null;
|
|
164
|
-
result[r] = f(aItem, bItem);
|
|
165
|
-
}
|
|
166
|
-
return result;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// m/m (m/v v/m)
|
|
170
|
-
if (ac == null) {
|
|
171
|
-
// v/m
|
|
172
|
-
if (vmf != null) {
|
|
173
|
-
return vmf(a as VmConst[], b as VmConst[][], ar, br, bc!);
|
|
174
|
-
}
|
|
175
|
-
ac = ar!;
|
|
176
|
-
ar = 1;
|
|
177
|
-
a = [a];
|
|
178
|
-
}
|
|
179
|
-
if (bc == null) {
|
|
180
|
-
// m/v
|
|
181
|
-
if (mvf != null) {
|
|
182
|
-
return mvf(a as VmConst[][], b as VmConst[], ar, ac, br);
|
|
183
|
-
}
|
|
184
|
-
bc = br!;
|
|
185
|
-
br = 1;
|
|
186
|
-
b = [b];
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (mmf != null) {
|
|
190
|
-
return mmf(a as VmConst[][], b as VmConst[][], ar, ac, br, bc);
|
|
191
|
-
}
|
|
192
|
-
const rr = Math.max(ar, br);
|
|
193
|
-
const rc = Math.max(ac, bc);
|
|
194
|
-
const result: VmConst[][] = [];
|
|
195
|
-
for (let r = 0; r < rr; r++) {
|
|
196
|
-
const rRow: VmConst[] = [];
|
|
197
|
-
result[r] = rRow;
|
|
198
|
-
for (let c = 0; c < rc; c++) {
|
|
199
|
-
const aItem = (a as VmConst[][])[ar === 1 ? 0 : r]?.[ac === 1 ? 0 : c] ?? null;
|
|
200
|
-
const bItem = (b as VmConst[][])[br === 1 ? 0 : r]?.[bc === 1 ? 0 : c] ?? null;
|
|
201
|
-
rRow[c] = f(aItem, bItem);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return result;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export const entrywise = VmLib(
|
|
208
|
-
(a, b, f) => {
|
|
209
|
-
expectConst('a', a, null);
|
|
210
|
-
expectConst('b', b, null);
|
|
211
|
-
expectCallable('f', f, null);
|
|
212
|
-
return entrywiseImpl(a, b, (a, b) => {
|
|
213
|
-
Cp();
|
|
214
|
-
const ret = $Call(f, [a, b]);
|
|
215
|
-
if (!isVmConst(ret)) return null;
|
|
216
|
-
return ret;
|
|
217
|
-
});
|
|
218
|
-
},
|
|
219
|
-
{
|
|
220
|
-
summary: '逐项操作',
|
|
221
|
-
params: {
|
|
222
|
-
a: { type: 'any | any[] | any[][]', description: '第一个操作数' },
|
|
223
|
-
b: { type: 'any | any[] | any[][]', description: '第二个操作数' },
|
|
224
|
-
f: { type: 'fn(a: any, b: any) -> any', description: '操作函数' },
|
|
225
|
-
},
|
|
226
|
-
returns: { type: 'any | any[] | any[][]' },
|
|
227
|
-
examples: [`matrix.entrywise([1, 2], [3, 4], fn (x, y) { x + y }) // [4, 6]`],
|
|
228
|
-
},
|
|
229
|
-
);
|
|
230
|
-
|
|
231
|
-
export const add = VmLib(
|
|
232
|
-
(a, b) => {
|
|
233
|
-
expectConst('a', a, null);
|
|
234
|
-
expectConst('b', b, null);
|
|
235
|
-
return entrywiseImpl(a, b, $Add);
|
|
236
|
-
},
|
|
237
|
-
{
|
|
238
|
-
summary: '逐项相加',
|
|
239
|
-
params: {
|
|
240
|
-
a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
|
|
241
|
-
b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
|
|
242
|
-
},
|
|
243
|
-
returns: { type: 'number | number[] | number[][]' },
|
|
244
|
-
examples: ['matrix.add([1, 2], [3, 4]) // [4, 6]'],
|
|
245
|
-
},
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
export const subtract = VmLib(
|
|
249
|
-
(a, b) => {
|
|
250
|
-
expectConst('a', a, null);
|
|
251
|
-
expectConst('b', b, null);
|
|
252
|
-
return entrywiseImpl(a, b, $Sub);
|
|
253
|
-
},
|
|
254
|
-
{
|
|
255
|
-
summary: '逐项相减',
|
|
256
|
-
params: {
|
|
257
|
-
a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
|
|
258
|
-
b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
|
|
259
|
-
},
|
|
260
|
-
returns: { type: 'number | number[] | number[][]' },
|
|
261
|
-
examples: ['matrix.subtract([3, 4], [1, 2]) // [2, 2]'],
|
|
262
|
-
},
|
|
263
|
-
);
|
|
264
|
-
|
|
265
|
-
export const entrywise_multiply = VmLib(
|
|
266
|
-
(a, b) => {
|
|
267
|
-
expectConst('a', a, null);
|
|
268
|
-
expectConst('b', b, null);
|
|
269
|
-
return entrywiseImpl(a, b, $Mul);
|
|
270
|
-
},
|
|
271
|
-
{
|
|
272
|
-
summary: '逐项相乘',
|
|
273
|
-
params: {
|
|
274
|
-
a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
|
|
275
|
-
b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
|
|
276
|
-
},
|
|
277
|
-
returns: { type: 'number | number[] | number[][]' },
|
|
278
|
-
examples: ['matrix.entrywise_multiply([1, 2], [3, 4]) // [3, 8]'],
|
|
279
|
-
},
|
|
280
|
-
);
|
|
281
|
-
|
|
282
|
-
export const entrywise_divide = VmLib(
|
|
283
|
-
(a, b) => {
|
|
284
|
-
expectConst('a', a, null);
|
|
285
|
-
expectConst('b', b, null);
|
|
286
|
-
return entrywiseImpl(a, b, $Div);
|
|
287
|
-
},
|
|
288
|
-
{
|
|
289
|
-
summary: '逐项相除',
|
|
290
|
-
params: {
|
|
291
|
-
a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
|
|
292
|
-
b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
|
|
293
|
-
},
|
|
294
|
-
returns: { type: 'number | number[] | number[][]' },
|
|
295
|
-
examples: ['matrix.entrywise_divide([4, 6], [2, 3]) // [2, 2]'],
|
|
296
|
-
},
|
|
297
|
-
);
|
|
298
|
-
|
|
299
|
-
export const multiply = VmLib(
|
|
300
|
-
(a, b) => {
|
|
301
|
-
expectConst('a', a, null);
|
|
302
|
-
expectConst('b', b, null);
|
|
303
|
-
return entrywiseImpl(
|
|
304
|
-
a,
|
|
305
|
-
b,
|
|
306
|
-
$Mul,
|
|
307
|
-
(a, b, al, bl) => {
|
|
308
|
-
const l = Math.max(al, bl);
|
|
309
|
-
let s = 0;
|
|
310
|
-
for (let i = 0; i < l; i++) {
|
|
311
|
-
s += num(a[i]) * num(b[i]);
|
|
312
|
-
}
|
|
313
|
-
return s;
|
|
314
|
-
},
|
|
315
|
-
(a, b, ar, ac, br, bc) => {
|
|
316
|
-
if (ac !== br) throwError(`Incompatible matrix dimensions`, null);
|
|
317
|
-
const result: VmConst[][] = [];
|
|
318
|
-
for (let r = 0; r < ar; r++) {
|
|
319
|
-
const rRow: VmConst[] = [];
|
|
320
|
-
result[r] = rRow;
|
|
321
|
-
for (let c = 0; c < bc; c++) {
|
|
322
|
-
let item = 0;
|
|
323
|
-
for (let k = 0; k < ac; k++) {
|
|
324
|
-
item += num((a as VmConst[][])[r]?.[k]) * num((b as VmConst[][])[k]?.[c]);
|
|
325
|
-
}
|
|
326
|
-
rRow[c] = item;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
return result;
|
|
330
|
-
},
|
|
331
|
-
(a, b, al, br, bc) => {
|
|
332
|
-
if (al !== br) throwError(`Incompatible matrix dimensions`, null);
|
|
333
|
-
const result: VmConst[] = [];
|
|
334
|
-
for (let c = 0; c < bc; c++) {
|
|
335
|
-
let item = 0;
|
|
336
|
-
for (let k = 0; k < al; k++) {
|
|
337
|
-
item += num(a[k]) * num((b as VmConst[][])[k]?.[c]);
|
|
338
|
-
}
|
|
339
|
-
result[c] = item;
|
|
340
|
-
}
|
|
341
|
-
return result;
|
|
342
|
-
},
|
|
343
|
-
(a, b, ar, ac, bl) => {
|
|
344
|
-
if (ac !== bl) throwError(`Incompatible matrix dimensions`, null);
|
|
345
|
-
const result: VmConst[] = [];
|
|
346
|
-
for (let r = 0; r < ar; r++) {
|
|
347
|
-
let item = 0;
|
|
348
|
-
for (let k = 0; k < ac; k++) {
|
|
349
|
-
item += num((a as VmConst[][])[r]?.[k]) * num(b[k]);
|
|
350
|
-
}
|
|
351
|
-
result[r] = item;
|
|
352
|
-
}
|
|
353
|
-
return result;
|
|
354
|
-
},
|
|
355
|
-
);
|
|
356
|
-
},
|
|
357
|
-
{
|
|
358
|
-
summary: '矩阵相乘',
|
|
359
|
-
params: {
|
|
360
|
-
a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
|
|
361
|
-
b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
|
|
362
|
-
},
|
|
363
|
-
returns: { type: 'number | number[] | number[][]' },
|
|
364
|
-
examples: ['matrix.multiply([[1, 2], [3, 4]], [5, 6]) // [17, 39]'],
|
|
365
|
-
},
|
|
366
|
-
);
|
|
367
|
-
|
|
368
|
-
export const invert = VmLib(
|
|
369
|
-
(a) => {
|
|
370
|
-
expectConst('a', a, null);
|
|
371
|
-
const [rows, cols] = sizeImpl(a);
|
|
372
|
-
if (rows == null) return 1 / num(a); // 标量取倒数
|
|
373
|
-
if (cols == null) return map(a, (v) => 1 / num(v)); // 向量按元素取倒数
|
|
374
|
-
|
|
375
|
-
if (rows !== cols) throwError(`Matrix must be square`, a);
|
|
376
|
-
const m = a as VmConst[][];
|
|
377
|
-
// https://github.com/josdejong/mathjs
|
|
378
|
-
if (rows === 1) {
|
|
379
|
-
// 1x1 矩阵
|
|
380
|
-
const e = num(m[0]?.[0]);
|
|
381
|
-
// if (e === 0) {
|
|
382
|
-
// throwError(`Matrix is singular`, null);
|
|
383
|
-
// }
|
|
384
|
-
return [[1 / e]];
|
|
385
|
-
}
|
|
386
|
-
if (rows === 2) {
|
|
387
|
-
// 2x2 矩阵
|
|
388
|
-
const a = num(m[0]?.[0]);
|
|
389
|
-
const b = num(m[0]?.[1]);
|
|
390
|
-
const c = num(m[1]?.[0]);
|
|
391
|
-
const d = num(m[1]?.[1]);
|
|
392
|
-
|
|
393
|
-
const det = a * d - b * c;
|
|
394
|
-
// if (det === 0) throwError(`Matrix is singular`, null);
|
|
395
|
-
return [
|
|
396
|
-
[d / det, -b / det],
|
|
397
|
-
[-c / det, a / det],
|
|
398
|
-
];
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// 更高阶矩阵 使用高斯消元法
|
|
402
|
-
|
|
403
|
-
// 初始化输入
|
|
404
|
-
const A: number[][] = [];
|
|
405
|
-
// 初始化结果为单位矩阵
|
|
406
|
-
const B: number[][] = [];
|
|
407
|
-
for (let r = 0; r < rows; r++) {
|
|
408
|
-
const Ar: number[] = [];
|
|
409
|
-
const Br: number[] = [];
|
|
410
|
-
A[r] = Ar;
|
|
411
|
-
B[r] = Br;
|
|
412
|
-
for (let c = 0; c < cols; c++) {
|
|
413
|
-
Ar[c] = num(m[r]?.[c]);
|
|
414
|
-
Br[c] = r === c ? 1 : 0;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// loop over all columns, and perform row reductions
|
|
419
|
-
for (let c = 0; c < cols; c++) {
|
|
420
|
-
// Pivoting: Swap row c with row r, where row r contains the largest element A[r][c]
|
|
421
|
-
let ABig = Math.abs(A[c]![c]!);
|
|
422
|
-
let rBig = c;
|
|
423
|
-
let r = c + 1;
|
|
424
|
-
while (r < rows) {
|
|
425
|
-
if (Math.abs(A[r]![c]!) > ABig) {
|
|
426
|
-
ABig = Math.abs(A[r]![c]!);
|
|
427
|
-
rBig = r;
|
|
428
|
-
}
|
|
429
|
-
r++;
|
|
430
|
-
}
|
|
431
|
-
// if (ABig === 0) {
|
|
432
|
-
// throwError(`Matrix is singular`, null);
|
|
433
|
-
// }
|
|
434
|
-
r = rBig;
|
|
435
|
-
if (r !== c) {
|
|
436
|
-
const temp1 = A[c]!;
|
|
437
|
-
A[c] = A[r]!;
|
|
438
|
-
A[r] = temp1;
|
|
439
|
-
const temp2 = B[c]!;
|
|
440
|
-
B[c] = B[r]!;
|
|
441
|
-
B[r] = temp2;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// eliminate non-zero values on the other rows at column c
|
|
445
|
-
const Ac = A[c]!;
|
|
446
|
-
const Bc = B[c]!;
|
|
447
|
-
for (r = 0; r < rows; r++) {
|
|
448
|
-
const Ar = A[r]!;
|
|
449
|
-
const Br = B[r]!;
|
|
450
|
-
if (r !== c) {
|
|
451
|
-
// eliminate value at column c and row r
|
|
452
|
-
if (Ar[c] !== 0) {
|
|
453
|
-
const f = -Ar[c]! / Ac[c]!;
|
|
454
|
-
|
|
455
|
-
// add (f * row c) to row r to eliminate the value
|
|
456
|
-
// at column c
|
|
457
|
-
for (let s = c; s < cols; s++) {
|
|
458
|
-
Ar[s] = Ar[s]! + f * Ac[s]!;
|
|
459
|
-
}
|
|
460
|
-
for (let s = 0; s < cols; s++) {
|
|
461
|
-
Br[s] = Br[s]! + f * Bc[s]!;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
} else {
|
|
465
|
-
// normalize value at Acc to 1,
|
|
466
|
-
// divide each value on row r with the value at Acc
|
|
467
|
-
const f = Ac[c]!;
|
|
468
|
-
for (let s = c; s < cols; s++) {
|
|
469
|
-
Ar[s] = Ar[s]! / f;
|
|
470
|
-
}
|
|
471
|
-
for (let s = 0; s < cols; s++) {
|
|
472
|
-
Br[s] = Br[s]! / f;
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
return B;
|
|
478
|
-
},
|
|
479
|
-
{
|
|
480
|
-
summary: '矩阵求逆',
|
|
481
|
-
params: { a: { type: 'number | number[][]', description: '待求逆的矩阵' } },
|
|
482
|
-
returns: { type: 'number | number[][]' },
|
|
483
|
-
examples: ['matrix.invert([[1, 2], [3, 4]]) // [[-2, 1], [1.5, -0.5]]'],
|
|
484
|
-
},
|
|
485
|
-
);
|
|
486
|
-
|
|
487
|
-
/** 填充 */
|
|
488
|
-
function filled(size: readonly VmAny[], value: VmConst): VmArray {
|
|
489
|
-
const s = getNumbers(size);
|
|
490
|
-
if (s.length === 0) return [];
|
|
491
|
-
while (s.length > 0) {
|
|
492
|
-
const repeat = arrayLen(s.pop());
|
|
493
|
-
Cp();
|
|
494
|
-
const data: VmConst[] = [];
|
|
495
|
-
data.length = repeat;
|
|
496
|
-
// 从 MiraScript 语义而言,可以使用同一个引用
|
|
497
|
-
data.fill(value);
|
|
498
|
-
value = data;
|
|
499
|
-
}
|
|
500
|
-
return value as VmArray;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
export const zeros = VmLib((...size) => filled(size, 0), {
|
|
504
|
-
summary: '创建一个全零的矩阵',
|
|
505
|
-
params: { '..size': { type: 'number[]', description: '矩阵的维度' } },
|
|
506
|
-
returns: { type: 'number[][]' },
|
|
507
|
-
examples: ['matrix.zeros(2, 3) // [[0, 0, 0], [0, 0, 0]]'],
|
|
508
|
-
});
|
|
509
|
-
|
|
510
|
-
export const ones = VmLib((...size) => filled(size, 1), {
|
|
511
|
-
summary: '创建一个全一的矩阵',
|
|
512
|
-
params: { '..size': { type: 'number[]', description: '矩阵的维度' } },
|
|
513
|
-
returns: { type: 'number[][]' },
|
|
514
|
-
examples: ['matrix.ones(2, 2) // [[1, 1], [1, 1]]'],
|
|
515
|
-
});
|
|
516
|
-
|
|
517
|
-
export const identity = VmLib(
|
|
518
|
-
(...size) => {
|
|
519
|
-
let s = getNumbers(size);
|
|
520
|
-
if (s.length === 0) return [];
|
|
521
|
-
if (s.length > 2) throwError('Invalid matrix size', []);
|
|
522
|
-
if (s.length === 1) s = [s[0]!, s[0]!];
|
|
523
|
-
const m = arrayLen(s[0]);
|
|
524
|
-
const n = arrayLen(s[1]);
|
|
525
|
-
// 由于 `filled` 函数返回只读数组,其每行为相同引用,这里需要手动创建每行
|
|
526
|
-
const ret: number[][] = [];
|
|
527
|
-
for (let i = 0; i < m; i++) {
|
|
528
|
-
const row: number[] = [];
|
|
529
|
-
ret[i] = row;
|
|
530
|
-
row.length = n;
|
|
531
|
-
row.fill(0);
|
|
532
|
-
if (i < n) row[i] = 1;
|
|
533
|
-
}
|
|
534
|
-
return ret;
|
|
535
|
-
},
|
|
536
|
-
{
|
|
537
|
-
summary: '创建一个单位矩阵',
|
|
538
|
-
params: { '..size': { type: '[number] | [number, number]', description: '矩阵的维度' } },
|
|
539
|
-
returns: { type: 'number[][]' },
|
|
540
|
-
examples: ['matrix.identity(3) // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]'],
|
|
541
|
-
},
|
|
542
|
-
);
|
|
543
|
-
|
|
544
|
-
export const diagonal = VmLib(
|
|
545
|
-
(x, k = 0) => {
|
|
546
|
-
expectArray('x', x, []);
|
|
547
|
-
const fk = expectInteger('k', k);
|
|
548
|
-
if (x.every((e) => isArray(e))) {
|
|
549
|
-
// 获取对角线元素
|
|
550
|
-
const diag: VmConst[] = [];
|
|
551
|
-
for (let i = 0; i < x.length; i++) {
|
|
552
|
-
const row = x[i] as VmArray | undefined;
|
|
553
|
-
const r = i + fk;
|
|
554
|
-
if (r < 0) continue;
|
|
555
|
-
if (!row || r >= row.length) break;
|
|
556
|
-
diag.push(row[r] ?? null);
|
|
557
|
-
}
|
|
558
|
-
return diag;
|
|
559
|
-
}
|
|
560
|
-
// 创建对角矩阵
|
|
561
|
-
const l = x.length;
|
|
562
|
-
const m = arrayLen(fk < 0 ? l - fk : l);
|
|
563
|
-
const n = arrayLen(fk > 0 ? l + fk : l);
|
|
564
|
-
const result: VmConst[][] = [];
|
|
565
|
-
for (let i = 0; i < m; i++) {
|
|
566
|
-
const row: VmConst[] = [];
|
|
567
|
-
result[i] = row;
|
|
568
|
-
row.length = n;
|
|
569
|
-
row.fill(0);
|
|
570
|
-
for (let j = 0; j < n; j++) {
|
|
571
|
-
if (i + fk === j) {
|
|
572
|
-
row[j] = x[fk >= 0 ? i : j] ?? null;
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
return result;
|
|
577
|
-
},
|
|
578
|
-
{
|
|
579
|
-
summary: '创建一个对角矩阵或获取矩阵的对角线',
|
|
580
|
-
params: {
|
|
581
|
-
x: { type: 'number[] | number[][]', description: '对角线元素或要获取对角线的矩阵' },
|
|
582
|
-
k: { type: 'number', description: '对角线偏移量,默认为 0' },
|
|
583
|
-
},
|
|
584
|
-
returns: { type: 'number[][] | number[]' },
|
|
585
|
-
examples: [
|
|
586
|
-
'matrix.diagonal([1, 2, 3]) // [[1, 0, 0], [0, 2, 0], [0, 0, 3]]',
|
|
587
|
-
'matrix.diagonal([[1, 2], [3, 4]]) // [1, 4]',
|
|
588
|
-
],
|
|
589
|
-
},
|
|
590
|
-
);
|