UNPKG

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