UNPKG

3.35 kBJavaScriptView Raw
1'use strict'
2
3function factory (type, config, load, typed) {
4 const latex = require('../../utils/latex')
5
6 const matrix = load(require('../../type/matrix/function/matrix'))
7
8 const algorithm03 = load(require('../../type/matrix/utils/algorithm03'))
9 const algorithm05 = load(require('../../type/matrix/utils/algorithm05'))
10 const algorithm12 = load(require('../../type/matrix/utils/algorithm12'))
11 const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
12 const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
13
14 /**
15 * Logical `or`. Test if at least one value is defined with a nonzero/nonempty value.
16 * For matrices, the function is evaluated element wise.
17 *
18 * Syntax:
19 *
20 * math.or(x, y)
21 *
22 * Examples:
23 *
24 * math.or(2, 4) // returns true
25 *
26 * a = [2, 5, 0]
27 * b = [0, 22, 0]
28 * c = 0
29 *
30 * math.or(a, b) // returns [true, true, false]
31 * math.or(b, c) // returns [false, true, false]
32 *
33 * See also:
34 *
35 * and, not, xor
36 *
37 * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check
38 * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check
39 * @return {boolean | Array | Matrix}
40 * Returns true when one of the inputs is defined with a nonzero/nonempty value.
41 */
42 const or = typed('or', {
43
44 'number, number': function (x, y) {
45 return !!(x || y)
46 },
47
48 'Complex, Complex': function (x, y) {
49 return (x.re !== 0 || x.im !== 0) || (y.re !== 0 || y.im !== 0)
50 },
51
52 'BigNumber, BigNumber': function (x, y) {
53 return (!x.isZero() && !x.isNaN()) || (!y.isZero() && !y.isNaN())
54 },
55
56 'Unit, Unit': function (x, y) {
57 return or(x.value || 0, y.value || 0)
58 },
59
60 'SparseMatrix, SparseMatrix': function (x, y) {
61 return algorithm05(x, y, or)
62 },
63
64 'SparseMatrix, DenseMatrix': function (x, y) {
65 return algorithm03(y, x, or, true)
66 },
67
68 'DenseMatrix, SparseMatrix': function (x, y) {
69 return algorithm03(x, y, or, false)
70 },
71
72 'DenseMatrix, DenseMatrix': function (x, y) {
73 return algorithm13(x, y, or)
74 },
75
76 'Array, Array': function (x, y) {
77 // use matrix implementation
78 return or(matrix(x), matrix(y)).valueOf()
79 },
80
81 'Array, Matrix': function (x, y) {
82 // use matrix implementation
83 return or(matrix(x), y)
84 },
85
86 'Matrix, Array': function (x, y) {
87 // use matrix implementation
88 return or(x, matrix(y))
89 },
90
91 'SparseMatrix, any': function (x, y) {
92 return algorithm12(x, y, or, false)
93 },
94
95 'DenseMatrix, any': function (x, y) {
96 return algorithm14(x, y, or, false)
97 },
98
99 'any, SparseMatrix': function (x, y) {
100 return algorithm12(y, x, or, true)
101 },
102
103 'any, DenseMatrix': function (x, y) {
104 return algorithm14(y, x, or, true)
105 },
106
107 'Array, any': function (x, y) {
108 // use matrix implementation
109 return algorithm14(matrix(x), y, or, false).valueOf()
110 },
111
112 'any, Array': function (x, y) {
113 // use matrix implementation
114 return algorithm14(matrix(y), x, or, true).valueOf()
115 }
116 })
117
118 or.toTex = {
119 2: `\\left(\${args[0]}${latex.operators['or']}\${args[1]}\\right)`
120 }
121
122 return or
123}
124
125exports.name = 'or'
126exports.factory = factory