UNPKG

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