sympy: How do I taylor expand each factor of an expression? - sympy

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.

Related

How to represent the elements of the Galois filed GF(2^8) and perform arithmetic in NTL library

I am new to NTL library for its GF2X, GF2E, GF2EX, etc. Now, I want to perform multiplication on the Galois field GF(2^8). The problem is as following:
Rijndael (standardised as AES) uses the characteristic 2 finite field with 256 elements,
which can also be called the Galois field GF(2^8).
It employs the following reducing polynomial for multiplication:
x^8 + x^4 + x^3 + x^1 + 1.
For example, {53} • {CA} = {01} in Rijndael's field because
(x^6 + x^4 + x + 1)(x^7 + x^6 + x^3 + x)
= (x^13 + x^12 + x^9 + x^7) + (x^11 + x^10 + x^7 + x^5) + (x^8 + x^7 + x^4 + x^2) + (x^7 + x^6 + x^3 + x)
= x^13 + x^12 + x^9 + x^11 + x^10 + x^5 + x^8 + x^4 + x^2 + x^6 + x^3 + x
= x^13 + x^12 + x^11 + x^10 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + x^2 + x
and
x^13 + x^12 + x^11 + x^10 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + x^2 + x modulo x^8 + x^4 + x^3 + x^1 + 1
= (11111101111110 mod 100011011)
= {3F7E mod 11B} = {01}
= 1 (decimal)
My question is how to represent the reducing polynomial x^8 + x^4 + x^3 + x^1 + 1 and the polynomials x^6 + x^4 + x + 1 and x^7 + x^6 + x^3 + x in NTL. Then perform multiplication on these polynomials, and get the result {01}.
This is a good example for me to use this library.
Again, I don't know NTL, and I'm running Visual Studio 2015 on Windows 7. I've downloaded what I need, but have to build a library with all the supplied source files which will take a while to figure out. However, based on another answer, this should get you started. First, initialize the reducing polynomial for GF(256):
GF2X P; // apparently the length doesn't need to be set
SetCoeff(P, 0, 1);
SetCoeff(P, 1, 1);
SetCoeff(P, 3, 1);
SetCoeff(P, 4, 1);
SetCoeff(P, 8, 1);
GF2E::init(P);
Next, assign variables as polynomials:
GF2X A;
SetCoeff(A, 0, 1);
SetCoeff(A, 1, 1);
SetCoeff(A, 4, 1);
SetCoeff(A, 6, 1);
GF2X B;
SetCoeff(B, 1, 1);
SetCoeff(B, 3, 1);
SetCoeff(B, 6, 1);
SetCoeff(B, 7, 1);
GF2X C;
Looks like there is an override for multiply so this would work assuming that the multiply override is based on the GF(2^8) extension field GF2E::init(P).
C = A * B:
As commented after the question, NTL is more oriented to large fields. For GF(256) it would be faster to use bytes and lookup tables. For up to GF(2^64), xmm register intrinsics with carryless multiply (PCLMULQDQ) can be used to implement finite field math quickly without tables (some constants will be needed, the polynomial and it's multiplicative inverse). For fields greater than GF(2^64), extended precision math methods would be needed. For fields GF(p^n), where p != 2 and n > 1, unsigned integers can be used with lookup tables. Building the tables would involve some mapping between integers and GF(p) polynomial coefficients.

Shifting the coordinates of one pixel

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*y1*y3*y3 + x0*x0*x2*y1*y1*y2 - 2*x0*x0*x2*y1*y2*y3 + x0*x0*x2*y2*y3*y3 - 2*x0*x0*x3*y1*y1*y2 + x0*x0*x3*y1*y1*y3 + 2*x0*x0*x3*y1*y2*y2 - x0*x0*x3*y2*y2*y3 + x0*x1*x1*y0*y2*y2 - 2*x0*x1*x1*y0*y2*y3 + x0*x1*x1*y0*y3*y3 - 2*x0*x1*x2*y0*y1*y3 + 2*x0*x1*x2*y0*y2*y3 + 2*x0*x1*x2*y1*y3*y3 - 2*x0*x1*x2*y2*y3*y3 + 2*x0*x1*x3*y0*y1*y2 - 2*x0*x1*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21 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/

Why can't the subs function of the sympy replace the value of the symbol sometimes?

I have the following code:
u_ini = 0.1
v_ini = 0.1
z_ini = 0.1 # 初始化三个拉格朗日乘子
q = 0
lis = list(range(2))
u = list(sp.symbols('u:{}'.format(len(lis))))
v = list(sp.symbols('v:{}'.format(len(lis))))
z = sp.symbols('z')
p = list(sp.symbols('p:{}'.format(len(lis))))
lag1 = 0
lag2 = 0
lag3 = 0
p_symbol_sum = np.sum(p)
for i in range(k):
if i < k-1:
lag1 += B*ts_ratio[i]*sp.log(1+g[i]*p[i]/(sgm_2+g[i]*np.sum(p[i+1:k])),2)-q*(af_eff*p[i]+Pc-eh_eff*(1-ts_ratio[i])*g[i]*p_symbol_sum)
lag2 -= u[i] * (R_min - ts_ratio[i] * B * sp.log(1 + g[i] * p[i] / (sgm_2 + g[i] * np.sum(p[i + 1:k])),2))
elif i == k-1:
lag1 += B*ts_ratio[i]*sp.log(1+g[i]*p[i]/(sgm_2+g[i]*p[i]),2)-q*(af_eff*p[i]+Pc-eh_eff*(1-ts_ratio[i])*g[i]*p_symbol_sum)
lag2 -= u[i] * (R_min - ts_ratio[i] * B * sp.log(1+g[i]*p[i]/(sgm_2+g[i]*p[i]),2))
lag3 -= v[i] * (E_min - (1 - ts_ratio[i])*eh_eff*g[i]*p_symbol_sum) + z * (p[i] - p_max)
lag_fun = lag1 + lag2 + lag3
print("lag_fun:",lag_fun)
for i in range(k):
lag_fun.subs([(u[i],u_ini), (v[i],v_ini), (z,z_ini), (p[i],p_ini)]).evalf()
print("lag_fun:",lag_fun)
Why does the value of the expression not change after I count down the subs of the second line。
This is the output of the program. The first line is the output before using subs. The second is the output after using subs. Why hasn't it changed?
lag_fun: -u0*(-0.5*log(0.0410609879149758*p0/(0.0410609879149758*p1 + 0.001) + 1)/log(2) + 2) - u1*(-0.5*log(0.0123909311217172*p1/(0.0123909311217172*p1 + 0.001) + 1)/log(2) + 2) - v0*(-0.00205304939574879*p0 - 0.00205304939574879*p1 + 0.2) - v1*(-0.000619546556085859*p0 - 0.000619546556085859*p1 + 0.2) - z*(p0 - 20) - z*(p1 - 20) + 0.5*log(0.0410609879149758*p0/(0.0410609879149758*p1 + 0.001) + 1)/log(2) + 0.5*log(0.0123909311217172*p1/(0.0123909311217172*p1 + 0.001) + 1)/log(2)
lag_fun: -u0*(-0.5*log(0.0410609879149758*p0/(0.0410609879149758*p1 + 0.001) + 1)/log(2) + 2) - u1*(-0.5*log(0.0123909311217172*p1/(0.0123909311217172*p1 + 0.001) + 1)/log(2) + 2) - v0*(-0.00205304939574879*p0 - 0.00205304939574879*p1 + 0.2) - v1*(-0.000619546556085859*p0 - 0.000619546556085859*p1 + 0.2) - z*(p0 - 20) - z*(p1 - 20) + 0.5*log(0.0410609879149758*p0/(0.0410609879149758*p1 + 0.001) + 1)/log(2) + 0.5*log(0.0123909311217172*p1/(0.0123909311217172*p1 + 0.001) + 1)/log(2)
subs doesn't change anything in place, you have to capture the result for the same reason that this loop fails to change x:
>>> x = 0
>>> for i in range(10): x + 1
>>> x
0
So it must be
lag_fun = lag_fun.subs(etc...)

How to increase enough stack size in Pari/Gp for command to work

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.

R: cannot grep() on "+" character?

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