Ask Your Question

How to define a smart class using cython?

asked 2010-11-01 20:08:45 -0500

roland gravatar image

Hi, I managed to rewrite my problem so I only have to represent numbers in the following way: [(2,3),(3,3),(13,1)] like in list(factor(82713)). Thus [(prime,exponent), (prime,exponent), ...].

With such a representation multiplying is easy, and I can skip to factor numbers (again). What I'm looking for is a fast cython approach with a class where I can define a function for multiplying, but also where I'm able to store and retrieve a large set of numbers efficiently (within the program). I looked for examples in the Cython Tutorial et cetera, but I could not find an approach which helped me. Maybe someone is willing and able to share his/her expertise? Thanks in advance! Roland

edit retag flag offensive close merge delete


Could you give some more information, e.g., examples, about your problem and what your class should do?

Mitesh Patel gravatar imageMitesh Patel ( 2010-11-02 23:58:16 -0500 )edit

It is always better to start with a Python class. Once you are sure about your data structure you can switch to cython and this is easy. Once you are in cython it becomes much harder to rewrite your class.

vdelecroix gravatar imagevdelecroix ( 2014-06-29 09:58:09 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-05-14 01:29:52 -0500

nbruin gravatar image

I think this class is already implemented in sage:

sage: a,b
(2 * 3, 3 * 5)
sage: a*b
2 * 3^2 * 5
sage: a=factor(6); a
2 * 3
sage: b=factor(15); b
3 * 5
sage: a*b
2 * 3^2 * 5
sage: type(a*b)
<class 'sage.structure.factorization.Factorization'>
sage: type(a)
<class 'sage.structure.factorization_integer.IntegerFactorization'>

A good start would be to compare your code with what is implemented in sage already. It might give some hints.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2010-11-01 20:08:45 -0500

Seen: 427 times

Last updated: May 14 '15