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.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • _**π**_ 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. – JAlex Apr 21 '21 at 13:32
  • 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. – JAlex Apr 21 '21 at 13:45
  • @JAlex 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. – Tarik Apr 21 '21 at 14:18
  • 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)=0` – JAlex Apr 21 '21 at 15:13
  • @JAlex Yes, it makes sense. – Tarik Apr 21 '21 at 19:09
  • 1
    `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](https://github.com/JuliaSymbolics/Symbolics.jl/issues/new). – mbauman Apr 21 '21 at 19:53
  • @mbauman Thanks for your comment. How come multiplying Symbolics.pi by a variable x gives πx (π does not get expanded)? – Tarik Apr 21 '21 at 20:47
  • 1
    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). – mbauman Apr 21 '21 at 23:11

1 Answers1

1

Try:

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

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

Szabó Áron
  • 185
  • 1
  • 4
  • My question is about having a symbolic expression show `5π` and only evaluate upon call to eval. Will try though. – Tarik Apr 28 '21 at 16:38