I need to use a fast random (complex) number generatot.

The following :

sage: %%cython
....: from sage.misc.prandom import random
....: cpdef complex crand(double x):
....:     return x*random()+x*1j*random()

is about 77 times faster than its Python equivalent :

sage: def prand(x):
....:     return CDF(2*(x*(random()-0.5)+I*x*(random()-0.5)))

sage: %timeit foo=crand(8)
The slowest run took 14.72 times longer than the fastest. This could mean 
that an intermediate result is being cached.
1000000 loops, best of 3: 810 ns per loop
sage: 623000.0/810.0

No small beer...

However :

sage: prand(8).parent()
Complex Double Field
sage: crand(8).parent()
AttributeError                            Traceback (most recent call last)
<ipython-input-112-473e92fbb9eb> in <module>()
----> 1 crand(Integer(8)).parent()

AttributeError: 'complex' object has no attribute 'parent'

Is there a way to return a CDF (or RDF) value from a Cython function ?

Bonus question : if so, where is it documented ? Neither the reference 
manual nor Google gave me any hint...

Emmanuel Charpentier

You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to