https://github.com/lapets/polynomium
Library for symbolically representing and working with polynomials.
https://github.com/lapets/polynomium
javascript-library library math mathematics polynomial polynomials symbolic-math
Last synced: 7 months ago
JSON representation
Library for symbolically representing and working with polynomials.
- Host: GitHub
- URL: https://github.com/lapets/polynomium
- Owner: lapets
- License: mit
- Created: 2017-12-31T16:01:32.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-26T02:47:38.000Z (over 7 years ago)
- Last Synced: 2025-02-27T02:51:50.644Z (7 months ago)
- Topics: javascript-library, library, math, mathematics, polynomial, polynomials, symbolic-math
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/polynomium
- Size: 22.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# polynomium
Library for symbolically representing and working with polynomials.[](https://badge.fury.io/js/polynomium)
## Package Installation and Usage
The package is available on npm:
```shell
npm install polynomium
```
The library can be imported in the usual ways:
```javascript
var polynomium = require('polynomium');
```
The library also supports standalone usage in browsers:
```html```
## Examples
The library supports the creation of objects that represent polynomials of zero or more variables:
```javascript
var x = polynomium.v('x');
var y = polynomium.v('y');
var a = polynomium.c(2);
var b = polynomium.c(3);
var c = polynomium.c(5);
var p = (y.mul(x.add(b)));
var q = (y.mul(x.add(b))).mul(a);
var r = (y.mul(x.add(b))).mul((y.mul(x.add(b))));
```
Given the polynomials above, it is possible to display them in a human-readable way:
```javascript
> x.toString()
'x'
> b.toString()
'3'
> (y.add(x.add(b))).toString()
'y + x + 3'
> p.toString()
'x*y + 3y'
> q.toString()
'2x*y + 6y'
> q.maxCoefficients(p).toString()
'2x*y + 6y'
> r.toString()
'x^2*y^2 + 6x*y^2 + 9y^2'
```
When possible, the operator functions will convert arguments that are numeric constants and valid variable names into polynomium objects:
```javascript
> (y.add(x.add(5))).toString()
'y + x + 5'
> (y.add('x')).toString()
'y + x'
```
By default, the terms in the outer sum are in order of descending significance (where a term's significance is the sum of its exponents across its factors). The individual variables within factors are in ascending alphabetical order.It is also possible to evaluate a polynomial by supplying an object that binds explicit values to each variable:
```javascript
> r.evaluate({"x":2, "y":5})
625
> r({"x":2, "y":5})
625
```
In some cases, function objects cannot be used within data structures (such as when converting to JSON). A method is included for such scenarios:
```javascript
> var o = r.toObject()
> o
{ polynomium: true,
terms:
{ 'y': { '2': 9 },
'x,y': { '1,2': 6, '2,2': 1 } } }
> polynomium.add(o, o).toObject()
{ polynomium: true,
terms:
{ 'y': { '2': 18 },
'x,y': { '1,2': 12, '2,2': 2 } } }
```## Testing
Unit tests are included in `test/test.js`. They can be run using [Mocha](https://mochajs.org/):
```javascript
npm test
```