Matrix Multiplication with Numpy

Suppose I have an affine matrix A and a diagonal matrix D. How can I calculate the Laplace matrix in Python with nympy?

L = D ^ (- 1/2) AD ^ (1/2)

I am currently using L = D ** (- 1/2) * A * D ** (1/2). Is it correct?

Thank.

+5
source share
4 answers

Numpy allows you to directly express a diagonal matrix with positive elements:

m = diag(range(1, 11))
print m**0.5

However, this really does not allow you to express any matrix directly:

m = matrix([[1, 1], [1, 2]])
print m**0.5

creates an observable TypeError (an exception indicates that the exponent must be an integer for matrices that can be diagonalized with positive coefficients).

, , D , .

+2

, numpy array matrix: . . , ... , D ** 0.5 , numpy, . :

import numpy as np
from numpy import dot, diag
D = diag([1., 2., 3.])
print D**(-0.5)
[[ 1.                 Inf         Inf]
 [        Inf  0.70710678         Inf]
 [        Inf         Inf  0.57735027]]

, - . numpy ,

D = np.array([1., 2., 3.]) # note that we define D just by its diagonal elements
A = np.cov(np.random.randn(3,100)) # a random symmetric positive definite matrix
L = dot(diag(D**(-0.5)), dot(A, diag(D**0.5)))
+4

, , , , Python 2.6.x( from __future__ import division), 1/2 0, . , D ** (-. 5) * A * D **. 5. float- 1./2 1/2.

, .

Edit:

, , D**.5. numpy.power.

from numpy import power
power(D, -.5) * A * power(D, .5)
+1

numpy ? sqrt (D) (D ** (1/2))

,

L = (D**(-1/2)) * A * (D**(1/2)) 

Based on the previous comment, this formula should work if D is a diagonal matrix (I have no chance to prove it now).

0
source

All Articles