简体   繁体   中英

Calculating exponential of a matrix in python

I want to calculate the exponential of a 200x200 matrix (expm(B)) and get the following problem. Thanks a lot for your help.

exp_matrix2 = expm(B)

File ".../python2.7/site-packages/scipy/linalg/matfuncs.py", line 261, in expm return scipy.sparse.linalg.expm(A)

File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py", line 582, in expm return _expm(A, use_exact_onenorm='auto')

File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py", line 618, in _expm eta_1 = max(h.d4_loose, h.d6_loose)

File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py", line 457, in d4_loose structure=self.structure)**(1/4.)

File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py", line 301, in _onenormest_matrix_power MatrixPowerOperator(A, p, structure=structure))

File ".../python2.7/site-packages/scipy/sparse/linalg/_onenormest.py", line 95, in onenormest est, v, w, nmults, nresamples = _onenormest_core(A, AH, t, itmax)

File "/python2.7/site-packages/scipy/sparse/linalg/_onenormest.py", line 424, in _onenormest_core Z = np.asarray(AT_linear_operator.matmat(S))

File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py", line 326, in matmat Y = self._matmat(X)

File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py", line 468, in _matmat return super(_CustomLinearOperator, self)._matmat(X)

File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py", line 174, in _matmat return np.hstack([self.matvec(col.reshape(-1,1)) for col in XT])

File "/home/dk2518/anaconda2/lib/python2.7/site-packages/scipy/sparse/linalg/interface.py", line 219, in matvec y = self._matvec(x)

File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py", line 471, in _matvec return self.__matvec_impl(x)

File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py", line 266, in rmatvec y = self._rmatvec(x)

File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py", line 203, in _rmatvec x = A_T.dot(x)

ValueError: shapes (207,207) and (1,207) not aligned: 207 (dim 1) != 1 (dim 0)

As discussed in @TomNash's link, a large np.matrix is the problem.

ndarray and sparse matrix work fine:

In [309]: slg.expm(np.ones((200,200)));                                         
In [310]: slg.expm(sparse.csc_matrix(np.ones((200,200))));                      
In [311]: slg.expm(np.matrix(np.ones((200,200))));  
ValueError: shapes (200,200) and (1,200) not aligned: 200 (dim 1) != 1 (dim 0)

Not every np.matrix gives problems:

In [313]: slg.expm(np.matrix(np.eye(200)));

Turning the np.matrix back into ndarray works:

In [315]: slg.expm(np.matrix(np.ones((200,200))).A);

This uses slg.matfuncs._expm(A, use_exact_onenorm='auto')

which has an test, early one, for:

if use_exact_onenorm == "auto":
    # Hardcode a matrix order threshold for exact vs. estimated one-norms.
    use_exact_onenorm = A.shape[0] < 200

That explains, in part, why we get the problem with a (200,200) matrix, but not a (199,199).

This works:

slg.matfuncs._expm(M, use_exact_onenorm=True);

It fails with False . But from there I get lost in the details of how it sets up _ExpmPadeHelper and attempts a Pade order 3 .

In short - avoid np.matrix , especially if (200,200) or larger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM