Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scipy BLAS routine does not overwrite input despite using overwrite_a=True

I am used to using BLAS routines in cython (from scipy.linalg.cython_blas), where the input is very often modified in place (for example, in the dger routine). I'm trying to do the same with scipy.linalg.blas.dger, but despite using overwrite_a=True, a is untouched

(dger performs a rank one update, meaning a + np.outer(x, y))

In [29]: x = np.array([1, 0, 2])

In [30]: y = np.array([-1,  1,  0,  2])

In [31]: a = np.arange(12).reshape(3, 4)

In [32]: a + np.outer(x, y)
Out[32]: 
array([[-1,  2,  2,  5],
       [ 4,  5,  6,  7],
       [ 6, 11, 10, 15]])

In [33]: dger(1.0, x, y, a=a, overwrite_a=True)
Out[33]: 
array([[ -1.,   2.,   2.,   5.],
       [  4.,   5.,   6.,   7.],
       [  6.,  11.,  10.,  15.]])

In [34]: a  # still the original value
Out[34]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

What use is overwrite_a=True then ? And how can I perform the update in place ?

EDIT: I know that BLAS routines are in Fortran so a should be in Fortran order, but even changing this does not change a.

like image 334
P. Camilleri Avatar asked Nov 02 '25 11:11

P. Camilleri


1 Answers

Quite interesting: it's because

  • a needs to be Fortran ordered because the output of dger, a Fortran routine, is.
  • a.dtype is np.int64, so dger does not want to change its type to np.float64 and does not overwrite it.
like image 99
P. Camilleri Avatar answered Nov 04 '25 01:11

P. Camilleri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!