MyNixOS website logo
Description

Polynomials.

Polynomials backed by Vectors.

poly Hackage Stackage LTS Stackage Nightly Coverage Status

Haskell library for univariate and multivariate polynomials, backed by Vectors.

> -- Univariate polynomials
> (X + 1) + (X - 1) :: VPoly Integer
2 * X
> (X + 1) * (X - 1) :: UPoly Int
1 * X^2 + (-1)

> -- Multivariate polynomials
> (X + Y) * (X - Y) :: VMultiPoly 2 Integer
1 * X^2 + (-1) * Y^2
> (X + Y + Z) ^ 2 :: UMultiPoly 3 Int
1 * X^2 + 2 * X * Y + 2 * X * Z + 1 * Y^2 + 2 * Y * Z + 1 * Z^2

> -- Laurent polynomials
> (X^-2 + 1) * (X - X^-1) :: VLaurent Integer
1 * X + (-1) * X^-3
> (X^-1 + Y) * (X + Y^-1) :: UMultiLaurent 2 Int
1 * X * Y + 2 + 1 * X^-1 * Y^-1

Vectors

Poly v a is polymorphic over a container v, implementing the Vector interface, and coefficients of type a. Usually v is either a boxed vector from Data.Vector or an unboxed vector from Data.Vector.Unboxed. Use unboxed vectors whenever possible, e. g., when the coefficients are Ints or Doubles.

There are handy type synonyms:

type VPoly a = Poly Data.Vector.Vector         a
type UPoly a = Poly Data.Vector.Unboxed.Vector a

Construction

The simplest way to construct a polynomial is using the pattern X:

> X^2 - 3 * X + 2 :: UPoly Int
1 * X^2 + (-3) * X + 2

(Unfortunately, types are often ambiguous and must be given explicitly.)

While being convenient to read and write in REPL, X is relatively slow. The fastest approach is to use toPoly, providing it with a vector of coefficients (constant term first):

> toPoly (Data.Vector.Unboxed.fromList [2, -3, 1 :: Int])
1 * X^2 + (-3) * X + 2

Alternatively one can enable {-# LANGUAGE OverloadedLists #-} and simply write

> [2, -3, 1] :: UPoly Int
1 * X^2 + (-3) * X + 2

There is a shortcut to construct a monomial:

> monomial 2 3.5 :: UPoly Double
3.5 * X^2 + 0.0 * X + 0.0

Operations

Most operations are provided by means of instances, like Eq and Num. For example,

> (X^2 + 1) * (X^2 - 1) :: UPoly Int
1 * X^4 + 0 * X^3 + 0 * X^2 + 0 * X + (-1)

One can also find it convenient to scale by a monomial (cf. monomial above):

> scale 2 3.5 (X^2 + 1) :: UPoly Double
3.5 * X^4 + 0.0 * X^3 + 3.5 * X^2 + 0.0 * X + 0.0

While Poly cannot be made an instance of Integral (because there is no meaningful toInteger), it is an instance of GcdDomain and Euclidean from the semirings package. These type classes cover the main functionality of Integral, providing division with remainder and gcd / lcm:

> Data.Euclidean.gcd (X^2 + 7 * X + 6) (X^2 - 5 * X - 6) :: UPoly Int
1 * X + 1

> Data.Euclidean.quotRem (X^3 + 2) (X^2 - 1 :: UPoly Double)
(1.0 * X + 0.0,1.0 * X + 2.0)

Miscellaneous utilities include eval for evaluation at a given point, and deriv / integral for taking the derivative and an indefinite integral, respectively:

> eval (X^2 + 1 :: UPoly Int) 3
10

> deriv (X^3 + 3 * X) :: UPoly Double
3.0 * X^2 + 0.0 * X + 3.0

> integral (3 * X^2 + 3) :: UPoly Double
1.0 * X^3 + 0.0 * X^2 + 3.0 * X + 0.0

Deconstruction

Use unPoly to deconstruct a polynomial to a vector of coefficients (constant term first):

> unPoly (X^2 - 3 * X + 2 :: UPoly Int)
[2,-3,1]

Further, leading is a shortcut to obtain the leading term of a non-zero polynomial, expressed as a power and a coefficient:

> leading (X^2 - 3 * X + 2 :: UPoly Double)
Just (2,1.0)

Flavours

  • Data.Poly provides dense univariate polynomials with a Num-based interface. This is a default choice for most users.

  • Data.Poly.Semiring provides dense univariate polynomials with a Semiring-based interface.

  • Data.Poly.Laurent provides dense univariate Laurent polynomials with a Semiring-based interface.

  • Data.Poly.Sparse provides sparse univariate polynomials with a Num-based interface. Besides that, you may find it easier to use in the REPL because of a more readable Show instance, skipping zero coefficients.

  • Data.Poly.Sparse.Semiring provides sparse univariate polynomials with a Semiring-based interface.

  • Data.Poly.Sparse.Laurent provides sparse univariate Laurent polynomials with a Semiring-based interface.

  • Data.Poly.Multi provides sparse multivariate polynomials with a Num-based interface.

  • Data.Poly.Multi.Semiring provides sparse multivariate polynomials with a Semiring-based interface.

  • Data.Poly.Multi.Laurent provides sparse multivariate Laurent polynomials with a Semiring-based interface.

All flavours are available backed by boxed or unboxed vectors.

Performance

As a rough guide, poly is at least 20x-40x faster than the polynomial library. Multiplication is implemented via the Karatsuba algorithm. Here are a couple of benchmarks for UPoly Int:

Benchmarkpolynomial, μspoly, μsspeedup
addition, 100 coeffs.45222x
addition, 1000 coeffs.4411725x
addition, 10000 coeffs.654516739x
multiplication, 100 coeffs.17333352x
multiplication, 1000 coeffs.4420001456303x

Due to being polymorphic by multiple axis, the performance of poly crucially depends on specialisation of instances. Clients are strongly recommended to compile with ghc-options: -fspecialise-aggressively and suggested to enable -O2.

Additional resources

  • Polynomials in Haskell, MuniHac, 12.09.2020: slides, video.
Metadata

Version

0.5.1.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows