# Symbolics to Float64

**URL:** <https://discourse.julialang.org/t/symbolics-to-float64/119022>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [September 4, 2024, 9:20am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022 "2024-09-04T09:20:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 4, 2024, 9:20am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/1 "2024-09-04T09:20:16Z")

</div>

I have the output from symbolics\_solve

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/e/5ed796b5f6264315365659bcbb8b68f61ea51dce.png)

What am I to do with this? Can someone tell me how to access this information in a usable form as in I want the maximum value for this output ?

The answer should be  
π\*2 + π var"##320"

I think the answer given is incorrect?

Cheers, Paul

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 4, 2024, 10:35am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/2 "2024-09-04T10:35:03Z")

</div>

The solution to cos(x)=0 would be π/2 + πn. acos(0)=π/2, so that part at least is right, if weirdly unsimplified. Not sure why there’s a π\*2n there.

---

<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:** [September 4, 2024, 11:40am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/3 "2024-09-04T11:40:24Z")

</div>

> [@Paul\_Warburton](#):
>
> at am I to do with this? Can someone tell me how to access this information in a usable form as in I want the maximum value for this out

`symbolics_to_float`

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 4, 2024, 11:52am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/4 "2024-09-04T11:52:14Z")

</div>

I am not aware of this function? Would you please indicate how to use it and let me know if it is in the Symbolics library? I do not find it there.

---

<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:** [September 4, 2024, 11:56am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/5 "2024-09-04T11:56:01Z")

</div>

> [@ChrisRackauckas](#):
>
> symbolics\_to\_float

> **[Expression Manipulation · Symbolics.jl](https://docs.sciml.ai/Symbolics/dev/manual/expression_manipulation/#Symbolics.symbolic_to_float)**
>
> Documentation for Symbolics.jl.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 6, 2024, 3:48am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/6 "2024-09-06T03:48:14Z")

</div>

It’s not apparent how this helps for an expression that isn’t equal to an exact number

```julia
julia> foo
acos(0) + π*2var"##225"

julia> Symbolics.symbolic_to_float(foo)
ERROR: UndefVarError: `_x` not defined

```

Not that there is a maximum for a linear function. The more pressing issue is that the solution seems wrong, unless I’ve sorely misunderstood what `symbolics_solve` is solving:

```julia
julia> [(x->(x,cos(x)))(substitute(acos(0)+pi*2x, Dict(x=>z))) for z in -2:2]
5-element Vector{Tuple{Num, Num}}:
 (-10.995574287564276, -4.286263797015736e-16)
 (-4.71238898038469, -1.8369701987210297e-16)
 (1.5707963267948966, 6.123233995736766e-17)
 (7.853981633974483, 3.061616997868383e-16)
 (14.137166941154069, 5.51091059616309e-16)

julia> (x->(x,cos(x))).([-5pi/2, -3pi/2, -pi/2, pi/2, 3pi/2, 5pi/2]) # zeros of cos(x)
6-element Vector{Tuple{Float64, Float64}}:
 (-7.853981633974483, 3.061616997868383e-16)
 (-4.71238898038469, -1.8369701987210297e-16)
 (-1.5707963267948966, 6.123233995736766e-17)
 (1.5707963267948966, 6.123233995736766e-17)
 (4.71238898038469, -1.8369701987210297e-16)
 (7.853981633974483, 3.061616997868383e-16)

```

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 6, 2024, 8:54am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/7 "2024-09-06T08:54:57Z")

</div>

I think the answer given is correct, however, just in a very awkward form. I have no idea how you would use it, say to give an explicit solution to the problem, or as I intended, to find the maximum value of a third derivative of a function.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 6, 2024, 5:56pm UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/8 "2024-09-06T17:56:30Z")

</div>

The [zeroes of cosine](https://proofwiki.org/wiki/Zeroes_of_Sine_and_Cosine) are π apart, the output’s elements are 2π apart, which I evaluated earlier to check. Maybe the author @Yassin_ElBedwihy can help explain, I’m not an experienced user of Symbolics and can’t find an explanation in the documentation.

> [@Paul\_Warburton](#):
>
> to find the maximum value of a third derivative of a function.

If you were trying to find a maximum value of a function by checking where its derivative is zero, there isn’t always one unique place for its maximum. Sine peaks at 1 periodically, and it coincides with only “half” its derivative cosine’s zeroes.

---

<div class="post-metadata">

**Author:** ![Yassin\_ElBedwihy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yassin_elbedwihy/32/208506_2.png) [@Yassin\_ElBedwihy](https://discourse.julialang.org/u/Yassin_ElBedwihy)\
**Post date:** [September 6, 2024, 7:00pm UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/9 "2024-09-06T19:00:55Z")

</div>

You are correct. I would like to ask however, which would you prefer? @Benny @Paul_Warburton

```julia
symbolic_solve(cos(x), x)
-> pi/2 + pi*N

```

or  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/2/02ff202acb03500471b1288cafad7f06cd59d7aa.png)  
similarly:

```julia
symbolic_solve(sin(x), x)
-> pi*N

```

or  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/7/07de43643f7bb19902703d5c9d3deaca316ca0b8.png)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 6, 2024, 7:34pm UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/10 "2024-09-06T19:34:37Z")

</div>

To me, the first ones seem obviously better for showing the periodicity [cos(x)=0 (wolframalpha.com)](https://www.wolframalpha.com/input?i=cos%28x%29%3D0). Turns out Wolfram Alpha could do something like the second shifted ones for more complex equations, I tried the example in the `symbolic_solve` announcement [sin(x^2 +1)^2 + sin(x^2 + 1) + 3=0 (wolframalpha.com)](https://www.wolframalpha.com/input?i=sin%28x%5E2+%2B1%29%5E2+%2B+sin%28x%5E2+%2B+1%29+%2B+3%3D0) and clicking “more roots” and “exact forms” gives 8 roots but half of them are shifted (incidentally they also simplify away the leading `1//2` and `4` coefficients by multiplying). I’m not positive but it looks like those could also be collapsed into 4, which would also deviate from the `symbolic_solve` output.

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 6, 2024, 11:11pm UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/11 "2024-09-06T23:11:29Z")

</div>

I also think the first form is easier to use, however, it does not explicitly tell you what N is, this would need to be defined somehow.

In Mathematica you can access this information if required but I don’t remember how. I think it is using the substitution command (/.)

With the first format, is N automatically made into a variable? What will happen if there is more than 1 coefficient in the answer?

Somehow it would be most useful to be able to solve this equation then, say, plot the first 10 results for example.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 6, 2024, 11:31pm UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/12 "2024-09-06T23:31:44Z")

</div>

> [@Paul\_Warburton](#):
>
> With the first format, is N automatically made into a variable?

I think that was just an easier shorthand for the generated var"##\_\_", there’s no good way to generate simpler symbols that could easily collide with the user’s current or likely upcoming symbols.

> [@Paul\_Warburton](#):
>
> to solve this equation then, say, plot the first 10 results for example.

There’s no “first” results for a periodic function, so we have to specify the inputs ourselves. I haven’t figured out how to reference a generated variable for `substitute`, but that can evaluate expressions with user-defined variables to a numerical `Num`, which I showed earlier. `Symbolics.value(::Num)` unwraps to the underlying `Number` subtype, could be `Int`, `Float64`, `Rational{Int}`,etc.

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 7, 2024, 12:01am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/13 "2024-09-07T00:01:49Z")

</div>

The user could provide the symbols to use

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 7, 2024, 12:14am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/14 "2024-09-07T00:14:04Z")

</div>

That’s an idea, but we can’t possibly do it in advance when we don’t know how many generated variables would be needed. Maybe a way to do it afterward, like counting the number of generated variables and a method to replace them with a vector of enough of our own variables. Maybe there’s already a way to do that for any variables now already, I just never learned.

---

<div class="post-metadata">

**Author:** ![Yassin\_ElBedwihy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yassin_elbedwihy/32/208506_2.png) [@Yassin\_ElBedwihy](https://discourse.julialang.org/u/Yassin_ElBedwihy)\
**Post date:** [September 7, 2024, 8:59am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/15 "2024-09-07T08:59:43Z")

</div>

@Paul_Warburton @Benny , The variable information is given in the Info in blue when calling symbolic\_solve.

```julia
julia> root = symbolic_solve(cos(x), x)[1]
[ Info: var"##233" ϵ Ζ
π / 2 + π*var"##233"

```

Afterwards, you can use `Symbolics.get_variables` in order to get the only variable in some printed out root and subtitute it for whatever value you want, here is 0 for example:

```julia
julia> root
π / 2 + π*var"##233"

julia> Symbolics.substitute(root, Dict(Symbolics.get_variables(root)[1]=>0))
1.5707963267948966

```

if you dont want it to fold, I advise using `Symbolics.ssubs` instead of `Symbolics.substitute`

```julia
julia> Symbolics.ssubs(root, Dict(Symbolics.get_variables(root)[1]=>0))
(1//2)*π

```

but this may break if complex nums are present in the equation

---

<div class="post-metadata">

**Author:** ![Yassin\_ElBedwihy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yassin_elbedwihy/32/208506_2.png) [@Yassin\_ElBedwihy](https://discourse.julialang.org/u/Yassin_ElBedwihy)\
**Post date:** [September 7, 2024, 9:10am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/16 "2024-09-07T09:10:49Z")

</div>

Also, @Benny , the example `sin(x^2 +1)^2 + sin(x^2 + 1) + 3=0 `, this is what we currently output:

```julia
julia> symbolic_solve(sin(x^2 + 1)^2 + sin(x^2 + 1) + 3, x)
[ Info: var"##268" ϵ Ζ
[ Info: var"##271" ϵ Ζ
4-element Vector{SymbolicUtils.BasicSymbolic{Complex{Real}}}:
 (1//2)*√(-4(1 - asin((1//2)*(-1 + (0 + 1im)*√(11))) - (π*var"##268")))
 (-1//2)*√(-4(1 - asin((1//2)*(-1 + (0 + 1im)*√(11))) - (π*var"##268")))
 (1//2)*√(-4(1 - asin((1//2)*(-1 - ((0 + 1im)*√(11)))) - (π*var"##271")))
 (-1//2)*√(-4(1 - asin((1//2)*(-1 - ((0 + 1im)*√(11)))) - (π*var"##271")))

```

Which i think is correct given that we now are doing `pi*N` so that accounts for the 8 roots mathematica is showing, please correct me if im mistaken.

```julia
julia> root = symbolic_solve(sin(x^2 + 1)^2 + sin(x^2 + 1) + 3, x)[1]
[ Info: var"##286" ϵ Ζ
[ Info: var"##289" ϵ Ζ
(1//2)*√(-4(1 - asin((1//2)*(-1 + (0 + 1im)*√(11))) - (π*var"##286")))

julia> Symbolics.substitute(root, Dict(Symbolics.get_variables(root)[1]=>0))
0.5279984677890944883195449371158433523824004298838572993541435240980004752596828 + 1.238360873135359676020102047979975902000464573894570345327213453060598580248021im

```

which is one of the mathematica roots found:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b9ba1fd81665425267c7e5a2c876eb5f9edf936.png)  
and since matheamtica is looping over `2pi*N`, our root equals this and some other shifted root they have.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 7, 2024, 9:54am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/17 "2024-09-07T09:54:37Z")

</div>

I’m assuming your result is on an updated version. For reference, this is the result on Julia v1.10.5, Symbolics v6.5.0, Nemo v0.46.2:

```julia
julia> symbolic_solve(sin(x^2 + 1)^2 + sin(x^2 + 1) + 3, x)
[ Info: var"##230" ϵ Ζ
[ Info: var"##233" ϵ Ζ
4-element Vector{SymbolicUtils.BasicSymbolic{Complex{Real}}}:
 (1//2)*√(-4(1 - asin((1//2)*(-1 + (0 + 1im)*√(11))) - (π*2var"##230")))
 (-1//2)*√(-4(1 - asin((1//2)*(-1 + (0 + 1im)*√(11))) - (π*2var"##230")))
 (1//2)*√(-4(1 - asin((1//2)*(-1 - ((0 + 1im)*√(11)))) - (π*2var"##233")))
 (-1//2)*√(-4(1 - asin((1//2)*(-1 - ((0 + 1im)*√(11)))) - (π*2var"##233")))

julia> symbolic_solve(sin(x^2 + 1)^2 + sin(x^2 + 1) + 3, x) # repeat shuffles the terms
[ Info: var"##240" ϵ Ζ
[ Info: var"##243" ϵ Ζ
4-element Vector{SymbolicUtils.BasicSymbolic{Complex{Real}}}:
 (1//2)*√(-4(1 - (π*2var"##240") - asin((1//2)*(-1 + (0 + 1im)*√(11)))))
 (-1//2)*√(-4(1 - (π*2var"##240") - asin((1//2)*(-1 + (0 + 1im)*√(11)))))
 (1//2)*√(-4(1 - asin((1//2)*(-1 - ((0 + 1im)*√(11)))) - (π*2var"##243")))
 (-1//2)*√(-4(1 - asin((1//2)*(-1 - ((0 + 1im)*√(11)))) - (π*2var"##243")))

```

> [@Yassin\_ElBedwihy](#):
>
> Which i think is correct given that we now are doing `pi*N` so that accounts for the 8 roots mathematica is showing, please correct me if im mistaken.

I’m really not qualified to correct any math, I’m just an occasional user (so those tips are very appreciated). I can at least note that the `asin` terms all have a coefficient of -1, which would simplify to +1 if we cancel out the 1/2 and -4 terms. However, Wolfram Alpha’s `asin` terms’ coefficients vary between +1 and -1, adding the extra π when it’s -1. I don’t know if there’s an `asin` identity that reconciles that _and_ collapses the 8 roots into 4 closer spaced roots, so my earlier guess seems unfounded from my perspective, just happened to superficially resemble a simpler case we could verify.

Rather than manually tweaking expressions to compare to Wolfram Alpha results, which aren’t verified to be right either, could periodic or otherwise infinite sequence of roots be verified numerically, even within a finite range? Is there a CAS principle where solving simpler problems correctly (`y^2+y+3=0`, `y=sin(z)`, `z=x^2+1` would be simpler parts) would solve more complex problems correctly?

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 7, 2024, 11:15pm UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/18 "2024-09-07T23:15:57Z")

</div>

I think part of the issue here is that you are taking the arcsin of a complex number, which really means you need to use hyperbolic sin solutions.

The best way to resolve this will be to solve this problem by hand, explicitly and understand the process.

Yassin, does you code turn this into a quadratic first in sin(x^2+1), solve the quadratic then solve for x^2+1 = roots of the quadratic?

---

<div class="post-metadata">

**Author:** ![Yassin\_ElBedwihy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yassin_elbedwihy/32/208506_2.png) [@Yassin\_ElBedwihy](https://discourse.julialang.org/u/Yassin_ElBedwihy)\
**Post date:** [September 8, 2024, 9:34am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/19 "2024-09-08T09:34:55Z")

</div>

> [@Benny](#):
>
> I’m assuming your result is on an updated version.

Yes, this is under development [here](https://github.com/JuliaSymbolics/Symbolics.jl/pull/1264)

> [@Benny](#):
>
> Is there a CAS principle where solving simpler problems correctly (`y^2+y+3=0`, `y=sin(z)`, `z=x^2+1` would be simpler parts) would solve more complex problems correctly?

Well, our solver actually polynomializes this input expression and turns it into `Y^2 + Y + 3 == 0`. We then solve this expression and find the roots:

```julia
julia> symbolic_solve(Y^2 + Y + 3)
2-element Vector{SymbolicUtils.BasicSymbolic{Complex{Real}}}:
 (-1//2 + 0//1*im) + (0//1 + 1//2*im)*√(11)
 (-1//2 + 0//1*im) + (0//1 - 1//2*im)*√(11)

```

this array is then used to solve for x inside Y. so essentially we’re solving `sin(x^2 + 1) == Y`

@Paul_Warburton Had this comment written out yesterday and forgot to post it, hope this helps.

---

<div class="post-metadata">

**Author:** ![Yassin\_ElBedwihy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yassin_elbedwihy/32/208506_2.png) [@Yassin\_ElBedwihy](https://discourse.julialang.org/u/Yassin_ElBedwihy)\
**Post date:** [September 8, 2024, 9:38am UTC](https://discourse.julialang.org/t/symbolics-to-float64/119022/20 "2024-09-08T09:38:33Z")

</div>

> [@Paul\_Warburton](#):
>
> I think part of the issue here is that you are taking the arcsin of a complex number, which really means you need to use hyperbolic sin solutions.

Computing this isnt our problem, as long as the roots are correct we dont care how the asin function carries out the computation.
