Struct xxcalc::polynomial::Polynomial [] [src]

pub struct Polynomial { /* fields omitted */ }

Polynomial struct represents a mathematical concept of polynomials.

A polynomial is defined by real values of its coefficients corresponding to natural-numbered powers of the variable x. As coefficients are represented as continous vector, storing polynomials of high degree, but with most terms empty is quite expensive (x^1023 makes vector of size 1024). A SmallVec is used, so coefficients up to power of 3 are stored on the stack.

As one can imagine x^3.14 is not a polynomial, while 3.14x^2+2 is a polynomial.

Examples

assert_eq!(String::from(Polynomial::new(&[1.0])), "1");
assert_eq!(String::from(Polynomial::new(&[0.0, 1.0])), "x");
assert_eq!(String::from(Polynomial::new(&[2.0, 0.0, 3.14])), "3.14x^2+2");Run

Methods

impl Polynomial
[src]

Creates new polynomial from given coefficients

Examples

assert_eq!(Polynomial::new(&[1.0, 2.0, 3.0])[0], 1.0);
assert_eq!(Polynomial::new(&[1.0, 2.0, 3.0])[2], 3.0);Run

Creates a linear polynomial (a polynomial of degree one). Such polynomial represents a linear expression ax+b.

Examples

assert_eq!(Polynomial::linear(1.0, 0.0), Polynomial::new(&[1.0, 0.0]));
assert_eq!(Polynomial::linear(0.0, 1.0), Polynomial::new(&[0.0, 1.0]));Run

Creates a constant (degenerative) polynomial (a polynomial of degree zero).

Examples

assert_eq!(Polynomial::constant(0.0), Polynomial::new(&[0.0]));
assert_eq!(Polynomial::constant(3.14), Polynomial::new(&[3.14]));Run

Creates a zero polynomial (a polynomial with constant 0.0). While the math does not define degree of such polynomial, for sake of usability, in this library such polynomial is defined with a degree zero.

Examples

assert_eq!(Polynomial::zero(), Polynomial::constant(0.0));
assert_eq!(Polynomial::zero().degree(), 0);Run

Computes degree of the polynomial. This method has a linear complexity as degree is not stored precomputed.

Degree is a largest power with non-zero coefficient, however a zero polynomial has a zero degree. It should be observed that size of coefficients vector may not be equal to the degree, as some of coefficients may be zero.

Examples

assert_eq!(Polynomial::zero().degree(), 0);
assert_eq!(Polynomial::constant(0.0).degree(), 0);
assert_eq!(Polynomial::linear(1.0, 0.0).degree(), 0);
assert_eq!(Polynomial::linear(1.0, 1.0).degree(), 1);
assert_eq!(Polynomial::new(&[1.0, 1.0, 1.0, 1.0]).degree(), 3);
assert_eq!(Polynomial::new(&[1.0, 1.0, 1.0, 1.0, 0.0, 0.0]).degree(), 3);Run

Computes value of the polynomial by binding the variable with given constant value.

A Horner method is used for this computation to keep it linear and efficient.

Examples

assert_eq!(Polynomial::constant(3.0).bind(9.0), Polynomial::constant(3.0));
assert_eq!(Polynomial::linear(0.0, 1.0).bind(2.0), Polynomial::constant(2.0));
assert_eq!(Polynomial::linear(1.0, 4.0).bind(2.0), Polynomial::constant(9.0));
assert_eq!(Polynomial::new(&[1.0, 4.0, 4.0]).bind(2.0), Polynomial::constant(25.0));Run

Tries to interpret polynomial as real value.

Polynomial can be transformed to real value (double) only if its degree is zero (if the polynomial is constant), otherwise value of polynomial can be binded for particular value of variable.

Errors

Will return PolynomialError::NonConstantError when the degree is greater than zero.

Examples

assert_eq!(Polynomial::constant(3.0).as_f64(), Ok(3.0));
assert_eq!(Polynomial::zero().as_f64(), Ok(0.0));
assert_eq!(Polynomial::linear(1.0, 0.0).as_f64(), Ok(1.0));
assert_eq!(Polynomial::linear(1.0, 1.0).as_f64(), Err(PolynomialError::NonConstantError));Run

Returns human readable representation of the polynomial. Uses provided name as name of the variable.

The returned string is as short as possible version of the polynomial starting with the largest exponents. Terms with empty coefficients are skipped, so as unnecessary symbols and exponents. In example 2x+1 is returned instead of 0*x^2+2*x^2+2*x^0.

Examples

assert_eq!(Polynomial::new(&[0.0, 2.0]).as_string("x"), "2x");
assert_eq!(Polynomial::new(&[-1.0, 2.0]).as_string("x"), "2x-1");
assert_eq!(Polynomial::new(&[-1.0, 2.0, 4.0]).as_string("x"), "4x^2+2x-1");
assert_eq!(Polynomial::new(&[-1.0, 2.0, 0.0, 4.0]).as_string("x"), "4x^3+2x-1");
assert_eq!(Polynomial::new(&[-1.0, 2.0, 0.0, 4.0]).as_string("foo"), "4foo^3+2foo-1");Run

Trait Implementations

impl Debug for Polynomial
[src]

Formats the value using the given formatter.

impl Clone for Polynomial
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Default for Polynomial
[src]

A zero polynomial is returned by default

Examples

assert_eq!(Polynomial::default(), Polynomial::zero());Run

Returns the "default value" for a type. Read more

impl Display for Polynomial
[src]

Implements Display trait with x symbol as a variable.

Examples

Prints expr: 2x+1:

println!("expr: {}", Polynomial::linear(1.0, 2.0));Run

Formats the value using the given formatter.

impl Index<usize> for Polynomial
[src]

Implements index operator for accessing coefficients.

An index may be interpreted as related exponent.

Panics

Panics when index is greater than number of coefficients.

Examples

assert_eq!(Polynomial::new(&[1.0, 2.0, 3.0])[1], 2.0);
assert_eq!(Polynomial::linear(1.0, 2.0)[1], 2.0);Run

The returned type after indexing

The method for the indexing (Foo[Bar]) operation

impl IndexMut<usize> for Polynomial
[src]

Implements mutable index operator for accessing and modifying coefficients.

An index may be interpreted as related exponent. If index is greater than current number of coefficients, new coefficients are added with default value of zero (so these uninitialiazed terms have no effect on value or degree of the polynomial).

Panics

May panic when coefficients cannot be resized (as in lack of memory).

Examples

let mut p = Polynomial::zero();

p[0] = 2.0;
assert_eq!(p[0], 2.0);
p[123] = 4.0;
assert_eq!(p[122], 0.0);
assert_eq!(p[123], 4.0);
assert_eq!(p.degree(), 123);Run

The method for the indexing (Foo[Bar]) operation

impl<'a, 'b> Add<&'b Polynomial> for &'a mut Polynomial
[src]

Performs polynomial addition in-place.

This addition operator requires first operand to be a mutable reference (which is returned back), the other operand is immutable. No other new instance of Polynomial is created when this operation is executed.

Polynomial addition is a linear time operation, it is a simple vector addition, as one needs to add coefficients of appropriate terms.

Examples

let a = &mut Polynomial::constant(1.0);
let b = &Polynomial::linear(1.0, 1.0);

let c = a + b;

assert_eq!(*c, Polynomial::linear(2.0, 1.0));Run

The resulting type after applying the + operator

The method for the + operator

impl AddAssign for Polynomial
[src]

Implementation of assign with addition += operator.

Internally it just uses addition operator on mutable reference.

Examples

let mut a = Polynomial::constant(1.0);
let b = Polynomial::linear(1.0, 1.0);

a += b;

assert_eq!(a, Polynomial::linear(2.0, 1.0));Run

The method for the += operator

impl Add for Polynomial
[src]

Implementation of addition operator for polynomials. A new instance of Polynomial is created.

Internally first operand is cloned, than assign with addition is used and the modified operand is returned as a result.

Examples

let a = Polynomial::constant(1.0);
let b = Polynomial::linear(1.0, 1.0);

assert_eq!(a+b, Polynomial::linear(2.0, 1.0));Run

The resulting type after applying the + operator

The method for the + operator

impl<'a, 'b> Sub<&'b Polynomial> for &'a mut Polynomial
[src]

Performs polynomial subtraction in-place.

This subtraction operator requires first operand to be a mutable reference (which is returned back), the other operand is immutable. No other new instance of Polynomial is created when this operation is executed.

Polynomial subtraction is a linear time operation, it is a simple vector addition, as one needs to subtract coefficients of appropriate terms.

Examples

let a = &mut Polynomial::constant(1.0);
let b = &Polynomial::linear(1.0, 1.0);

let c = a - b;

assert_eq!(*c, Polynomial::linear(0.0, -1.0));Run

The resulting type after applying the - operator

The method for the - operator

impl SubAssign for Polynomial
[src]

Implementation of assign with subtraction -= operator.

Internally it just uses subtraction operator on mutable reference.

Examples

let mut a = Polynomial::constant(1.0);
let b = Polynomial::linear(1.0, 1.0);

a -= b;

assert_eq!(a, Polynomial::linear(0.0, -1.0));Run

The method for the -= operator

impl Sub for Polynomial
[src]

Implementation of subtraction operator for polynomials. A new instance of Polynomial is created.

Internally first operand is cloned, than assign with subtraction is used and the modified operand is returned as a result.

Examples

let a = Polynomial::constant(1.0);
let b = Polynomial::linear(1.0, 1.0);

assert_eq!(a-b, Polynomial::linear(0.0, -1.0));Run

The resulting type after applying the - operator

The method for the - operator

impl<'a, 'b> Mul<&'b Polynomial> for &'a mut Polynomial
[src]

Performs polynomial multiplication without creating new Polynomial.

This multiplication operator requires first operand to be a mutable reference (which is returned back), the other operand is immutable. No other new instance of Polynomial is created when this operation is executed.

Polynomial multiplication is implemented as O(n*m) operation, which results in polynomial of degree n+m. Internally an appropriatly sized temporary vector is used. If both polynomials are degree zero, a simple arithmetic multiplication is used.

Examples

let a = &mut Polynomial::new(&[0.0, 2.0, 1.0]);
let b = &Polynomial::linear(1.0, 2.0);

let c = a * b;

assert_eq!(*c, Polynomial::new(&[0.0, 2.0, 5.0, 2.0]));Run

The resulting type after applying the * operator

The method for the * operator

impl MulAssign for Polynomial
[src]

Implementation of assign with multiplication *= operator.

Internally it just uses multiplication operator on mutable reference.

Examples

let mut a = Polynomial::new(&[0.0, 2.0, 1.0]);
let b = Polynomial::linear(1.0, 2.0);

a *= b;

assert_eq!(a, Polynomial::new(&[0.0, 2.0, 5.0, 2.0]));Run

The method for the *= operator

impl<'a, 'b> MulAssign<&'b Polynomial> for &'a mut Polynomial
[src]

Implementation of assign with multiplication *= operator for references.

Internally it just uses multiplication operator on mutable reference (it is just a syntactic sugar around it).

Examples

let mut a = &mut Polynomial::new(&[0.0, 2.0, 1.0]);
let b = &Polynomial::linear(1.0, 2.0);

a *= b;

assert_eq!(*a, Polynomial::new(&[0.0, 2.0, 5.0, 2.0]));Run

The method for the *= operator

impl Mul for Polynomial
[src]

Implementation of multiplication operator for polynomials. A new instance of Polynomial is created.

Internally first operand is cloned, than assign with multiplication is used and the modified operand is returned as a result.

Examples

let a = Polynomial::new(&[0.0, 2.0, 1.0]);
let b = Polynomial::linear(1.0, 2.0);

assert_eq!(a*b, Polynomial::new(&[0.0, 2.0, 5.0, 2.0]));Run

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<f64> for Polynomial
[src]

Specialization of assign with multiplication *= operator for polynomial and double number.

Internally it just multiplies each of coefficient by given double number.

Examples

let mut a = Polynomial::new(&[0.0, 2.0, 1.0]);

a *= 2.0f64;

assert_eq!(a, Polynomial::new(&[0.0, 4.0, 2.0]));Run

The method for the *= operator

impl<'a, 'b> Div<&'b Polynomial> for &'a mut Polynomial
[src]

Performs polynomial division without creating new Polynomial.

This division operator requires first operand to be a mutable reference (which is returned back), the other operand is immutable. No other new instance of Polynomial is created when this operation is executed.

Polynomial division is implemented using long division algorithm, Internally two temporary polynomials are used. However, if divisior is a constant, a simple arithmetic division over each coefficient of divident is used.

Errors

This division operator does not panic on division by zero, as such operation is defined on double numbers. However in special cases some errors may be returned.

However division by zero of non-constant polynomial will result in DivisionByZero error, as such operation is not defined.

let a = &mut Polynomial::new(&[0.0, 2.0, 1.0]);
let b = &Polynomial::zero();

let c = a / b;

assert_eq!(c.unwrap_err(), PolynomialError::DivisionByZero);Run

A DividentDegreeMismatch error is returned when divident degree is smaller than divisor degree, as performing such operation would result in nonpolynomial result.

let a = &mut Polynomial::new(&[0.0, 2.0, 1.0]);
let b = &Polynomial::new(&[1.0, 2.0, 2.0, 2.0]);

let c = a / b;

assert_eq!(c.unwrap_err(), PolynomialError::DividentDegreeMismatch);Run

Examples

let a = &mut Polynomial::new(&[0.0, 2.0, 1.0]);
let b = &Polynomial::linear(1.0, 2.0);

let c = a / b;

assert_eq!(c, Ok(&Polynomial::new(&[0.75, 0.5])));Run

The resulting type after applying the / operator

The method for the / operator

impl DivAssign for Polynomial
[src]

Implementation of assign with division /= operator.

Internally it just uses division operator on mutable reference.

Panics

It will panic when divident degree is smaller than divisor degree, or if divident is a non constant polynomial and divisor is zero. Internally result of division in unwrap, which may cause these errors.

Examples

let mut a = Polynomial::new(&[0.0, 2.0, 1.0]);
let b = Polynomial::linear(1.0, 2.0);

a /= b;

assert_eq!(a, Polynomial::new(&[0.75, 0.5]));Run

The method for the /= operator

impl Div for Polynomial
[src]

Implementation of division operator for polynomials. A new instance of Polynomial is created.

Internally first operand is cloned, than assign with division is used and the modified operand is returned as a result.

Panics

It will panic when divident degree is smaller than divisor degree, or if divident is a non constant polynomial and divisor is zero. Internally result of division in unwrap, which may cause these errors.

Examples

let a = Polynomial::new(&[0.0, 2.0, 1.0]);
let b = Polynomial::linear(1.0, 2.0);

assert_eq!(a/b, Polynomial::new(&[0.75, 0.5]));Run

The resulting type after applying the / operator

The method for the / operator

impl PartialEq for Polynomial
[src]

Checks if two polynomials are equal.

Polynomials must have the same degree and the same coefficients to be considered equal.

Examples

assert!(Polynomial::new(&[1.0, 2.0]) == Polynomial::new(&[1.0, 2.0]));
assert!(Polynomial::new(&[1.0, 2.0]) == Polynomial::new(&[1.0, 2.0, 0.0, 0.0]));Run

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for Polynomial
[src]