Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change function kron to output a 1d vector when input is 1d #3133

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/function/matrix/kron.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export const createKron = /* #__PURE__ */ factory(name, dependencies, ({ typed,
const t = []
let r = []

// Check if both a and b are of length 1
if (a.length === 1 && b.length === 1) {
return a[0].flatMap((y) => b[0].map((x) => multiplyScalar(y, x)))
}

return a.map(function (a) {
return b.map(function (b) {
r = []
Expand Down
5 changes: 2 additions & 3 deletions test/unit-tests/function/matrix/kron.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ describe('kron', function () {
})

it('should calculate product for empty 2D Arrays', function () {
assert.deepStrictEqual(math.kron([[]], [[]]), [[]])
assert.deepStrictEqual(math.kron([[]], [[]]), [])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the inputs are 2d arrays, the output should stay a 2d array [[]] like it was before.

})

it('should calculate product for 1D Arrays', function () {
assert.deepStrictEqual(math.kron([1, 1], [[1, 0], [0, 1]]), [[1, 0, 1, 0], [0, 1, 0, 1]])
assert.deepStrictEqual(math.kron([[1, 0], [0, 1]], [1, 1]), [[1, 1, 0, 0], [0, 0, 1, 1]])
assert.deepStrictEqual(math.kron([1, 2, 6, 8], [12, 1, 2, 3]), [[12, 1, 2, 3, 24, 2, 4, 6, 72, 6, 12, 18, 96, 8, 16, 24]])
assert.deepStrictEqual(math.kron([1, 2, 6, 8], [12, 1, 2, 3]), [12, 1, 2, 3, 24, 2, 4, 6, 72, 6, 12, 18, 96, 8, 16, 24])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed the change we want 👌. Now, the name of the test is a bit misleading. Can you split the tests? One testing 2d inputs, one testing a mix of 1d and 2d input, and one testing 1d inputs?

})

it('should support complex numbers', function () {
Expand Down