How do I easily remove real or imaginary components throughout the expression that are absolutely below a certain threshold? I would think this would be commonly needed.
I have been unable to find a successful example for doing this with SymbolicUtils rules. Is there an easy way to walk the entire expression tree looking for small constants, even for complex?
Open an issue. I can’t recall a function that’ll do this automatically, though it’s not too hard to come up with an expression walk that does it.
For the example expression you gave, the suggestion from @pitsianis seems to work fine – are there example expressions where it doesn’t do the right thing?
It seems to me that complex numbers are always represented as realpart + im * imaginarypart – and also the symbolic expressions get sorted automatically in such a way that the Prewalk already only consumes real numbers, not complex ones.
But probably there a cases I'm not thinking about right now…
using Metatheory
tol_scale = 10.0
zerosones = @theory x begin
x::(xs -> xs isa AbstractFloat && abs(xs) < tol_scale * eps(typeof(xs))) --> 0
x::(xs -> xs isa AbstractFloat && abs(xs - 1) < tol_scale * eps(typeof(xs))) --> sign(x)*1
0 * x --> 0
x * 0 --> 0
0 + x --> x
x + 0 --> x
x - 0 --> x
0 - x --> -x
1 * x --> x
x * 1 --> x
x / 1 --> x
-1 * x --> -x
end
rewrite(:( (1e-16*x[1] + (-1.00000000000000001 -1e-16im)*x[2])^2 ), zerosones)