Module

# Data.Polynomial

Package
purescript-polynomials
Repository
hdgarrood/purescript-polynomials

### #PolynomialSource

``newtype Polynomial a``

Finite-degree polynomials, with coefficients given by the type argument. So for example, a `Polynomial Int` is a polynomial with integer coefficients.

The `Monoid` instance works by considering polynomials as functions, where `<>` corresponds to function composition and the identity polynomial `mempty` is nothing more than the identity function `P(x) = x`.

#### Instances

• `Functor Polynomial`
• `(Eq a) => Eq (Polynomial a)`
• `(Eq a, Semiring a, Arbitrary a) => Arbitrary (Polynomial a)`
• `(Eq a, Semiring a) => Semiring (Polynomial a)`
• `(Eq a, Ring a) => Ring (Polynomial a)`
• `(Eq a, CommutativeRing a) => CommutativeRing (Polynomial a)`
• `(Eq a, Field a) => EuclideanRing (Polynomial a)`
• `(Eq a, Semiring a) => Semigroup (Polynomial a)`
• `(Eq a, Semiring a) => Monoid (Polynomial a)`
• `(Show a, Semiring a, Eq a) => Show (Polynomial a)`

### #fromCoefficientsSource

``fromCoefficients :: forall a. Eq a => Semiring a => Array a -> Polynomial a``

Construct a polynomial from coefficients. The constant coefficient comes first, so for example the polynomial `x^4 + 2x^3 + 3x^2 + 4` could be constructed by writing `fromCoefficients [4,3,2,1]`. Any trailing zeros are ignored.

### #coefficientsSource

``coefficients :: forall a. Polynomial a -> Array a``

Inverse of `fromCoefficients`, up to trailing zeros.

### #constantSource

``constant :: forall a. Eq a => Semiring a => a -> Polynomial a``

Create a polynomial with a single (given) term and no dependence in x: that is, a constant polynomial; one of degree 0.

### #identitySource

``identity :: forall a. Semiring a => Polynomial a``

The identity polynomial; `P(x) = x`.

### #evaluateSource

``evaluate :: forall a. Semiring a => Polynomial a -> a -> a``

Evaluate a polynomial by supplying a value for x.

### #derivativeSource

``derivative :: Polynomial Number -> Polynomial Number``

Gives the derivative of a polynomial. For example, the derivative of `x^2 + 3x + 2` is `2x + 3`.

``````antiderivative (fromCoefficients [2.0,3.0,1.0])
== fromCoefficients [3.0,2.0]
``````

### #antiderivativeSource

``antiderivative :: Polynomial Number -> Polynomial Number``

Gives the antiderivative of a particular polynomial having a constant term of 0. For example, an antiderivative of `2x + 3` is `x^2 + 3x`.

``````antiderivative (fromCoefficients [3.0,2.0])
== fromCoefficients [0.0,3.0,1.0]
``````

### #innerProductSource

``innerProduct :: Polynomial Number -> Polynomial Number -> Number``

We can consider the set of polynomials with real coefficients as a real vector space. In this case, this function defines an inner product given by the integral of the product of the arguments over the interval [0,1].

### #normSource

``norm :: Polynomial Number -> Number``

The square root of the inner product of a polynomial with itself.

### #projectionSource

``projection :: Polynomial Number -> Polynomial Number -> Polynomial Number``

Considering polynomials as vectors, `projection p q` gives the orthogonal projection of `q` onto `p`. If we let `r = projection p q`, then `r` satisfies the following properties:

• `innerProduct (q - r) p == 0.0` (approximately)
• `innerProduct p r == norm p * norm r`, i.e. `p` and `r` are linearly dependent.

### #prettySource

``pretty :: forall a. Show a => Semiring a => Eq a => Polynomial a -> String``