How to represent 5*π symbolically in Julia
Asked Answered
B

1

6

Related to a previous question: I am using Julia Symbolics package and would like to represent 5*pi symbolically. I tried the following to no avail:

using Symbolics
5 * Symbolics.pi      # expanded to 15.707963267948966
Num(5) * Symbolics.pi # expanded to 15.707963267948966

@variables x
Symbolics.pi * x # Gives πx, so it works with variables

Desired result, a symbolic expression using Symbolics.pi and the constant that are not numerically calculated on the spot but are kept as a product of 5 and π and show up as 5π, using Symbolics.pi.

Boutin answered 18/4, 2021 at 18:58 Comment(8)
π is a named constant and not a variable, and I am hoping Symbolics supports such constructs. Any decent symbolic package should support things like π, e, and i by deferring evaluation until numeric execution.Myrmeco
Maybe issue a bug request because evaluating sin(Symbolics.pi) isn't zero. So Symbolics.pi isn't π but the closest numeric approximation of it using IEEE 754 floating point.Myrmeco
@Myrmeco Yes, but πx does not become 3.1415...x, it stays as is. Thanks for your comments though. Posting a bug might be the thing to do. I will wait a couple of days and do that if nothing comes out of this question.Boutin
If you use SymEngine then you the expected behavior sin(SymEngine.PI)=0. The same goes for sin(5*SymEngine.PI)=0 and also sin(5000000*SymEngine.PI)=0Myrmeco
@Myrmeco Yes, it makes sense.Boutin
Symbolics.pi is Base.pi. That is, there's not necessarily any Symbolics smarts to it at all just because you accessed it through Symbolics.pi. Since modules (by default) are using Base, non-symbolicsy things can be accessed by way of Symbolics.name. For example, try Symbolics.pwd() or even Symbolics.exit(). You likely won't rally an answer on SO — even with a bounty — at least not yet. I'd start here.Aircraftman
@Aircraftman Thanks for your comment. How come multiplying Symbolics.pi by a variable x gives πx (π does not get expanded)?Boutin
It's because of how pi itself works in Julia (without Symbolics). It'll remain irrational until you do something with it — like multiplying it by five or takin the sine — at which point it'll promote (likely to a Float64). Doing π*x is just stashing pi inside the term without actually doing the multiplication or a promotion (yet).Aircraftman
G
1

Try:

x = Symbolics.pi * Num(5)
print(x)

Here x should be evaluated, hence pi should be promoted.

Glasgo answered 28/4, 2021 at 7:46 Comment(1)
My question is about having a symbolic expression show and only evaluate upon call to eval. Will try though.Boutin

© 2022 - 2024 — McMap. All rights reserved.