简体   繁体   中英

linear algebra in python

Given a tall m by n matrix X , I need to calculate s = 1 + x(XT X)^{-1} xT . Here, x is a row vector and s is scalar. Is there an efficient (or, recommended) way to compute this in python?

Needless to say, XT X will be symmetric positive definite.

My attempt:

If we consider the QR decomposition of X , ie, X = QR , where Q is orthogonal, R is upper triangular, then XT X = RT R .

QR decomposition can be easily obtained using numpy.linalg.qr , that is

Q,R = numpy.linalg.qr(X)

But then again, is there a particularly efficient way to calculate inv(RT R) ?

If you are doing the QR factorization of X , resulting in XT X = RT R , you may avoid using np.linalg.inv (and np.linalg.solve ) by using forward and backward substitution instead ( RT is lower triangular!) with scipy.linalg.solve_triangular :

import numpy as np
import scipy.linalg as LA
Q,R = np.linalg.qr(X)
# solve R.T R z = x such that R z = y 
# with step (a) then (b)
# step (a) solve  R.T y = x
y   = LA.solve_triangular(R,x,trans='T') 
# step (b) solve R z = y
z   = LA.solve_triangular(R,x)
s   = 1 + x @ z

where @ is the python3 matrix multiplication operator.

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