UNPKG

4.99 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03'
3import { createAlgorithm07 } from '../../type/matrix/utils/algorithm07'
4import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12'
5import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
6import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
7
8const name = 'unequal'
9const dependencies = [
10 'typed',
11 'config',
12 'equalScalar',
13 'matrix',
14 'DenseMatrix'
15]
16
17export const createUnequal = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, equalScalar, matrix, DenseMatrix }) => {
18 const algorithm03 = createAlgorithm03({ typed })
19 const algorithm07 = createAlgorithm07({ typed, DenseMatrix })
20 const algorithm12 = createAlgorithm12({ typed, DenseMatrix })
21 const algorithm13 = createAlgorithm13({ typed })
22 const algorithm14 = createAlgorithm14({ typed })
23
24 /**
25 * Test whether two values are unequal.
26 *
27 * The function tests whether the relative difference between x and y is
28 * larger than the configured epsilon. The function cannot be used to compare
29 * values smaller than approximately 2.22e-16.
30 *
31 * For matrices, the function is evaluated element wise.
32 * In case of complex numbers, x.re must unequal y.re, or x.im must unequal y.im.
33 * Strings are compared by their numerical value.
34 *
35 * Values `null` and `undefined` are compared strictly, thus `null` is unequal
36 * with everything except `null`, and `undefined` is unequal with everything
37 * except `undefined`.
38 *
39 * Syntax:
40 *
41 * math.unequal(x, y)
42 *
43 * Examples:
44 *
45 * math.unequal(2 + 2, 3) // returns true
46 * math.unequal(2 + 2, 4) // returns false
47 *
48 * const a = math.unit('50 cm')
49 * const b = math.unit('5 m')
50 * math.unequal(a, b) // returns false
51 *
52 * const c = [2, 5, 1]
53 * const d = [2, 7, 1]
54 *
55 * math.unequal(c, d) // returns [false, true, false]
56 * math.deepEqual(c, d) // returns false
57 *
58 * math.unequal(0, null) // returns true
59 * See also:
60 *
61 * equal, deepEqual, smaller, smallerEq, larger, largerEq, compare
62 *
63 * @param {number | BigNumber | Fraction | boolean | Complex | Unit | string | Array | Matrix | undefined} x First value to compare
64 * @param {number | BigNumber | Fraction | boolean | Complex | Unit | string | Array | Matrix | undefined} y Second value to compare
65 * @return {boolean | Array | Matrix} Returns true when the compared values are unequal, else returns false
66 */
67 const unequal = typed('unequal', {
68
69 'any, any': function (x, y) {
70 // strict equality for null and undefined?
71 if (x === null) { return y !== null }
72 if (y === null) { return x !== null }
73 if (x === undefined) { return y !== undefined }
74 if (y === undefined) { return x !== undefined }
75
76 return _unequal(x, y)
77 },
78
79 'SparseMatrix, SparseMatrix': function (x, y) {
80 return algorithm07(x, y, _unequal)
81 },
82
83 'SparseMatrix, DenseMatrix': function (x, y) {
84 return algorithm03(y, x, _unequal, true)
85 },
86
87 'DenseMatrix, SparseMatrix': function (x, y) {
88 return algorithm03(x, y, _unequal, false)
89 },
90
91 'DenseMatrix, DenseMatrix': function (x, y) {
92 return algorithm13(x, y, _unequal)
93 },
94
95 'Array, Array': function (x, y) {
96 // use matrix implementation
97 return unequal(matrix(x), matrix(y)).valueOf()
98 },
99
100 'Array, Matrix': function (x, y) {
101 // use matrix implementation
102 return unequal(matrix(x), y)
103 },
104
105 'Matrix, Array': function (x, y) {
106 // use matrix implementation
107 return unequal(x, matrix(y))
108 },
109
110 'SparseMatrix, any': function (x, y) {
111 return algorithm12(x, y, _unequal, false)
112 },
113
114 'DenseMatrix, any': function (x, y) {
115 return algorithm14(x, y, _unequal, false)
116 },
117
118 'any, SparseMatrix': function (x, y) {
119 return algorithm12(y, x, _unequal, true)
120 },
121
122 'any, DenseMatrix': function (x, y) {
123 return algorithm14(y, x, _unequal, true)
124 },
125
126 'Array, any': function (x, y) {
127 // use matrix implementation
128 return algorithm14(matrix(x), y, _unequal, false).valueOf()
129 },
130
131 'any, Array': function (x, y) {
132 // use matrix implementation
133 return algorithm14(matrix(y), x, _unequal, true).valueOf()
134 }
135 })
136
137 function _unequal (x, y) {
138 return !equalScalar(x, y)
139 }
140
141 return unequal
142})
143
144export const createUnequalNumber = factory(name, ['typed', 'equalScalar'], ({ typed, equalScalar }) => {
145 return typed(name, {
146 'any, any': function (x, y) {
147 // strict equality for null and undefined?
148 if (x === null) { return y !== null }
149 if (y === null) { return x !== null }
150 if (x === undefined) { return y !== undefined }
151 if (y === undefined) { return x !== undefined }
152
153 return !equalScalar(x, y)
154 }
155 })
156})