-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
44 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,40 @@ | ||
import { checkBroadcastingRules } from '../../../utils/array.js' | ||
import { factory } from '../../../utils/factory.js' | ||
|
||
const name = 'broadcast' | ||
|
||
const dependancies = ['concat'] | ||
|
||
export const createBroadcast = /* #__PURE__ */ factory( | ||
name, dependancies, | ||
({ concat }) => { | ||
/** | ||
* Broadcasts two matrices, and return both in an array | ||
* It checks if it's possible with broadcasting rules | ||
* | ||
* @param {Matrix} A First Matrix | ||
* @param {Matrix} B Second Matrix | ||
* | ||
* @return {Matrix[]} [ broadcastedA, broadcastedB ] | ||
*/ | ||
return function (A, B) { | ||
const N = Math.max(A._size.length, B._size.length) // max number of dims | ||
if (A._size.length === B._size.length) { | ||
if (A._size.every((dim, i) => dim === B._size[i])) { | ||
// If matrices have the same size return them | ||
return [A, B] | ||
} | ||
} | ||
|
||
const sizeA = _padLeft(A._size, N, 0) // pad to the left to align dimensions to the right | ||
const sizeB = _padLeft(B._size, N, 0) // pad to the left to align dimensions to the right | ||
|
||
// calculate the max dimensions | ||
const sizeMax = [] | ||
|
||
for (let dim = 0; dim < N; dim++) { | ||
sizeMax[dim] = Math.max(sizeA[dim], sizeB[dim]) | ||
} | ||
|
||
// check if the broadcasting rules applyes for both matrices | ||
checkBroadcastingRules(sizeA, sizeMax) | ||
checkBroadcastingRules(sizeB, sizeMax) | ||
|
||
// reshape A or B if needed to make them ready for concat | ||
let AA = A.clone() | ||
let BB = B.clone() | ||
if (AA._size.length < N) { | ||
AA.reshape(_padLeft(AA._size, N, 1)) | ||
} else if (BB._size.length < N) { | ||
BB.reshape(_padLeft(BB._size, N, 1)) | ||
} | ||
|
||
// stretches the matrices on each dimension to make them the same size | ||
for (let dim = 0; dim < N; dim++) { | ||
if (AA._size[dim] < sizeMax[dim]) { AA = _stretch(AA, sizeMax[dim], dim) } | ||
if (BB._size[dim] < sizeMax[dim]) { BB = _stretch(BB, sizeMax[dim], dim) } | ||
} | ||
|
||
// return the array with the two broadcasted matrices | ||
return [AA, BB] | ||
} | ||
|
||
function _padLeft (shape, N, filler) { | ||
// pads an array of dimensions with numbers to the left, unitl the number of dimensions is N | ||
return [...Array(N - shape.length).fill(filler), ...shape] | ||
} | ||
import { broadcastSizes, broadcastTo } from '../../../utils/array.js' | ||
import { deepStrictEqual } from '../../../utils/object.js' | ||
|
||
/** | ||
* Broadcasts two matrices, and return both in an array | ||
* It checks if it's possible with broadcasting rules | ||
* | ||
* @param {Matrix} A First Matrix | ||
* @param {Matrix} B Second Matrix | ||
* | ||
* @return {Matrix[]} [ broadcastedA, broadcastedB ] | ||
*/ | ||
|
||
export function broadcast (A, B) { | ||
if (deepStrictEqual(A.size(), B.size())) { | ||
// If matrices have the same size return them | ||
return [A, B] | ||
} | ||
|
||
function _stretch (arrayToStretch, sizeToStretch, dimToStretch) { | ||
// stretches a matrix up to a certain size in a certain dimension | ||
return concat(...Array(sizeToStretch).fill(arrayToStretch), dimToStretch) | ||
} | ||
// calculate the broadcasted sizes | ||
const newSize = broadcastSizes(A.size(), B.size()) | ||
|
||
// return the array with the two broadcasted matrices | ||
return [A, B].map(M => _broadcastTo(M, newSize)) | ||
} | ||
|
||
/** | ||
* Broadcasts a matrix to the given size. | ||
* | ||
* @param {Matrix} M - The matrix to be broadcasted. | ||
* @param {number[]} size - The desired size of the broadcasted matrix. | ||
* @returns {Matrix} The broadcasted matrix. | ||
* @throws {Error} If the size parameter is not an array of numbers. | ||
*/ | ||
function _broadcastTo (M, size) { | ||
if (deepStrictEqual(M.size(), size)) { | ||
return M | ||
} | ||
) | ||
return M.create(broadcastTo(M.valueOf(), size), M.datatype()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters