Coder Social home page Coder Social logo

polynomials.jl's Introduction

Polynomials

Basic arithmetic, integration, differentiation, evaluation, and root finding over dense univariate polynomials.

Build Status

Poly{T<:Number}(a::Vector)

Construct a polynomial from its coefficients, lowest order first.

julia> Poly([1,0,3,4])
Poly(1 + 3x^2 + 4x^3)

An optional variable parameter can be added.

julia> Poly([1,2,3], :s)
Poly(1 + 2s + 3s^2)

poly(r::AbstractVector)

Construct a polynomial from its roots. This is in contrast to the Poly constructor, which constructs a polynomial from its coefficients.

// Represents (x-1)*(x-2)*(x-3)
julia> poly([1,2,3])
Poly(-6 + 11x - 6x^2 + x^3)

+, -, *, /, ==

The usual arithmetic operators are overloaded to work on polynomials, and combinations of polynomials and scalars.

julia> p = Poly([1,2])
Poly(1 + 2x)

julia> q = Poly([1, 0, -1])
Poly(1 - x^2)

julia> 2p
Poly(2 + 4x)

julia> 2+p
Poly(3 + 2x)

julia> p - q
Poly(2x + x^2)

julia> p*q
Poly(1 + 2x - x^2 - 2x^3)

julia> q/2
Poly(0.5 - 0.5x^2)

Note that operations involving polynomials with different variables will error.

julia> p = Poly([1, 2, 3], :x)
julia> q = Poly([1, 2, 3], :s)
julia> p + q
ERROR: Polynomials must have same variable.

To get the degree of the polynomial use degree method

julia> degree(p)
1

julia> degree(p^2)
2

julia> degree(p-p)
0

polyval(p::Poly, x::Number)

Evaluate the polynomial p at x.

julia> polyval(Poly([1, 0, -1]), 0.1)
0.99

polyint(p::Poly, k::Number=0)

Integrate the polynomial p term by term, optionally adding constant term k. The order of the resulting polynomial is one higher than the order of p.

julia> polyint(Poly([1, 0, -1]))
Poly(x - 0.3333333333333333x^3)

julia> polyint(Poly([1, 0, -1]), 2)
Poly(2.0 + x - 0.3333333333333333x^3)

polyder(p::Poly)

Differentiate the polynomial p term by term. The order of the resulting polynomial is one lower than the order of p.

julia> polyder(Poly([1, 3, -1]))
Poly(3 - 2x)

roots(p::Poly)

Return the roots (zeros) of p, with multiplicity. The number of roots returned is equal to the order of p. The returned roots may be real or complex.

julia> roots(Poly([1, 0, -1]))
2-element Array{Float64,1}:
 -1.0
  1.0

julia> roots(Poly([1, 0, 1]))
2-element Array{Complex{Float64},1}:
 0.0+1.0im
 0.0-1.0im

julia> roots(Poly([0, 0, 1]))
2-element Array{Float64,1}:
 0.0
 0.0

polynomials.jl's People

Contributors

jverzani avatar keno avatar vtjnash avatar pwl avatar jwmerrill avatar yuyichao avatar jcrist avatar pjabardo avatar magistere avatar tkelman avatar mikaelslevinsky avatar daviddelaat avatar eschnett avatar ivanslapnicar avatar jeffreysarnoff avatar mfalt avatar mortenpi avatar neveritt avatar mzaffalon avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.