Skip to content

Commit 57c4cb2

Browse files
committed
Added tests and removed useMatrixForArrayScalar.js
1 parent af646a8 commit 57c4cb2

File tree

10 files changed

+86
-25
lines changed

10 files changed

+86
-25
lines changed

src/function/bitwise/leftShift.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { createMatAlgo02xDS0 } from '../../type/matrix/utils/matAlgo02xDS0.js'
22
import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js'
33
import { createMatAlgo14xDs } from '../../type/matrix/utils/matAlgo14xDs.js'
4+
import { createMatAlgo15xAs } from '../../type/matrix/utils/matAlgo15xAs.js'
45
import { createMatAlgo01xDSid } from '../../type/matrix/utils/matAlgo01xDSid.js'
56
import { createMatAlgo10xSids } from '../../type/matrix/utils/matAlgo10xSids.js'
67
import { createMatAlgo08xS0Sid } from '../../type/matrix/utils/matAlgo08xS0Sid.js'
78
import { factory } from '../../utils/factory.js'
89
import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js'
9-
import { createUseMatrixForArrayScalar } from './useMatrixForArrayScalar.js'
1010
import { leftShiftNumber } from '../../plain/number/index.js'
1111
import { leftShiftBigNumber } from '../../utils/bignumber/bitwise.js'
12+
import { deepMap, clone } from '../../utils/array.js'
1213

1314
const name = 'leftShift'
1415
const dependencies = [
@@ -27,8 +28,8 @@ export const createLeftShift = /* #__PURE__ */ factory(name, dependencies, ({ ty
2728
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
2829
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
2930
const matAlgo14xDs = createMatAlgo14xDs({ typed })
31+
const matAlgo15xAs = createMatAlgo15xAs()
3032
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
31-
const useMatrixForArrayScalar = createUseMatrixForArrayScalar({ typed, matrix })
3233

3334
/**
3435
* Bitwise left logical shift of a value x by y number of bits, `x << y`.
@@ -78,6 +79,14 @@ export const createLeftShift = /* #__PURE__ */ factory(name, dependencies, ({ ty
7879
return matAlgo14xDs(x, y, self, false)
7980
}),
8081

82+
'Array, number | BigNumber': typed.referToSelf(self => (x, y) => {
83+
// check scalar
84+
if (equalScalar(y, 0)) {
85+
return clone(x)
86+
}
87+
return matAlgo15xAs(x, y, self, false)
88+
}),
89+
8190
'number | BigNumber, SparseMatrix': typed.referToSelf(self => (x, y) => {
8291
// check scalar
8392
if (equalScalar(x, 0)) {
@@ -92,9 +101,15 @@ export const createLeftShift = /* #__PURE__ */ factory(name, dependencies, ({ ty
92101
return zeros(y.size(), y.storage())
93102
}
94103
return matAlgo14xDs(y, x, self, true)
104+
}),
105+
'number | BigNumber, Array': typed.referToSelf(self => (x, y) => {
106+
// check scalar
107+
if (equalScalar(x, 0)) {
108+
return deepMap(y, () => x)
109+
}
110+
return matAlgo15xAs(y, x, self, true)
95111
})
96112
},
97-
useMatrixForArrayScalar,
98113
matrixAlgorithmSuite({
99114
SS: matAlgo08xS0Sid,
100115
DS: matAlgo01xDSid,

src/function/bitwise/rightArithShift.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { rightArithShiftBigNumber } from '../../utils/bignumber/bitwise.js'
22
import { createMatAlgo02xDS0 } from '../../type/matrix/utils/matAlgo02xDS0.js'
33
import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js'
44
import { createMatAlgo14xDs } from '../../type/matrix/utils/matAlgo14xDs.js'
5+
import { createMatAlgo15xAs } from '../../type/matrix/utils/matAlgo15xAs.js'
56
import { createMatAlgo01xDSid } from '../../type/matrix/utils/matAlgo01xDSid.js'
67
import { createMatAlgo10xSids } from '../../type/matrix/utils/matAlgo10xSids.js'
78
import { createMatAlgo08xS0Sid } from '../../type/matrix/utils/matAlgo08xS0Sid.js'
89
import { factory } from '../../utils/factory.js'
910
import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js'
10-
import { createUseMatrixForArrayScalar } from './useMatrixForArrayScalar.js'
1111
import { rightArithShiftNumber } from '../../plain/number/index.js'
12+
import { clone, deepMap } from '../../utils/array.js'
1213

1314
const name = 'rightArithShift'
1415
const dependencies = [
@@ -27,8 +28,8 @@ export const createRightArithShift = /* #__PURE__ */ factory(name, dependencies,
2728
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
2829
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
2930
const matAlgo14xDs = createMatAlgo14xDs({ typed })
31+
const matAlgo15xAs = createMatAlgo15xAs()
3032
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
31-
const useMatrixForArrayScalar = createUseMatrixForArrayScalar({ typed, matrix })
3233

3334
/**
3435
* Bitwise right arithmetic shift of a value x by y number of bits, `x >> y`.
@@ -78,6 +79,14 @@ export const createRightArithShift = /* #__PURE__ */ factory(name, dependencies,
7879
return matAlgo14xDs(x, y, self, false)
7980
}),
8081

82+
'Array, number | BigNumber': typed.referToSelf(self => (x, y) => {
83+
// check scalar
84+
if (equalScalar(y, 0)) {
85+
return clone(x)
86+
}
87+
return matAlgo15xAs(x, y, self, false)
88+
}),
89+
8190
'number | BigNumber, SparseMatrix': typed.referToSelf(self => (x, y) => {
8291
// check scalar
8392
if (equalScalar(x, 0)) {
@@ -92,9 +101,15 @@ export const createRightArithShift = /* #__PURE__ */ factory(name, dependencies,
92101
return zeros(y.size(), y.storage())
93102
}
94103
return matAlgo14xDs(y, x, self, true)
104+
}),
105+
'number | BigNumber, Array': typed.referToSelf(self => (x, y) => {
106+
// check scalar
107+
if (equalScalar(x, 0)) {
108+
return deepMap(y, () => x)
109+
}
110+
return matAlgo15xAs(y, x, self, true)
95111
})
96112
},
97-
useMatrixForArrayScalar,
98113
matrixAlgorithmSuite({
99114
SS: matAlgo08xS0Sid,
100115
DS: matAlgo01xDSid,

src/function/bitwise/rightLogShift.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { createMatAlgo14xDs } from '../../type/matrix/utils/matAlgo14xDs.js'
44
import { createMatAlgo01xDSid } from '../../type/matrix/utils/matAlgo01xDSid.js'
55
import { createMatAlgo10xSids } from '../../type/matrix/utils/matAlgo10xSids.js'
66
import { createMatAlgo08xS0Sid } from '../../type/matrix/utils/matAlgo08xS0Sid.js'
7+
import { createMatAlgo15xAs } from '../../type/matrix/utils/matAlgo15xAs.js'
78
import { factory } from '../../utils/factory.js'
89
import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js'
910
import { rightLogShiftNumber } from '../../plain/number/index.js'
10-
import { createUseMatrixForArrayScalar } from './useMatrixForArrayScalar.js'
11+
import { deepMap, clone } from '../../utils/array.js'
1112

1213
const name = 'rightLogShift'
1314
const dependencies = [
@@ -26,8 +27,8 @@ export const createRightLogShift = /* #__PURE__ */ factory(name, dependencies, (
2627
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
2728
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
2829
const matAlgo14xDs = createMatAlgo14xDs({ typed })
30+
const matAlgo15xAs = createMatAlgo15xAs()
2931
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
30-
const useMatrixForArrayScalar = createUseMatrixForArrayScalar({ typed, matrix })
3132

3233
/**
3334
* Bitwise right logical shift of value x by y number of bits, `x >>> y`.
@@ -76,6 +77,14 @@ export const createRightLogShift = /* #__PURE__ */ factory(name, dependencies, (
7677
return matAlgo14xDs(x, y, self, false)
7778
}),
7879

80+
'Array, number | BigNumber': typed.referToSelf(self => (x, y) => {
81+
// check scalar
82+
if (equalScalar(y, 0)) {
83+
return clone(x)
84+
}
85+
return matAlgo15xAs(x, y, self, false)
86+
}),
87+
7988
'number | BigNumber, SparseMatrix': typed.referToSelf(self => (x, y) => {
8089
// check scalar
8190
if (equalScalar(x, 0)) {
@@ -90,9 +99,16 @@ export const createRightLogShift = /* #__PURE__ */ factory(name, dependencies, (
9099
return zeros(y.size(), y.storage())
91100
}
92101
return matAlgo14xDs(y, x, self, true)
102+
}),
103+
104+
'number | BigNumber, Array': typed.referToSelf(self => (x, y) => {
105+
// check scalar
106+
if (equalScalar(x, 0)) {
107+
return deepMap(y, () => x)
108+
}
109+
return matAlgo15xAs(y, x, self, true)
93110
})
94111
},
95-
useMatrixForArrayScalar,
96112
matrixAlgorithmSuite({
97113
SS: matAlgo08xS0Sid,
98114
DS: matAlgo01xDSid,

src/function/bitwise/useMatrixForArrayScalar.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/unit-tests/function/arithmetic/add.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe('add', function () {
3131
assert.deepStrictEqual(add([3, 4], 2), [5, 6])
3232
})
3333

34+
it('should add a scalar and a jagged array correctly', function () {
35+
assert.deepStrictEqual(add(2, [[3, 4], 5]), [[5, 6], 7])
36+
assert.deepStrictEqual(add([[3, 4], 5], 2), [[5, 6], 7])
37+
})
38+
3439
it('should add broadcastable arrays correctly', function () {
3540
const a2 = [1, 2]
3641
const a3 = [[3], [4]]

test/unit-tests/function/arithmetic/mod.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ describe('mod', function () {
155155
approxDeepEqual(mod(3, [[4, 3], [2, 1]]), [[3, 0], [1, 0]])
156156
})
157157

158+
it('should perform element-wise modulus on a jagged array and a scalar', function () {
159+
approxDeepEqual(mod([[-4, -3, 0, -1], [0, 1, 2]], 3), [[2, 0, 0, 2], [0, 1, 2]])
160+
approxDeepEqual(mod(3, [[4, 3, 2], [2, 1]]), [[3, 0, 1], [1, 0]])
161+
})
162+
158163
it('should perform element-wise modulus on broadcastable arrays', function () {
159164
approxDeepEqual(mod([-40, -31], [[3], [1]]), [[2, 2], [0, 0]])
160165
approxDeepEqual(mod([[-40], [-31]], [3, 1]), [[2, 0], [2, 0]])

test/unit-tests/function/arithmetic/subtract.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ describe('subtract', function () {
148148
assert.deepStrictEqual(subtract([3, 0], 2), [1, -2])
149149
})
150150

151-
it('should substract broadcastable arrays correctly', function () {
151+
it('should subtract a scalar and a jagged array correctly', function () {
152+
assert.deepStrictEqual(subtract(2, [[3, 4], 5]), [[-1, -2], -3])
153+
assert.deepStrictEqual(subtract([[3, 4], 5], 2), [[1, 2], 3])
154+
})
155+
156+
it('should subtract broadcastable arrays correctly', function () {
152157
const a2 = [1, 2]
153158
const a3 = [[3], [4]]
154159
const a4 = subtract(a2, a3)

test/unit-tests/function/bitwise/bitAnd.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ describe('bitAnd', function () {
199199
assert.deepStrictEqual(bitAnd([3, 9], 12), [0, 8])
200200
})
201201

202+
it('should bitwise and a scalar and an jagged array correctly', function () {
203+
assert.deepStrictEqual(bitAnd(12, [[3, 9], 5]), [[0, 8], 4])
204+
assert.deepStrictEqual(bitAnd([[3, 9], 5], 12), [[0, 8], 4])
205+
})
206+
202207
it('should bitwise and broadcastable arrays correctly', function () {
203208
assert.deepStrictEqual(bitAnd([12, 13], [[3], [9]]), [[0, 1], [8, 9]])
204209
assert.deepStrictEqual(bitAnd([[12], [13]], [3, 9]), [[0, 8], [1, 9]])

test/unit-tests/function/bitwise/bitOr.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ describe('bitOr', function () {
199199
assert.deepStrictEqual(bitOr([3, 9], 12), [15, 13])
200200
})
201201

202+
it('should bitwise or a scalar and a jagged array correctly', function () {
203+
assert.deepStrictEqual(bitOr(12, [[3, 9], 5]), [[15, 13], 13])
204+
assert.deepStrictEqual(bitOr([[3, 9], 5], 12), [[15, 13], 13])
205+
})
206+
202207
it('should bitwise or broadcastable arrays correctly', function () {
203208
const a = [6, 4, 28]
204209
const b = [[13], [92], [101]]

test/unit-tests/function/bitwise/leftShift.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ describe('leftShift', function () {
111111
assert.deepStrictEqual(leftShift(2, [[1, 2], [8, 0]]), [[4, 8], [512, 2]])
112112
})
113113

114+
it('should left shift a jagged array and scalar', function () {
115+
assert.deepStrictEqual(leftShift([[1, 2], [8, 0, 2]], 2), [[4, 8], [32, 0, 8]])
116+
assert.deepStrictEqual(leftShift(2, [[1, 2], [8, 0, 8]]), [[4, 8], [512, 2, 512]])
117+
})
118+
114119
it('should left shift array - array', function () {
115120
assert.deepStrictEqual(leftShift([[1, 2], [8, 0]], [[4, 8], [32, 0]]), [[16, 512], [8, 0]])
116121
assert.deepStrictEqual(leftShift([[4, 8], [32, 0]], [[1, 2], [8, 0]]), [[8, 32], [8192, 0]])

0 commit comments

Comments
 (0)