本文整理汇总了Python中sympy.Matrix.simplify方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.simplify方法的具体用法?Python Matrix.simplify怎么用?Python Matrix.simplify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.Matrix
的用法示例。
在下文中一共展示了Matrix.simplify方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_symbolic_twoport
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import simplify [as 别名]
def test_symbolic_twoport():
circuit.default_toolkit = symbolic
cir = SubCircuit()
k = symbolic.kboltzmann
var('R1 R0 C1 w T', real=True, positive=True)
s = 1j*w
cir['R0'] = R(1, gnd, r=R0)
cir['R1'] = R(1, 2, r=R1)
# cir['C1'] = C(2, gnd, c=C1)
## Add an AC source to verify that the source will not affect results
# cir['IS'] = IS(1, gnd, iac=1)
## Run symbolic 2-port analysis
twoport_ana = TwoPortAnalysis(cir, Node('1'), gnd, Node('2'), gnd,
noise = True, toolkit=symbolic,
noise_outquantity = 'v')
result = twoport_ana.solve(freqs=s, complexfreq=True)
ABCD = Matrix(result['twoport'].A)
ABCD.simplify()
assert_array_equal(ABCD, np.array([[1 + 0*R1*C1*s, R1],
[(1 + 0*R0*C1*s + 0*R1*C1*s) / R0, (R0 + R1)/R0]]))
assert_array_equal(simplify(result['Sin'] - (4*k*T/R0 + 4*R1*k*T/R0**2)), 0)
assert_array_equal(simplify(result['Svn']), 4*k*T*R1)
示例2: _mat_inv_mul
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import simplify [as 别名]
def _mat_inv_mul(self, A, B):
"""Internal Function
Computes A^-1 * B symbolically w/ substitution, where B is not
necessarily a vector, but can be a matrix.
"""
# Note: investigate difficulty in only creating symbols for non-zero
# entries; this could speed things up, perhaps?
r1, c1 = A.shape
r2, c2 = B.shape
temp1 = Matrix(r1, c1, lambda i, j: Symbol('x' + str(j + r1 * i)))
temp2 = Matrix(r2, c2, lambda i, j: Symbol('y' + str(j + r2 * i)))
for i in range(len(temp1)):
if A[i] == 0:
temp1[i] = 0
for i in range(len(temp2)):
if B[i] == 0:
temp2[i] = 0
temp3 = []
for i in range(c2):
temp3.append(temp1.LUsolve(temp2.extract(range(r2), [i])))
temp3 = Matrix([i.T for i in temp3]).T
if Kane.simp == True:
temp3.simplify()
return temp3.subs(dict(zip(temp1, A))).subs(dict(zip(temp2, B)))
示例3: test_simplify
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import simplify [as 别名]
def test_simplify():
x,y,f,n = symbols('xyfn')
M = Matrix([ [ 1/x + 1/y, (x + x*y)/ x ],
[(f(x) + y*f(x))/f(x), 2 * (1/n - cos(n * pi)/n)/ pi ]
])
M.simplify()
assert M == Matrix([[(x + y)/(x * y), 1 + y ],
[ 1 + y, (2 - 2*cos(pi*n))/(pi*n) ]])
示例4: test_simplify
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import simplify [as 别名]
def test_simplify():
x,y,f,n = symbols('x y f n')
M = Matrix([ [ 1/x + 1/y, (x + x*y)/ x ],
[(f(x) + y*f(x))/f(x), 2 * (1/n - cos(n * pi)/n)/ pi ]
])
M.simplify()
assert M == Matrix([[(x + y)/(x * y), 1 + y ],
[ 1 + y, 2*((1 - 1*cos(pi*n))/(pi*n)) ]])
M = Matrix([[(1 + x)**2]])
M.simplify()
assert M == Matrix([[(1 + x)**2]])
M.simplify(ratio=oo)
assert M == Matrix([[1 + 2*x + x**2]])
示例5: test_linearize_pendulum_kane_nonminimal
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import simplify [as 别名]
def test_linearize_pendulum_kane_nonminimal():
# Create generalized coordinates and speeds for this non-minimal realization
# q1, q2 = N.x and N.y coordinates of pendulum
# u1, u2 = N.x and N.y velocities of pendulum
q1, q2 = dynamicsymbols('q1:3')
q1d, q2d = dynamicsymbols('q1:3', level=1)
u1, u2 = dynamicsymbols('u1:3')
u1d, u2d = dynamicsymbols('u1:3', level=1)
L, m, t = symbols('L, m, t')
g = 9.8
# Compose world frame
N = ReferenceFrame('N')
pN = Point('N*')
pN.set_vel(N, 0)
# A.x is along the pendulum
theta1 = atan(q2/q1)
A = N.orientnew('A', 'axis', [theta1, N.z])
# Locate the pendulum mass
P = pN.locatenew('P1', q1*N.x + q2*N.y)
pP = Particle('pP', P, m)
# Calculate the kinematic differential equations
kde = Matrix([q1d - u1,
q2d - u2])
dq_dict = solve(kde, [q1d, q2d])
# Set velocity of point P
P.set_vel(N, P.pos_from(pN).dt(N).subs(dq_dict))
# Configuration constraint is length of pendulum
f_c = Matrix([P.pos_from(pN).magnitude() - L])
# Velocity constraint is that the velocity in the A.x direction is
# always zero (the pendulum is never getting longer).
f_v = Matrix([P.vel(N).express(A).dot(A.x)])
f_v.simplify()
# Acceleration constraints is the time derivative of the velocity constraint
f_a = f_v.diff(t)
f_a.simplify()
# Input the force resultant at P
R = m*g*N.x
# Derive the equations of motion using the KanesMethod class.
KM = KanesMethod(N, q_ind=[q2], u_ind=[u2], q_dependent=[q1],
u_dependent=[u1], configuration_constraints=f_c,
velocity_constraints=f_v, acceleration_constraints=f_a, kd_eqs=kde)
(fr, frstar) = KM.kanes_equations([(P, R)], [pP])
# Set the operating point to be straight down, and non-moving
q_op = {q1: L, q2: 0}
u_op = {u1: 0, u2: 0}
ud_op = {u1d: 0, u2d: 0}
A, B, inp_vec = KM.linearize(op_point=[q_op, u_op, ud_op], A_and_B=True,
new_method=True, simplify=True)
assert A == Matrix([[0, 1], [-9.8/L, 0]])
assert B == Matrix([])
示例6: linearize
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import simplify [as 别名]
def linearize(self, op_point=None, A_and_B=False, simplify=False):
"""Linearize the system about the operating point. Note that
q_op, u_op, qd_op, ud_op must satisfy the equations of motion.
These may be either symbolic or numeric.
Parameters
----------
op_point : dict or iterable of dicts, optional
Dictionary or iterable of dictionaries containing the operating
point conditions. These will be substituted in to the linearized
system before the linearization is complete. Leave blank if you
want a completely symbolic form. Note that any reduction in
symbols (whether substituted for numbers or expressions with a
common parameter) will result in faster runtime.
A_and_B : bool, optional
If A_and_B=False (default), (M, A, B) is returned for forming
[M]*[q, u]^T = [A]*[q_ind, u_ind]^T + [B]r. If A_and_B=True,
(A, B) is returned for forming dx = [A]x + [B]r, where
x = [q_ind, u_ind]^T.
simplify : bool, optional
Determines if returned values are simplified before return.
For large expressions this may be time consuming. Default is False.
Note that the process of solving with A_and_B=True is computationally
intensive if there are many symbolic parameters. For this reason,
it may be more desirable to use the default A_and_B=False,
returning M, A, and B. More values may then be substituted in to these
matrices later on. The state space form can then be found as
A = P.T*M.LUsolve(A), B = P.T*M.LUsolve(B), where
P = Linearizer.perm_mat.
"""
# Compose dict of operating conditions
if isinstance(op_point, dict):
op_point_dict = op_point
elif isinstance(op_point, collections.Iterable):
op_point_dict = {}
for op in op_point:
op_point_dict.update(op)
else:
op_point_dict = {}
# Extract dimension variables
l, m, n, o, s, k = self._dims
# Rename terms to shorten expressions
M_qq = self._M_qq
M_uqc = self._M_uqc
M_uqd = self._M_uqd
M_uuc = self._M_uuc
M_uud = self._M_uud
M_uld = self._M_uld
A_qq = self._A_qq
A_uqc = self._A_uqc
A_uqd = self._A_uqd
A_qu = self._A_qu
A_uuc = self._A_uuc
A_uud = self._A_uud
B_u = self._B_u
C_0 = self._C_0
C_1 = self._C_1
C_2 = self._C_2
# Build up Mass Matrix
# |M_qq 0_nxo 0_nxk|
# M = |M_uqc M_uuc 0_mxk|
# |M_uqd M_uud M_uld|
if o != 0:
col2 = Matrix([zeros(n, o), M_uuc, M_uud])
if k != 0:
col3 = Matrix([zeros(n + m, k), M_uld])
if n != 0:
col1 = Matrix([M_qq, M_uqc, M_uqd])
if o != 0 and k != 0:
M = col1.row_join(col2).row_join(col3)
elif o != 0:
M = col1.row_join(col2)
else:
M = col1
elif k != 0:
M = col2.row_join(col3)
else:
M = col2
M_eq = _subs_keep_derivs(M, op_point_dict)
# Build up state coefficient matrix A
# |(A_qq + A_qu*C_1)*C_0 A_qu*C_2|
# A = |(A_uqc + A_uuc*C_1)*C_0 A_uuc*C_2|
# |(A_uqd + A_uud*C_1)*C_0 A_uud*C_2|
# Col 1 is only defined if n != 0
if n != 0:
r1c1 = A_qq
if o != 0:
r1c1 += (A_qu * C_1)
r1c1 = r1c1 * C_0
if m != 0:
r2c1 = A_uqc
if o != 0:
#.........这里部分代码省略.........