You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1003 B

5 years ago
  1. /*
  2. This library do operations on polinomials where their coefficients are in field F
  3. The polynomial P(x) = p0 + p1 * x + p2 * x^2 + p3 * x^3, ...
  4. is represented by the array [ p0, p1, p2, p3, ... ]
  5. */
  6. class PolField {
  7. constructor (F) {
  8. this.F = F;
  9. }
  10. _reduce(a) {
  11. let i = a.length-1;
  12. while ((i>=0) && (this.F.isZero(a[i])) ) i--;
  13. return (i < a.length-1) ? a.slice(0, i+1) : a;
  14. }
  15. add(a, b) {
  16. const maxGrade = Math.max(a.length, b.length);
  17. const res = new Array(maxGrade);
  18. for (let i=0; i<maxGrade; i++) {
  19. res[i] = this.F.add(a[i], b[i]);
  20. }
  21. return this._reduce(res);
  22. }
  23. sub(a, b) {
  24. throw new Error("Not Implementted");
  25. }
  26. mul(a, b) {
  27. throw new Error("Not Implementted");
  28. }
  29. div(a, b) {
  30. throw new Error("Not Implementted");
  31. }
  32. lagrange(points) {
  33. throw new Error("Not Implementted");
  34. }
  35. }
  36. module.exports = PolField;