# Symbolics.jl expressions that don't reduce to single type

**URL:** <https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551>\
**Category:** Modelling & Simulations\
**Tags:** question, symbolics\
**Created:** [August 7, 2023, 8:31am UTC](https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551 "2023-08-07T08:31:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eemilp](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Eemilp](https://discourse.julialang.org/u/Eemilp)\
**Post date:** [August 7, 2023, 8:31am UTC](https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551/1 "2023-08-07T08:31:00Z")

</div>

Hi,

I’m trying to use Symbolics.jl to simplify some expressions in the domain of quantum mechanics. The problem I’m facing is that I would like to simplify a partial expression that does not exactly reduce to a single type.

For example

```julia
using Symbolics
using SymbolicUtils

abstract type MyType end

@syms a::MyType b::Number
@acrule(*(~~a) + *(~b,~~a) => *(1 + ~b, (~~a)...))

simplify(a + a*b)

```

brings up `MethodError: no method matching *(::SymbolicUtils.BasicSymbolic{Number}, ::SymbolicUtils.BasicSymbolic{MyType})`.

How could I simplify the expression as far as it can using the rules, even if all operations are not defined?

---

<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 7, 2023, 12:39pm UTC](https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551/2 "2023-08-07T12:39:49Z")

</div>

You’ll need to define a custom rule set and pass that into simplify.

---

<div class="post-metadata">

**Author:** ![Eemilp](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Eemilp](https://discourse.julialang.org/u/Eemilp)\
**Post date:** [August 8, 2023, 7:02am UTC](https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551/3 "2023-08-08T07:02:14Z")

</div>

Okay, thanks.

I’m not super familiar with the syntax but I can’t seem to even apply single rule to my expression, or am I missing something here?

```julia
using Symbolics
using SymbolicUtils

abstract type MyType end

@syms a::MyType b::Number
rule = @acrule(*(~~a) + *(~b,~~a) => *(1 + ~b, (~~a)...))

rule(a + b*a)

```

brings up the same error as before.

---

<div class="post-metadata">

**Author:** ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)\
**Post date:** [August 8, 2023, 1:45pm UTC](https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551/4 "2023-08-08T13:45:08Z")

</div>

The issue is not with the rules so far, it’s with `a*b`

We only define the arithmetic methods for `Symbolic{<:Number}`, that’s why `*` is not defined for `Symbolic{MyType}`.

It should work if you make `MyType` a subtype of `Number`. It will have the default behavior that is suitable for numbers, you can of course overload specific methods if they behave differently.

As for applying the rules, you can use the Rewriters to compose the rules into something that does what you want (e.g. traverse the tree and apply the rule to each node, etc.) The docs for this are here [SymbolicUtils.jl](https://symbolicutils.juliasymbolics.org/rewrite/)

---

<div class="post-metadata">

**Author:** ![Eemilp](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Eemilp](https://discourse.julialang.org/u/Eemilp)\
**Post date:** [August 8, 2023, 2:13pm UTC](https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551/5 "2023-08-08T14:13:18Z")

</div>

Thanks for the insightful response, but the problem is that making `MyType` a subtype of `Number` makes it behave like a number (commute in multiplication etc.) which is the behaviour I want to avoid by making my own type.

---

<div class="post-metadata">

**Author:** ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)\
**Post date:** [September 3, 2023, 4:27pm UTC](https://discourse.julialang.org/t/symbolics-jl-expressions-that-dont-reduce-to-single-type/102551/6 "2023-09-03T16:27:33Z")

</div>

> [@shashi](#):
>
> It will have the default behavior that is suitable for numbers, you can of course overload specific methods if they behave differently.

By this I meant that you might have to define your own \* method on `Symbolic{MyType}` objects.
