UNPKG

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