use super::domain::EvaluationDomain;
use super::polynomial::Polynomial;
use core::ops::{Add, AddAssign, DivAssign, Index, Mul, MulAssign, Sub, SubAssign};
use dusk_bls12_381::Scalar;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Evaluations {
pub evals: Vec<Scalar>,
#[doc(hidden)]
domain: EvaluationDomain,
}
impl Evaluations {
pub fn from_vec_and_domain(evals: Vec<Scalar>, domain: EvaluationDomain) -> Self {
Self { evals, domain }
}
pub fn interpolate_by_ref(&self) -> Polynomial {
Polynomial::from_coefficients_vec(self.domain.ifft(&self.evals))
}
pub fn interpolate(self) -> Polynomial {
let Self { mut evals, domain } = self;
domain.ifft_in_place(&mut evals);
Polynomial::from_coefficients_vec(evals)
}
}
impl Index<usize> for Evaluations {
type Output = Scalar;
fn index(&self, index: usize) -> &Scalar {
&self.evals[index]
}
}
impl<'a, 'b> Mul<&'a Evaluations> for &'b Evaluations {
type Output = Evaluations;
#[inline]
fn mul(self, other: &'a Evaluations) -> Evaluations {
let mut result = self.clone();
result *= other;
result
}
}
impl<'a> MulAssign<&'a Evaluations> for Evaluations {
#[inline]
fn mul_assign(&mut self, other: &'a Evaluations) {
assert_eq!(self.domain, other.domain, "domains are unequal");
self.evals
.iter_mut()
.zip(&other.evals)
.for_each(|(a, b)| *a *= b);
}
}
impl<'a, 'b> Add<&'a Evaluations> for &'b Evaluations {
type Output = Evaluations;
#[inline]
fn add(self, other: &'a Evaluations) -> Evaluations {
let mut result = self.clone();
result += other;
result
}
}
impl<'a> AddAssign<&'a Evaluations> for Evaluations {
#[inline]
fn add_assign(&mut self, other: &'a Evaluations) {
assert_eq!(self.domain, other.domain, "domains are unequal");
self.evals
.iter_mut()
.zip(&other.evals)
.for_each(|(a, b)| *a += b);
}
}
impl<'a, 'b> Sub<&'a Evaluations> for &'b Evaluations {
type Output = Evaluations;
#[inline]
fn sub(self, other: &'a Evaluations) -> Evaluations {
let mut result = self.clone();
result -= other;
result
}
}
impl<'a> SubAssign<&'a Evaluations> for Evaluations {
#[inline]
fn sub_assign(&mut self, other: &'a Evaluations) {
assert_eq!(self.domain, other.domain, "domains are unequal");
self.evals
.iter_mut()
.zip(&other.evals)
.for_each(|(a, b)| *a -= b);
}
}
impl<'a> DivAssign<&'a Evaluations> for Evaluations {
#[inline]
fn div_assign(&mut self, other: &'a Evaluations) {
assert_eq!(self.domain, other.domain, "domains are unequal");
self.evals
.iter_mut()
.zip(&other.evals)
.for_each(|(a, b)| *a *= b.invert().unwrap());
}
}