Skip to content

Commit

Permalink
Add 'matrix multiplication' algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Feb 24, 2019
1 parent 8da3840 commit 700ea03
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
27 changes: 27 additions & 0 deletions src-js/libs/matrix.js
@@ -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;
};
77 changes: 77 additions & 0 deletions src-js/libs/matrix.test.js
@@ -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');
});
1 change: 1 addition & 0 deletions src-js/libs/mod.js
@@ -1 +1,2 @@
export * from './primes'; export * from './primes';
export * from './matrix';
12 changes: 10 additions & 2 deletions src-js/main.js
@@ -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)),
]
}); });
6 changes: 3 additions & 3 deletions src-js/web/chart.js
Expand Up @@ -81,11 +81,11 @@ const _runBenchmark = (payload, chart, times = 5) => {
return runBenchmark(payload, times, return runBenchmark(payload, times,
value => { value => {
_chart_addData(chart, `${value.workerName.toUpperCase()} Benchmark`, value.performance.measure.duration.toFixed(2)); _chart_addData(chart, `${value.workerName.toUpperCase()} Benchmark`, value.performance.measure.duration.toFixed(2));
console.log(value); // console.log(value);
return value; return value;
} }
).then(value => { ).then(value => {
console.log(value); // console.log(value);
}); });
} }


Expand All @@ -95,7 +95,7 @@ export const createBenchmarkChart = payload => {
element_chartContainer.classList.add('chart-container'); element_chartContainer.classList.add('chart-container');


let element_chartCanvas = document.createElement('canvas'); let element_chartCanvas = document.createElement('canvas');
let chart = _createChart(element_chartCanvas, `${payload.method}(${payload.args})`); let chart = _createChart(element_chartCanvas, `${payload.method}`);


element_chartContainer.appendChild(element_chartCanvas); element_chartContainer.appendChild(element_chartCanvas);
document.getElementById('root').appendChild(element_chartContainer); document.getElementById('root').appendChild(element_chartContainer);
Expand Down
2 changes: 1 addition & 1 deletion src-js/web/utility.js
Expand Up @@ -52,7 +52,7 @@ const _promiseSequential = fns => {


export const runBenchmark = (payload, times = 5, eachTime = value => value) => { export const runBenchmark = (payload, times = 5, eachTime = value => value) => {
return _promiseSequential(_cloneArrayElements([ return _promiseSequential(_cloneArrayElements([
() => rsWorker.postMessage(payload).then(eachTime), // () => rsWorker.postMessage(payload).then(eachTime),
() => jsWorker.postMessage(payload).then(eachTime), () => jsWorker.postMessage(payload).then(eachTime),
], times)); ], times));
} }

0 comments on commit 700ea03

Please sign in to comment.