Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] fix: TDim simplify(), multiplication distribution: a*(b+c) to a*b+a*c #1391

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion data/src/dim/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,20 @@ impl TDim {
(_, 0) => Val(coef_prod), // Case #1: If 0 variables, return product
(0, _) => Val(0), // Case #2: Result is 0 if coef is 0
(1, 1) => vars.remove(0), // Case #3: Product is 1, so return the only term
(1, _) => Mul(vars), // Case #4: Product is 1, so return the non-integer terms
(1, _) => {
if let Some(position) = vars.iter().position(|a| matches!(a, Add(_))) {
let add = vars.remove(position);
let Add(add) = add else { unreachable!() };
vars.iter()
.cartesian_product(add)
.map(|(a, b)| a.clone() * b)
.sum::<TDim>()
.simplify()
} else {
Mul(vars.into_iter().sorted_by(tdim_compare).collect())
// Case #4: Product is 1, so return the non-integer terms
}
}
(_, 1) => MulInt(coef_prod, Box::new(vars.remove(0))), // Case #5: Single variable, convert to 1 MulInt
_ => MulInt(coef_prod, Box::new(Mul(vars))), // Case #6: Multiple variables, convert to MulInt
}
Expand Down Expand Up @@ -723,6 +736,8 @@ impl<I: AsPrimitive<u64> + PrimInt> ops::Rem<I> for TDim {

#[cfg(test)]
mod tests {
use crate::prelude::ToDim;

use super::*;

macro_rules! b( ($e:expr) => { Box::new($e) } );
Expand Down Expand Up @@ -927,6 +942,32 @@ mod tests {
assert_eq!(e, TDim::from(0));
}

#[test]
fn reduce_distribute_simple() {
let a = S.0.sym("a").to_dim();
let c = S.0.sym("c").to_dim();
let d = S.0.sym("d").to_dim();

let e: TDim = a.clone() * (c.clone() + d.clone());
let f: TDim = a.clone() * c.clone() + a.clone() * d.clone();
assert_eq!(e, f);
}

#[test]
fn reduce_distribute() {
let a = S.0.sym("a").to_dim();
let b = S.0.sym("b").to_dim();
let c = S.0.sym("c").to_dim();
let d = S.0.sym("d").to_dim();

let e: TDim = (a.clone() + b.clone()) * (c.clone() + d.clone());
let f: TDim = (a.clone() * c.clone())
+ a.clone() * d.clone()
+ b.clone() * c.clone()
+ b.clone() * d.clone();
assert_eq!(e, f);
}

#[test]
fn conv2d_ex_1() {
let e = (TDim::from(1) - 1 + 1).div_ceil(1);
Expand Down
Loading