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