Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqrt of constant representation in Julia Symbolics

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?

like image 901
Tarik Avatar asked Apr 17 '21 18:04

Tarik


People also ask

What is the use of sqrt() in Julia?

The sqrt () is an inbuilt function in julia which is used to return the square root of the specified number and throws DomainError for negative Real parameter. It also accept complex negative parameter. x: Specified values. Returns: It returns the square root of the specified number and throws DomainError for negative Real parameter.

How do you do symbolic differentiation in Julia?

Symbolic Differentiation in Julia Now that we know how Julia expressions are built, we can design a very simple prototype system for doing symbolic differentiation in Julia. We’ll build up our system in pieces using some of the most basic rules of calculus: The Constant Rule: d/dx c = 0

What is @juliasymbolics?

JuliaSymbolics is the Julia organization dedicated to building a fully-featured and high performance Computer Algebra System (CAS) for the Julia programming language. It is currently home to a layered architecture of packages: Layer 3: Symbolics.jl – A fast symbolic system designed for everyday symbolic computing needs. It features:

How are Julian expressions represented in the Julia interpreter?

Like Lisp, the Julia interpreter represents Julian expressions using normal data structures: every Julian expression is represented using an object of type Expr.


3 Answers

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)
like image 121
Nasser Avatar answered Oct 07 '22 14:10

Nasser


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)
like image 3
Picaud Vincent Avatar answered Oct 07 '22 14:10

Picaud Vincent


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.

like image 1
Samson Avatar answered Oct 07 '22 15:10

Samson