MCPcopy Index your code
hub / github.com/trekhleb/javascript-algorithms / generate

Function generate

src/algorithms/math/matrix/Matrix.js:86–108  ·  view source on GitHub ↗
(mShape, fill)

Source from the content-addressed store, hash-verified

84 * @returns {Matrix}
85 */
86export const generate = (mShape, fill) => {
87 /**
88 * Generates the matrix recursively.
89 *
90 * @param {Shape} recShape - the shape of the matrix to generate
91 * @param {CellIndices} recIndices
92 * @returns {Matrix}
93 */
94 const generateRecursively = (recShape, recIndices) => {
95 if (recShape.length === 1) {
96 return Array(recShape[0])
97 .fill(null)
98 .map((cellValue, cellIndex) => fill([...recIndices, cellIndex]));
99 }
100 const m = [];
101 for (let i = 0; i < recShape[0]; i += 1) {
102 m.push(generateRecursively(recShape.slice(1), [...recIndices, i]));
103 }
104 return m;
105 };
106
107 return generateRecursively(mShape, []);
108};
109
110/**
111 * Generates the matrix of zeros of specified shape.

Callers 1

zerosFunction · 0.85

Calls 1

generateRecursivelyFunction · 0.85

Tested by

no test coverage detected