Skip to content
Permalink
Browse files

Add 'matrix multiplication' algorithm

  • Loading branch information...
marcomontalbano committed Feb 24, 2019
1 parent 8da3840 commit 700ea03e66462d70992052537ae47103b7064435
Showing with 119 additions and 6 deletions.
  1. +27 −0 src-js/libs/matrix.js
  2. +77 −0 src-js/libs/matrix.test.js
  3. +1 −0 src-js/libs/mod.js
  4. +10 −2 src-js/main.js
  5. +3 −3 src-js/web/chart.js
  6. +1 −1 src-js/web/utility.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;
};
@@ -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 +1,2 @@
export * from './primes';
export * from './matrix';
@@ -1,6 +1,14 @@
import { createBenchmarkChart } from './web/chart';

// createBenchmarkChart({
// method: 'get_primes',
// args: [100000]
// });

createBenchmarkChart({
method: 'get_primes',
args: [100000]
method: 'multiply',
args: [
Array(500).fill(Array(500).fill(1)),
Array(500).fill(Array(500).fill(1)),
]
});
@@ -81,11 +81,11 @@ const _runBenchmark = (payload, chart, times = 5) => {
return runBenchmark(payload, times,
value => {
_chart_addData(chart, `${value.workerName.toUpperCase()} Benchmark`, value.performance.measure.duration.toFixed(2));
console.log(value);
// console.log(value);
return value;
}
).then(value => {
console.log(value);
// console.log(value);
});
}

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

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);
document.getElementById('root').appendChild(element_chartContainer);
@@ -52,7 +52,7 @@ const _promiseSequential = fns => {

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

0 comments on commit 700ea03

Please sign in to comment.
You can’t perform that action at this time.