UNPKG

4.1 kBJavaScriptView Raw
1'use strict'
2
3const isInteger = require('../../utils/number').isInteger
4
5function factory (type, config, load, typed) {
6 const latex = require('../../utils/latex')
7
8 const matrix = load(require('../../type/matrix/function/matrix'))
9 const equalScalar = load(require('../relational/equalScalar'))
10 const zeros = load(require('../matrix/zeros'))
11
12 const algorithm01 = load(require('../../type/matrix/utils/algorithm01'))
13 const algorithm02 = load(require('../../type/matrix/utils/algorithm02'))
14 const algorithm08 = load(require('../../type/matrix/utils/algorithm08'))
15 const algorithm10 = load(require('../../type/matrix/utils/algorithm10'))
16 const algorithm11 = load(require('../../type/matrix/utils/algorithm11'))
17 const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
18 const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
19
20 /**
21 * Bitwise right logical shift of value x by y number of bits, `x >>> y`.
22 * For matrices, the function is evaluated element wise.
23 * For units, the function is evaluated on the best prefix base.
24 *
25 * Syntax:
26 *
27 * math.rightLogShift(x, y)
28 *
29 * Examples:
30 *
31 * math.rightLogShift(4, 2) // returns number 1
32 *
33 * math.rightLogShift([16, -32, 64], 4) // returns Array [1, 2, 3]
34 *
35 * See also:
36 *
37 * bitAnd, bitNot, bitOr, bitXor, leftShift, rightLogShift
38 *
39 * @param {number | Array | Matrix} x Value to be shifted
40 * @param {number} y Amount of shifts
41 * @return {number | Array | Matrix} `x` zero-filled shifted right `y` times
42 */
43
44 const rightLogShift = typed('rightLogShift', {
45
46 'number, number': function (x, y) {
47 if (!isInteger(x) || !isInteger(y)) {
48 throw new Error('Integers expected in function rightLogShift')
49 }
50
51 return x >>> y
52 },
53
54 // 'BigNumber, BigNumber': ..., // TODO: implement BigNumber support for rightLogShift
55
56 'SparseMatrix, SparseMatrix': function (x, y) {
57 return algorithm08(x, y, rightLogShift, false)
58 },
59
60 'SparseMatrix, DenseMatrix': function (x, y) {
61 return algorithm02(y, x, rightLogShift, true)
62 },
63
64 'DenseMatrix, SparseMatrix': function (x, y) {
65 return algorithm01(x, y, rightLogShift, false)
66 },
67
68 'DenseMatrix, DenseMatrix': function (x, y) {
69 return algorithm13(x, y, rightLogShift)
70 },
71
72 'Array, Array': function (x, y) {
73 // use matrix implementation
74 return rightLogShift(matrix(x), matrix(y)).valueOf()
75 },
76
77 'Array, Matrix': function (x, y) {
78 // use matrix implementation
79 return rightLogShift(matrix(x), y)
80 },
81
82 'Matrix, Array': function (x, y) {
83 // use matrix implementation
84 return rightLogShift(x, matrix(y))
85 },
86
87 'SparseMatrix, number | BigNumber': function (x, y) {
88 // check scalar
89 if (equalScalar(y, 0)) {
90 return x.clone()
91 }
92 return algorithm11(x, y, rightLogShift, false)
93 },
94
95 'DenseMatrix, number | BigNumber': function (x, y) {
96 // check scalar
97 if (equalScalar(y, 0)) {
98 return x.clone()
99 }
100 return algorithm14(x, y, rightLogShift, false)
101 },
102
103 'number | BigNumber, SparseMatrix': function (x, y) {
104 // check scalar
105 if (equalScalar(x, 0)) {
106 return zeros(y.size(), y.storage())
107 }
108 return algorithm10(y, x, rightLogShift, true)
109 },
110
111 'number | BigNumber, DenseMatrix': function (x, y) {
112 // check scalar
113 if (equalScalar(x, 0)) {
114 return zeros(y.size(), y.storage())
115 }
116 return algorithm14(y, x, rightLogShift, true)
117 },
118
119 'Array, number | BigNumber': function (x, y) {
120 // use matrix implementation
121 return rightLogShift(matrix(x), y).valueOf()
122 },
123
124 'number | BigNumber, Array': function (x, y) {
125 // use matrix implementation
126 return rightLogShift(x, matrix(y)).valueOf()
127 }
128 })
129
130 rightLogShift.toTex = {
131 2: `\\left(\${args[0]}${latex.operators['rightLogShift']}\${args[1]}\\right)`
132 }
133
134 return rightLogShift
135}
136
137exports.name = 'rightLogShift'
138exports.factory = factory