def differential(P, Q): """ Polynomial differential operator. Args: P (Poly) : Polynomial to be differentiated. Q (Poly) : Polynomial to differentiate by. Must be decomposed. If polynomial array, the output is the Jacobian matrix. """ P, Q = Poly(P), Poly(Q) if not poly.is_decomposed(Q): differential(poly.decompose(Q)).sum(0) if Q.shape: return Poly([differential(P, q) for q in Q]) if Q.dim > P.dim: P = poly.setdim(P, Q.dim) else: Q = poly.setdim(Q, P.dim) qkey = Q.keys[0] A = {} for key in P.keys: newkey = np.array(key) - np.array(qkey) if np.any(newkey < 0): continue A[tuple(newkey)] = P.A[key]*np.prod([fac(key[i], \ exact=True)/fac(newkey[i], exact=True) \ for i in range(P.dim)]) return Poly(A, P.dim, None)
def differential(P, Q): """ Polynomial differential operator. Args: P (Poly) : Polynomial to be differentiated. Q (Poly) : Polynomial to differentiate by. Must be decomposed. If polynomial array, the output is the Jacobian matrix. """ P, Q = Poly(P), Poly(Q) if not poly.is_decomposed(Q): differential(poly.decompose(Q)).sum(0) if Q.shape: return Poly([differential(P, q) for q in Q]) if Q.dim>P.dim: P = poly.setdim(P, Q.dim) else: Q = poly.setdim(Q, P.dim) qkey = Q.keys[0] A = {} for key in P.keys: newkey = np.array(key) - np.array(qkey) if np.any(newkey<0): continue A[tuple(newkey)] = P.A[key]*np.prod([fac(key[i], \ exact=True)/fac(newkey[i], exact=True) \ for i in range(P.dim)]) return Poly(A, P.dim, None)
def swapdim(P, dim1=1, dim2=0): """ Swap the dim between two variables. Args: P (Poly) : Input polynomial. dim1 (int) : First dim dim2 (int) : Second dim. Returns: (Poly) : Polynomial with swapped dimensions. Examples -------- >>> x,y = variable(2) >>> P = x**4-y >>> print(P) q0^4-q1 >>> print(swapdim(P)) q1^4-q0 """ if not isinstance(P, Poly): return np.swapaxes(P, dim1, dim2) dim = P.dim shape = P.shape dtype = P.dtype if dim1 == dim2: return P m = max(dim1, dim2) if P.dim <= m: P = poly.setdim(P, m + 1) A = {} for key in P.keys: val = P.A[key] key = list(key) key[dim1], key[dim2] = key[dim2], key[dim1] A[tuple(key)] = val return Poly(A, dim, shape, dtype)
def swapdim(P, dim1=1, dim2=0): """ Swap the dim between two variables. Args: P (Poly) : Input polynomial. dim1 (int) : First dim dim2 (int) : Second dim. Returns: (Poly) : Polynomial with swapped dimensions. Examples -------- >>> x,y = variable(2) >>> P = x**4-y >>> print(P) q0^4-q1 >>> print(swapdim(P)) q1^4-q0 """ if not isinstance(P, Poly): return np.swapaxes(P, dim1, dim2) dim = P.dim shape = P.shape dtype = P.dtype if dim1==dim2: return P m = max(dim1, dim2) if P.dim <= m: P = poly.setdim(P, m+1) A = {} for key in P.keys: val = P.A[key] key = list(key) key[dim1], key[dim2] = key[dim2], key[dim1] A[tuple(key)] = val return Poly(A, dim, shape, dtype)