本文整理匯總了Python中numpy.linalg方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.linalg方法的具體用法?Python numpy.linalg怎麽用?Python numpy.linalg使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.linalg方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: eig
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def eig(h, s):
from pyscf import symm
nirrep = len(mol.symm_orb)
h = symm.symmetrize_matrix(h, mol.symm_orb)
s = symm.symmetrize_matrix(s, mol.symm_orb)
cs = []
es = []
#
# Linear dependency are removed by looping over different symmetry irreps.
#
for ir in range(nirrep):
d, t = numpy.linalg.eigh(s[ir])
x = t[:,d>1e-8] / numpy.sqrt(d[d>1e-8])
xhx = reduce(numpy.dot, (x.T, h[ir], x))
e, c = numpy.linalg.eigh(xhx)
cs.append(reduce(numpy.dot, (mol.symm_orb[ir], x, c)))
es.append(e)
e = numpy.hstack(es)
c = numpy.hstack(cs)
return e, c
示例2: _weights
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def _weights(V , X_treated, X_control, w_pen):
V = np.diag(V) #make square
#weights = np.zeros((X_control.shape[0], X_treated.shape[0]))
w_pen_mat = 2 * w_pen * np.diag(np.ones(X_control.shape[0]))
A = X_control.dot(2 * V).dot(X_control.T) + w_pen_mat # 5
B = (
X_treated.dot(2 * V).dot(X_control.T).T + 2 * w_pen / X_control.shape[0]
) # 6
try:
b = scipy.linalg.solve(A, B)
except scipy.linalg.LinAlgError as exc:
print("Unique weights not possible.")
if w_pen == 0:
print("Try specifying a very small w_pen rather than 0.")
raise exc
return b
示例3: test_eigvalsh
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def test_eigvalsh():
if not imported_scipy:
raise SkipTest("Scipy needed for the geigvalsh op.")
import scipy.linalg
A = theano.tensor.dmatrix('a')
B = theano.tensor.dmatrix('b')
f = function([A, B], eigvalsh(A, B))
rng = numpy.random.RandomState(utt.fetch_seed())
a = rng.randn(5, 5)
a = a + a.T
for b in [10 * numpy.eye(5, 5) + rng.randn(5, 5)]:
w = f(a, b)
refw = scipy.linalg.eigvalsh(a, b)
numpy.testing.assert_array_almost_equal(w, refw)
# We need to test None separatly, as otherwise DebugMode will
# complain, as this isn't a valid ndarray.
b = None
B = theano.tensor.NoneConst
f = function([A], eigvalsh(A, B))
w = f(a)
refw = scipy.linalg.eigvalsh(a, b)
numpy.testing.assert_array_almost_equal(w, refw)
示例4: test_perform
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def test_perform(self):
if not imported_scipy:
raise SkipTest('kron tests need the scipy package to be installed')
for shp0 in [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]:
x = tensor.tensor(dtype='floatX',
broadcastable=(False,) * len(shp0))
a = numpy.asarray(self.rng.rand(*shp0)).astype(config.floatX)
for shp1 in [(6,), (6, 7), (6, 7, 8), (6, 7, 8, 9)]:
if len(shp0) + len(shp1) == 2:
continue
y = tensor.tensor(dtype='floatX',
broadcastable=(False,) * len(shp1))
f = function([x, y], kron(x, y))
b = self.rng.rand(*shp1).astype(config.floatX)
out = f(a, b)
# Newer versions of scipy want 4 dimensions at least,
# so we have to add a dimension to a and flatten the result.
if len(shp0) + len(shp1) == 3:
scipy_val = scipy.linalg.kron(
a[numpy.newaxis, :], b).flatten()
else:
scipy_val = scipy.linalg.kron(a, b)
utt.assert_allclose(out, scipy_val)
示例5: test_numpy_compare
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def test_numpy_compare(self):
rng = numpy.random.RandomState(utt.fetch_seed())
M = tensor.matrix("A", dtype=theano.config.floatX)
V = tensor.vector("V", dtype=theano.config.floatX)
a = rng.rand(4, 4).astype(theano.config.floatX)
b = rng.rand(4).astype(theano.config.floatX)
A = ( [None, 'fro', 'inf', '-inf', 1, -1, None, 'inf', '-inf', 0, 1, -1, 2, -2],
[M, M, M, M, M, M, V, V, V, V, V, V, V, V],
[a, a, a, a, a, a, b, b, b, b, b, b, b, b],
[None, 'fro', inf, -inf, 1, -1, None, inf, -inf, 0, 1, -1, 2, -2])
for i in range(0, 14):
f = function([A[1][i]], norm(A[1][i], A[0][i]))
t_n = f(A[2][i])
n_n = numpy.linalg.norm(A[2][i], A[3][i])
assert _allclose(n_n, t_n)
示例6: logdet_symm
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def logdet_symm(m, check_symm=False):
"""
Return log(det(m)) asserting positive definiteness of m.
Parameters
----------
m : array-like
2d array that is positive-definite (and symmetric)
Returns
-------
logdet : float
The log-determinant of m.
"""
from scipy import linalg
if check_symm:
if not np.all(m == m.T): # would be nice to short-circuit check
raise ValueError("m is not symmetric.")
c, _ = linalg.cho_factor(m, lower=True)
return 2*np.sum(np.log(c.diagonal()))
示例7: setUp
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def setUp(self):
"""Sets up all variables needed for Davidson class."""
dimension = 10
matrix = generate_matrix(dimension)
def mat_vec(vec):
"""Trivial matvec with a numpy matrix."""
return numpy.dot(matrix, vec)
self.linear_operator = scipy.sparse.linalg.LinearOperator(
(dimension, dimension), matvec=mat_vec)
self.diagonal = numpy.diag(matrix)
self.davidson = Davidson(linear_operator=self.linear_operator,
linear_operator_diagonal=self.diagonal)
self.matrix = matrix
self.initial_guess = numpy.eye(self.matrix.shape[0], 10)
self.eigen_values = numpy.array([
1.15675714, 1.59132505, 2.62268014, 4.44533793, 5.3722743,
5.54393114, 7.73652405, 8.50089897, 9.4229309, 15.54405993,
])
示例8: variance
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def variance(operator, state):
"""Compute variance of operator with a state.
Args:
operator(scipy.sparse.spmatrix or scipy.sparse.linalg.LinearOperator):
The operator whose expectation value is desired.
state(numpy.ndarray or scipy.sparse.spmatrix): A numpy array
representing a pure state or a sparse matrix representing a density
matrix.
Returns:
A complex number giving the variance.
Raises:
ValueError: Input state has invalid format.
"""
return (expectation(operator ** 2, state) -
expectation(operator, state) ** 2)
示例9: get_gap
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def get_gap(sparse_operator, initial_guess=None):
"""Compute gap between lowest eigenvalue and first excited state.
Args:
sparse_operator (LinearOperator): Operator to find the ground state of.
initial_guess (ndarray): Initial guess for eigenspace. A good
guess dramatically reduces the cost required to converge.
Returns: A real float giving eigenvalue gap.
"""
if not is_hermitian(sparse_operator):
raise ValueError('sparse_operator must be Hermitian.')
values, _ = scipy.sparse.linalg.eigsh(
sparse_operator, k=2, v0=initial_guess, which='SA', maxiter=1e7)
gap = abs(values[1] - values[0])
return gap
示例10: generate_linear_qubit_operator
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def generate_linear_qubit_operator(qubit_operator, n_qubits=None, options=None):
""" Generates a LinearOperator from a QubitOperator.
Args:
qubit_operator(QubitOperator): A qubit operator to be applied on
vectors.
n_qubits(int): The total number of qubits
options(LinearQubitOperatorOptions): Options for the
ParallelLinearQubitOperator.
Returns:
linear_operator(scipy.sparse.linalg.LinearOperator): A linear operator.
"""
if options is None:
linear_operator = LinearQubitOperator(qubit_operator, n_qubits)
else:
linear_operator = ParallelLinearQubitOperator(
qubit_operator, n_qubits, options)
return linear_operator
示例11: generate_random_vectors
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def generate_random_vectors(row, col, real_only=False):
"""Generates orthonormal random vectors with col columns.
Args:
row(int): Number of rows for the vectors.
col(int): Number of columns for the vectors.
real_only(bool): Real vectors or complex ones.
Returns:
random_vectors(numpy.ndarray(complex)): Orthonormal random vectors.
"""
random_vectors = numpy.random.rand(row, col)
if not real_only:
random_vectors = random_vectors + numpy.random.rand(row, col) * 1.0j
random_vectors = scipy.linalg.orth(random_vectors)
return random_vectors
示例12: xavier_vector
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def xavier_vector(word, D=300):
"""
Returns a D-dimensional vector for the word.
We hash the word to always get the same vector for the given word.
"""
seed_value = hash_string(word)
numpy.random.seed(seed_value)
neg_value = - math.sqrt(6)/math.sqrt(D)
pos_value = math.sqrt(6)/math.sqrt(D)
rsample = numpy.random.uniform(low=neg_value, high=pos_value, size=(D,))
norm = numpy.linalg.norm(rsample)
rsample_normed = rsample/norm
return rsample_normed
示例13: calculateNormalsFromIndexedVertices
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def calculateNormalsFromIndexedVertices(vertices: numpy.ndarray, indices: numpy.ndarray, face_count: int) -> numpy.ndarray:
"""Calculate the normals of this mesh of triagles using indexes.
:param vertices: :type{narray} list of vertices as a 1D list of float triples
:param indices: :type{narray} list of indices as a 1D list of integers
:param face_count: :type{integer} the number of triangles defined by the indices array
:return: :type{narray} list normals as a 1D array of floats, each group of 3 floats is a vector
"""
start_time = time()
# Numpy magic!
# First, reset the normals
normals = numpy.zeros((face_count*3, 3), dtype=numpy.float32)
for face in indices[0:face_count]:
normals[face[0]] = numpy.cross(vertices[face[0]] - vertices[face[1]], vertices[face[0]] - vertices[face[2]])
length = numpy.linalg.norm(normals[face[0]])
normals[face[0]] /= length
normals[face[1]] = normals[face[0]]
normals[face[2]] = normals[face[0]]
end_time = time()
Logger.log("d", "Calculating normals took %s seconds", end_time - start_time)
return normals
示例14: rotation_matrix_axis_and_angle_2
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def rotation_matrix_axis_and_angle_2(R, debug=False, errorThreshold=10**-7, msg=None):
w, v = numpy.linalg.eig(R) #this method is not used at the primary method as numpy.linalg.eig does not return answers in high enough precision
angle, axis = None, None
eigErrors = abs(w -1) #errors from 1+0j
i = (eigErrors == min(eigErrors)).tolist().index(True)
axis = numpy.real(v[:,i])
if i != 1:
angle = arccos2( numpy.real( w[1] ) )
else:
angle = arccos2( numpy.real( w[0] ) )
error = norm(axis_rotation_matrix(angle, *axis) - R)
if debug: print('rotation_matrix_axis_and_angle error %1.1e' % error)
if error > errorThreshold:
angle = -angle
error = norm(axis_rotation_matrix(angle, *axis) - R)
if error > errorThreshold:
R_pickle_str = pickle.dumps(R)
#R_abs_minus_identity = abs(R) - numpy.eye(3)
print(R*R.transpose())
raise ValueError( 'rotation_matrix_axis_and_angle_2: no solution found! locals %s' % str(locals()))
return axis, angle
示例15: adet
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import linalg [as 別名]
def adet(z, x):
"""d|A|/dA = adj(A).T
See Jacobi's formula: https://en.wikipedia.org/wiki/Jacobi%27s_formula
"""
adjugate = numpy.linalg.det(x) * numpy.linalg.pinv(x)
d[x] = d[z] * numpy.transpose(adjugate)
#
# Tangent adjoints
#