Skip to content

Added Freivalds Algorithm #226

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

Merged
merged 2 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"list": {
"euclidean_algorithm": "Euclidean Algorithm",
"sieve_of_eratosthenes": "Sieve of Eratosthenes",
"freivalds_algorithm": "Freivalds Algorithm",
"miller_rabin_primality_test": "Miller-Rabin primality test"
},
"name": "Number Theory"
Expand Down
55 changes: 55 additions & 0 deletions algorithm/number_theory/freivalds_algorithm/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function FreivaldsAlgorithm() {
var k = 5;
var i, j, tmp, tmpB, tmpC, n = A.length;

while (k--) {
logger._print('Iterations remained: #' + k);

// Generate random vector
var r = [], P = [];
for (i = 0; i < n; i++) {
P.push(-1);
r.push( (Math.random() < 0.5) << 0);
}
_r._setData(r)._wait();

// Compute Br, Cr
var Br = [], Cr = [];
for (i = 0; i < n; i++) {
tmpB = 0;
tmpC = 0;
for (j = 0; j < n; j++) {
tmpB += r[j] * B[j][i];
tmpC += r[j] * C[j][i];
}
Br.push(tmpB);
Cr.push(tmpC);
}

// Compute A * Br - Cr
P = [];
for (i = 0; i < n; i++) {
tmp = 0;
for (j = 0; j < n; j++) {
tmp += (A[i][j] * Br[i]) - Cr[i];
}
P.push(tmp);
}
_p._setData(P)._wait();

for (i = 0; i < n; i++) {
if (P[i] !== 0) {
logger._print('P[' + i + '] !== 0 (' + P[i] + '), exit');
return false;
}
}

logger._print('Result vector is identity, continue...');


}

return true;
}

FreivaldsAlgorithm();
12 changes: 12 additions & 0 deletions algorithm/number_theory/freivalds_algorithm/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var A = [[2,3],[3,4]];
var B = [[1,0],[1,2]];
var C = [[6,5],[8,7]];

var _a = new Array2DTracer('Matrix A'); _a._setData(A);
var _b = new Array2DTracer('Matrix B'); _b._setData(B);
var _c = new Array2DTracer('Matrix C'); _c._setData(C);

var logger = new LogTracer();

var _r = new Array1DTracer('Random Vector');
var _p = new Array1DTracer('Result Vector');
12 changes: 12 additions & 0 deletions algorithm/number_theory/freivalds_algorithm/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Freivalds Algorithm": "Freivalds' algorithm is a probabilistic randomized algorithm used to verify matrix multiplication. Given three n × n matrices A, B, and C, a general problem is to verify whether A*B=C",
"Complexity": {
"time": "$O(n^2)$"
},
"References": [
"<a href='https://www.wikiwand.com/en/Freivalds%27_algorithm'>Wikipedia</a>"
],
"files": {
"basic": "Freivalds Algorithm"
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy