Skip to content

Commit

Permalink
refactor: cleaned up traits by using a supertrait
Browse files Browse the repository at this point in the history
  • Loading branch information
ariostas committed May 18, 2024
1 parent d5c7943 commit 94e1b1d
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 250 deletions.
59 changes: 6 additions & 53 deletions src/fundamental_period.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
pub mod error;

use crate::factorial::{factorial_prod, harmonic, RecipMut};
use crate::polynomial::Polynomial;
use crate::factorial::{factorial_prod, harmonic};
use crate::polynomial::{Polynomial, coefficient::PolynomialCoeff};
use crate::pool::NumberPool;
use crate::semigroup::Semigroup;
use crate::PolynomialProperties;
use core::marker::{Send, Sync};
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
use core::slice::Iter;
use error::FundamentalPeriodError;
use nalgebra::{DMatrix, DMatrixView, DVector};
use rug::Assign;
use std::collections::{HashMap, HashSet};
use std::sync::mpsc::{channel, Sender};
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -52,16 +49,7 @@ fn compute_c_0neg<T>(
curves_dot_q0: DMatrixView<i32>,
beta_pairs: &[(usize, usize)],
) where
for<'a> T: Clone
+ RecipMut
+ Assign<u64>
+ MulAssign<i32>
+ MulAssign<u64>
+ DivAssign<u64>
+ Assign<&'a T>
+ AddAssign<&'a T>
+ SubAssign<&'a T>
+ MulAssign<&'a T>,
T: PolynomialCoeff<T>,
{
let mut a: Vec<_> = (0..q.ncols()).map(|_| template_var.clone()).collect();
let mut tmp_num0 = template_var.clone();
Expand Down Expand Up @@ -151,16 +139,7 @@ fn compute_c_1neg<T>(
curves_dot_q0: DMatrixView<i32>,
beta_pairs: &[(usize, usize)],
) where
for<'a> T: Clone
+ RecipMut
+ Assign<u64>
+ MulAssign<i32>
+ MulAssign<u64>
+ DivAssign<u64>
+ Assign<&'a T>
+ AddAssign<&'a T>
+ SubAssign<&'a T>
+ MulAssign<&'a T>,
T: PolynomialCoeff<T>,
{
let mut a: Vec<_> = (0..q.ncols()).map(|_| template_var.clone()).collect();
let mut tmp_fact = template_var.clone();
Expand Down Expand Up @@ -262,16 +241,7 @@ fn compute_c_2neg<T>(
curves_dot_q0: DMatrixView<i32>,
beta_pairs: &[(usize, usize)],
) where
for<'a> T: Clone
+ RecipMut
+ Assign<u64>
+ MulAssign<i32>
+ MulAssign<u64>
+ DivAssign<u64>
+ Assign<&'a T>
+ AddAssign<&'a T>
+ SubAssign<&'a T>
+ MulAssign<&'a T>,
T: PolynomialCoeff<T>,
{
let mut tmp_fact = template_var.clone();
let mut tmp_final = template_var.clone();
Expand Down Expand Up @@ -349,24 +319,7 @@ pub fn compute_omega<T>(
intnum_idxpairs: &HashSet<(usize, usize)>,
) -> Result<FundamentalPeriod<T>, FundamentalPeriodError>
where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<u64>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ DivAssign<u64>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ MulAssign<u64>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>
+ Send
+ Sync
+ RecipMut,
T: PolynomialCoeff<T>,
{
let curves = &sg.elements;
let h11 = q.ncols();
Expand Down
94 changes: 7 additions & 87 deletions src/instanton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
//! This module contains functions to compute instanton correction.
use crate::fundamental_period::FundamentalPeriod;
use crate::polynomial::{error::PolynomialError, properties::PolynomialProperties, Polynomial};
use crate::polynomial::{error::PolynomialError, properties::PolynomialProperties, Polynomial, coefficient::PolynomialCoeff};
use crate::NumberPool;
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
use rug::Assign;
use std::collections::{HashMap, HashSet};
use std::sync::mpsc::{channel, Sender};
use std::sync::{Arc, Mutex};
Expand All @@ -24,20 +22,7 @@ fn compute_alpha_thread<T>(
poly_props: &PolynomialProperties<T>,
np: &mut NumberPool<T>,
) where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>
+ Send
+ Sync,
T: PolynomialCoeff<T>,
{
loop {
let t;
Expand All @@ -60,20 +45,7 @@ fn compute_beta_thread<T>(
poly_props: &PolynomialProperties<T>,
np: &mut NumberPool<T>,
) where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>
+ Send
+ Sync,
T: PolynomialCoeff<T>,
{
loop {
let (t0, t1);
Expand All @@ -98,20 +70,7 @@ fn compute_f_thread<T>(
poly_props: &PolynomialProperties<T>,
np: &mut NumberPool<T>,
) where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>
+ Send
+ Sync,
T: PolynomialCoeff<T>,
{
loop {
let (t0, t1);
Expand Down Expand Up @@ -139,20 +98,7 @@ fn compute_inst_thread<T>(
intnum_dict: &HashMap<(usize, usize, usize), i32>,
is_threefold: bool,
) where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>
+ Send
+ Sync,
T: PolynomialCoeff<T>,
{
let h11 = poly_props.semigroup.elements.nrows();
let mut intnum_ind = [0_usize; 3];
Expand Down Expand Up @@ -207,20 +153,7 @@ fn compute_expalpha_thread<T>(
poly_props: &PolynomialProperties<T>,
np: &mut NumberPool<T>,
) where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>
+ Send
+ Sync,
T: PolynomialCoeff<T>,
{
loop {
let t;
Expand Down Expand Up @@ -253,20 +186,7 @@ pub fn compute_instanton_data<T>(
is_threefold: bool,
) -> Result<InstantonData<T>, PolynomialError>
where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>
+ Send
+ Sync,
T: PolynomialCoeff<T>,
{
let h11 = poly_props.semigroup.elements.nrows();
let n_threads = thread::available_parallelism()
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ pub use polynomial::{
};
pub use pool::NumberPool;
pub use semigroup::Semigroup;

// Re-export main trait
pub use polynomial::coefficient::PolynomialCoeff;
33 changes: 6 additions & 27 deletions src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
//! This module provides tools to work with polynomials containing a fixed set
//! of monomials.
pub mod coefficient;
pub mod error;
pub mod prettyprint;
pub mod properties;

use crate::pool::NumberPool;
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
use core::ops::{DivAssign, MulAssign};
use error::PolynomialError;
use coefficient::PolynomialCoeff;
use nalgebra::DVector;
use properties::PolynomialProperties;
use rug::Assign;
use std::collections::HashMap;

/// A polynomial structure.
Expand All @@ -29,18 +30,7 @@ pub struct Polynomial<T> {

impl<T> Default for Polynomial<T>
where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>,
T: PolynomialCoeff<T>,
{
fn default() -> Self {
Self::new()
Expand All @@ -49,18 +39,7 @@ where

impl<T> Polynomial<T>
where
for<'a> T: Clone
+ AddAssign<&'a T>
+ Assign<i32>
+ Assign<&'a T>
+ DivAssign<&'a T>
+ DivAssign<u32>
+ MulAssign<&'a T>
+ MulAssign<i32>
+ MulAssign<u32>
+ PartialEq<i32>
+ PartialOrd<T>
+ SubAssign<&'a T>,
T: PolynomialCoeff<T>,
{
/// Create a new polynomial.
pub fn new() -> Self {
Expand Down Expand Up @@ -508,7 +487,7 @@ mod tests {
use super::*;
use crate::semigroup;
use nalgebra::{DMatrix, RowDVector};
use rug::Rational;
use rug::{Assign, Rational};
use semigroup::Semigroup;

// Construct simple data for testing
Expand Down
Loading

0 comments on commit 94e1b1d

Please sign in to comment.