1 | 'use strict'
|
2 |
|
3 | const array = require('../../utils/array')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | const matrix = load(require('../../type/matrix/function/matrix'))
|
7 | const subtract = load(require('../arithmetic/subtract'))
|
8 | const multiply = load(require('../arithmetic/multiply'))
|
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 | const cross = typed('cross', {
|
44 | 'Matrix, Matrix': function (x, y) {
|
45 | return matrix(_cross(x.toArray(), y.toArray()))
|
46 | },
|
47 |
|
48 | 'Matrix, Array': function (x, y) {
|
49 | return matrix(_cross(x.toArray(), y))
|
50 | },
|
51 |
|
52 | 'Array, Matrix': function (x, y) {
|
53 | return matrix(_cross(x, y.toArray()))
|
54 | },
|
55 |
|
56 | 'Array, Array': _cross
|
57 | })
|
58 |
|
59 | cross.toTex = {
|
60 | 2: `\\left(\${args[0]}\\right)\\times\\left(\${args[1]}\\right)`
|
61 | }
|
62 |
|
63 | return cross
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function _cross (x, y) {
|
73 | const highestDimension = Math.max(array.size(x).length, array.size(y).length)
|
74 |
|
75 | x = array.squeeze(x)
|
76 | y = array.squeeze(y)
|
77 |
|
78 | const xSize = array.size(x)
|
79 | const ySize = array.size(y)
|
80 |
|
81 | if (xSize.length !== 1 || ySize.length !== 1 || xSize[0] !== 3 || ySize[0] !== 3) {
|
82 | throw new RangeError('Vectors with length 3 expected ' +
|
83 | '(Size A = [' + xSize.join(', ') + '], B = [' + ySize.join(', ') + '])')
|
84 | }
|
85 |
|
86 | const product = [
|
87 | subtract(multiply(x[1], y[2]), multiply(x[2], y[1])),
|
88 | subtract(multiply(x[2], y[0]), multiply(x[0], y[2])),
|
89 | subtract(multiply(x[0], y[1]), multiply(x[1], y[0]))
|
90 | ]
|
91 |
|
92 | if (highestDimension > 1) {
|
93 | return [product]
|
94 | } else {
|
95 | return product
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 | exports.name = 'cross'
|
101 | exports.factory = factory
|