mlinsights.mlmodel.direct_blas_lapack

A simple try to call scipy blas and lapack function from cython.

Lapack

mlinsights.mlmodel.direct_blas_lapack.dgelss(double[:, ::1] A, double[:, ::1] B, double prec=-1.)

Finds X in the problem \(AX=B\) by minimizing \(\norm{AX - B}^2\). Uses function dgels.

Parameters:
  • A – matrix with 2 dimensions

  • B – matrix with 2 dimensions

  • prec – precision

Returns:

integer (INFO)

INFO is:

  • = 0: successful exit

  • < 0: if INFO = -i, the i-th argument had an illegal value

  • > 0: if INFO = i, the i-th diagonal element of the triangular factor of A is zero, so that A does not have full rank; the least squares solution could not be computed.

Note

::1 indicates A, B, C must be contiguous arrays. Arrays A, B are modified by the function. B contains the solution.

Use lapack function dgelss

C minimizes the problem \(\norm{AX - B}^2\).

<<<

import numpy
from scipy.linalg.lapack import dgelss as scipy_dgelss
from mlinsights.mlmodel.direct_blas_lapack import dgelss

A = numpy.array([[10.0, 1.0], [12.0, 1.0], [13.0, 1]])
B = numpy.array([[20.0, 22.0, 23.0]]).T
v, x, s, rank, work, info = scipy_dgelss(A, B)
print(x[:2])

A = A.T.copy()
info = dgelss(A, B)
assert info == 0
print(B[:2])

>>>

    [[ 1.]
     [10.]]
    [[ 1.]
     [10.]]