Added is_poly_power

pull/2/head
Mark 2023-07-31 20:43:50 -07:00
parent e768df08a7
commit 4ea39a7f60
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 40 additions and 1 deletions

View File

@ -46,6 +46,41 @@ impl Expression {
} }
} }
// True if this is a power operator applied to a constant or variable
// and an integer.
// Examples: pi^2, x ^ 3
pub fn is_poly_power(&self) -> bool {
match self {
Expression::Operator(_, Operator::Power, a) => {
// Assuming len(a) = 2, which should be true in this case
assert!(a.len() == 2);
let base = &a[0];
let power = &a[1];
// Make sure base ks const or variable
match base {
Expression::Constant(_, _)
| Expression::Variable(_, _)
=> {},
_ => { return false; }
};
// Make sure power is an integer
match power {
Expression::Quantity(_, q) => {
return q.unitless() && q.fract().is_zero();
},
_ => { return false; }
}
},
_ => { return false; }
};
}
#[inline(always)] #[inline(always)]
pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Expression>> { pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Expression>> {
match self { match self {

View File

@ -272,7 +272,11 @@ impl Operator {
p.unitless() && !q.unitless() p.unitless() && !q.unitless()
} else if let Expression::Constant(_, _) = b { } else if let Expression::Constant(_, _) = b {
true true
} else {false} } else if let Expression::Variable(_, _) = b {
true
} else {
b.is_poly_power()
}
} else {false} } else {false}
}; };