Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array order in `numpy.dot`

Tags:

python

numpy

In Python's numerical library NumPy, how does the numpy.dot function deal with arrays of different memory-order? numpy.dot(c-order, f-order) vs. dot(f-order, c-order) etc.

The reason I ask is that long time ago (numpy 1.0.4?), I made some tests and noticed numpy.dot performed worse than calling dgemm from scipy.linalg directly, with the correct transposition flags, though both call the same BLAS library internally. (I suspected the reason was copying of the input matrices inside numpy.dot, which is tragic if the input is large.)

Now I tried again and actually numpy.dot performs the same as dgemm, so there is no reason to keep the arrays in specific order and set transposition flags manually. Much cleaner code.

So my question is, how does a recent (let's say 1.6.0) numpy.dot work, guarantees on when things are copied and when not? I'm concerned about 1) memory 2) performance here. Cheers.

like image 905
Radim Avatar asked May 19 '11 16:05

Radim


People also ask

Does order matter NP dot?

Scalar multiplication or dot product with numpy.dot Each element in the matrix is multiplied by the scalar, which makes the output the same shape as the original matrix. With scalar multiplication, the order doesn't matter.

What does dot do in NumPy?

dot() will multiply every value of the array by the scalar (i.e., scalar multiplication). If both inputs are 1-dimensional arrays, np. dot() will compute the dot product of the inputs. If both inputs are 2-dimensional arrays, then np.

What does dot () do in Python?

dot() in Python. The numpy module of Python provides a function to perform the dot product of two arrays. If both the arrays 'a' and 'b' are 1-dimensional arrays, the dot() function performs the inner product of vectors (without complex conjugation).

What is order in NumPy array?

Sorting means putting elements in an ordered sequence. Ordered sequence is any sequence that has an order corresponding to elements, like numeric or alphabetical, ascending or descending. The NumPy ndarray object has a function called sort() , that will sort a specified array.


1 Answers

Possibly what you were seeing may have been related to a blas-optimized dot import error being caught and handled silently (this code snippet is from numeric.py)

# try to import blas optimized dot if available
try:
    # importing this changes the dot function for basic 4 types
    # to blas-optimized versions.
    from _dotblas import dot, vdot, inner, alterdot, restoredot
except ImportError:
    # docstrings are in add_newdocs.py
    inner = multiarray.inner
    dot = multiarray.dot
like image 199
wim Avatar answered Sep 18 '22 20:09

wim