Another question: If I need the constant pi, and I use math.pi or numpy.pi in a C function (or a C method of a C class), does it add an inefficiency (Python lookup)?<div><br></div><div>Just wondering...</div><div>--v<br><div>
<br><div class="gmail_quote">On Sat, Apr 4, 2009 at 2:57 PM, Robert Bradshaw <span dir="ltr"><<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Apr 4, 2009, at 9:09 AM, Vic Kelson wrote:<br>
<br>
> Robert,<br>
> Thatnks for your helpful note. I can't seem to make it work with gcc<br>
> on OS X.<br>
<br>
</div>Hmm... that's the same platform I use.<br>
<div class="im"><br>
> I keep getting an error from gcc. I haven't had time to sort<br>
> through it yet. I think I'll build a little test problem (I tried<br>
> embedding your cod in my existing code and I might have created<br>
> another error).<br>
<br>
</div>Maybe you could post a snippet of the error you're getting?<br>
<div class="im"><br>
> Just to be sure, when I use your code to make a cdouble, the real and<br>
> imaginary parts are the same size as a python float, right?<br>
<br>
</div>Yes. Python floats are wrappers of double (and Python complexes wrap<br>
a pair of doubles).<br>
<div><div></div><div class="h5"><br>
> Thanks!<br>
> --v<br>
><br>
><br>
> On 4/4/09, Robert Bradshaw <<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>> wrote:<br>
>> On Apr 3, 2009, at 10:21 PM, Vic Kelson wrote:<br>
>><br>
>>> Greetings all,<br>
>>><br>
>>> Sorry for the incomplete message, I'm re-sending it.<br>
>>><br>
>>> I'm in need of fast computations on complex numbers from Cython.<br>
>>> I've been converting a Python groundwater flow model code to<br>
>>> Cython, but I'm reaching the point where I'd really prefer to have<br>
>>> native C complex math. Up to now, I've used some little C routines<br>
>>> that I can call with multiple floats (the C code converts to/from<br>
>>> complex for my purposes). However, I'm nearly to the point where<br>
>>> I'll need to store series coefficients for performance purposes,<br>
>>> and then I'll either need to hide them in float arrays (and that's<br>
>>> extremely nasty), or something like that.<br>
>>><br>
>>> I'd like to say something like<br>
>>><br>
>>> cdef class Foo:<br>
>>> cdef cfloat z<br>
>>><br>
>>> def __cinit__(self, float x, float y):<br>
>>> self.z = cfloat(x, y)<br>
>>><br>
>>> cdef cfloat func(Foo self, float x, float y):<br>
>>> return-some-gnarly-function of x, y, and self.z<br>
>>><br>
>>><br>
>>> Or something like that, where 'cfloat' maps to the appropriate type<br>
>>> in the C compiler, e.g. "double _Complex" in gcc.<br>
>>><br>
>>> I've seen this discussed in the archives. Has it been implemented?<br>
>>> Is there some pre-release code that essentially works? I only need<br>
>>> the four math functions, conjugate, abs, and log.<br>
>><br>
>> Yes, I've started this, as it comes up in my research too (number<br>
>> theory, computing values of L-functions). I hope to have something<br>
>> releasable soon. In the meantime, you can do<br>
>><br>
>> cdef extern from "complex.h":<br>
>> ctypedef double cdouble "double complex"<br>
>> cdef double creal(cdouble)<br>
>> cdef double cimag(cdouble)<br>
>> cdef cdouble _Complex_I<br>
>><br>
>> cdef inline cdouble new_cdouble(double x, double y):<br>
>> return x + _Complex_I*y<br>
>><br>
>> Of course, the ctypedef is wrong, it will think it can go back to<br>
>> doubles (and Python objects) without any problem, but it works for<br>
>> now.<br>
>><br>
>>> THANKS! I think Cython will revolutionize my research work!<br>
>><br>
>> That's the goal :) Please cite us if it does.<br>
>><br>
>> - Robert<br>
>><br>
>> _______________________________________________<br>
>> Cython-dev mailing list<br>
>> <a href="mailto:Cython-dev@codespeak.net">Cython-dev@codespeak.net</a><br>
>> <a href="http://codespeak.net/mailman/listinfo/cython-dev" target="_blank">http://codespeak.net/mailman/listinfo/cython-dev</a><br>
>><br>
><br>
> --<br>
> Sent from my mobile device<br>
><br>
> Vic Kelson<br>
> <a href="mailto:vic.kelson@gmail.com">vic.kelson@gmail.com</a><br>
> _______________________________________________<br>
> Cython-dev mailing list<br>
> <a href="mailto:Cython-dev@codespeak.net">Cython-dev@codespeak.net</a><br>
> <a href="http://codespeak.net/mailman/listinfo/cython-dev" target="_blank">http://codespeak.net/mailman/listinfo/cython-dev</a><br>
<br>
_______________________________________________<br>
Cython-dev mailing list<br>
<a href="mailto:Cython-dev@codespeak.net">Cython-dev@codespeak.net</a><br>
<a href="http://codespeak.net/mailman/listinfo/cython-dev" target="_blank">http://codespeak.net/mailman/listinfo/cython-dev</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Vic Kelson<br><a href="mailto:vic.kelson@gmail.com">vic.kelson@gmail.com</a><br>
</div></div>