Implementing the Arithmetic of Polynomials in Rust.
Following up with the story about basic definitions around polynomials, we want to look in this story how one can represent the commutative ring of polynomials in Rust.
Prerequisites to understand this article are some basic knowledge of Rust, as can be obtained from here, and the content of the aforementioned story.
Our task is to implement F[x]
where F
is an arbitrary field. In this story we already regarded a way how one can represent a field F
in Rust. We will just use these idea here but repeat the main construct for convenience:
use std::ops::{Add, Div, Mul, Neg, Sub};
pub trait Field
where
Self: Copy
+ Add<Self, Output = Self>
+ Mul<Self, Output = Self>
+ Neg<Output = Self>
+ Sub<Self, Output = Self>
+ Div<Self, Output = Self>,
{
const ONE: Self;
const ZERO: Self;
fn equals(&self, lhs: Self) -> bool;
}
So, everything is a Field
that implements addition, multiplication, … in the usual associative, distributive and commutative way.