Consider the following trivariate polynomial with two parameters a and b:
P(x,y,z) = ((x*x+y*y+1)*(a*x*x+b*y*y)+z*z*(b*x*x+a*y*y)-2*(a-b)*x*y*z-a*b*(x*x+y*y))^2-4*(x*x+y*y)*(a*x*x+b*y*y-x*y*z*(a-b))^2
In POV-Ray, I want to plot the algebraic isosurface of equation P(x,y,z)=0 for some values of a and b. In POV-Ray, one has to define the polynomial by listing its monomials where each monomial is given as follows:
xyz(i,j,k): coef
where i,j,k are the exponents and coef is the coefficient of x^i y^j z^k.
For example one has the monomials b^2 y^8 = b^2 x^0 y^8 z^0 and 2*b*a x^6 z^2 = 2*b*a x^6 y^0 z^2 and then they must be given as follows:
xyz(0, 8, 0): pow(b,2),
xyz(6, 0, 2): 2*b*a,
......
It is not funny to manually expand the given polynomial. I want to generate this POV-Ray code with R.
I found a solution with the packages Ryacas and partitions.
First, let's enter the polynomial in Ryacas:
Let's see the degree of each variable:
So we need the exponents
i,j,kwith possible values foriandjbetween0and8and possible values forkbetween0and4. We use the partitions package to generate the compositions of these three integers. For example, the compositions summing to4are obtained by:Here is a function which takes a composition
(i,j,k)and returns the coefficient ofx^i y^j z^kprepended byxyz(i,j,k)::Then we loop over the degree:
We obtain the desired code in the file polynomial.pov: