Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A JavaScript library to work with polynomials

License

NotificationsYou must be signed in to change notification settings

rawify/Polynomial.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM PackageMIT license

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

Examples

Adding two polynomials

constp=newPolynomial("3x^2").add("-x^2");// 2x^2

Second derivative of polynomial

constp=newPolynomial("5+3x^3+6x^5").derive(2);// 120x^3+18x

Parser

Any function (see below) as well as the constructor of thePolynomial 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.

Objects

newPolynomial({'3':4,'5':'9'});// 9x^5+4x^3newPolynomial([1,2,3]);//3x^2+2x+1

Doubles

newPolynomial(55);// 55x^0

Strings

newPolynomial("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 numbersPolynomial.setField("C");newPolynomial("98x^2+i+23ix^4");// Example with rational numbersPolynomial.setField("Q");newPolynomial("5/3x^3+4/3x");

Fields

Polynomial.js is held general in order to operate on various fields.Fraction.js andComplex.js build the perfect base to extend polynomials to rational and complex numbers.

  • ℚ: Rational numbers supported byFraction.js
  • ℂ: Complex numbers supported byComplex.js
  • ℍ: Quaternions supported byQuaternion.js
  • p: Field of integers mod p, with p prime
  • ℝ: Field of real numbers

Accessing Coefficients

constp=newPolynomial("98x^2+4+23x^4");console.log(p.coeff);

Examples

Polynomial.setField("Q");Polynomial("3/2x^2-4x").mod("5x");// 0Polynomial.setField("Z11");Polynomial("3x^2+x+7").gcd("3x^2+x+7");// x^2+4x+6Polynomial.setField("Z7");Polynomial("9x^2+4").pow(3);// x^6+6x^4+5x^2+1Polynomial.setField("R");Polynomial("3x^3-1").mul(4);// 12x^3-4// Derivative of polynomialPolynomial.setField("Q");Polynomial("5+3x^3+6x^5").derive();// 30x^4+9x^2// Integrated polynomialPolynomial.setField("Q");Polynomial("3x^2").integrate();// x^3

Functions

Polynomial add(n)

Returns the sum of the actual polynomial and the parameter n

Polynomial sub(n)

Returns the difference of the actual polynomial and the parameter n

Polynomial mul(n)

Returns the product of the actual polynomial and the parameter n

Polynomial addmul(x, y)

Adds the product of x and y to the actual number

Polynomial div(n)

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;newPolynomial("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"]

Polynomial neg(n)

Returns the negated polynomial

Polynomial reciprocal(n)

Returns thereciprocal polynomial

Polynomial lc()

Gets the leading coefficient

Polynomial lm()

Gets the leading monomial

Polynomial monic()

Divide all coefficients of the polynomial by lc()

Polynomial derive(n)

Returns the n-th derivative of the polynomial

Polynomial integrate(n)

Returns the n-th integration of the polynomial

mixed eval(x)

Evaluate the polynomial at point x, usingHorner's method. Type for x must be a valid value for the given field.

mixed result(x)

(Deprecated) Alias foreval.

Polynomial pow(exp)

Returns the power of the actual polynomial, raised to an integer exponent.

Polynomial mod(n)

Returns the modulus (rest of the division) of the actual polynomial and n (this % n).

Polynomial gcd(n)

Returns the greatest common divisor of two polynomials

Number degree()

Returns the degree of the polynomial

String toString()

Generates a string representation of the actual polynomial. This makes use of thetoString() function of the field.

String toLatex()

Generates a LaTeX representation of the actual polynomial.

String toHorner()

Formats the actual polynomial to aHorner Scheme

Polynomial clone()

Creates a copy of the actual Polynomial object

Polynomial Polynomial::fromRoots(roots)

Creates a new (monic) Polynomial whose roots lie at the values provided in the arrayroots

Polynomial::setField(x)

Sets the field globally. Choose one of the following strings forx:

  • "R": real numbers
  • "Q": rational numbers
  • "H": quaternions
  • "C": complex numbers
  • "Zp": with p being a prime number, like "Z7"
  • or an object with the field operators. See examples folders for BigInt

Exceptions

If a really hard error occurs (parsing error, division by zero),polynomial.js throws exceptions! Please make sure you handle them correctly.

Installation

Installing Polynomial.js is as easy as cloning this repo or use one of the following command:

npm install polynomial

Using Polynomial.js with the browser

<scriptsrc="fraction.min.js"></script><!-- Needed for field/ring Q --><scriptsrc="complex.min.js"></script><!-- Needed for field C --><scriptsrc="polynomial.min.js"></script><script>Polynomial.setField("C")console.log(Polynomial("4x+3i"));</script>

Coding Style

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.

Building the library

After cloning the Git repository run:

npm installnpm run build

Run a test

Testing the source against the shipped test suite is as easy as

npm run test

Copyright and Licensing

Copyright (c) 2024,Robert EiseleLicensed under the MIT license.

About

A JavaScript library to work with polynomials

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp