UNPKG

3.17 kBJavaScriptView Raw
1'use strict'
2
3const isInteger = require('../../utils/number').isInteger
4const bigBitXor = require('../../utils/bignumber/bitXor')
5
6function factory (type, config, load, typed) {
7 const latex = require('../../utils/latex')
8
9 const matrix = load(require('../../type/matrix/function/matrix'))
10
11 const algorithm03 = load(require('../../type/matrix/utils/algorithm03'))
12 const algorithm07 = load(require('../../type/matrix/utils/algorithm07'))
13 const algorithm12 = load(require('../../type/matrix/utils/algorithm12'))
14 const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
15 const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
16
17 /**
18 * Bitwise XOR two values, `x ^ y`.
19 * For matrices, the function is evaluated element wise.
20 *
21 * Syntax:
22 *
23 * math.bitXor(x, y)
24 *
25 * Examples:
26 *
27 * math.bitXor(1, 2) // returns number 3
28 *
29 * math.bitXor([2, 3, 4], 4) // returns Array [6, 7, 0]
30 *
31 * See also:
32 *
33 * bitAnd, bitNot, bitOr, leftShift, rightArithShift, rightLogShift
34 *
35 * @param {number | BigNumber | Array | Matrix} x First value to xor
36 * @param {number | BigNumber | Array | Matrix} y Second value to xor
37 * @return {number | BigNumber | Array | Matrix} XOR of `x` and `y`
38 */
39 const bitXor = typed('bitXor', {
40
41 'number, number': function (x, y) {
42 if (!isInteger(x) || !isInteger(y)) {
43 throw new Error('Integers expected in function bitXor')
44 }
45
46 return x ^ y
47 },
48
49 'BigNumber, BigNumber': bigBitXor,
50
51 'SparseMatrix, SparseMatrix': function (x, y) {
52 return algorithm07(x, y, bitXor)
53 },
54
55 'SparseMatrix, DenseMatrix': function (x, y) {
56 return algorithm03(y, x, bitXor, true)
57 },
58
59 'DenseMatrix, SparseMatrix': function (x, y) {
60 return algorithm03(x, y, bitXor, false)
61 },
62
63 'DenseMatrix, DenseMatrix': function (x, y) {
64 return algorithm13(x, y, bitXor)
65 },
66
67 'Array, Array': function (x, y) {
68 // use matrix implementation
69 return bitXor(matrix(x), matrix(y)).valueOf()
70 },
71
72 'Array, Matrix': function (x, y) {
73 // use matrix implementation
74 return bitXor(matrix(x), y)
75 },
76
77 'Matrix, Array': function (x, y) {
78 // use matrix implementation
79 return bitXor(x, matrix(y))
80 },
81
82 'SparseMatrix, any': function (x, y) {
83 return algorithm12(x, y, bitXor, false)
84 },
85
86 'DenseMatrix, any': function (x, y) {
87 return algorithm14(x, y, bitXor, false)
88 },
89
90 'any, SparseMatrix': function (x, y) {
91 return algorithm12(y, x, bitXor, true)
92 },
93
94 'any, DenseMatrix': function (x, y) {
95 return algorithm14(y, x, bitXor, true)
96 },
97
98 'Array, any': function (x, y) {
99 // use matrix implementation
100 return algorithm14(matrix(x), y, bitXor, false).valueOf()
101 },
102
103 'any, Array': function (x, y) {
104 // use matrix implementation
105 return algorithm14(matrix(y), x, bitXor, true).valueOf()
106 }
107 })
108
109 bitXor.toTex = {
110 2: `\\left(\${args[0]}${latex.operators['bitXor']}\${args[1]}\\right)`
111 }
112
113 return bitXor
114}
115
116exports.name = 'bitXor'
117exports.factory = factory