1 | import { factory } from '../../utils/factory'
|
2 |
|
3 | const name = 'deepEqual'
|
4 | const dependencies = [
|
5 | 'typed',
|
6 | 'equal'
|
7 | ]
|
8 |
|
9 | export const createDeepEqual = /* #__PURE__ */ factory(name, dependencies, ({ typed, equal }) => {
|
10 | /**
|
11 | * Test element wise whether two matrices are equal.
|
12 | * The function accepts both matrices and scalar values.
|
13 | *
|
14 | * Strings are compared by their numerical value.
|
15 | *
|
16 | * Syntax:
|
17 | *
|
18 | * math.deepEqual(x, y)
|
19 | *
|
20 | * Examples:
|
21 | *
|
22 | * math.deepEqual(2, 4) // returns false
|
23 | *
|
24 | * a = [2, 5, 1]
|
25 | * b = [2, 7, 1]
|
26 | *
|
27 | * math.deepEqual(a, b) // returns false
|
28 | * math.equal(a, b) // returns [true, false, true]
|
29 | *
|
30 | * See also:
|
31 | *
|
32 | * equal, unequal
|
33 | *
|
34 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First matrix to compare
|
35 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second matrix to compare
|
36 | * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
|
37 | * Returns true when the input matrices have the same size and each of their elements is equal.
|
38 | */
|
39 | return typed(name, {
|
40 | 'any, any': function (x, y) {
|
41 | return _deepEqual(x.valueOf(), y.valueOf())
|
42 | }
|
43 | })
|
44 |
|
45 | /**
|
46 | * Test whether two arrays have the same size and all elements are equal
|
47 | * @param {Array | *} x
|
48 | * @param {Array | *} y
|
49 | * @return {boolean} Returns true if both arrays are deep equal
|
50 | */
|
51 | function _deepEqual (x, y) {
|
52 | if (Array.isArray(x)) {
|
53 | if (Array.isArray(y)) {
|
54 | const len = x.length
|
55 | if (len !== y.length) {
|
56 | return false
|
57 | }
|
58 |
|
59 | for (let i = 0; i < len; i++) {
|
60 | if (!_deepEqual(x[i], y[i])) {
|
61 | return false
|
62 | }
|
63 | }
|
64 |
|
65 | return true
|
66 | } else {
|
67 | return false
|
68 | }
|
69 | } else {
|
70 | if (Array.isArray(y)) {
|
71 | return false
|
72 | } else {
|
73 | return equal(x, y)
|
74 | }
|
75 | }
|
76 | }
|
77 | })
|