Sqrt of constant representation in Julia Symbolics
Asked Answered
C

3

6

I am looking for a way to represent a symbolic expression such as sqrt(3)*x so as to avoid sqrt(3) being calculated upfront.

Sample code:

using Symbolics
@variables x
y = sqrt(3)*x

Showing y, we can see that sqrt(3) has become a floating point.

Is there a mechanism to keep the sqrt in symbolic form?

Contingent answered 17/4, 2021 at 18:56 Comment(1)
Are you speaking of (1/3)x or (1//3)x ? Because (1//3)x works as expected...Roodepoortmaraisburg
P
4

is there a mechanism to keep the sqrt in symbolic form?

julia> using Symbolics
julia> @variables x
(x,)
julia> y = Symbolics.Term(sqrt,[3])*x

x*sqrt(3)
Palmer answered 18/4, 2021 at 0:25 Comment(1)
Thanks for your nice answer. I have been looking for Symbolics thorough documentation but only found something basic. Could you provide a pointer.Contingent
R
2

I cannot reproduce:

julia> using Symbolics

julia> @variables x
(x,)

julia> y = 1//3*x
(1//3)*x

julia> y
(1//3)*x

My config:

[0c5d862f] Symbolics v0.1.21

and

julia> versioninfo()

Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Roodepoortmaraisburg answered 17/4, 2021 at 19:7 Comment(1)
Sorry, I remember the problem. It was sqrt(3). Thanks for your follow up though. I have updated my question.Contingent
H
1

Funny, y = 1//3 * x works, but not y = sqrt(3) * x as according to the orignal question. So, the answer from user Nasser is really helpful thanks.

Highbinder answered 18/4, 2021 at 16:16 Comment(1)
Thinking about it, when calling sqrt with am integer of float, multiple dispatch will call sqrt(::Int64) or sqrt(::Float64). However, when using the // operator between an Int64 and a Symbolics Num, multiple dispatch will select the corresponding function with a signature operator // (Int64, Symbolics.Num)Contingent

© 2022 - 2024 — McMap. All rights reserved.