test.pyx 1.09 KB
Newer Older
Mozul Rémy's avatar
Mozul Rémy committed
1 2

import math
Mozul Rémy's avatar
Mozul Rémy committed
3 4
cimport numpy
import numpy
Mozul Rémy's avatar
Mozul Rémy committed
5 6

import cython
7 8 9 10 11
from ctest cimport p1, p2, myctype, increment, print_type

numpy.import_array()

p = [p1, p2]
Mozul Rémy's avatar
Mozul Rémy committed
12 13 14

cdef class pytype(object):

15 16 17 18
  #x = cython.declare(myctype)
  cdef myctype x

  cdef public numpy.ndarray p_params
Mozul Rémy's avatar
Mozul Rémy committed
19

Mozul Rémy's avatar
Mozul Rémy committed
20 21 22
  @cython.locals(a=numpy.ndarray)
  def __init__(self,a):
    a = numpy.asfarray(a)
23 24 25 26 27 28 29 30
    self.x = {'i':a.shape[0],'j':a.shape[1],'mat':<double *>a.data}
    #cdef double * params  = cython.address(self.x).params
    #self.p_params = numpy.asfarray(<numpy.double64_t[:4]> params)
    #self.p_params = numpy.asfarray(<numpy.double64_t[:4]> cython.address(self.x).params)
    cdef numpy.npy_intp s[1]
    s[0] = <numpy.npy_intp> 4
    self.p_params = numpy.PyArray_SimpleNewFromData(1, s, numpy.NPY_DOUBLE, cython.address(self.x).params)
    self.p_params[0:4] = 0.
Mozul Rémy's avatar
Mozul Rémy committed
31 32 33 34 35 36
  
  def dist(self):
    return math.sqrt(self.x.i**2 + self.x.j**2)

  def incr(self):
    increment(cython.address(self.x))
37
    self.p_params[0:4] += 1.
Mozul Rémy's avatar
Mozul Rémy committed
38 39 40 41 42 43 44

  def prin(self):
    print_type(cython.address(self.x))

#  @cython.returns(cython.struc)
#  def get_x(self):
#    return self.x