1 | import { flatten } from '../../utils/array'
|
2 | import { factory } from '../../utils/factory'
|
3 |
|
4 | const name = 'setCartesian'
|
5 | const dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']
|
6 |
|
7 | export const createSetCartesian = factory(name, dependencies, ({ typed, size, subset, compareNatural, Index, DenseMatrix }) => {
|
8 | |
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | return typed(name, {
|
29 | 'Array | Matrix, Array | Matrix': function (a1, a2) {
|
30 | let result = []
|
31 |
|
32 | if (subset(size(a1), new Index(0)) !== 0 && subset(size(a2), new Index(0)) !== 0) {
|
33 | const b1 = flatten(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural)
|
34 | const b2 = flatten(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural)
|
35 | result = []
|
36 | for (let i = 0; i < b1.length; i++) {
|
37 | for (let j = 0; j < b2.length; j++) {
|
38 | result.push([b1[i], b2[j]])
|
39 | }
|
40 | }
|
41 | }
|
42 |
|
43 | if (Array.isArray(a1) && Array.isArray(a2)) {
|
44 | return result
|
45 | }
|
46 |
|
47 | return new DenseMatrix(result)
|
48 | }
|
49 | })
|
50 | })
|