Tio Miserias
Tio Miserias

Reputation: 495

How to convert sum of terms in a vector in Julia?

Do you know how to convert a sum of terms in a vector in Julia?

For instance, If we have a polynomial p(x,y)=1 +x+y+xy+x^2+y^2+xy^4, the array v associated to p(x,y) would be the following:

v=[1,x,y,xy,x^2,y^2,xy^4]

Other example: If you have the following expression: a+b+c+abc+ac, the array v associated to this expression would be the following:

v=[a,b,c,abc,ac]

Upvotes: 4

Views: 190

Answers (2)

jalex
jalex

Reputation: 1524

Use metaprogramming to parse the expression and look at the resulting expression tree

ex = Meta.parse("1 +x+y+x*y+x^2+y^2+x*y^4")
ex.args
8-element Vector{Any}:
  :+
 1
  :x
  :y
  :(x * y)
  :(x ^ 2)
  :(y ^ 2)
  :(x * y ^ 4)

Upvotes: 1

Jules Gagnon-Marchand
Jules Gagnon-Marchand

Reputation: 3781

You can lookup SymPy.jl if you want to learn how to do symbolic computation in Julia. Here is a reasonable tutorial: https://mth229.github.io/symbolic.html Once you understand this, you should be able to achieve what you are trying to do.

Upvotes: 1

Related Questions