[Numpy-discussion] optimizing power() for complex and real cases

Travis Oliphant oliphant.travis at ieee.org
Mon Feb 20 19:45:04 CST 2006


Tim Hochberg wrote:

>> Hmm, ufuncs are passed a void* argument for passing info to them. Now,
>> what that argument is defined when the ufunc is created, but maybe
>> there's a way to piggy-back on it.
>>  
>>
> Yeah, I really felt like I was fighting the ufuncs when I was playing 
> with this. On the one hand, you really want to use the ufunc 
> machinery. On the other hand that forces you into using the same types 
> for both arguments.


This is not true.  Ufuncs can have different types for their 
arguments.   Perhaps you meant something else?

> Just to add a little more confusion to the mix. I did a little testing 
> to see how close pow(x,n) and x*x*... actually are. They are slightly 
> less close for small values of N and slightly closer for large values 
> of N than I would have expected. The upshot of this is that integer 
> powers between  -2 and +4 all seem to vary by the same amount when 
> computed using pow(x,n) versus multiplies. I'm including the test code 
> at the end. Assuming that this result is not a fluke that expands the 
> noncontroversial set by at least 3 more values. That's starting to 
> strain the ufunc aproach, so perhaps optimizing in @TYP at _power is the 
> way to go after all. Or,  more likely, adding @TYP at _int_power or maybe 
> @TYP at _fast_power (so as to be able to include some half integer 
> powers) and dispatching appropriately from array_power.
>
> The problem here, of course, is the overhead that PyArray_EnsureArray 
> runs into. I'm not sure if the ufuncs actually call that, but I was 
> using that to convert things to arrays at one point and I saw the 
> slowdown, so I suspect that the slowdown is in something 
> PyArray_EnsureArray calls if not in that routine itself. I'm afraid to 
> dig into that stuff though.. On the other hand, it would probably 
> speed up all kinds of stuff if that was sped up.

EnsureArray simply has some short cuts and then calls PyArray_FromAny.  
PyArray_FromAny is the big array conversion code.  It converts anything 
(it can) to an array.

>> Too bad we couldn't make a function generator :-) [Well, we could using
>> weave...]\
>>  
>>
> Yaigh!

That's actually an interesting approach that could use some attention..

-Travis





More information about the Numpy-discussion mailing list