Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Created October 8, 2023 23:50
Show Gist options
  • Save soltrinox/0ed19a95dd0fecb229ceea9245ae4033 to your computer and use it in GitHub Desktop.
Save soltrinox/0ed19a95dd0fecb229ceea9245ae4033 to your computer and use it in GitHub Desktop.

Polynomial Ring and Ideal Lattice in JavaScript

Given the mathematical concepts of Polynomial Ring and Ideal Lattice, we can implement them in JavaScript.

Definitions:

  1. Polynomial Ring: A polynomial ring is a set of polynomials in one or more indeterminates with coefficients in another ring,

  2. often a field. The polynomial ring, $$( K[X] )$$, in ( X ) over a field ( K ) can be defined as the set of expressions,

  3. called polynomials in ( X ), of the form: $$[ p = p_m X^m + p_{m-1} X^{m-1} + ... + p_1 X + p_0 ]$$ where ( p_0, p_1, …, p_m ) are elements of ( K ) and $$( X, X^2, … )$$ are symbols considered as "powers" of $$( X )$$.

  4. Ideal Lattice: Ideal lattices are lattices corresponding to ideals in rings of the form $$( \mathbb{Z}[x]/\langle f \rangle ) $$ for some irreducible polynomial ( f ) of degree ( n ).

JavaScript Implementation:

class IdealLattice {
  constructor(polynomialDegree, irreduciblePolynomial) {
    this.n = polynomialDegree;
    this.f = irreduciblePolynomial;
  }
}

class PolynomialRing extends IdealLattice {
  constructor(coefficients) {
    super(coefficients.length - 1, PolynomialRing.polynomialToString(coefficients));
    this.coefficients = coefficients;
  }

  static polynomialToString(coefficients) {
    return coefficients.map((coeff, index) => `${coeff}x^${index}`).join(' + ');
  }

  evaluate(xValue) {
    return this.coefficients.reduce((acc, coeff, index) => acc + coeff * Math.pow(xValue, index), 0);
  }
}

// Example usage:
const polynomialRing = new PolynomialRing([1, 0, -3, 4]); // Represents 1 + 0x - 3x^2 + 4x^3
console.log(polynomialRing);
console.log(`Value at x=2: ${polynomialRing.evaluate(2)}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment