Skip to content

Commit e6374ba

Browse files
authored
Mul mle by scalar (#845)
* poly * scalar works * updated changelog * clean up
1 parent 5a781ae commit e6374ba

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [\#772](https://github.com/arkworks-rs/algebra/pull/772) (`ark-ff`) Implementation of `mul` method for `BigInteger`.
66
- [\#794](https://github.com/arkworks-rs/algebra/pull/794) (`ark-ff`) Fix `wasm` compilation.
77
- [\#837](https://github.com/arkworks-rs/algebra/pull/837) (`ark-serialize`) Fix array deserialization panic.
8+
- [\#845](https://github.com/arkworks-rs/algebra/pull/845) (`Algebra`) Implementation of `mul` method for `DenseMultilinearExtension<F> * F`.
89

910
### Breaking changes
1011

poly/src/evaluations/multivariate/multilinear/dense.rs

+53-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ark_std::{
1111
fmt::Formatter,
1212
iter::IntoIterator,
1313
log2,
14-
ops::{Add, AddAssign, Index, Neg, Sub, SubAssign},
14+
ops::{Add, AddAssign, Index, Mul, MulAssign, Neg, Sub, SubAssign},
1515
rand::Rng,
1616
slice::{Iter, IterMut},
1717
vec::*,
@@ -331,6 +331,44 @@ impl<'a, F: Field> SubAssign<&'a DenseMultilinearExtension<F>> for DenseMultilin
331331
}
332332
}
333333

334+
impl<F: Field> Mul<F> for DenseMultilinearExtension<F> {
335+
type Output = DenseMultilinearExtension<F>;
336+
337+
fn mul(self, scalar: F) -> Self::Output {
338+
&self * &scalar
339+
}
340+
}
341+
342+
impl<'a, 'b, F: Field> Mul<&'a F> for &'b DenseMultilinearExtension<F> {
343+
type Output = DenseMultilinearExtension<F>;
344+
345+
fn mul(self, scalar: &'a F) -> Self::Output {
346+
if scalar.is_zero() {
347+
return DenseMultilinearExtension::zero();
348+
} else if scalar.is_one() {
349+
return self.clone();
350+
}
351+
let result: Vec<F> = self.evaluations.iter().map(|&x| x * scalar).collect();
352+
353+
DenseMultilinearExtension {
354+
num_vars: self.num_vars,
355+
evaluations: result,
356+
}
357+
}
358+
}
359+
360+
impl<F: Field> MulAssign<F> for DenseMultilinearExtension<F> {
361+
fn mul_assign(&mut self, scalar: F) {
362+
*self = &*self * &scalar
363+
}
364+
}
365+
366+
impl<'a, F: Field> MulAssign<&'a F> for DenseMultilinearExtension<F> {
367+
fn mul_assign(&mut self, scalar: &'a F) {
368+
*self = &*self * scalar
369+
}
370+
}
371+
334372
impl<F: Field> fmt::Debug for DenseMultilinearExtension<F> {
335373
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
336374
write!(f, "DenseML(nv = {}, evaluations = [", self.num_vars)?;
@@ -394,7 +432,7 @@ impl<F: Field> Polynomial<F> for DenseMultilinearExtension<F> {
394432
#[cfg(test)]
395433
mod tests {
396434
use crate::{DenseMultilinearExtension, MultilinearExtension, Polynomial};
397-
use ark_ff::{Field, Zero};
435+
use ark_ff::{Field, One, Zero};
398436
use ark_std::{ops::Neg, test_rng, vec::*, UniformRand};
399437
use ark_test_curves::bls12_381::Fr;
400438

@@ -471,6 +509,7 @@ mod tests {
471509
const NV: usize = 10;
472510
let mut rng = test_rng();
473511
for _ in 0..20 {
512+
let scalar = Fr::rand(&mut rng);
474513
let point: Vec<_> = (0..NV).map(|_| Fr::rand(&mut rng)).collect();
475514
let poly1 = DenseMultilinearExtension::rand(NV, &mut rng);
476515
let poly2 = DenseMultilinearExtension::rand(NV, &mut rng);
@@ -482,6 +521,8 @@ mod tests {
482521
assert_eq!((&poly1 - &poly2).evaluate(&point), v1 - v2);
483522
// test negate
484523
assert_eq!(poly1.clone().neg().evaluate(&point), -v1);
524+
// test mul poly by scalar
525+
assert_eq!((&poly1 * &scalar).evaluate(&point), v1 * scalar);
485526
// test add assign
486527
{
487528
let mut poly1 = poly1.clone();
@@ -515,6 +556,16 @@ mod tests {
515556
assert_eq!(zero.evaluate(&point), scalar * v1);
516557
}
517558
}
559+
// test mul_assign for poly * scalar
560+
{
561+
let mut poly1_cloned = poly1.clone();
562+
poly1_cloned *= Fr::one();
563+
assert_eq!(poly1_cloned.evaluate(&point), v1);
564+
poly1_cloned *= scalar;
565+
assert_eq!(poly1_cloned.evaluate(&point), v1 * scalar);
566+
poly1_cloned *= Fr::zero();
567+
assert_eq!(poly1_cloned, DenseMultilinearExtension::zero());
568+
}
518569
}
519570
}
520571

0 commit comments

Comments
 (0)