UNPKG

4.22 kBJavaScriptView Raw
1'use strict'
2
3const isInteger = require('../../utils/number').isInteger
4const bigRightArithShift = require('../../utils/bignumber/rightArithShift')
5
6function factory (type, config, load, typed) {
7 const latex = require('../../utils/latex')
8
9 const matrix = load(require('../../type/matrix/function/matrix'))
10 const equalScalar = load(require('../relational/equalScalar'))
11 const zeros = load(require('../matrix/zeros'))
12
13 const algorithm01 = load(require('../../type/matrix/utils/algorithm01'))
14 const algorithm02 = load(require('../../type/matrix/utils/algorithm02'))
15 const algorithm08 = load(require('../../type/matrix/utils/algorithm08'))
16 const algorithm10 = load(require('../../type/matrix/utils/algorithm10'))
17 const algorithm11 = load(require('../../type/matrix/utils/algorithm11'))
18 const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
19 const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
20
21 /**
22 * Bitwise right arithmetic shift of a value x by y number of bits, `x >> y`.
23 * For matrices, the function is evaluated element wise.
24 * For units, the function is evaluated on the best prefix base.
25 *
26 * Syntax:
27 *
28 * math.rightArithShift(x, y)
29 *
30 * Examples:
31 *
32 * math.rightArithShift(4, 2) // returns number 1
33 *
34 * math.rightArithShift([16, -32, 64], 4) // returns Array [1, -2, 3]
35 *
36 * See also:
37 *
38 * bitAnd, bitNot, bitOr, bitXor, rightArithShift, rightLogShift
39 *
40 * @param {number | BigNumber | Array | Matrix} x Value to be shifted
41 * @param {number | BigNumber} y Amount of shifts
42 * @return {number | BigNumber | Array | Matrix} `x` sign-filled shifted right `y` times
43 */
44 const rightArithShift = typed('rightArithShift', {
45
46 'number, number': function (x, y) {
47 if (!isInteger(x) || !isInteger(y)) {
48 throw new Error('Integers expected in function rightArithShift')
49 }
50
51 return x >> y
52 },
53
54 'BigNumber, BigNumber': bigRightArithShift,
55
56 'SparseMatrix, SparseMatrix': function (x, y) {
57 return algorithm08(x, y, rightArithShift, false)
58 },
59
60 'SparseMatrix, DenseMatrix': function (x, y) {
61 return algorithm02(y, x, rightArithShift, true)
62 },
63
64 'DenseMatrix, SparseMatrix': function (x, y) {
65 return algorithm01(x, y, rightArithShift, false)
66 },
67
68 'DenseMatrix, DenseMatrix': function (x, y) {
69 return algorithm13(x, y, rightArithShift)
70 },
71
72 'Array, Array': function (x, y) {
73 // use matrix implementation
74 return rightArithShift(matrix(x), matrix(y)).valueOf()
75 },
76
77 'Array, Matrix': function (x, y) {
78 // use matrix implementation
79 return rightArithShift(matrix(x), y)
80 },
81
82 'Matrix, Array': function (x, y) {
83 // use matrix implementation
84 return rightArithShift(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, rightArithShift, 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, rightArithShift, 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, rightArithShift, 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, rightArithShift, true)
117 },
118
119 'Array, number | BigNumber': function (x, y) {
120 // use matrix implementation
121 return rightArithShift(matrix(x), y).valueOf()
122 },
123
124 'number | BigNumber, Array': function (x, y) {
125 // use matrix implementation
126 return rightArithShift(x, matrix(y)).valueOf()
127 }
128 })
129
130 rightArithShift.toTex = {
131 2: `\\left(\${args[0]}${latex.operators['rightArithShift']}\${args[1]}\\right)`
132 }
133
134 return rightArithShift
135}
136
137exports.name = 'rightArithShift'
138exports.factory = factory