本文整理汇总了Python中LinearAlgebra.singular_value_decomposition方法的典型用法代码示例。如果您正苦于以下问题:Python LinearAlgebra.singular_value_decomposition方法的具体用法?Python LinearAlgebra.singular_value_decomposition怎么用?Python LinearAlgebra.singular_value_decomposition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinearAlgebra
的用法示例。
在下文中一共展示了LinearAlgebra.singular_value_decomposition方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import LinearAlgebra [as 别名]
# 或者: from LinearAlgebra import singular_value_decomposition [as 别名]
def __init__(self, atoms, constraints):
self.atoms = atoms
natoms = len(self.atoms)
nconst = reduce(operator.add, map(len, constraints))
b = Numeric.zeros((nconst, natoms), Numeric.Float)
c = Numeric.zeros((nconst,), Numeric.Float)
i = 0
for cons in constraints:
cons.setCoefficients(self.atoms, b, c, i)
i = i + len(cons)
u, s, vt = LinearAlgebra.singular_value_decomposition(b)
self.rank = 0
for i in range(min(natoms, nconst)):
if s[i] > 0.:
self.rank = self.rank + 1
self.b = b
self.bi = LinearAlgebra.generalized_inverse(b)
self.p = Numeric.identity(natoms)-Numeric.dot(self.bi, self.b)
self.c = c
self.bi_c = Numeric.dot(self.bi, c)
c_test = Numeric.dot(self.b, self.bi_c)
if Numeric.add.reduce((c_test-c)**2)/nconst > 1.e-12:
Utility.warning("The charge constraints are inconsistent."
" They will be applied as a least-squares"
" condition.")
示例2: multivariate_normal
# 需要导入模块: import LinearAlgebra [as 别名]
# 或者: from LinearAlgebra import singular_value_decomposition [as 别名]
def multivariate_normal(mean, cov, shape=[]):
"""multivariate_normal(mean, cov) or multivariate_normal(mean, cov, [m, n, ...])
returns an array containing multivariate normally distributed random numbers
with specified mean and covariance.
mean must be a 1 dimensional array. cov must be a square two dimensional
array with the same number of rows and columns as mean has elements.
The first form returns a single 1-D array containing a multivariate
normal.
The second form returns an array of shape (m, n, ..., cov.shape[0]).
In this case, output[i,j,...,:] is a 1-D array containing a multivariate
normal."""
# Check preconditions on arguments
mean = Numeric.array(mean)
cov = Numeric.array(cov)
if len(mean.shape) != 1:
raise ArgumentError, "mean must be 1 dimensional."
if (len(cov.shape) != 2) or (cov.shape[0] != cov.shape[1]):
raise ArgumentError, "cov must be 2 dimensional and square."
if mean.shape[0] != cov.shape[0]:
raise ArgumentError, "mean and cov must have same length."
# Compute shape of output
if isinstance(shape, IntType): shape = [shape]
final_shape = list(shape[:])
final_shape.append(mean.shape[0])
# Create a matrix of independent standard normally distributed random
# numbers. The matrix has rows with the same length as mean and as
# many rows are necessary to form a matrix of shape final_shape.
x = ranlib.standard_normal(Numeric.multiply.reduce(final_shape))
x.shape = (Numeric.multiply.reduce(final_shape[0:len(final_shape)-1]),
mean.shape[0])
# Transform matrix of standard normals into matrix where each row
# contains multivariate normals with the desired covariance.
# Compute A such that matrixmultiply(transpose(A),A) == cov.
# Then the matrix products of the rows of x and A has the desired
# covariance. Note that sqrt(s)*v where (u,s,v) is the singular value
# decomposition of cov is such an A.
(u,s,v) = LinearAlgebra.singular_value_decomposition(cov)
x = Numeric.matrixmultiply(x*Numeric.sqrt(s),v)
# The rows of x now have the correct covariance but mean 0. Add
# mean to each row. Then each row will have mean mean.
Numeric.add(mean,x,x)
x.shape = final_shape
return x
示例3: testSVD
# 需要导入模块: import LinearAlgebra [as 别名]
# 或者: from LinearAlgebra import singular_value_decomposition [as 别名]
def testSVD(self):
"""
From bug #930735. Numbers redone in Maple.
"""
import LinearAlgebra
a = Numeric.array([[2,4],[1,3],[0,0],[0,0]])
u, s, vt = LinearAlgebra.singular_value_decomposition(a)
s34d2 = math.sqrt(34.)/2
s26d2 = math.sqrt(26.)/2
assert_eq(s, Numeric.array([s34d2+s26d2, s34d2-s26d2]))
vt_c = Numeric.array([[-0.404553584833756919, -0.914514295677304467],
[-0.914514295677304467, 0.404553584833756919]])
assert_eq(vt, vt_c)
u_c = Numeric.array([[-0.817415560470363233, -0.576048436766320782],
[-0.576048436766320782, 0.817415560470363344],
[0, 0],
[0, 0]])
assert_eq(u, u_c)
assert_eq(a, Numeric.dot(u*s, vt))
示例4: svd
# 需要导入模块: import LinearAlgebra [as 别名]
# 或者: from LinearAlgebra import singular_value_decomposition [as 别名]
def svd(v):
"""[u,x,v] = svd(m) return the singular value decomposition of m.
"""
return LinearAlgebra.singular_value_decomposition(v)
示例5:
# 需要导入模块: import LinearAlgebra [as 别名]
# 或者: from LinearAlgebra import singular_value_decomposition [as 别名]
import ranlib