0

I'm new on python and Spyder and I'm trying to cythonize a for loop. I'm using Spyder 3.2.7 from anaconda navigator, so I run the code by just pushing the green triangular button.

My pyx file looks like:

import numpy as np

def saluti():
  print('hello world')

def new_sum(double[:] X,  double[:] beta, double[:,:] theta):

  cdef int N = X.shape[0]
  cdef double[:] Y = np.zeros(N)
  cdef int i, j

  for i in range(N):
      for j in range(i):
          Y[i] += beta[j] * np.exp(-theta[i, j]*(X[i] - X[j]))
  return Y

While my py file:

import numpy as np

import pyximport
pyximport.install()

from prova_cythonn import saluti, new_sum

def old_sum (x, beta, theta):
  N = x.shape[0]
  y = np.zeros(N)
  for i in range(N):
     for j in range(i):
         y[i] = y[i] + beta[j]*np.exp(-theta[i, j]*(x[i] - x[j]))
  return y

... do stuff (like print(np.sum(old_sum) or print(np.sum(new_sum)) ...

When importing 'saluti' only, I get no error and obtain the 'hello world' message printed in the Ipython console. If I try to import the new_sum function I get ImportError: cannot import name 'new_sum' It seems to me that the problem is that if I modify and save the pyx savings are not 'read' when launching the py

acini
  • 49
  • 1
  • 6

0 Answers0