Picon

Passing a bounded method in Cython as argument

I am trying to wrap some C++ code into Cython and I came up with some trouble trying to pass a method from a class as an argument to a function.

I do not know if it makes it more clear, but class A represents a statistical model (so myAMethod uses not only the arguments passed but many instance variables) and B has different methods for minimizing the function passed.

In C++ I have something of this style: 

class A
{
public:
   
double myAMethod(double*);
};

class B
{
public:
   
double myBMethod(A&, double (A::*f) (double*);
};

So what I am trying to do is to use instances of both A and B in Cython code. I had no trouble wrapping the classes, but when I try to use myBMethod, I don't know how to pass a pointer of the kind


 A::*myAMethod

If I do this:


 myBMethod(ptrToAObj[0], &ptrToAObj.myAMethod)

, then Cython compiles this code to


[...] &ptrToAObj->myAMethod [...]

, and I get the message one would expect from g++:

    "ISO C++ forbids taking the address of a bound member function to form a pointer to member function."

But if I try to point straight to the class method, and do

myBMethod(ptrToAObj[0], A.myAMethod)

,

then Cython won't compile and say that

myAMethod is not a static member from A.

And that's pretty much all I was able to advance. I could work at C++ level and avoid any of these anoyances, but if I were able to use instances of A and B in Python (via Cython) interactively, it would help me in speedig my development pace. Any help will be really appreciated, and I apologize if this question as been already answered and/or is available in a referece - I searched SO, Cython reference and Smith's "Cython" book and I did not found this theme adressed. Thanks in advance!

PS I have put this exact same quiestion in SO.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Martin Bammer | 17 Mar 17:53 2015
Picon

Improvement suggestion

Hi,

following example produces slow code, because dict is always created newly with each call:

def test(x):
    return { "A" : 1, "B" : 2 }[x]

But when I rewrite this to:

TestDict = { "A" : 1, "B" : 2 }

def test(x):
    return TestDict[x]

Then the dict is only created once and the code executes much faster.
My suggestion would be that Cython detects such dicts, lists and tuples and automatically optimized the code like above.

Further if a global dict, list, tuple with only "static" items, as above, is used within the same module as it was created the reference counting could be omitted.

Cheers, Martin

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
cjw | 13 Mar 23:21 2015
Picon

Install Failure

At the start of the installation, I get:

Unable to find pgen, not compiling formal grammar.
running install
running bdist_egg
running egg_info
writing Cython.egg-info\PKG-INFO

At the tail of the installation I get: C:\Downloads\Cython-0.22>python setup.py install > installation-022.txt warning: no files found matching '*.pyx' under directory 'Cython\Debugger\Tests' warning: no files found matching '*.pxd' under directory 'Cython\Debugger\Tests' warning: no files found matching '*.h' under directory 'Cython\Debugger\Tests' warning: no files found matching '*.pxd' under directory 'Cython\Utility' C:\Downloads\Cython-0.22>

The rest of the installation has the right flavour.

I am a newcomer to Cython, does anybody have suggestions? Colin W.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan Goldbaum | 11 Mar 01:04 2015
Picon

Unable to use the -a option for the cython magic in python 3

Hi all,

I'm trying to use the cython cell magic in IPython 3.0.  While the vanilla version of the magic works well, I'm unable to use the -a option to generate an annotated HTML view of my cython snippets.

Here is a bare-bones example that exhibits the issue I'm seeing:

http://nbviewer.ipython.org/gist/ngoldbaum/855a629d997aa7959254

Thanks for any help you can provide,

-Nathan

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sebastian Koslowski | 13 Mar 13:09 2015
Picon

Compiler error with complex-valued Typed Memoryview and addition assignment operator

Hey,

I get an unexpected error when using the addition assignment operator on a element of a complex-valued Typed Memoryview
Consider the following example code:

    import numpy as np
    cdef float[:] f = np.zeros(10, dtype="float32")
    cdef float complex[:] c = np.zeros(10, dtype="complex64")

    f[0] += 1   # works fine
    c[0] += 1  # fails (see below)

The error output is

    cython_error.c: In function ‘PyInit_cython_error’:
    cython_error.c:13216:130: error: invalid operands to binary + (have ‘__pyx_t_float_complex’ and ‘__pyx_t_float_complex’)
       *((__pyx_t_float_complex *) ( /* dim=0 */ (__pyx_v_12cython_error_c.data + __pyx_t_8 * __pyx_v_12cython_error_c.strides[0]) ))         += __pyx_t_float_complex_from_parts(1, 0);
                                                                                                                                          ^
    error: command 'gcc' failed with exit status 1

Note, that the float version works fine, only the complex-valued fails. My work-around at the moment is

    cdef float complex *cp
    cp = &c[0]
    cp[0] += 1


Sebastian

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Christoph Groth | 12 Mar 16:06 2015
Picon

cumbersome complex numbers

Dear all,

Consider the following Cython program:

cimport libcpp.vector
cdef libcpp.vector.vector[double complex] v
cdef double complex a = 0, b = 1j
v.push_back(b)
a += v[0]
a += b
print a

With Cython 21.1 the line "a += v[0]" is colored yellow in HTML
output and compiles to

  __pyx_t_1 = (__pyx_v_11complextest_v[0]);
  __pyx_v_11complextest_a = __Pyx_c_sum(__pyx_v_11complextest_a,
__pyx_t_double_complex_from_parts(__Pyx_CREAL(__pyx_t_1), __Pyx_CIMAG(__pyx_t_1)));

While the line "a += b" is colored white and compiles to

  __pyx_v_11complextest_a = __Pyx_c_sum(__pyx_v_11complextest_a, __pyx_v_11complextest_b);

In both cases a complex number is added to another one, but in the first
case that number is taken from a STL vector.

Is this how it is supposed to work?  I suppose that in this simple case
the redundant splitting-and-recombination of v[0] will be optimized
away by the C++ compiler, but in more complicated cases (where I've stumbled
across this issue), I'm not so sure.

All the best
Christoph

--

-- 

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

Luc Bourhis | 11 Mar 18:34 2015
Picon

Is it all right to abuse "cdef cppclass" for non-C++ types?

I found the following trick useful:

cdef extern from "cryspy/math/tinyvec.h":
    cdef cppclass vec4i
:
        vec4i
operator+(vec4i)

where vec4i is a GCC vector extension (which clang supports as well):

typedef int vec4i __attribute__ ((vector_size(16)));

This works because Cython does not care about what is actually in that header: it uses the "cdef cppclass" to deduce that

cdef vec4i x, y, z
z
= x + y

translates to C (yes, C not C++) as

vec4i x, y, z;
z
= x + y;

and that's it. So my question is whether one day some evolution of Cython will not support this trick anymore.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
T J | 11 Mar 16:53 2015
Picon

pyximport side-effects

Hi,

I have pre-compiled Cython extensions and they normally import without issue. But now, I'm importing another library first which loads and installs pyximport. When my module is subsequently loaded, it no longer tries to import the .so file and instead tries to build it (which fails).

Is there a way that I can isolate the effects of pyximport? This seems odd, so perhaps something is misconfigured?

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jerome Kieffer | 11 Mar 10:40 2015

Cython low-end computer ...

Hi,

I am testing my code on low-end computers, for example the Raspberry pi.
My test suite seg-faults and while debugging I discovered that malloc
failed (returned a null pointer).
Many users of my code reported such crashes on windows 32 bits and I suspect they
are facing the same error.

As all malloc have been removed from my cython code, thanks to
memory-views, this comes either from Cython generated code, either from
NumPy.

How can I know where these culprit malloc are called ?
Thanks,
-- 
Jérôme Kieffer

--

-- 

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

Jeroen Demeyer | 10 Mar 15:09 2015
Picon

Empty documentation pages

I just noticed that the following pages of documentation are empty:

http://docs.cython.org/src/reference/limitations.html
http://docs.cython.org/src/reference/special_mention.html

--

-- 

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

Brian | 10 Mar 06:36 2015
Picon

Fast numpy array declarations -- what's possible today?

Hi all,

From Using Cython with Numpy: "Fast array declarations can currently only be used with function local variables and arguments to def-style functions (not with arguments to cpdef or cdef, and neither with fields in cdef classes or as global variables). These limitations are considered known defects and we hope to remove them eventually. In most circumstances it is possible to work around these limitations rather easily and without a significant speed penalty, as all NumPy arrays can also be passed as untyped objects."

I think those docs are a little old, so I am curious to know if the situation has changed since? From what I understand, typed memoryviews initialized by numpy arrays do not still retain their numpy functionality (e.g. two such memoryviews cannot be added together using the + operator). So, how would one go about using fast array declarations in a cdef class?

--

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

Gmane