# Basic question on Julia symbolic. How to keep exp(1) as is and not convert it to a floating point number?

**URL:** <https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476>\
**Category:** New to Julia\
**Created:** [August 28, 2022, 11:36pm UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476 "2022-08-28T23:36:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 28, 2022, 11:36pm UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/1 "2022-08-28T23:36:23Z")

</div>

I am converting some symbolic integrals to see if I can evaluate them in Julia symbolic. Some of these have things like `exp(1)` but Julia automatically convert this to number 2.718281828459045

What do I need to keep it as `exp(1)`? Here is an example

```julia
using Symbolics
@syms x 

julia> x*exp(1)
2.718281828459045x

```

Thanks  
–Nasser

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 29, 2022, 12:17am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/2 "2022-08-29T00:17:20Z")

</div>

How about ‘@syms o’ and ‘exp(o)‘ whenever you want to prevent evaluation?

---

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 29, 2022, 12:20am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/3 "2022-08-29T00:20:08Z")

</div>

Are you saying I should make `1` as `@syms 1` ? I do not understand.  
Could you please show in code how your suggestion will work for the example I’ve given so that the result will come out as `x*exp(1)`

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 29, 2022, 12:52am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/4 "2022-08-29T00:52:14Z")

</div>

```julia
julia> SymbolicUtils.Term(exp, [x])
exp(x)

julia> log(SymbolicUtils.Term(exp, [x]))
log(exp(x))

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [August 29, 2022, 1:04am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/5 "2022-08-29T01:04:20Z")

</div>

```julia
julia> using Symbolics

julia> Symbolics.Term(exp, 1)
exp(1)

julia> ans / ans
1

julia> Symbolics.Term(exp, ans)
exp(1)

julia> Symbolics.Term(exp, [ans])
exp(exp(1))

```

There are earlier threads on the same topic, btw:

> [@Symbolic sqrt(2) and sin(pi)](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633/):
>
> I asked this as a genuine question but looking at Symbolics.jl more closely I get the impression that the answer is that there is no way to do this even for sqrt(2). If that’s the case then this is something very different from other CAS… Am I misunderstanding this? How can sqrt(2) be represented exactly?

---

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 29, 2022, 1:23am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/6 "2022-08-29T01:23:55Z")

</div>

Thanks, This helps a little. But for what I am doing, I see two problems.

I am trying to use the symbolic integrator at [Home · SymbolicNumericIntegration.jl](https://symbolicnumericintegration.sciml.ai/dev/) and when called with `x*exp(1)` it changes it back to floating point. Here is the code

```julia
using Symbolics
using SymbolicNumericIntegration
using SymbolicUtils

@syms x
expr=x*Symbolics.Term(exp,[1])

```

Gives `x*exp(1)` which is good. But now

```julia
julia> SymbolicNumericIntegration.integrate(expr,x)
(1.3591409142295223(x^2), 0, 2.220446049250313e-16)

```

So it changed it back to floating point.

Second issue. I am reading hundreds of integrals. with exp(1) and exp(25) and any other such expressions.

Is there a way to automate this, so that it automatically replaces `exp( a_numeric_value)` to `Symbolics.Term(exp,[a_numeric_value])` so I do not have to manually edit hundreds of expressions and do this by hand for each case?

btw, thanks for the link you gave. I tried to replace the numbers with `SymbolicValue(1)` but it said it could not find `SymbolicValue`

```julia
julia> exp(SymbolicValue(1))
ERROR: UndefVarError: SymbolicValue not defined
Stacktrace:
 [1] top-level scope
   @ REPL[33]:1

julia> exp(Symbolics.SymbolicValue(1))
ERROR: UndefVarError: SymbolicValue not defined
Stacktrace:
 [1] getproperty(x::Module, f::Symbol)
   @ Base ./Base.jl:31
 [2] top-level scope
   @ REPL[34]:1

julia> exp(SymbolicUtils.SymbolicValue(1))
ERROR: UndefVarError: SymbolicValue not defined
Stacktrace:
 [1] getproperty(x::Module, f::Symbol)
   @ Base ./Base.jl:31
 [2] top-level scope
   @ REPL[35]:1

julia>

```

ps. I am newbie in Julia symbolics.

---

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 29, 2022, 4:44am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/7 "2022-08-29T04:44:20Z")

</div>

Fyi, tried this trick below, but it seems to broke things

```julia
>julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.8.0 (2022-08-17)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using Symbolics
julia> using SymbolicNumericIntegration
julia> @syms x
(x,)

julia> exp(1)
2.718281828459045

julia> import Base: exp
julia> typeof(1)
Int64

julia> exp(x::Int64) = Symbolics.Term(exp,[x])
exp (generic function with 43 methods)

julia> exp(1)
exp(1)

julia> SymbolicNumericIntegration.integrate(x*exp(1),x)
ERROR: MethodError: no method matching zero(::Type{SymbolicUtils.Term{Real, Nothing}})
Closest candidates are:
  zero(::Union{Type{P}, P}) where P<:Dates.Period at /usr/share/julia/stdlib/v1.8/Dates/src/periods.jl:53
  zero(::Union{AbstractAlgebra.Generic.LaurentSeriesFieldElem{T}, AbstractAlgebra.Generic.LaurentSeriesRingElem{T}} where T<:AbstractAlgebra.RingElement) at ~/.julia/packages/AbstractAlgebra/ElVzc/src/generic/LaurentSeries.jl:466
  zero(::Union{AbstractAlgebra.Generic.LaurentSeriesFieldElem{T}, AbstractAlgebra.Generic.LaurentSeriesRingElem{T}} where T<:AbstractAlgebra.RingElement, ::String; cached) at ~/.julia/packages/AbstractAlgebra/ElVzc/src/generic/LaurentSeries.jl:480
  ...
Stacktrace:
 [1] DataDrivenDiffEq.STLSQ(threshold::Vector{SymbolicUtils.Term{Real, Nothing}})
   @ DataDrivenDiffEq ~/.julia/packages/DataDrivenDiffEq/s9jl3/src/optimizers/stlsq.jl:32
 [2] integrate(eq::SymbolicUtils.Mul{Number, Int64, Dict{Any, Number}, Nothing}, x::SymbolicUtils.Sym{Number, Nothing})
   @ SymbolicNumericIntegration ~/.julia/packages/SymbolicNumericIntegration/OUbIs/src/integral.jl:36
 [3] top-level scope
   @ REPL[9]:1

```

---

<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 29, 2022, 10:41am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/8 "2022-08-29T10:41:00Z")

</div>

> [@Nasser](#):
>
> `SymbolicValue`

That’s not a thing. What are you trying to do?

---

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 29, 2022, 10:50am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/9 "2022-08-29T10:50:10Z")

</div>

"That’s not a thing. "

Did you check the link given in the answer where I was replying to which uses it there? I just copied it. Here is the link again

> [@Symbolic sqrt(2) and sin(pi)](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633):
>
> I asked this as a genuine question but looking at Symbolics.jl more closely I get the impression that the answer is that there is no way to do this even for sqrt(2). If that’s the case then this is something very different from other CAS… Am I misunderstanding this? How can sqrt(2) be represented exactly?

someone used `SymbolicValue` there. I have no idea where it came from.

---

<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 29, 2022, 11:05am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/10 "2022-08-29T11:05:24Z")

</div>

> [@Nasser](#):
>
> someone used `SymbolicValue` there. I have no idea where it came from.

That was proposed pseudocode, not actual code. That issue in DataDrivenDiffEq.jl is probably solvable if the element type is allowed to be set directly. The issue is that it’s `exp(1) * x` so `exp(1)` is the coefficient which is assumed to be numeric, and that is then used to set the numeric type in the STLSQ sparse regression, but since it’s symbolic it does not have numeric operations.

---

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 29, 2022, 11:33am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/11 "2022-08-29T11:33:29Z")

</div>

OK.  
But back to main issue which is still not resolved.  
How to make Julia not convert exp(1) to floating point? Would you know of a solution?

Few replies above I showed that once I call

`SymbolicNumericIntegration.integrate(expr,x)`

Then it converts `exp(1)` back to number looking at the result.

This is not how other CAS systems work.

---

<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 29, 2022, 11:34am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/12 "2022-08-29T11:34:52Z")

</div>

> [@Nasser](#):
>
> How to make Julia not convert exp(1) to floating point? Would you know of a solution?

`Symbolics.Term(exp,[1])`

> [@Nasser](#):
>
> Few replies above I showed that once I call
> 
> `SymbolicNumericIntegration.integrate(expr,x)`

That’s a SymbolicNumericIntegration issue because it’s a numerical algorithm, not a symbolic one.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 29, 2022, 11:44am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/13 "2022-08-29T11:44:00Z")

</div>

> [@ChrisRackauckas](#):
>
> Symbolics.Term(exp,[1])

Why not `Symbolics.Term(exp, 1)`?

---

<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 29, 2022, 11:51am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/14 "2022-08-29T11:51:07Z")

</div>

> [@DNF](#):
>
> Why not `Symbolics.Term(exp, 1)`?

They are always supposed to have an array of arguments ([https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/src/types.jl#L34](https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/src/types.jl#L34))

---

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 29, 2022, 11:51am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/15 "2022-08-29T11:51:24Z")

</div>

> [@ChrisRackauckas](#):
>
> That’s a SymbolicNumericIntegration issue because it’s a numerical algorithm, not a symbolic one.

Well, it says " hybrid symbolic/numerical integration package that works on the [Julia Symbolics](https://github.com/JuliaSymbolics/Symbolics.jl) expressions."

Do you know of a different symbolic Julia integrator than the above? It is the only one I found when googling.

But I just opened an issue for the above package. Here is the link

> <https://github.com/SciML/SymbolicNumericIntegration.jl/issues/21>
>
> reference
> 
> https://discourse.julialang.org/t/basic-question-on-julia-symbolic-…how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476
> 
> Using this code
> 
> \`\`\`
> using Symbolics
> using SymbolicNumericIntegration
> using SymbolicUtils
> 
> @syms x
> expr=x\*Symbolics.Term(exp,\[1\])
> julia\> SymbolicNumericIntegration.integrate(expr,x)
> (1.3591409142295223(x^2), 0, 2.220446049250313e-16)
> 
> \`\`\`
> 
> As can be seen, exp(1) before the call remained exp(1) because of using \`Symbolics.Term(exp,\[1\])\` to prevent evaluation.
> 
> But inside/during the call \`SymbolicNumericIntegration.integrate\` it was converted at one point to floating point.
> 
> Is there a way to keep it as \`exp(1)\` so the result should be \`exp(1)\*x^2/2\` ?
> 
> Julia 1.8 on Linux

---

<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 29, 2022, 11:53am UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/16 "2022-08-29T11:53:06Z")

</div>

> [@Nasser](#):
>
> Do you know of a different symbolic Julia integrator than the above? It is the only one I found when googling.

That’s the only one right now.

> [@Nasser](#):
>
> Well, it says " hybrid symbolic/numerical integration package that works on the [Julia Symbolics](https://github.com/JuliaSymbolics/Symbolics.jl) expressions."

Yes, and it’s symbolic-numeric, as in, it uses numerical methods to solve for the symbolic equation. So it needs to numerically evaluate, and it cannot keep a closed representation of transcendentals (nor algebraic variables, I think it can do rationals in some sense though?)

---

<div class="post-metadata">

**Author:** ![Nasser](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Nasser](https://discourse.julialang.org/u/Nasser)\
**Post date:** [August 29, 2022, 12:02pm UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/17 "2022-08-29T12:02:06Z")

</div>

But it should be able to see that `exp(1)` is clearly a constant and keep it as is.

Hopefully in the future Julia will get a symbolic integrator added to it.

---

<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 29, 2022, 12:09pm UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/18 "2022-08-29T12:09:07Z")

</div>

> [@Nasser](#):
>
> But it should be able to see that `exp(1)` is clearly a constant and keep it as is.

no, because a numerical integration methods cannot keep that as constant.

---

<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 29, 2022, 12:15pm UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/19 "2022-08-29T12:15:53Z")

</div>

> [@Nasser](#):
>
> Hopefully in the future Julia will get a symbolic integrator added to it.

It will, it just hasn’t been done because they are generally slower and less robust than this new thing.

> **[Symbolic-Numeric Integration of Univariate Expressions based on Sparse...](https://arxiv.org/abs/2201.12468)**
>
> Most computer algebra systems (CAS) support symbolic integration as core functionality. The majority of the integration packages use a combination of heuristic algebraic and rule-based (integration table) methods. In this paper, we present a hybrid...

It’s neither here nor there: it has some upsides over other methods, and downsides over others. It’s just that given the SciML ecosystem, algorithms like this are extremely easy to write so it came first. There are some in Risch and rule-based methods in development, but of course those then have major trade-offs in speed and types of integrals they tend to fail on.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 29, 2022, 3:40pm UTC](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476/20 "2022-08-29T15:40:03Z")

</div>

> [@ChrisRackauckas](#):
>
> an array of arguments

Is it expected that `Symbolics.Term(exp, [1] )` is 100x slower than `Symbolics.Term(exp, 1)`?

[Next page](https://discourse.julialang.org/t/basic-question-on-julia-symbolic-how-to-keep-exp-1-as-is-and-not-convert-it-to-a-floating-point-number/86476.md?page=2)
