Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Feb 13, 2025
1 parent 105759c commit 78d5254
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/type/matrix/DenseMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
if (maxDepth === 0) return me.clone()

const result = new DenseMatrix(me)
const fastCallback = optimizeCallback(callback, me._data, 'map')
const fastCallback = optimizeCallback(callback, me, 'map')
if (maxDepth === 1) {
return new DenseMatrix(
me._data.map((value, index) => fastCallback(value, [index], me))
Expand All @@ -629,7 +629,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
*/
DenseMatrix.prototype.forEach = function (callback) {
const me = this
const fastCallback = optimizeCallback(callback, me._data, 'map')
const fastCallback = optimizeCallback(callback, me, 'map')
me._forEach(function (arr, i, index) {
fastCallback(arr[i], index, me)
})
Expand Down
31 changes: 19 additions & 12 deletions src/utils/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ export function containsCollections (array) {
*/
export function deepForEach (array, callback) {
if (isMatrix(array)) {
array = array.valueOf()
array.forEach(callback)
} else {
recurseForEach(array, callback)
}

for (let i = 0, ii = array.length; i < ii; i++) {
const value = array[i]

function recurseForEach (value) {
if (Array.isArray(value)) {
deepForEach(value, callback)
value.forEach(recurseForEach)
} else {
callback(value)
}
Expand All @@ -54,13 +53,21 @@ export function deepForEach (array, callback) {
* @return {Array | Matrix} res
*/
export function deepMap (array, callback, skipZeros) {
if (array && (typeof array.map === 'function')) {
// TODO: replace array.map with a for loop to improve performance
return array.map(function (x) {
return deepMap(x, callback, skipZeros)
})
const skipZerosCallback = skipZeros
? value => value === 0 ? 0 : callback(value)
: callback
if (isMatrix(array)) {
// TODO: use a callback that uses only one arguemnt
return array.map(v => skipZerosCallback(v))
} else {
return callback(array)
return recurseMap(array)
}
function recurseMap (value) {
if (Array.isArray(value)) {
return value.map(recurseMap)
} else {
return skipZerosCallback(value)
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/benchmark/forEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ const bench = new Bench({ time: 100, iterations: 100 })
.add('numberMatrix.forEach(abs.signatures.number)', () => {
numberMatrix.forEach(abs.signatures.number)
})
.add('genericMatrix.forEach(abs+idx)', () => {
genericMatrix.forEach((x, idx) => abs(x) + idx[0] - idx[1])
})
.add('numberMatrix.forEach(abs+idx)', () => {
numberMatrix.forEach((x, idx) => abs(x) + idx[0] - idx[1])
})
.add('forEach(genericMatrix, abs+idx)', () => {
forEach(genericMatrix, (x, idx) => abs(x) + idx[0] - idx[1])
})
.add('genericMatrix.forEach(abs+idx+arr)', () => {
genericMatrix.forEach((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
})
.add('numberMatrix.forEach(abs+idx+arr)', () => {
numberMatrix.forEach((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
})
.add('forEach(genericMatrix, abs+idx+arr)', () => {
forEach(genericMatrix, (x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
})
.add('forEach(array, abs+idx+arr)', () => {
forEach(array, (x, idx, X) => abs(x) + idx[0] - idx[1] + X[0][0])
})
.add('genericMatrix iterate', () => {
for (const v of genericMatrix) {
abs(v.value)
}
})

bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
await bench.run()
24 changes: 24 additions & 0 deletions test/benchmark/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ const bench = new Bench({ time: 100, iterations: 100 })
.add('numberMatrix.map(abs.signatures.number)', () => {
numberMatrix.map(abs.signatures.number)
})
.add('map(array, abs + idx)', () => {
map(array, (x, idx) => abs(x) + idx[0] - idx[1])
})
.add('genericMatrix.map(abs + idx)', () => {
genericMatrix.map((x, idx) => abs(x) + idx[0] - idx[1])
})
.add('numberMatrix.map(abs + idx)', () => {
numberMatrix.map((x, idx) => abs(x) + idx[0] - idx[1])
})
.add('map(array, abs + idx + arr)', () => {
map(array, (x, idx, X) => abs(x) + idx[0] - idx[1] + X[0][0])
})
.add('genericMatrix.map(abs + idx + matrix)', () => {
genericMatrix.map((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
})
.add('numberMatrix.map(abs + idx + matrix)', () => {
numberMatrix.map((x, idx, X) => abs(x) + idx[0] - idx[1] + X.get([0, 0]))
})
.add('genericMatrix iterate', () => {
const result = genericMatrix.clone()
for (const v of genericMatrix) {
result.set(v.index, abs(v.value))
}
})

bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
await bench.run()
8 changes: 4 additions & 4 deletions test/unit-tests/type/matrix/DenseMatrix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,22 +818,22 @@ describe('DenseMatrix', function () {
])
expected = []
m.forEach((value, index) => { expected.push({ value, index }) })
assert.deepStrictEqual(expected, [...m])
assert.deepStrictEqual([...m], expected)

m = new DenseMatrix([1])
expected = []
m.forEach((value, index) => { expected.push({ value, index }) })
assert.deepStrictEqual(expected, [...m])
assert.deepStrictEqual([...m], expected)

m = new DenseMatrix([1, 2, 3])
expected = []
m.forEach((value, index) => { expected.push({ value, index }) })
assert.deepStrictEqual(expected, [...m])
assert.deepStrictEqual([...m], expected)

m = new DenseMatrix([])
expected = []
m.forEach((value, index) => { expected.push({ value, index }) })
assert.deepStrictEqual(expected, [...m])
assert.deepStrictEqual([...m], expected)
})
})

Expand Down

0 comments on commit 78d5254

Please sign in to comment.