UNPKG

3.98 kBJavaScriptView Raw
1import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03';
2import { createAlgorithm07 } from '../../type/matrix/utils/algorithm07';
3import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12';
4import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13';
5import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14';
6import { factory } from '../../utils/factory';
7import { xorNumber } from '../../plain/number';
8var name = 'xor';
9var dependencies = ['typed', 'matrix', 'DenseMatrix'];
10export var createXor =
11/* #__PURE__ */
12factory(name, dependencies, function (_ref) {
13 var typed = _ref.typed,
14 matrix = _ref.matrix,
15 DenseMatrix = _ref.DenseMatrix;
16 var algorithm03 = createAlgorithm03({
17 typed: typed
18 });
19 var algorithm07 = createAlgorithm07({
20 typed: typed,
21 DenseMatrix: DenseMatrix
22 });
23 var algorithm12 = createAlgorithm12({
24 typed: typed,
25 DenseMatrix: DenseMatrix
26 });
27 var algorithm13 = createAlgorithm13({
28 typed: typed
29 });
30 var algorithm14 = createAlgorithm14({
31 typed: typed
32 });
33 /**
34 * Logical `xor`. Test whether one and only one value is defined with a nonzero/nonempty value.
35 * For matrices, the function is evaluated element wise.
36 *
37 * Syntax:
38 *
39 * math.xor(x, y)
40 *
41 * Examples:
42 *
43 * math.xor(2, 4) // returns false
44 *
45 * a = [2, 0, 0]
46 * b = [2, 7, 0]
47 * c = 0
48 *
49 * math.xor(a, b) // returns [false, true, false]
50 * math.xor(a, c) // returns [true, false, false]
51 *
52 * See also:
53 *
54 * and, not, or
55 *
56 * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check
57 * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check
58 * @return {boolean | Array | Matrix}
59 * Returns true when one and only one input is defined with a nonzero/nonempty value.
60 */
61
62 var xor = typed(name, {
63 'number, number': xorNumber,
64 'Complex, Complex': function ComplexComplex(x, y) {
65 return (x.re !== 0 || x.im !== 0) !== (y.re !== 0 || y.im !== 0);
66 },
67 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
68 return (!x.isZero() && !x.isNaN()) !== (!y.isZero() && !y.isNaN());
69 },
70 'Unit, Unit': function UnitUnit(x, y) {
71 return xor(x.value || 0, y.value || 0);
72 },
73 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
74 return algorithm07(x, y, xor);
75 },
76 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
77 return algorithm03(y, x, xor, true);
78 },
79 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
80 return algorithm03(x, y, xor, false);
81 },
82 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
83 return algorithm13(x, y, xor);
84 },
85 'Array, Array': function ArrayArray(x, y) {
86 // use matrix implementation
87 return xor(matrix(x), matrix(y)).valueOf();
88 },
89 'Array, Matrix': function ArrayMatrix(x, y) {
90 // use matrix implementation
91 return xor(matrix(x), y);
92 },
93 'Matrix, Array': function MatrixArray(x, y) {
94 // use matrix implementation
95 return xor(x, matrix(y));
96 },
97 'SparseMatrix, any': function SparseMatrixAny(x, y) {
98 return algorithm12(x, y, xor, false);
99 },
100 'DenseMatrix, any': function DenseMatrixAny(x, y) {
101 return algorithm14(x, y, xor, false);
102 },
103 'any, SparseMatrix': function anySparseMatrix(x, y) {
104 return algorithm12(y, x, xor, true);
105 },
106 'any, DenseMatrix': function anyDenseMatrix(x, y) {
107 return algorithm14(y, x, xor, true);
108 },
109 'Array, any': function ArrayAny(x, y) {
110 // use matrix implementation
111 return algorithm14(matrix(x), y, xor, false).valueOf();
112 },
113 'any, Array': function anyArray(x, y) {
114 // use matrix implementation
115 return algorithm14(matrix(y), x, xor, true).valueOf();
116 }
117 });
118 return xor;
119});
\No newline at end of file