Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'matrix multiplication' algorithm
- Loading branch information
1 parent
8da3840
commit 700ea03
Showing
6 changed files
with
119 additions
and
6 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
export const multiply = (a, b) => { | |||
let c = []; | |||
for (let i = 0; i < a.length; i++) { | |||
for (let j = 0; j < b[0].length; j++) { | |||
for (let k = 0; k < b.length; k++) { | |||
c[i] = c[i] || []; | |||
c[i][j] = c[i][j] || 0; | |||
c[i][j] += a[i][k] * b[k][j]; | |||
} | |||
} | |||
} | |||
return c; | |||
}; | |||
|
|||
export const multiply_slower = (a, b) => { | |||
let c = []; | |||
for (let k = 0; k < b.length; k++) { | |||
for (let j = 0; j < b[0].length; j++) { | |||
for (let i = 0; i < a.length; i++) { | |||
c[i] = c[i] || []; | |||
c[i][j] = c[i][j] || 0; | |||
c[i][j] += a[i][k] * b[k][j]; | |||
} | |||
} | |||
} | |||
return c; | |||
}; |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,77 @@ | |||
import assert from 'assert'; | |||
|
|||
import * as matrix from './matrix'; | |||
|
|||
const createTestFor = method => { | |||
describe(`.${method}()`, () => { | |||
it('test 1', () => { | |||
const expected = [ | |||
[7, 10], | |||
[15, 22], | |||
]; | |||
|
|||
assert.deepEqual(matrix[method]( | |||
[ | |||
[1, 2], | |||
[3, 4] | |||
], | |||
[ | |||
[1, 2], | |||
[3, 4] | |||
] | |||
), expected); | |||
}); | |||
|
|||
it('test 2', () => { | |||
const expected = [ | |||
[58, 64], | |||
[139, 154], | |||
]; | |||
|
|||
assert.deepEqual(matrix[method]( | |||
[ | |||
[1, 2, 3], | |||
[4, 5, 6] | |||
], | |||
[ | |||
[7, 8], | |||
[9, 10], | |||
[11, 12] | |||
] | |||
), expected); | |||
}); | |||
|
|||
it('test 3', () => { | |||
const expected = [[83, 63, 37, 75]]; | |||
|
|||
assert.deepEqual(matrix[method]( | |||
[[3, 4, 2]], | |||
[ | |||
[13, 9, 7, 15], | |||
[8, 7, 4, 6], | |||
[6, 4, 0, 3] | |||
] | |||
), expected); | |||
}); | |||
|
|||
it('test 4', () => { | |||
const expected = [[4, 4], [10, 8]]; | |||
|
|||
assert.deepEqual(matrix[method]( | |||
[ | |||
[1, 2], | |||
[3, 4], | |||
], | |||
[ | |||
[2, 0], | |||
[1, 2], | |||
] | |||
), expected); | |||
}); | |||
}); | |||
} | |||
|
|||
describe('matrix', () => { | |||
createTestFor('multiply'); | |||
createTestFor('multiply_slower'); | |||
}); |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1,2 @@ | |||
export * from './primes'; | export * from './primes'; | ||
export * from './matrix'; |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,6 +1,14 @@ | |||
import { createBenchmarkChart } from './web/chart'; | import { createBenchmarkChart } from './web/chart'; | ||
|
|
||
// createBenchmarkChart({ | |||
// method: 'get_primes', | |||
// args: [100000] | |||
// }); | |||
|
|||
createBenchmarkChart({ | createBenchmarkChart({ | ||
method: 'get_primes', | method: 'multiply', | ||
args: [100000] | args: [ | ||
Array(500).fill(Array(500).fill(1)), | |||
Array(500).fill(Array(500).fill(1)), | |||
] | |||
}); | }); |
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