Skip to content

Create Polynomial struct #20

@dawnandrew100

Description

@dawnandrew100

In the wise words of people far smarter than me (thank you kind people of the rustfully discord), a polynomial struct should be added to this repository.

An example implementation (all f64 might have to be replaced with generic variable to allow for floats and ints)

use crate::polynomials::{ascii_letters, Term, core::PolynomialError};
use crate::{eval_polynomial, parse_polynomial, parse_multivar, eval_multivar};
use std::str::FromStr;

struct Polynomial {
    coefficents: Vec<f64>,
}

pub struct MultivariatePolynomial {
    terms: Vec<Term>,
}

// inherient methods
impl Polynomial {
    fn from(&self, coefficents: &str) -> Result<Self, PolynomialError> {
        let parsed = parse_polynomial(coefficents)?;
        Ok(Self {
            coefficents: parsed,
        })
    }
    fn solve(&self, point: f64) -> f64 {
        eval_polynomial(point, &self.coefficents)
    }
}

impl MultivariatePolynomial {
    fn from(&self, coefficents: &str) -> Result<Self, PolynomialError> {
        // change parse_multivar output from option to Result
        let parsed = parse_multivar(coefficents)?; <- parameters need to be adjusted 
        Ok(Self {
            terms: parsed,
        })
    }
    fn solve(&self, point: f64) -> f64 {
        // Need to create eval_multivar function
        eval_multivar(point, &self.terms)
    }
}

// trait implementation
impl FromStr for Polynomial {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        todo!("parse string into self or error")
    }
}

impl FromStr for MultivariatePolynomial {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        todo!("parse string into self or error")
    }
}

After creating the struct, it can be added as an output of parse_polynomial and parse_multivar and this struct should be the input of the bisection and nrm functions (replacing the string inputs that are parsed within the functions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions