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