# Questions about Symbolics: remove small constant terms

**URL:** <https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595>\
**Category:** New to Julia\
**Created:** [August 14, 2025, 3:09am UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595 "2025-08-14T03:09:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![agascoig](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@agascoig](https://discourse.julialang.org/u/agascoig)\
**Post date:** [August 14, 2025, 3:09am UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/1 "2025-08-14T03:09:19Z")

</div>

I have the following symbolic expression with small terms in Julia:

(2.23606797749979 - 2.220446049250313e-16im)\*(x[1] + x[4]) + (-2.23606797749979 + 2.220446049250313e-16im)\*x[2] + (-2.23606797749979 + 2.220446049250313e-16im)\*x[3]

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?

Thanks for any help.

Alex G

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 14, 2025, 4:40am UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/2 "2025-08-14T04:40:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [August 14, 2025, 8:04am UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/3 "2025-08-14T08:04:57Z")

</div>

A similar question came up here [Set as zero low values in symbolic expressions](https://discourse.julialang.org/t/set-as-zero-low-values-in-symbolic-expressions/114793)

Maybe the same solution works for you?

---

<div class="post-metadata">

**Author:** ![pitsianis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitsianis/32/26588_2.png) [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Post date:** [August 14, 2025, 5:46pm UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/4 "2025-08-14T17:46:37Z")

</div>

The recommendation by @Sevi works. I have only modified it to make more explicit the tolerance

```julia
using Symbolics
using Symbolics.SymbolicUtils.Rewriters

tol_scale = 10.0 # tolerance scale 
rule_too_small = @rule ~x::(xs -> xs isa AbstractFloat && abs(xs) <= tol_scale * eps(typeof(xs)))) => 0
eliminatezeros = Prewalk(PassThrough(rule_too_small))

@variables x[1:4]
expr = (2.23606797749979 - 2.220446049250313e-16im)*(x[1] + x[4]) + (-2.23606797749979 + 2.220446049250313e-16im)*x[2] + (-2.23606797749979 + 2.220446049250313e-16im)*x[3]

simplify(expr; rewriter=eliminatezeros) 

```

It gives the output

```julia-repl
simplify(expr; rewriter=eliminatezeros)
2.23606797749979(x[1] + x[4]) - 2.23606797749979x[2] - 2.23606797749979x[3]

```

It would be nice to have rules like these into a `Metatheory.@theory` to eliminate multiplications by near ones and additions of near zeros etc

Edit: I have made the tolerance type-specific and part of the match predicate.

---

<div class="post-metadata">

**Author:** ![agascoig](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@agascoig](https://discourse.julialang.org/u/agascoig)\
**Post date:** [August 14, 2025, 8:02pm UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/6 "2025-08-14T20:02:45Z")

</div>

I ended up doing this (thanks for you suggestions) for now, but I don’t like using applicable:

```julia-auto
function remove_small_coefficients(p,tol=1e-12)
    small_float = @rule ~x::(xs -> xs isa AbstractFloat && abs(~x) < tol) => 0   
    small_complex_re = @rule ~x::(xs -> xs isa Complex && applicable(real,xs) && abs(real(xs)) < tol) => im*imag(~x)
    small_complex_im = @rule ~x::(xs -> xs isa Complex && applicable(imag,xs) && abs(imag(xs)) < tol) => real(~x)

    r_sm_float = Prewalk(PassThrough(small_float))
    r_sm_complex_re = Prewalk(PassThrough(small_complex_re))
    r_sm_complex_im = Prewalk(PassThrough(small_complex_im))

    p=simplify(p,r_sm_float)
    p=simplify(p,r_sm_complex_re)
    p=simplify(p,r_sm_complex_im)
    simplify(p)
end

```

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [August 15, 2025, 6:43am UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/7 "2025-08-15T06:43:49Z")

</div>

Do you actually need `applicable` ?

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…

---

<div class="post-metadata">

**Author:** ![agascoig](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@agascoig](https://discourse.julialang.org/u/agascoig)\
**Post date:** [August 15, 2025, 2:04pm UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/8 "2025-08-15T14:04:40Z")

</div>

Your solution is working better for me. Thanks.

---

<div class="post-metadata">

**Author:** ![pitsianis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitsianis/32/26588_2.png) [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Post date:** [August 15, 2025, 6:13pm UTC](https://discourse.julialang.org/t/questions-about-symbolics-remove-small-constant-terms/131595/9 "2025-08-15T18:13:49Z")

</div>

And a follow-up to myself using Metatheory.jl

```julia
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)

```

results to

```julia-repl
julia> rewrite(:( (1e-16*x[1] + (-1.00000000000000001 -1e-16im)*x[2])^2 ), zerosones)
:((-(x[2])) ^ 2)

```

Any more complete/shorter/efficient/elegant solution?
