Re: [Numpy-discussion] ufunc for sum of squared difference

2016-11-14 Thread Jerome Kieffer
On Fri, 11 Nov 2016 11:25:58 -0500
Matthew Harrigan  wrote:

> I started a ufunc to compute the sum of square differences here
> .
> It is about 4x faster and uses half the memory compared to
> np.sum(np.square(x-c)). 

Hi Matt,

Using *blas* you win already a factor two (maybe more depending on you blas 
implementation):

% python -m timeit -s "import numpy as np;x=np.linspace(0,1,int(1e7))" 
"np.sum(np.square(x-2.))"
10 loops, best of 3: 135 msec per loop

% python -m timeit -s "import numpy as np;x=np.linspace(0,1,int(1e7))" 
"y=x-2.;np.dot(y,y)"
10 loops, best of 3: 70.2 msec per loop


Cheers,
-- 
Jérôme Kieffer
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
https://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] ufunc for sum of squared difference

2016-11-14 Thread eat
Yeah,

but it's not so obvious what's happening "under the hoods". Consider this
(with an old Win7 machine):
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64
bit (AMD64)]
np.__version__
'1.11.1'

On Mon, Nov 14, 2016 at 10:38 AM, Jerome Kieffer 
wrote:

> On Fri, 11 Nov 2016 11:25:58 -0500
> Matthew Harrigan  wrote:
>
> > I started a ufunc to compute the sum of square differences here
> > .
> > It is about 4x faster and uses half the memory compared to
> > np.sum(np.square(x-c)).
>
> Hi Matt,
>
> Using *blas* you win already a factor two (maybe more depending on you
> blas implementation):
>
> % python -m timeit -s "import numpy as np;x=np.linspace(0,1,int(1e7))"
> "np.sum(np.square(x-2.))"
> 10 loops, best of 3: 135 msec per loop
>
> % python -m timeit -s "import numpy as np;x=np.linspace(0,1,int(1e7))"
> "y=x-2.;np.dot(y,y)"
> 10 loops, best of 3: 70.2 msec per loop
>
x= np.linspace(0, 1, int(1e6))

timeit np.sum(np.square(x- 2.))
10 loops, best of 3: 23 ms per loop

y= x- 2.

timeit np.dot(y, y)
The slowest run took 18.60 times longer than the fastest. This could mean
that an intermediate result is being cached.
1000 loops, best of 3: 1.78 ms per loop

timeit np.dot(y, y)
1000 loops, best of 3: 1.73 ms per loop

Best,
eat

>
>
> Cheers,
> --
> Jérôme Kieffer
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
https://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Ensuring one can operate on array-like argument in place

2016-11-14 Thread Chris Barker
I tend to use ndarray.copy() in python code -- no reason you couldn't do
the same in Cython.

If you want to take any array-like object that may not have a copy()
method, you could call asanyarray() first:

-CHB





On Sat, Nov 12, 2016 at 9:00 AM, Pavlyk, Oleksandr <
oleksandr.pav...@intel.com> wrote:

> Hi,
>
>
>
> In my Cython code a function processes it argument x as follows:
>
>
>
> x_arr = PyArray_CheckFromAny(
>
>   x, NULL, 0, 0,
>
>   cnp.NPY_ELEMENTSTRIDES | cnp.NPY_ENSUREARRAY |
> cnp.NPY_NOTSWAPPED, NULL)
>
>
>
> if x_arr is not x:
>
>in_place = 1  # a copy was made, so we can work in place.
>
>
>
> The logic is of the last line turns out to be incorrect, because the input
> x can be a class with an array interface:
>
>
>
> class FakeArray(object):
>
> def __init__(self, data):
>
> self._data = data
>
> self.__array_interface__ = data.__array_interface__
>
>
>
> Feeding my function FakeArray(xx),  x_arr will point into the content of
> xx, resulting in unwarranted content
> overwrite of xx.
>
>
>
> I am trying to replace that condition with
>
>
>
> if x_arr is not x and cnp.PyArray_Check(x):
>
># a copy was made, so we can work in place.
>
>in_place = 1 if cnp.PyArray_CHKFLAGS(x_arr, cnp.NPY_WRITEABLE) else
> 0
>
>
>
> I am wondering if I perhaps overlooked some case.
>
>
>
> Thank you,
>
> Sasha
>
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
https://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] ufunc for sum of squared difference

2016-11-14 Thread Matthew Harrigan
Still slower and worse uses 2x the memory for the intermediate temporary
array.

I propose allowing implicit reductions with ufuncs.  Specifically if out is
provided with shape[axis] = 1, then pass it on to the ufunc with a stride
of 0.  That should allow this to work:

x = np.arange(10)
def add_square_diff(x1, x2, x3):
return x1 + (x2-x3)**2
result  =np.zeros(1)
np.frompyfunc(add_square_diff, 3, 1)(result, x, np.mean(x), result)

Essentially it creates a reduce for a function which isn't binary.  I think
this would be generally useful.  For instance, finding the min and max in
one pass would be nice:

def minmax(x1, x2, x3):
return min(x1,x3), max(x2,x3)
minn = np.array([np.inf])
maxx = np.array([-np.inf])
np.frompyfunc(minmax, 3, 2)(minn, maxx, x, minn, maxx)

Note it also allows for arbitrary initial values or identity to be
specified, possibly determined at run time.  I think this would make ufuncs
even more universal.

On Mon, Nov 14, 2016 at 3:38 AM, Jerome Kieffer 
wrote:

> On Fri, 11 Nov 2016 11:25:58 -0500
> Matthew Harrigan  wrote:
>
> > I started a ufunc to compute the sum of square differences here
> > .
> > It is about 4x faster and uses half the memory compared to
> > np.sum(np.square(x-c)).
>
> Hi Matt,
>
> Using *blas* you win already a factor two (maybe more depending on you
> blas implementation):
>
> % python -m timeit -s "import numpy as np;x=np.linspace(0,1,int(1e7))"
> "np.sum(np.square(x-2.))"
> 10 loops, best of 3: 135 msec per loop
>
> % python -m timeit -s "import numpy as np;x=np.linspace(0,1,int(1e7))"
> "y=x-2.;np.dot(y,y)"
> 10 loops, best of 3: 70.2 msec per loop
>
>
> Cheers,
> --
> Jérôme Kieffer
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
https://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] ufunc for sum of squared difference

2016-11-14 Thread Stephan Hoyer
On Mon, Nov 14, 2016 at 5:40 PM, Matthew Harrigan <
harrigan.matt...@gmail.com> wrote:

> Essentially it creates a reduce for a function which isn't binary.  I
> think this would be generally useful.
>

NumPy already has a generic enough interface for creating such ufuncs. In
fact, it's called a "generalized ufunc":
https://docs.scipy.org/doc/numpy/reference/c-api.generalized-ufuncs.html

I think you could already write "implicit reductions" using gufuncs?
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
https://mail.scipy.org/mailman/listinfo/numpy-discussion