Code Monkey home page Code Monkey logo

rustnomial's Introduction

rustnomial

This crate provides utilities for operating on polynomials, including:

  • Parsing polynomials from strings, and creating strings from polynomials
  • Math operations with polynomials
  • Integration and derivation of polynomials
  • Finding polynomial roots

Examples

  • Parsing polynomials from / to strings:
use std::str::FromStr;

use rustnomial::{GenericPolynomial, Polynomial};

fn main() {
    let poly = Polynomial::<i32>::from_str("1+x^2-3+11x").unwrap();
    // x^2 + 11x - 2
    println!("{}", poly);
}
  • Integration
use rustnomial::integral;

fn main() {
    let poly = integral!(5., 2., 0.);
    // 1.6666666666666667x^3 + x^2 + C
    println!("{}", poly);
    // 2.666666666666667
    println!("{}", poly.eval(0., 1.));
}
  • Derivation
use rustnomial::derivative;

fn main() {
    let poly = derivative!(5., 2., 0.);
    // 10x + 2
    println!("{}", poly);
}
  • Rootfinding
use rustnomial::{GenericPolynomial, Polynomial};

fn main() {
    let poly = Polynomial::<f64>::new(vec![1., 2.]).pow(9) * Polynomial::new(vec![1., 3.]);
    // ManyRealRoots([-3.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0])
    println!("{:?}", poly.roots());
}

rustnomial's People

Contributors

dependabot[bot] avatar philippeitis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

xsro tumtum23

rustnomial's Issues

Problem calculating fourth-degree poly roots

Unfortunately the library fails to evaluate roots of fourth-degree equations. Take this code for example:

use rustnomial::{Polynomial};

fn main() {
    let poly = Polynomial::<f64>::new(vec![1.0, 1.0, 1.0, 1.0, 1.0]);
    let roots = poly.roots();
    match roots {
        rustnomial::Roots::OnlyRealRoots(_) => {
            println!("roots() returned `Only Real Roots`");
        },
        _ => {
            println!("roots() returned some other type.");
        }
    }
}

Running this with rustnomial = "0.2.0" prints:

roots() returned `Only Real Roots`

while in fact x^4 + x^3 + x^2 + x + 1 has no real roots. The vector V in OnlyRealRoots(V) is empty too.

Some other times, the library returns OnlyRealRoots(V) with V containing only real roots, skipping complex ones.

Maybe I'm missing something here, but my understanding was that the library should have returned ManyComplexRoots in these cases.

BTW, thanks for the nice package. I'm using it in an educational project.

Wrong solution to cubic equation

Hi again

The solution find_roots returns for x^3 + 36 * x^2 + 144 * x + 64 = 0 is wrong. If you run this code on rustnomial 0.3.2 (or older versions):

fn main() {
    let poly = Polynomial::<f64>::new(vec![1.0, 36.0, 144.0, 64.0]);
    let roots = poly.roots();
    match roots {
        rustnomial::Roots::ManyRealRoots(v) => {
            println!("roots() returned `Many Real Roots` with values:");
            for v_ in v {
                println!("{}", v_);
            }
        },
        _ => {
            println!("roots() returned some other type.");
        }
    }
}

it returns:

roots() returned `Many Real Roots` with values:
-31.053752658339498
1.4912025299402378
-6.43744987160074

but in reality the equation has different roots:

-4
-31.492
-0.50807

(See here for exact forms)

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.