Note that there are some explanatory texts on larger screens.

plurals
  1. PODefining and using mathematical functions in cython
    primarykey
    data
    text
    <p>I am new to python and struggling to get cython to speed up some of my calculations. I have tried every possible way i can think of but it seems that i don't yet understand something so i fail miserably to this. I have these files: The first one called <code>Myfunc.pyx</code>:</p> <pre><code>import math from sympy import Symbol x = Symbol('x') wi=10 cdef float MyList(float a,float b): return [(xa,ya,wi+a.subs(x,xa)*math.sin(xa)*b.subs(x,xa)*math.exp(1/ya**2)) for xa in range(1,5001,10) for ya in range(-2501,2501,10)] </code></pre> <p>The <code>setup.py</code>:</p> <pre><code>from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules=[ Extension("Myfunc", ["Myfunc.pyx"], libraries=["m"]) # Unix-like specific ] setup( name = "Demos", cmdclass = {"build_ext": build_ext}, ext_modules = ext_modules ) </code></pre> <p>finally the python script called <code>calc.py</code> which i run to get the list i want, called "MyList1":</p> <pre><code>import Myfunc from sympy import Symbol import math x = Symbol('x') a1=(10*x)**(0.5) b1=(20*x)**(0.5) wi=10 MyList1=Myfunc.MyList(a1,b1) print MyList1 </code></pre> <p>Now some clarifications: 1) x,y are used in previous scripts purely as symbols to derive formulas. I use the <code>a.subs(x,xa)</code> and <code>b.subs(x,xa)</code> (see the first file Myfunc.pyx) to get rid of the x and replace it with xa to finally get numeric and not symbolic solutions. 2) I think that on the top of the third file (calc.py) i should not use <code>import Myfunc</code> but something else i don't get. <strong>What i think i should do:</strong> run the setup.py and then since there are 2 new files created the Myfunc.c and the Myfunc.so i just run the calc.py file and supposedly i will have my numbers faster tahn in pure python. <strong>What must be done:</strong> I finally have no idea... <strong>What i want if possible:</strong> The <code>exp()</code> and <code>a.subs(x,xa)</code> and <code>b.subs(x,xa)</code> i think are slowing down my calculations. I try to convert this piece of code from pure python to "cython" (i think conversion is not a good word to describe this). The tutorials in the official site are a bit incrompehensible for me so i always fail to achieve this. Excuse my english because my hovercraft is full of eels</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload