Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:27:35+00:00 2026-06-09T15:27:35+00:00

I am new to python and struggling to get cython to speed up some

  • 0

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 Myfunc.pyx:

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)]

The setup.py:

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
)

finally the python script called calc.py which i run to get the list i want, called “MyList1”:

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

Now some clarifications:
1) x,y are used in previous scripts purely as symbols to derive formulas. I use the a.subs(x,xa) and b.subs(x,xa) (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 import Myfunc but something else i don’t get. What i think i should do: 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. What must be done: I finally have no idea… What i want if possible: The exp() and a.subs(x,xa) and b.subs(x,xa) 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:27:37+00:00Added an answer on June 9, 2026 at 3:27 pm

    It is not possible to combine sympy with cython. Cython speeds up python by translating cdef classes and declared variables into C and compiling it. sympy.Symbol or any other sympy function has no C++ equivalent it can thus not be compiled into C, hence no speed ups.

    More concretely. When you declare a with float a, it is a C double not a sympy type. it therefore has no .subs method. (and it can’t have)

    If I understand what you are working on numpy is a much better solution than cython. Essentially numpy.arrays are C data types. When you perform calculations with them it happens at C++ speed. It is documented how to use numpy with sympy. If for any reason you can not achieve what you intend with numpy(unlikely) numpy still works well with cython.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to python and can't understand why this isn't working. I have a
I'm relatively new to Python and struggling to reconcile features of the language with
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
I am new to Python and have been studying its fundementals for 3 months
I'm a new to Python. I have installed Python 2.7 64 bit on Win7
I am quite new to Python and I am now struggling with formatting my
I am new python programmer,what I have understood so far,yield keyword returns an object
I've been searching for a way in python to get only 4 digits on
I'm new to python and after struggling with myself a little bit I almost
I'm just new to python and pyramid and I'm struggling with how to process

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.