Zack
Zack

Reputation: 191

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!

Upvotes: 4

Views: 284

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42234

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

Upvotes: 5

Related Questions