following is my python code.
k1, k2, k3, and k4 are long polynomials. I want to verify that k1*k3+k2*k4==0 but when I write (k1*k3+k2*k4).expand, the program has no response for a long time. However, when I write similar code in matlab (expand(k1*k3+k2*k4)), matlab gives the expected answer (0) in five seconds. How do I get this answer in sympy?
from sympy import symbols,expand,simplify
u1,u2.u3=symbols("u1 u2 u3")
k1= -2*u2*u3*(2*u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) - 2*(-u3**2 + (u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)))*(-u1*u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2)*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(sqrt(3)*u2**2*(u1 - u2) + u2*u3*(2*u1 - 3*u2) + u3**3 - sqrt(3)*u3**2*(u1 - 3*u2)) - u1*u3**2*(-3*u2**2 + u3**2)*(u3**2 - 3*(u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2**2*(u1 - u2) - sqrt(3)*u2*u3*(2*u1 - 3*u2) - sqrt(3)*u3**3 - u3**2*(u1 - 3*u2))) - (-2*u2*u3*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2**2*(u1 - u2) - sqrt(3)*u2*u3*(2*u1 - 3*u2) - sqrt(3)*u3**3 - u3**2*(u1 - 3*u2)) - (u2**2 - u3**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(sqrt(3)*u2**2*(u1 - u2) + u2*u3*(2*u1 - 3*u2) + u3**3 - sqrt(3)*u3**2*(u1 - 3*u2)))*(-u1*u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)) + u3*(2*u1 - 2*u2)*(-u1*u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2)*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)) - u1*u3**2*(-3*u2**2 + u3**2)*(u3**2 - 3*(u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2))) + u3*(u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) - (-u3**2 + (u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2))))
k2= -(u2**2 - u3**2)*(2*u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) - 2*(-u3**2 + (u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)))*(-u1*u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2)*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(sqrt(3)*u2**2*(u1 - u2) + u2*u3*(2*u1 - 3*u2) + u3**3 - sqrt(3)*u3**2*(u1 - 3*u2)) - u1*u3**2*(-3*u2**2 + u3**2)*(u3**2 - 3*(u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2**2*(u1 - u2) - sqrt(3)*u2*u3*(2*u1 - 3*u2) - sqrt(3)*u3**3 - u3**2*(u1 - 3*u2))) - (-2*u2*u3*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2**2*(u1 - u2) - sqrt(3)*u2*u3*(2*u1 - 3*u2) - sqrt(3)*u3**3 - u3**2*(u1 - 3*u2)) - (u2**2 - u3**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(sqrt(3)*u2**2*(u1 - u2) + u2*u3*(2*u1 - 3*u2) + u3**3 - sqrt(3)*u3**2*(u1 - 3*u2)))*(u1*u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) + u2*(u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) - (-u3**2 + (u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2))) - (-u3**2 + (u1 - u2)**2)*(-u1*u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2)*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)) - u1*u3**2*(-3*u2**2 + u3**2)*(u3**2 - 3*(u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2))))
k3= -u1*u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)) + u3*(2*u1 - 2*u2)*(-u1*u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2)*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)) - u1*u3**2*(-3*u2**2 + u3**2)*(u3**2 - 3*(u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2))) - u3*(u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) - (-u3**2 + (u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)))
k4= u1*u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) - u2*(u3*(2*u1 - 2*u2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))**2*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)) - (-u3**2 + (u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2))) - (-u3**2 + (u1 - u2)**2)*(-u1*u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2)*(u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) + u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(-sqrt(3)*u2*(u1 - u2)**2 - u3**3 - sqrt(3)*u3**2*(2*u1 - 3*u2) + u3*(u1 - 3*u2)*(u1 - u2)) - u1*u3**2*(-3*u2**2 + u3**2)*(u3**2 - 3*(u1 - u2)**2)*(-u2*u3*(-u2**2 + 3*u3**2)*(u3**2 - 3*(u1 - u2)**2) - u3*(u1 - u2)*(-3*u2**2 + u3**2)*(3*u3**2 - (u1 - u2)**2))*(u2*(u1 - u2)**2 - sqrt(3)*u3**3 + u3**2*(2*u1 - 3*u2) + sqrt(3)*u3*(u1 - 3*u2)*(u1 - u2)))
Related
I have been struggling to taylor expand. I have a complicating expression which is of the form: P(x,y)Q(x,y).../(K(x,y)L(x,y)....) where the capital letters are polynomials wrt x and y. If I taylor expand it with respect to x and y, the program does not work.
I would like to taylor expand each factor polynomial individually and then multiply and divide them and do a second taylor expansion hoping that the program will work.
My function is this: D*(-c3*(k**3*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + 2*k**2*p**2 + k*p**3*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta)) + (c1*(k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2) + 2*c2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta))*(k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2))*(-c3*(-2*cos_psi*k**2*p*q - cos_theta*k**3*q - cos_theta*k*q*(-2*cos_psi*p*q + p**2 + q**2) + k**3*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + k**2*p**2 + k**2*q**2 + k**2*(-2*cos_psi*p*q + p**2 + q**2) + k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta)*(-2*cos_psi*p*q + p**2 + q**2))/2 + (c1*(-2*cos_psi*p*q - 2*cos_theta*k*q + k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2 + q**2) - 2*c2*cos_theta*k*q + 2*c2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta))*(-2*cos_psi*p*q - 2*cos_theta*k*q + k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2 + q**2)/2)*(2*k**4*kappa + kappa*(k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2)**2 + kappa*(-2*cos_psi*p*q - 2*cos_theta*k*q + k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2 + q**2)**2)*(c1*q**2/2 + c2*cos_psi*p*q + c2*cos_theta*k*q - c2*k**2 - 2*c2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) - c2*p**2 + c3*cos_psi**2*p**2 + 2*c3*cos_psi*cos_theta*k*p - c3*cos_psi*p*q/2 + c3*cos_theta**2*k**2 - c3*cos_theta*k*q/2 - c3*k**2/2 - c3*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) - c3*p**2/2)/(k**2*kappa*(k**4*kappa + kappa*(k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2)**2)*(k**4*kappa + kappa*(-2*cos_psi*p*q - 2*cos_theta*k*q + k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2 + q**2)**2)*(kappa*(k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2)**2 + kappa*(-2*cos_psi*p*q - 2*cos_theta*k*q + k**2 + 2*k*p*((sin_theta·cos_phi)*sin_psi + cos_psi*cos_theta) + p**2 + q**2)**2))
You can display it using display from IPython.display import display.
I am trying to make a prototype for an algorithm to find the coordinates of a ball on a plate, and I want to make it as efficient as possible because I have to implement it in an FPGA. The pictures of the ball and plate are not always in the same orientation, so I need to shift the coordinates of the center of the ball, based on the coordinates of the corners of the plate.
To get an understanding what I mean, see the image below where the white sheet represents the plate.
Now I have already found a way to determine the coordinates of the ball on the picture, and the coordinates of the corners of the plate in the picture, but I want to find out where the ball is located on the plate.
I tried to do some things with getPerspectiveTransform() and wrapPerspective() and it worked, but this involves a lot of matrix computations and I think this is a bit overkill when I just want to shift the coordinates of one pixel (the center of the ball).
Do you know a more efficient way on how I can determine the coordinates of the center pixel of the ball on the plate?
Opencv's getPerspectiveTransform returns a 3x3 transformation matrix. All warpPerspective does is take the x,y coordinates of each pixel and multiply it with that matrix (augmenting [x,y] -> [x,y,1]).
If you want to just modify just one point P [x,y,1], then given the transformation matrix M, you can transform the point using:
numpy.matmul(M, P);
Here's some example code showing how it works. We make four example points and show that warping them using matmul is equivalent to using warpPerspective.
import cv2
import numpy as np
# test points
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]]);
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]]);
# transformation
M = cv2.getPerspectiveTransform(pts1,pts2);
# if our method works, then transforming each pts1 with M
# should result in pts2
transformed_points = [];
for p in pts1:
# augment point
point = np.array([p[0], p[1], 1], dtype = np.float32);
# multiply
transformed = np.matmul(M, point);
# unpack and clean up
x, y, scale = transformed;
x /= scale;
y /= scale;
x = round(x, 5); # to clear out long floating points
y = round(y, 5);
transformed_points.append([x,y]);
# compare points
for a in range(len(pts2)):
print("Target: " + str(pts2[a]));
print("Transformed: " + str(transformed_points[a]));
I solved it by using the answer of morotspaj of the post below and Matlab.
Calculate a 2D homogeneous perspective transformation matrix from 4 points in MATLAB
ui and vi are already known (these equal the resolution of the picture), so I filled them into the 8x8 matrix and got the following:
/ x0 y0 1 0 0 0 0 0 \ /m00\ / 0 \
| x1 y1 1 0 0 0 -x1*RES_H -y1*RES_H | |m01| |RES_H|
| x2 y2 1 0 0 0 0 0 | |m02| | 0 |
| x3 y3 1 0 0 0 -x3*RES_H -y3*RES_H |.|m10|=|RES_H|
| 0 0 0 x0 y0 1 0 0 | |m11| | 0 |
| 0 0 0 x1 y1 1 0 0 | |m12| | 0 |
| 0 0 0 x2 y2 1 -x2*RES_V -y2*RES_V | |m20| |RES_V|
\ 0 0 0 x3 y3 1 -x3*RES_V -y3*RES_V / \m21/ \RES_V/
Where RES_H = (640 - 1) and RES_V = (480 - 1). This equation can be seen as Ax=b. I putted the A matrix (8x8) and the b vector (8x1) in Matlab and used linsolve(A,b) to solve the linear system:
syms x0 x1 x2 x3 y0 y1 y2 y3 RES_H RES_V;
A = [ x0 y0 1 0 0 0 0 0 ; x1 y1 1 0 0 0 -x1*RES_H -y1*RES_H ; x2 y2 1 0 0 0 0 0 ; x3 y3 1 0 0 0 -x3*RES_H -y3*RES_H ; 0 0 0 x0 y0 1 0 0 ; 0 0 0 x1 y1 1 0 0 ; 0 0 0 x2 y2 1 -x2*RES_V -y2*RES_V ; 0 0 0 x3 y3 1 -x3*RES_V -y3*RES_V ];
b = [ 0 ; RES_H ; 0 ; RES_H ; 0 ; 0 ; RES_V ; RES_V ];
simplify(linsolve(A,B))
The result was the following:
m00 = -(RES_H*(y0 - y2)*(x0*y1 - x1*y0 - x0*y3 + x3*y0 + x1*y3 - x3*y1)*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2))/(- x0*x0*x1*y1*y2*y2 + 2*x0*x0*x1*y1*y2*y3 - x0*x0*x1*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*y3 - x1*x2*x2*y1*y3*y3 - 2*x1*x2*x3*y0*y0*y1 + 2*x1*x2*x3*y0*y0*y2 + 2*x1*x2*x3*y0*y1*y3 - 2*x1*x2*x3*y0*y2*y3 + x1*x3*x3*y0*y0*y1 - 2*x1*x3*x3*y0*y1*y2 + x1*x3*x3*y1*y2*y2 + x2*x2*x3*y0*y0*y3 - 2*x2*x2*x3*y0*y1*y3 + x2*x2*x3*y1*y1*y3 - x2*x3*x3*y0*y0*y2 + 2*x2*x3*x3*y0*y1*y2 - x2*x3*x3*y1*y1*y2);
Using m00, m01, m02, m10, m11 and m12, I constructed an M matrix. I omitted m20, m21 and m22 because they have no influence on the result of x and y.
Finally, I used this M matrix to shift the coordinates of the ball:
x_shifted = m00*x + m01*y + m02;
y_shifted = m10*x + m11*y + m12;
Which is based on this:
As you can see, the solution produced by Matlab results in extremely long equations. By accident, I found out when you swap xi with ui and yi with vi in the A matrix, you will get the inverse of the transformation matrix M for m00, m01, m02, m10, m11, m12, but the coefficients are computed with way shorter equations.
/ 0 0 1 0 0 0 0 0 \ /m00\ /x0\
| RES_H 0 1 0 0 0 -RES_H*x1 0 | |m01| |x1|
| 0 RES_V 1 0 0 0 0 -RES_V*x2 | |m02| |x2|
| RES_H RES_V 1 0 0 0 -RES_H*x3 -RES_V*x3 |.|m10|=|x3|
| 0 0 0 0 0 1 0 0 | |m11| |y0|
| 0 0 0 RES_H 0 1 -RES_H*y1 0 | |m12| |y1|
| 0 0 0 0 RES_V 1 0 -RES_V*y2 | |m20| |y2|
\ 0 0 0 RES_H RES_V 1 -RES_H*y3 -RES_V*y3 / \m21/ \y3/
When you let this solve by Matlab in the same way, you will get:
m00 = (x0*x2*y1 - x1*x2*y0 - x0*x3*y1 + x1*x3*y0 - x0*x2*y3 + x0*x3*y2 + x1*x2*y3 - x1*x3*y2)/(RES_H*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2));
m01 = -(x0*x1*y2 - x1*x2*y0 - x0*x1*y3 + x0*x3*y1 - x0*x3*y2 + x2*x3*y0 + x1*x2*y3 - x2*x3*y1)/(RES_V*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2));
m02 = x0;
m10 = (x0*y1*y2 - x1*y0*y2 - x0*y1*y3 + x1*y0*y3 - x2*y0*y3 + x3*y0*y2 + x2*y1*y3 - x3*y1*y2)/(RES_H*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2));
m11 = -(x0*y1*y2 - x2*y0*y1 - x1*y0*y3 + x3*y0*y1 - x0*y2*y3 + x2*y0*y3 + x1*y2*y3 - x3*y1*y2)/(RES_V*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2));
m12 = y0;
m20 = (x0*y2 - x2*y0 - x0*y3 - x1*y2 + x2*y1 + x3*y0 + x1*y3 - x3*y1)/(RES_H*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2));
m21 = -(x0*y1 - x1*y0 - x0*y3 + x1*y2 - x2*y1 + x3*y0 + x2*y3 - x3*y2)/(RES_V*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2));
m22 = 1.0;
When you put these coefficients into a matrix and invert this matrix, you will get the same results for m00, m01, m02, m10, m11, m12 as for the first method.
I used the program which I found here to invert the matrix:
https://codingtech2017.wordpress.com/2017/05/03/c-program-to-inverse-a-matrix3x3/
I am working with GP and minimum polynomials as follows running on ASUS x75:
(19:25) gp > elt=Mod(a*x^3+b*x^2+c*x+d,('x^5-1)/('x-1))
%122 = Mod(a*x^3 + b*x^2 + c*x + d, x^4 + x^3 + x^2 + x + 1)
(19:25) gp > (poly=minpoly(elt,x='x))
%123 = x^4 + (a + (b + (c - 4*d)))*x^3 + (a^2 + (-3*b + (2*c - 3*d))*a + (b^2 + (2*c - 3*d)*b + (c^2 - 3*d*c + 6*d^2)))*x^2 + (a^3 + (-2*b + (3*c - 2*d))*a^2 + (-2*b^2 + (c + 6*d)*b + (-2*c^2 - 4*d*c + 3*d^2))*a + (b^3 + (-2*c - 2*d)*b^2 + (3*c^2 - 4*d*c + 3*d^2)*b + (c^3 - 2*d*c^2 + 3*d^2*c - 4*d^3)))*x + (a^4 + (-b + (-c - d))*a^3 + (b^2 + (2*c + 2*d)*b + (c^2 - 3*d*c + d^2))*a^2 + (-b^3 + (-3*c + 2*d)*b^2 + (2*c^2 - d*c - 3*d^2)*b + (-c^3 + 2*d*c^2 + 2*d^2*c - d^3))*a + (b^4 + (-c - d)*b^3 + (c^2 + 2*d*c + d^2)*b^2 + (-c^3 - 3*d*c^2 + 2*d^2*c - d^3)*b + (c^4 - d*c^3 + d^2*c^2 - d^3*c + d^4)))
The first command came out successfully, while the second one below did finish successfully and gave an allocatemem() error. How is it possible to get the second command to work, without overheating the computer or program exhaustion? And the WHOLE output to command below this is needed. Thanks for the help.
(19:23) gp > elt=Mod(a*x^5+b*x^4+c*x^3+d*x^2+e*x+f,('x^7-1)/('x-1))
%120 = Mod(a*x^5 + b*x^4 + c*x^3 + d*x^2 + e*x + f, x^6 + x^5 + x^4 + x^3 + x^2 + x + 1)
(19:23) gp > (poly=minpoly(elt,x='x))
*** at top-level: poly=minpoly(elt,x='x)
*** ^-----------------
*** minpoly: the PARI stack overflows !
current stack size: 9000000 (8.583 Mbytes)
[hint] you can increase GP stack with allocatemem()
You can increase the PARI/GP's heap up to any limit you want at run-time following the example below (demonstrates how to set heap size to 120000000 bytes):
default(parisize, 120000000)
default(parisize, 10000000000) is more than 8 GB and in my case was
enough to make advanced calculations with matrices.
I have copied this code from the internet, but it doesn't seem to work.
All it does is draw a red line on one edge of a black picture it makes.
I have been playing with it quite a long time and found that when I print the contents of r1, r2 and r3, it writes:
(0,0)
(0.866025,0)
(-0.866025,0)
So that seems like one thing, that it displays the parts of the complex number in the wrong order, but why does it display r1 to be zero, when it is clearly not?
Also, does it seem to be the cause of this code not working?
GLuint CMyApp::NewtonFractalTexture()
{
const int pic_size = 256;
unsigned char tex[pic_size][pic_size][3];
int MaxCount = 255;
int color_multiplier = 15;
float precision = 0.0001;
std::complex<double> r1 = (1, 0);
std::complex<double> r2 = (-0.5, sin(2 * PI / 3));
std::complex<double> r3 = (-0.5, -sin(2 * PI / 3));
std::cout << r1 << " " << r2 << " " << r3 << std::endl;
std::cout << abs(r1) << " " << abs(r2) << " " << abs(r3) << std::endl;
/*
std::complex<double> roots[birds_num];
for (int i = 0; i < birds_num; ++i){
roots[i] = (bird_positions[i][0], bird_positions[i][2]);
}
*/
for (int i = 0; i < pic_size; ++i){
for (int j = 0; j < pic_size; ++j)
{
//
std::complex<double> z = (i, j);
//
int count = 0;
while (count < MaxCount && abs(z - r1) >= precision && abs(z - r2) >= precision && abs(z - r3) >= precision){
/*
std::complex<double> my_numerator = (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]);
std::complex<double> my_denominator = (z - roots[0])*(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[7])*(z - roots[8]) +
(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8])*(z - roots[8]);
*/
std::complex<double> my_numerator = z*z*z - 1.0;
std::complex<double> my_denominator = z*z * 3.0;
if (abs(z) > 0){
z = z - my_numerator / my_denominator;
}
++count;
}
//
tex[i][j][0] = 0;
tex[i][j][1] = 0;
tex[i][j][2] = 0;
//
if (abs(z - r1) < precision){
tex[i][j][0] = 255 - count * color_multiplier;
}
if (abs(z - r2) <= precision){
tex[i][j][1] = 255 - count * color_multiplier;
}
if (abs(z - r3) <= precision){
tex[i][j][2] = 255 - count * color_multiplier;
}
//
}
}
GLuint tmpID;
// generáljunk egy textúra erőforrás nevet
glGenTextures(1, &tmpID);
// aktiváljuk a most generált nevű textúrát
glBindTexture(GL_TEXTURE_2D, tmpID);
// töltsük fel adatokkal az...
gluBuild2DMipmaps( GL_TEXTURE_2D, // aktív 2D textúrát
GL_RGB8, // a vörös, zöld és kék csatornákat 8-8 biten tárolja a textúra
pic_size, pic_size, // kép méretének megadása
GL_RGB, // a textúra forrása RGB értékeket tárol, ilyen sorrendben
GL_UNSIGNED_BYTE, // egy-egy színkopmonenst egy unsigned byte-ról kell olvasni
tex); // és a textúra adatait a rendszermemória ezen szegletéből töltsük fel
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // bilineáris szűrés kicsinyítéskor
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // és nagyításkor is
glBindTexture(GL_TEXTURE_2D, 0);
return tmpID;
}
You're not initializing your complex numbers right. You either need to use
std::complex<double> r1(1, 0);
or
std::complex<double> r1 {1, 0};
(note the curly brackets here).
r1(1,0) is a constructor call, r1{1,0} is uniform initialization, and r1 = (1,0) is the same as r1 = 0 because the (1,0) is a use of the comma operator, whose value is the last expression.
Here is my data:
> rep$strand
[1] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[58] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + +
[115] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
[172] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
[229] + + + + + + + + + + + + + + + + + + + +
In hopes of separating the "+" from the "-", I tried running the following commands.
grepl("-",rep$strand) #this gives me a list of TRUE/FALSE that seems correct
grepl("+",rep$strand) #this is all TRUE for some mysterious reason
I can't figure out why the same grepl() command would work on "-" but not on "+".
Use
grepl("\\+", rep$strand)
or
grepl("+", rep$strand, fixed = TRUE)
or
"+" == rep$strand