1
0
forked from kalmar/DALGLI0
This commit is contained in:
Kacper Kruczek 2018-07-01 11:36:07 +02:00
parent 809ceae806
commit 9bf6d4721e

View File

@ -30,6 +30,50 @@ function add(p1, p2) {
} }
exports.add = add; exports.add = add;
function sub(p1, p2) {
let n;
if (p1.mod !== p2.mod) {
throw "different modulo"
} else {
n = p1.mod;
}
let len_p1 = p1.coefficients.length;
let len_p2 = p2.coefficients.length;
result = new Array(Math.max(len_p1, len_p2)).fill(0);
if (len_p1 > len_p2) {
for (let x = 0; x < len_p1 - len_p2; x++) p2.coefficients.push(0);
} else {
for (let x = 0; x < len_p2 - len_p1; x++) p1.coefficients.push(0);
}
for (let i = 0; i < result.length; i++) {
result[i] = (p1.coefficients[i] - p2.coefficients[i]) % n;
}
return new Polynomial(n, result);
}
exports.sub = sub;
function sub(p1, p2) {
let n;
if (p1.mod !== p2.mod) {
throw "different modulo"
} else {
n = p1.mod;
}
let len_p1 = p1.coefficients.length;
let len_p2 = p2.coefficients.length;
result = new Array(Math.max(len_p1, len_p2)).fill(0);
if (len_p1 > len_p2) {
for (let x = 0; x < len_p1 - len_p2; x++) p2.coefficients.push(0);
} else {
for (let x = 0; x < len_p2 - len_p1; x++) p1.coefficients.push(0);
}
for (let i = 0; i < result.length; i++) {
result[i] = (p1.coefficients[i] - p2.coefficients[i]) % n;
}
return new Polynomial(n, result);
}
exports.add = add;
function multiply(p1, p2) { function multiply(p1, p2) {
let n; let n;
if (p1.mod !== p2.mod) { if (p1.mod !== p2.mod) {