Skip to content

Commit

Permalink
removed unnecessary duplication from matrixFromRows/Columns
Browse files Browse the repository at this point in the history
  • Loading branch information
cshaa committed Apr 12, 2021
1 parent 4966b2d commit f80e4fb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 62 deletions.
37 changes: 4 additions & 33 deletions src/function/matrix/matrixFromColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export const createMatrixFromColumns = /* #__PURE__ */ factory(name, dependencie
return _createArray(arr)
},
'...Matrix': function (arr) {
return _createMatrix(arr)
return matrix(_createArray(arr.map(m => m.toArray())))
}

// TODO implement this properly for SparseMatrix
})

function _createArray (arr) {
Expand Down Expand Up @@ -64,39 +66,8 @@ export const createMatrixFromColumns = /* #__PURE__ */ factory(name, dependencie
return result
}

function _createMatrix (arr) {
if (arr.length === 0) throw new TypeError('At least one column is needed to construct a matrix.')
const N = checkVectorTypeAndReturnLength(arr[0])

// create an array with empty rows
const data = []
for (let i = 0; i < N; i++) {
data[i] = []
}

// loop columns
for (const col of arr) {
const colLength = checkVectorTypeAndReturnLength(col)

if (colLength !== N) {
throw new TypeError('The vectors had different length: ' + (N | 0) + ' ≠ ' + (colLength | 0))
}

// push a value to each row
let i = 0
col.forEach(value => data[i++].push(value))
}

return matrix(data)
}

function checkVectorTypeAndReturnLength (vec) {
let s
if (Array.isArray(vec)) {
s = size(vec)
} else {
s = vec.size()
}
const s = size(vec)

if (s.length === 1) { // 1D vector
return s[0]
Expand Down
33 changes: 4 additions & 29 deletions src/function/matrix/matrixFromRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export const createMatrixFromRows = /* #__PURE__ */ factory(name, dependencies,
return _createArray(arr)
},
'...Matrix': function (arr) {
return _createMatrix(arr)
return matrix(_createArray(arr.map(m => m.toArray())))
}

// TODO implement this properly for SparseMatrix
})

function _createArray (arr) {
Expand All @@ -53,35 +55,8 @@ export const createMatrixFromRows = /* #__PURE__ */ factory(name, dependencies,
return result
}

function _createMatrix (arr) {
if (arr.length === 0) throw new TypeError('At least one row is needed to construct a matrix.')
const N = checkVectorTypeAndReturnLength(arr[0])

const data = []
for (const row of arr) {
const rowLength = checkVectorTypeAndReturnLength(row)

if (rowLength !== N) {
throw new TypeError('The vectors had different length: ' + (N | 0) + ' ≠ ' + (rowLength | 0))
}

if (row.storage() === 'dense') {
data.push(flatten(row._data))
} else {
data.push(flatten(row.toArray()))
}
}

return matrix(data)
}

function checkVectorTypeAndReturnLength (vec) {
let s
if (Array.isArray(vec)) {
s = size(vec)
} else {
s = vec.size()
}
const s = size(vec)

if (s.length === 1) { // 1D vector
return s[0]
Expand Down

0 comments on commit f80e4fb

Please sign in to comment.