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

Viewed 139

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]

2 Answers

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.

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)
Related