Skip to content

Commit 78d5254

Browse files
committed
Initial commit
1 parent 105759c commit 78d5254

File tree

5 files changed

+75
-18
lines changed

5 files changed

+75
-18
lines changed

src/type/matrix/DenseMatrix.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
606606
if (maxDepth === 0) return me.clone()
607607

608608
const result = new DenseMatrix(me)
609-
const fastCallback = optimizeCallback(callback, me._data, 'map')
609+
const fastCallback = optimizeCallback(callback, me, 'map')
610610
if (maxDepth === 1) {
611611
return new DenseMatrix(
612612
me._data.map((value, index) => fastCallback(value, [index], me))
@@ -629,7 +629,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
629629
*/
630630
DenseMatrix.prototype.forEach = function (callback) {
631631
const me = this
632-
const fastCallback = optimizeCallback(callback, me._data, 'map')
632+
const fastCallback = optimizeCallback(callback, me, 'map')
633633
me._forEach(function (arr, i, index) {
634634
fastCallback(arr[i], index, me)
635635
})

src/utils/collection.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ export function containsCollections (array) {
2727
*/
2828
export function deepForEach (array, callback) {
2929
if (isMatrix(array)) {
30-
array = array.valueOf()
30+
array.forEach(callback)
31+
} else {
32+
recurseForEach(array, callback)
3133
}
32-
33-
for (let i = 0, ii = array.length; i < ii; i++) {
34-
const value = array[i]
35-
34+
function recurseForEach (value) {
3635
if (Array.isArray(value)) {
37-
deepForEach(value, callback)
36+
value.forEach(recurseForEach)
3837
} else {
3938
callback(value)
4039
}
@@ -54,13 +53,21 @@ export function deepForEach (array, callback) {
5453
* @return {Array | Matrix} res
5554
*/
5655
export function deepMap (array, callback, skipZeros) {
57-
if (array && (typeof array.map === 'function')) {
58-
// TODO: replace array.map with a for loop to improve performance
59-
return array.map(function (x) {
60-
return deepMap(x, callback, skipZeros)
61-
})
56+
const skipZerosCallback = skipZeros
57+
? value => value === 0 ? 0 : callback(value)
58+
: callback
59+
if (isMatrix(array)) {
60+
// TODO: use a callback that uses only one arguemnt
61+
return array.map(v => skipZerosCallback(v))
6262
} else {
63-
return callback(array)
63+
return recurseMap(array)
64+
}
65+
function recurseMap (value) {
66+
if (Array.isArray(value)) {
67+
return value.map(recurseMap)
68+
} else {
69+
return skipZerosCallback(value)
70+
}
6471
}
6572
}
6673

test/benchmark/forEach.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ const bench = new Bench({ time: 100, iterations: 100 })
4343
.add('numberMatrix.forEach(abs.signatures.number)', () => {
4444
numberMatrix.forEach(abs.signatures.number)
4545
})
46+
.add('genericMatrix.forEach(abs+idx)', () => {
47+
genericMatrix.forEach((x, idx) => abs(x) + idx[0] - idx[1])
48+
})
49+
.add('numberMatrix.forEach(abs+idx)', () => {
50+
numberMatrix.forEach((x, idx) => abs(x) + idx[0] - idx[1])
51+
})
52+
.add('forEach(genericMatrix, abs+idx)', () => {
53+
forEach(genericMatrix, (x, idx) => abs(x) + idx[0] - idx[1])
54+
})
55+
.add('genericMatrix.forEach(abs+idx+arr)', () => {
56+
genericMatrix.forEach((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
57+
})
58+
.add('numberMatrix.forEach(abs+idx+arr)', () => {
59+
numberMatrix.forEach((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
60+
})
61+
.add('forEach(genericMatrix, abs+idx+arr)', () => {
62+
forEach(genericMatrix, (x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
63+
})
64+
.add('forEach(array, abs+idx+arr)', () => {
65+
forEach(array, (x, idx, X) => abs(x) + idx[0] - idx[1] + X[0][0])
66+
})
67+
.add('genericMatrix iterate', () => {
68+
for (const v of genericMatrix) {
69+
abs(v.value)
70+
}
71+
})
4672

4773
bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
4874
await bench.run()

test/benchmark/map.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ const bench = new Bench({ time: 100, iterations: 100 })
4343
.add('numberMatrix.map(abs.signatures.number)', () => {
4444
numberMatrix.map(abs.signatures.number)
4545
})
46+
.add('map(array, abs + idx)', () => {
47+
map(array, (x, idx) => abs(x) + idx[0] - idx[1])
48+
})
49+
.add('genericMatrix.map(abs + idx)', () => {
50+
genericMatrix.map((x, idx) => abs(x) + idx[0] - idx[1])
51+
})
52+
.add('numberMatrix.map(abs + idx)', () => {
53+
numberMatrix.map((x, idx) => abs(x) + idx[0] - idx[1])
54+
})
55+
.add('map(array, abs + idx + arr)', () => {
56+
map(array, (x, idx, X) => abs(x) + idx[0] - idx[1] + X[0][0])
57+
})
58+
.add('genericMatrix.map(abs + idx + matrix)', () => {
59+
genericMatrix.map((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
60+
})
61+
.add('numberMatrix.map(abs + idx + matrix)', () => {
62+
numberMatrix.map((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
63+
})
64+
.add('genericMatrix iterate', () => {
65+
const result = genericMatrix.clone()
66+
for (const v of genericMatrix) {
67+
result.set(v.index, abs(v.value))
68+
}
69+
})
4670

4771
bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
4872
await bench.run()

test/unit-tests/type/matrix/DenseMatrix.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,22 +818,22 @@ describe('DenseMatrix', function () {
818818
])
819819
expected = []
820820
m.forEach((value, index) => { expected.push({ value, index }) })
821-
assert.deepStrictEqual(expected, [...m])
821+
assert.deepStrictEqual([...m], expected)
822822

823823
m = new DenseMatrix([1])
824824
expected = []
825825
m.forEach((value, index) => { expected.push({ value, index }) })
826-
assert.deepStrictEqual(expected, [...m])
826+
assert.deepStrictEqual([...m], expected)
827827

828828
m = new DenseMatrix([1, 2, 3])
829829
expected = []
830830
m.forEach((value, index) => { expected.push({ value, index }) })
831-
assert.deepStrictEqual(expected, [...m])
831+
assert.deepStrictEqual([...m], expected)
832832

833833
m = new DenseMatrix([])
834834
expected = []
835835
m.forEach((value, index) => { expected.push({ value, index }) })
836-
assert.deepStrictEqual(expected, [...m])
836+
assert.deepStrictEqual([...m], expected)
837837
})
838838
})
839839

0 commit comments

Comments
 (0)