Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transform a mathematical expression into an easier-to-parse way, in Julia?

Is there a predefined class or package in Julia that can handle this sort of transformation: x * (y - z) => [y, z, "-"; x, y, "*"] (this assumes that in the interest of saving memory space, the output of the first operation is stored in variable y before the second operation is applied).

Basically, I need to break down a mathematical expression into a series of steps which I can then apply to the variables involved, algorithmically. Thanks!

like image 367
Zack Avatar asked Oct 21 '25 14:10

Zack


1 Answers

You can get an abstract syntax tree from an expression. Since it is a Julian data structure you can than process or visualize it any way you want.

julia> dump(:(x * (y - z)))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol *
    2: Symbol x
    3: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol -
        2: Symbol y
        3: Symbol z

This mechanism is described in the Metaprogramming section of Julia manual

Depending on what you actually need to do this question might be also related: Julia What is The Fastest Way to Evaluate a Math Tree

like image 58
Przemyslaw Szufel Avatar answered Oct 23 '25 07:10

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!