Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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:
poly (Poly) : Polynomial to be differentiated.
diffvar (Poly) : Polynomial to differentiate by. Must be decomposed. If
polynomial array, the output is the Jacobian matrix.
Examples:
>>> q0, q1 = chaospy.variable(2)
>>> poly = chaospy.Poly([1, q0, q0*q1**2+1])
>>> print(poly)
[1, q0, q0q1^2+1]
>>> print(differential(poly, q0))
[0, 1, q1^2]
>>> print(differential(poly, q1))
[0, 0, 2q0q1]
"""
poly = Poly(poly)
diffvar = Poly(diffvar)
if not chaospy.poly.caller.is_decomposed(diffvar):
sum(differential(poly, chaospy.poly.caller.decompose(diffvar)))
if diffvar.shape:
return Poly([differential(poly, pol) for pol in diffvar])
if diffvar.dim > poly.dim:
poly = chaospy.poly.dimension.setdim(poly, diffvar.dim)
else:
diffvar = chaospy.poly.dimension.setdim(diffvar, poly.dim)
qkey = diffvar.keys[0]
core = {}
for key in poly.keys:
key[d], zeros[d] = zeros[d], key[d]
break
tmp = a*mom[tuple(key)]
if tuple(zeros) in out:
out[tuple(zeros)] = out[tuple(zeros)] + tmp
else:
out[tuple(zeros)] = tmp
for d in xrange(poly.dim):
for j in xrange(len(freeze)):
if freeze[j, d]:
key[d], zeros[d] = zeros[d], key[d]
break
out = chaospy.poly.Poly(out, poly.dim, poly.shape, float)
out = chaospy.poly.reshape(out, shape)
return out
above = np.sum(indices, -1)<=stop.item()
else:
stop = np.ones(dim, dtype=int)*stop
above = np.all(stop-indices>=0, -1)
pool = list(indices[above*bellow])
x = np.zeros(len(pool), dtype=int)
x[0] = 1
A = {}
for I in pool:
I = tuple(I)
A[I] = x
x = np.roll(x,1)
return Poly(A, dim)
# orthogonalize polynomial:
for idy in range(idx):
orth = chaospy.descriptives.E(
basis[idx]*polynomials[idy], dist, **kws)
basis[idx] = basis[idx] - polynomials[idy] * orth / norms[idy]
norms.append(
chaospy.descriptives.E(polynomials[-1]**2, dist, **kws))
if norms[-1] <= 0:
logger.warning("Warning: Polynomial cutoff at term %d", idx)
break
polynomials.append(basis[idx])
return chaospy.poly.Poly(polynomials, dim=dim, shape=(len(polynomials),))
>>> poly = chaospy.Poly([1, q0, q0*q1**2+1])
>>> print(poly)
[1, q0, q0q1^2+1]
>>> print(differential(poly, q0))
[0, 1, q1^2]
>>> print(differential(poly, q1))
[0, 0, 2q0q1]
"""
poly = Poly(poly)
diffvar = Poly(diffvar)
if not chaospy.poly.caller.is_decomposed(diffvar):
sum(differential(poly, chaospy.poly.caller.decompose(diffvar)))
if diffvar.shape:
return Poly([differential(poly, pol) for pol in diffvar])
if diffvar.dim > poly.dim:
poly = chaospy.poly.dimension.setdim(poly, diffvar.dim)
else:
diffvar = chaospy.poly.dimension.setdim(diffvar, poly.dim)
qkey = diffvar.keys[0]
core = {}
for key in poly.keys:
newkey = np.array(key) - np.array(qkey)
if np.any(newkey < 0):
continue
newkey = tuple(newkey)
>>> Z = cp.MvNormal([3, 4], [[2, .5], [.5, 1]])
>>> print(cp.Corr(Z))
[[ 1. 0.35355339]
[ 0.35355339 1. ]]
>>> x = cp.variable()
>>> Z = cp.Normal()
>>> print(cp.Corr([x, x**2], Z))
[[ 1. 0.]
[ 0. 1.]]
"""
if isinstance(poly, chaospy.dist.Dist):
x = chaospy.poly.variable(len(poly))
poly, dist = x, poly
else:
poly = chaospy.poly.Poly(poly)
C = Cov(poly, dist, **kws)
V = np.diag(C)
S = np.sqrt(np.outer(V, V))
return np.where(S>0, C/S, 0)
[ 0.5 1. ]]
>>> x = cp.variable()
>>> Z = cp.Normal()
>>> print(cp.Cov([x, x**2], Z))
[[ 1. 0.]
[ 0. 2.]]
"""
if not isinstance(poly, (chaospy.dist.Dist, chaospy.poly.Poly)):
poly = chaospy.poly.Poly(poly)
if isinstance(poly, chaospy.dist.Dist):
x = chaospy.poly.variable(len(poly))
poly, dist = x, poly
else:
poly = chaospy.poly.Poly(poly)
dim = len(dist)
shape = poly.shape
poly = chaospy.poly.flatten(poly)
keys = poly.keys
N = len(keys)
A = poly.A
keys1 = np.array(keys).T
if dim==1:
keys1 = keys1[0]
keys2 = sum(np.meshgrid(keys, keys))
else:
keys2 = np.empty((dim, N, N))
for i in xrange(N):
for j in xrange(N):
keys2[:, i, j] = keys1[:, i]+keys1[:, j]
def E_cond(poly, freeze, dist, **kws):
assert not dist.dependent()
if poly.dim