本文整理汇总了Python中sympy.zeros方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.zeros方法的具体用法?Python sympy.zeros怎么用?Python sympy.zeros使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy
的用法示例。
在下文中一共展示了sympy.zeros方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_equations
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def get_equations(self):
"""
:return: Functions to calculate A, B and f given state x and input u
"""
f = sp.zeros(3, 1)
x = sp.Matrix(sp.symbols('x y theta', real=True))
u = sp.Matrix(sp.symbols('v w', real=True))
f[0, 0] = u[0, 0] * sp.cos(x[2, 0])
f[1, 0] = u[0, 0] * sp.sin(x[2, 0])
f[2, 0] = u[1, 0]
f = sp.simplify(f)
A = sp.simplify(f.jacobian(x))
B = sp.simplify(f.jacobian(u))
f_func = sp.lambdify((x, u), f, 'numpy')
A_func = sp.lambdify((x, u), A, 'numpy')
B_func = sp.lambdify((x, u), B, 'numpy')
return f_func, A_func, B_func
示例2: get_equations
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def get_equations(self):
"""
:return: Functions to calculate A, B and f given state x and input u
"""
f = sp.zeros(6, 1)
x = sp.Matrix(sp.symbols('rx ry vx vy t w', real=True))
u = sp.Matrix(sp.symbols('gimbal T', real=True))
f[0, 0] = x[2, 0]
f[1, 0] = x[3, 0]
f[2, 0] = 1 / self.m * sp.sin(x[4, 0] + u[0, 0]) * u[1, 0]
f[3, 0] = 1 / self.m * (sp.cos(x[4, 0] + u[0, 0]) * u[1, 0] - self.m * self.g)
f[4, 0] = x[5, 0]
f[5, 0] = 1 / self.I * (-sp.sin(u[0, 0]) * u[1, 0] * self.r_T)
f = sp.simplify(f)
A = sp.simplify(f.jacobian(x))
B = sp.simplify(f.jacobian(u))
f_func = sp.lambdify((x, u), f, 'numpy')
A_func = sp.lambdify((x, u), A, 'numpy')
B_func = sp.lambdify((x, u), B, 'numpy')
return f_func, A_func, B_func
示例3: compute_channel_operation
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def compute_channel_operation(rho, operators):
"""
Given a quantum state's density function rho, the effect of the
channel on this state is:
rho -> sum_{i=1}^n E_i * rho * E_i^dagger
Args:
rho (number): Density function
operators (list): List of operators
Returns:
number: The result of applying the list of operators
"""
from sympy import zeros
return sum([E * rho * E.H for E in operators],
zeros(operators[0].rows))
示例4: get_matrix_from_channel
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def get_matrix_from_channel(channel, symbol):
"""
Extract the numeric parameter matrix.
Args:
channel (matrix): a 4x4 symbolic matrix.
symbol (list): a symbol xi
Returns:
matrix: a 4x4 numeric matrix.
Additional Information:
Each entry of the 4x4 symbolic input channel matrix is assumed to
be a polynomial of the form a1x1 + ... + anxn + c. The corresponding
entry in the output numeric matrix is ai.
"""
from sympy import Poly
n = channel.rows
M = numpy.zeros((n, n), dtype=numpy.complex_)
for (i, j) in itertools.product(range(n), range(n)):
M[i, j] = numpy.complex(
Poly(channel[i, j], symbol).coeff_monomial(symbol))
return M
示例5: get_const_matrix_from_channel
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def get_const_matrix_from_channel(channel, symbols):
"""
Extract the numeric constant matrix.
Args:
channel (matrix): a 4x4 symbolic matrix.
symbols (list): The full list [x1, ..., xn] of symbols
used in the matrix.
Returns:
matrix: a 4x4 numeric matrix.
Additional Information:
Each entry of the 4x4 symbolic input channel matrix is assumed to
be a polynomial of the form a1x1 + ... + anxn + c. The corresponding
entry in the output numeric matrix is c.
"""
from sympy import Poly
n = channel.rows
M = numpy.zeros((n, n), dtype=numpy.complex_)
for (i, j) in itertools.product(range(n), range(n)):
M[i, j] = numpy.complex(
Poly(channel[i, j], symbols).coeff_monomial(1))
return M
示例6: compute_P
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def compute_P(self, As):
"""
This method creates the matrix P in the
f(x) = 1/2(x*P*x)+q*x
representation of the objective function
Args:
As (list): list of symbolic matrices repersenting the channel matrices
Returns:
matrix: The matrix P for the description of the quadaric program
"""
from sympy import zeros
vs = [numpy.array(A).flatten() for A in As]
n = len(vs)
P = zeros(n, n)
for (i, j) in itertools.product(range(n), range(n)):
P[i, j] = 2 * numpy.real(numpy.dot(vs[i], numpy.conj(vs[j])))
return P
示例7: print_ode
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def print_ode(self, latex_output=False):
'''
Prints the ode in symbolic form onto the screen/console in actual
symbols rather than the word of the symbol.
Parameters
----------
latex_output: bool, optional
Defaults to false which prints the equation in terms of symbols,
if set to yes then the formula in terms of latex equations will
be printed onto the screen.
'''
A = self.get_ode_eqn()
B = sympy.zeros(A.rows,2)
for i in range(A.shape[0]):
B[i,0] = sympy.symbols('d' + str(self._stateList[i]) + '/dt=')
B[i,1] = A[i]
if latex_output:
print(sympy.latex(B, mat_str="array", mat_delim=None,
inv_trig_style='full'))
else:
sympy.pretty_print(B)
示例8: _computeOdeVector
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def _computeOdeVector(self):
# we are only testing it here because we want to be flexible and
# allow the end user to input more state than initially desired
if len(self.ode_list) <= self.num_state:
self._ode = sympy.zeros(self.num_state, 1)
fromList, _t, eqn = self._unrollTransitionList(self.ode_list)
for i, eqn in enumerate(eqn):
if len(fromList[i]) > 1:
raise InputError("An explicit ode cannot describe more " +
"than a single state")
else:
self._ode[fromList[i][0]] = eqn
else:
raise InputError("The total number of ode is %s " +
"where the number of state is %s" %
len(self.ode_list), self.num_state)
return None
示例9: _computeTransitionVector
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def _computeTransitionVector(self):
"""
Get all the transitions into a vector, arranged by state to
state transition then the birth death processes
"""
self._transitionVector = sympy.zeros(self.num_transitions, 1)
_f, _t, eqn = self._unrollTransitionList(self._getAllTransition())
for i, eqn in enumerate(eqn):
self._transitionVector[i] = eqn
return self._transitionVector
########################################################################
#
# Other type of matrices
#
########################################################################
示例10: _computeTransitionJacobian
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def _computeTransitionJacobian(self):
if self._GMat is None:
self._computeDependencyMatrix()
F = sympy.zeros(self.num_transitions, self.num_transitions)
for i in range(self.num_transitions):
for j, eqn in enumerate(self._transitionVector):
for k, state in enumerate(self._iterStateList()):
diffEqn = sympy.diff(eqn, state, 1)
tempEqn, isDifficult = simplifyEquation(diffEqn)
F[i,j] += tempEqn*self._vMat[k,i]
self._isDifficult = self._isDifficult or isDifficult
self._transitionJacobian = F
self._hasNewTransition.reset('transitionJacobian')
return F
示例11: test_cu3
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def test_cu3():
E = eye(2)
UPPER = Matrix([[1, 0], [0, 0]])
LOWER = Matrix([[0, 0], [0, 1]])
theta, phi, lambd = symbols("theta phi lambd")
U = Circuit().rz(lambd)[0].ry(theta)[0].rz(phi)[0].run_with_sympy_unitary()
actual_1 = Circuit().cu3(theta, phi, lambd)[0, 1].run(backend="sympy_unitary")
expected_1 = reduce(TensorProduct, [E, UPPER]) + reduce(TensorProduct, [U, LOWER])
print("actual")
print(simplify(actual_1))
print("expected")
print(simplify(expected_1))
print("diff")
print(simplify(actual_1 - expected_1))
assert simplify(actual_1 - expected_1) == zeros(4)
示例12: get_equations
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def get_equations(self):
"""
:return: Functions to calculate A, B and f given state x and input u
"""
f = sp.zeros(14, 1)
x = sp.Matrix(sp.symbols('m rx ry rz vx vy vz q0 q1 q2 q3 wx wy wz', real=True))
u = sp.Matrix(sp.symbols('ux uy uz', real=True))
g_I = sp.Matrix(self.g_I)
r_T_B = sp.Matrix(self.r_T_B)
J_B = sp.Matrix(self.J_B)
C_B_I = dir_cosine(x[7:11, 0])
C_I_B = C_B_I.transpose()
f[0, 0] = - self.alpha_m * u.norm()
f[1:4, 0] = x[4:7, 0]
f[4:7, 0] = 1 / x[0, 0] * C_I_B * u + g_I
f[7:11, 0] = 1 / 2 * omega(x[11:14, 0]) * x[7: 11, 0]
f[11:14, 0] = J_B ** -1 * (skew(r_T_B) * u) - skew(x[11:14, 0]) * x[11:14, 0]
f = sp.simplify(f)
A = sp.simplify(f.jacobian(x))
B = sp.simplify(f.jacobian(u))
f_func = sp.lambdify((x, u), f, 'numpy')
A_func = sp.lambdify((x, u), A, 'numpy')
B_func = sp.lambdify((x, u), B, 'numpy')
return f_func, A_func, B_func
示例13: channel_matrix_representation
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def channel_matrix_representation(self, operators):
"""
We convert the operators to a matrix by applying the channel to
the four basis elements of the 2x2 matrix space representing
density operators; this is standard linear algebra
Args:
operators (list): The list of operators to transform into a Matrix
Returns:
sympy.Matrix: The matrx representation of the operators
"""
from sympy import Matrix, zeros
shape = operators[0].shape
standard_base = []
for i in range(shape[0]):
for j in range(shape[1]):
basis_element_ij = zeros(*shape)
basis_element_ij[(i, j)] = 1
standard_base.append(basis_element_ij)
return (Matrix([
self.flatten_matrix(
self.compute_channel_operation(rho, operators))
for rho in standard_base
]))
示例14: get_equations
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def get_equations(self):
"""
:return: Functions to calculate A, B and f given state x and input u
"""
f = sp.zeros(14, 1)
x = sp.Matrix(sp.symbols('m rx ry rz vx vy vz q0 q1 q2 q3 wx wy wz', real=True))
u = sp.Matrix(sp.symbols('ux uy uz', real=True))
g_I = sp.Matrix(self.g_I)
r_T_B = sp.Matrix(self.r_T_B)
J_B = sp.Matrix(self.J_B)
C_B_I = dir_cosine(x[7:11, 0])
C_I_B = C_B_I.transpose()
f[0, 0] = - self.alpha_m * u.norm()
f[1:4, 0] = x[4:7, 0]
f[4:7, 0] = 1 / x[0, 0] * C_I_B * u + g_I
f[7:11, 0] = 1 / 2 * omega(x[11:14, 0]) * x[7: 11, 0]
f[11:14, 0] = J_B ** -1 * (skew(r_T_B) * u - skew(x[11:14, 0]) * J_B * x[11:14, 0])
f = sp.simplify(f)
A = sp.simplify(f.jacobian(x))
B = sp.simplify(f.jacobian(u))
f_func = sp.lambdify((x, u), f, 'numpy')
A_func = sp.lambdify((x, u), A, 'numpy')
B_func = sp.lambdify((x, u), B, 'numpy')
return f_func, A_func, B_func
示例15: get_grad_eqn
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import zeros [as 别名]
def get_grad_eqn(self):
'''
Return the gradient of the ode in algebraic form
Returns
-------
:class:`sympy.matrices.matrices`
A matrix of dimension [number of state x number of parameters]
'''
# finds
if self._Grad is None:
ode = self.get_ode_eqn()
self._Grad = sympy.zeros(self.num_state, self.num_param)
for i in range(self.num_state):
# need to adjust such that the first index is not
# included because it correspond to time
for j, p in enumerate(self._iterParamList()):
eqn, isDifficult = simplifyEquation(diff(ode[i], p, 1))
self._Grad[i,j] = eqn
self._isDifficult = self._isDifficult or isDifficult
if self._isDifficult:
self._GradCompile = self._SC.compileExprAndFormat(self._sp,
self._Grad,
modules='mpmath',
outType="mat")
else:
self._GradCompile = self._SC.compileExprAndFormat(self._sp,
self._Grad,
outType="mat")
self._hasNewTransition.reset('grad')
return self._Grad