On Sunday, May 8, 2016 at 1:10:44 PM UTC-7, Max Külshammer wrote:
>
> Thanks for answering! I was in holiday. Now I tested:
>
> %cython
>
> from sage.rings.integer cimport Integer
>
> cpdef int W():
>     cdef Integer x = 5
>     cdef Integer y = 2
>     if x._is_power_of(y) == True:
>         print 'yes'
>     return 1
>
> It looks like the "typecast" from "int" to "Integer" is not picked up 
automatically, so you have to spell it out:

    cdef Integer x = Integer(5)

Oddly enough, it does work in other situations. If you write
 
    cdef Integer x = Integer(5)+1

it will figure out how to convert the "1" to be an Integer (the conversion 
and addition will still be performed at runtime, though, because the C 
compiler cannot be expected to fold "constants" through user-defined types).

so this might actually be an issue in cython that can be improved: insert 
better code convert type of rvalue to that of the lvalue.
 

> But got the error:
>
> Exception TypeError: 'Cannot convert int to sage.rings.integer.Integer'
>
> Any ideas?
> Max
>
>
>
>
> Am Montag, 2. Mai 2016 10:47:23 UTC+2 schrieb Simon King:
>>
>> Hi Max, 
>>
>> On 2016-05-01, Max Külshammer <[email protected]> wrote: 
>> > I would like to use a sage function (is_power_of) in an cython program 
>> I am 
>> > writing (via %cython in SMC). To speed things up I would like to import 
>> the 
>> > c version of is_power_of which can be found 
>> > in 
>> https://github.com/sagemath/sage/blob/master/src/sage/rings/integer.pyx#L4271
>>  
>> > as 
>> https://github.com/sagemath/sage/blob/master/src/sage/rings/integer.pxd#L26. 
>>
>> > 
>> > Can someone explain how I import such an function into cython? My 
>> failed 
>> > attemps look like this: 
>> > 
>> > %cython 
>> > from libc.math cimport log 
>> > cimport integer 
>> > from integer cimport _is_power_of 
>>
>> _is_power_of is a method of the extension class "Integer". Hence, when 
>> doing 
>>   from sage.rings.integer cimport Integer 
>> and then have something like 
>>   cdef Integer x = 5 
>> in your code, then you can do 
>>   x._is_power_of(2) 
>>
>> Best regards, 
>> Simon 
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to