![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
polynomial
Advanced tools
Polynomials are defined as the sum of variables with increasing integer power and their coefficients in a certain field. For example the following might be still known from school:
P(x) = x^2 + 4x + 3
const p = new Polynomial("3x^2").add("-x^2"); // 2x^2
const p = new Polynomial("5+3x^3+6x^5").derive(2); // 120x^3+18x
Any function (see below) as well as the constructor of the Polynomial class parses its input like this.
You can pass either Objects, Doubles or Strings. Make sure strings don't contain any white-spaces or brackets. The parser doesn't analyse the string recursively.
new Polynomial({'3': 4, '5': '9'}); // 9x^5+4x^3
new Polynomial([1,2,3]); //3x^2+2x+1
new Polynomial(55); // 55x^0
new Polynomial("98x^2+4+23x^4");
The string parser passes every coefficient directly to the field parser, which allows to pass complex and rational coefficients as well:
// Example with complex numbers
Polynomial.setField("C");
new Polynomial("98x^2+i+23ix^4");
// Example with rational numbers
Polynomial.setField("Q");
new Polynomial("5/3x^3+4/3x");
Polynomial.js is held general in order to operate on various fields. Fraction.js and Complex.js build the perfect base to extend polynomials to rational and complex numbers.
const p = new Polynomial("98x^2+4+23x^4");
console.log(p.coeff);
Polynomial.setField("Q");
Polynomial("3/2x^2-4x").mod("5x"); // 0
Polynomial.setField("Z11");
Polynomial("3x^2+x+7").gcd("3x^2+x+7"); // x^2+4x+6
Polynomial.setField("Z7");
Polynomial("9x^2+4").pow(3); // x^6+6x^4+5x^2+1
Polynomial.setField("R");
Polynomial("3x^3-1").mul(4); // 12x^3-4
// Derivative of polynomial
Polynomial.setField("Q");
Polynomial("5+3x^3+6x^5").derive(); // 30x^4+9x^2
// Integrated polynomial
Polynomial.setField("Q");
Polynomial("3x^2").integrate(); // x^3
Returns the sum of the actual polynomial and the parameter n
Returns the difference of the actual polynomial and the parameter n
Returns the product of the actual polynomial and the parameter n
Adds the product of x and y to the actual number
Returns the quotient of the actual polynomial and the parameter n
There is a global variable to enable division tracing like this, if you want to output details:
Polynomial.trace = true;
new Polynomial("x^4+3x^3+2x^2+6x")
.div("x+3");
console.log(Polynomial.trace.map(x => x.toString())); // ["x^4+3x^3", "2x^2+6x", "0"]
Returns the negated polynomial
Returns the reciprocal polynomial
Gets the leading coefficient
Gets the leading monomial
Divide all coefficients of the polynomial by lc()
Returns the n-th derivative of the polynomial
Returns the n-th integration of the polynomial
Evaluate the polynomial at point x, using Horner's method. Type for x must be a valid value for the given field.
(Deprecated) Alias for eval
.
Returns the power of the actual polynomial, raised to an integer exponent.
Returns the modulus (rest of the division) of the actual polynomial and n (this % n).
Returns the greatest common divisor of two polynomials
Returns the degree of the polynomial
Generates a string representation of the actual polynomial. This makes use of the toString()
function of the field.
Generates a LaTeX representation of the actual polynomial.
Formats the actual polynomial to a Horner Scheme
Creates a copy of the actual Polynomial object
Creates a new (monic) Polynomial whose roots lie at the values provided in the array roots
Sets the field globally. Choose one of the following strings for x
:
If a really hard error occurs (parsing error, division by zero), polynomial.js throws exceptions! Please make sure you handle them correctly.
Installing Polynomial.js is as easy as cloning this repo or use one of the following command:
npm install polynomial
<script src="fraction.min.js"></script> <!-- Needed for field/ring Q -->
<script src="complex.min.js"></script> <!-- Needed for field C -->
<script src="polynomial.min.js"></script>
<script>
Polynomial.setField("C")
console.log(Polynomial("4x+3i"));
</script>
As every library I publish, Polynomial.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.
After cloning the Git repository run:
npm install
npm run build
Testing the source against the shipped test suite is as easy as
npm run test
Copyright (c) 2024, Robert Eisele Licensed under the MIT license.
FAQs
A polynomial library
The npm package polynomial receives a total of 66 weekly downloads. As such, polynomial popularity was classified as not popular.
We found that polynomial demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.