UNPKG

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