本文整理汇总了Python中sympy.Matrix.norm方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.norm方法的具体用法?Python Matrix.norm怎么用?Python Matrix.norm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.Matrix
的用法示例。
在下文中一共展示了Matrix.norm方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spin_vector_to_state
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
def spin_vector_to_state(spin):
from sympy import Matrix
from sglib_v0 import sigma_x, sigma_y, sigma_z, find_eigenvectors
from numpy import shape
# The spin vector must have three components.
if shape(spin) == () or len(spin) != 3:
print('Error: spin vector must have three elements.')
return
# Make sure the values are floats and make sure it's really
# a column vector.
spin = [ float(spin[0]), float(spin[1]), float(spin[2]) ]
spin = Matrix(spin)
# Make sure its normalized. Do this silently, since it will
# probably happen most of the time due to imprecision.
if spin.norm() != 1.0: spin = spin/spin.norm()
# Calculate the measurement operator as a weighted combination
# of the three Pauli matrices.
O_1 = spin[0]*sigma_x + spin[1]*sigma_y + spin[2]*sigma_z
O_1.simplify()
# the eigenvectors will be the two possible states. The
# "minus" state will be the first one.
evals, evecs = find_eigenvectors(O_1)
plus_state = evecs[1]
minus_state = evecs[0]
return(spin, plus_state, minus_state)
示例2: cal
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
def cal(self):
global X,Y,Z,Gauss_Curvature
xu=Matrix([diff(f,u) for f in self.S])
xv=Matrix([diff(f,v) for f in self.S])
xuu=Matrix([diff(f,u) for f in xu])
xvv=Matrix([diff(f,v) for f in xv])
xuv=Matrix([diff(f,v) for f in xu])
E=simplify(xu.dot(xu))
G=simplify(xv.dot(xv))
F=simplify(xu.dot(xv))
H1=Matrix([xuu,xu,xv]).reshape(3,3)
H2=Matrix([xvv,xu,xv]).reshape(3,3)
H3=Matrix([xuv,xu,xv]).reshape(3,3)
K=simplify(((H1.det()*H2.det() -(H3.det()**2)))/ (xu.norm()**2*xv.norm()**2 -F*F)**2)
#Pass to numpy
du=float(self.umax-self.umin)/100
dv=float(self.vmax-self.vmin)/100
[U,V] = np.mgrid[self.umin:self.umax+du:du,self.vmin:self.vmax+dv:dv]
# convert Sympy formula to numpy lambda functions
F1=lambdify((u,v), self.S[0], "numpy")
F2=lambdify((u,v), self.S[1], "numpy")
F3=lambdify((u,v), self.S[2], "numpy")
F4=lambdify((u,v), K, "numpy")
#Calculate numpy arrays
self.X=F1(U,V)
self.Y=F2(U,V)
self.Z=F3(U,V)
self.Gauss_Curvature=F4(U,V)
示例3: pLaplace_modes
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
def pLaplace_modes(*args, **kwargs):
"""Returns 2-tuple of DOLFIN Expressions initialized with *args and
**kwargs passed in and solving zero BC p-Laplace problem on unit square
as solution and corresponding right-hand side.
Mandatory kwargs:
kwargs['p'] > 1.0 ... Lebesgue exponent
kwargs['eps'] >= 0.0 ... amount of regularization
kwargs['n'] uint ... x-mode
kwargs['m'] uint ... y-mode
"""
p = Symbol('p', positive=True, constant=True)
eps = Symbol('eps', nonnegative=True, constant=True)
n = Symbol('n', integer=True, positive=True, constant=True)
m = Symbol('m', integer=True, positive=True, constant=True)
x = symbols('x[0] x[1]', real=True)
dim = len(x)
u = sin(n*pi*x[0])*sin(m*pi*x[1])
Du = Matrix([diff(u, x[j]) for j in xrange(dim)])
q = (eps + Du.norm(2)**2)**(p/2-1) * Du
f = -sum(diff(q[j], x[j]) for j in xrange(dim))
u_code = ccode(u)
# Prevent division by zero
f_code = "x[0]*x[0] + x[1]*x[1] < 1e-308 ? 0.0 : \n" + ccode(f)
return [Expression(u_code, *args, **kwargs),
Expression(f_code, *args, **kwargs)]
示例4: test_util
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
def test_util():
v1 = Matrix(1,3,[1,2,3])
v2 = Matrix(1,3,[3,4,5])
assert v1.cross(v2) == Matrix(1,3,[-2,4,-2])
assert v1.norm() == sqrt(14)
# cofactor
assert eye(3) == eye(3).cofactorMatrix()
test = Matrix([[1,3,2],[2,6,3],[2,3,6]])
assert test.cofactorMatrix() == Matrix([[27,-6,-6],[-12,2,3],[-3,1,0]])
test = Matrix([[1,2,3],[4,5,6],[7,8,9]])
assert test.cofactorMatrix() == Matrix([[-3,6,-3],[6,-12,6],[-3,6,-3]])
示例5: pLaplace_CarstensenKlose
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
def pLaplace_CarstensenKlose(*args, **kwargs):
p = Symbol('p', positive=True, constant=True)
eps = Symbol('eps', nonnegative=True, constant=True)
delta = Symbol('delta', positive=True, constant=True)
x = symbols('x[0] x[1]', real=True)
r = sqrt(x[0]**2 + x[1]**2)
theta = pi + atan2(-x[1], -x[0])
u = r**delta * sin(delta*theta)
dim = len(x)
Du = Matrix([diff(u, x[j]) for j in xrange(dim)])
q = (eps + Du.norm(2)**2)**(p/2-1) * Du
f = -sum(diff(q[j], x[j]) for j in xrange(dim))
u_code = ccode(u)
f_code = ccode(f)
# Use Quadrature element as f is infinite at r=0
kwargs_f = kwargs.copy()
kwargs_f['element'] = FiniteElement('Quadrature',
kwargs_f['domain'].ufl_cell(),
kwargs_f.pop('degree'),
quad_scheme='default')
return [Expression(u_code, *args, **kwargs),
Expression(f_code, *args, **kwargs_f)]
示例6: test_sparse_matrix
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
#.........这里部分代码省略.........
# test_LUsolve
A = SMatrix([[2,3,5],
[3,6,2],
[8,3,6]])
x = SMatrix(3,1,[3,7,5])
b = A*x
soln = A.LUsolve(b)
assert soln == x
A = SMatrix([[0,-1,2],
[5,10,7],
[8,3,4]])
x = SMatrix(3,1,[-1,2,5])
b = A*x
soln = A.LUsolve(b)
assert soln == x
# test_inverse
A = eye(4)
assert A.inv() == eye(4)
assert A.inv("LU") == eye(4)
assert A.inv("ADJ") == eye(4)
A = SMatrix([[2,3,5],
[3,6,2],
[8,3,6]])
Ainv = A.inv()
assert A*Ainv == eye(3)
assert A.inv("LU") == Ainv
assert A.inv("ADJ") == Ainv
# test_cross
v1 = Matrix(1,3,[1,2,3])
v2 = Matrix(1,3,[3,4,5])
assert v1.cross(v2) == Matrix(1,3,[-2,4,-2])
assert v1.norm(v1) == 14
# test_cofactor
assert eye(3) == eye(3).cofactorMatrix()
test = SMatrix([[1,3,2],[2,6,3],[2,3,6]])
assert test.cofactorMatrix() == SMatrix([[27,-6,-6],[-12,2,3],[-3,1,0]])
test = SMatrix([[1,2,3],[4,5,6],[7,8,9]])
assert test.cofactorMatrix() == SMatrix([[-3,6,-3],[6,-12,6],[-3,6,-3]])
# test_jacobian
x = Symbol('x')
y = Symbol('y')
L = SMatrix(1,2,[x**2*y, 2*y**2 + x*y])
syms = [x,y]
assert L.jacobian(syms) == Matrix([[2*x*y, x**2],[y, 4*y+x]])
L = SMatrix(1,2,[x, x**2*y**3])
assert L.jacobian(syms) == SMatrix([[1, 0], [2*x*y**3, x**2*3*y**2]])
# test_QR
A = Matrix([[1,2],[2,3]])
Q, S = A.QRdecomposition()
R = Rational
assert Q == Matrix([[5**R(-1,2), (R(2)/5)*(R(1)/5)**R(-1,2)], [2*5**R(-1,2), (-R(1)/5)*(R(1)/5)**R(-1,2)]])
assert S == Matrix([[5**R(1,2), 8*5**R(-1,2)], [0, (R(1)/5)**R(1,2)]])
assert Q*S == A
assert Q.T * Q == eye(2)
# test nullspace
# first test reduced row-ech form
R = Rational
M = Matrix([[5,7,2,1],
示例7: ReactionSystem
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
#.........这里部分代码省略.........
# attempt to calculate C directly
# definition: diploma thesis equation (3.30)
if self.C_attempt:
self.C = _lyapunov_equation_C(self, self.A, self.B)
if self.C:
self.C = matsimp(self.C, factorize=self.factorize)
else:
# instead the Taylor coefficients will be calculated if needed
self.C = None
if verbose:
self.print_out("ReactionSystem", verbose)
#-----------------------------------------
@staticmethod
def from_string(data=None, yaml_file=None, C_attempt=False,
factorize=False, verbose=0):
"""
Create object from strings in a dictionary or file.
:Parameters:
- `data`: dictionary of strings
- `yaml_file`: yaml file defining a dictionary of strings
- `C_attempt`: If True, the calculation of C is attempted.
This is in general not possible and may be
unnecessarily time consuming.
- `factorize`: factorize terms with factor() function
- `verbose`: print the obtained system definition (0: not at all,
or with 1: print, 2: sympy pprint, 3: IPython display)
The keys 'concentrations', 'extrinsic_variables', 'transition_rates'
and 'stoichiometric_matrix' are required and may in part defined by
'data' and by 'yaml_file'. To choose non-default symbols, there are
the optional keys 'normal_variances', 'inverse_correlation_times',
'frequency' and 'system_size'.
:Returns: ReactionSystem object
:Example:
see module docstring
"""
data = string_parser(data=data, yaml_file=yaml_file, verbose=verbose)
return ReactionSystem(data, C_attempt=C_attempt,
factorize=factorize, verbose=verbose)
#-----------------------------------------
def copy(self):
"""
Returns a clone of self.
"""
data = {'eta': self.eta, 'A': self.A, 'B': self.B}
new = ReactionSystem(data, map_dict=self.map_dict)
for key, item in self.__dict__.items():
setattr(new, key, item)
return new
#-----------------------------------------
def eval_at_phis(self, phis=None, solver=None, select=None,
C_attempt=False, verbose=0):
"""
Substitute concentrations phi by phis.
A warning is printed if g(phis) is not zero.
:Warning:
Be sure, that phis is the correct stationary state!
示例8: quad_casimir
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import norm [as 别名]
def quad_casimir(self, highest):
highest_weight = Matrix([highest]) * self.fund_weights
return highest_weight.norm()**2 + 2 * highest_weight.dot(self.delta)