Axel Etzold wrote:
Dear Ed,
Is your C code one of the "well-known" open source libraries that do
this kind of computation, or is it home-grown? The reason I ask is that
there are quite a few Python interfaces to the well-known libraries, and
it would be relatively easy to port some of them to Ruby.
It is home-grown ... partly because the divisions I have to do
involve several variables (see an example here: Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos) ... I am not aware that there are any "well-known" open-source libraries for this as it seems a more exotic part of computational algebra after all.
Groebner basis calculations are built in to most computer algebra systems, and there are some highly-tuned C-language libraries available. I don't know the field (pun unintended) well enough to know anything more than the names, but the ones that show up in the Gentoo repository are
* sci-mathematics/Macaulay2
* sci-mathematics/axiom
* sci-mathematics/gap
* sci-mathematics/ginac
* sci-mathematics/mathomatic
* sci-mathematics/maxima
* sci-mathematics/pari
* sci-mathematics/singular
* sci-mathematics/yacas
And there are some others in the Python-based Sage project at
There is also a project called "swiginac" which is building Python wrappers for the GiNaC library using SWIG.
[snip]
These things are done in Ruby, as it is so much nicer to code in
and as this is a part of the problem where speed of computation
is not so critical, as most of the computation is done in the division
process written in C.
Now, to connect the two, I looked at this tutorial:
How to create a Ruby extension in C in under 5 minutes
I've managed to make this work without any problem.
Now, what I still do not understand is quite what SWIG does precisely
for the communication of Ruby and C.
SWIG is fairly automatic once you define the interface (.i) files. Sometimes, all you need to do is feed SWIG the header files for the C library and it can generate the wrapper code automatically. But more often, you need to know the kinds of objects that must pass across the scripting language-library interface.
In the case of Ruby, you can define classes, methods and objects in C or C++ and SWIG will build wrapper code you can use to access these from Ruby. As I said earlier, if you're only interested in Ruby as the scripting language, you don't need SWIG.
The SWIG documentation is quite comprehensive, and there are quite a few open source packages that make good use of SWIG and can be used for coding examples if you want to go that way. Try
http://www.swig.org/Doc1.3/Contents.html#Contents
Read the first two chapters, and the third chapter if you're using Windows. Then read the Ruby chapter, chapter 30, at
http://www.swig.org/Doc1.3/Ruby.html#Ruby
1.) I faintly recall reading a post on this list that SWIG could be used not only to extend Ruby with C, but also the other way round ... so I was
thinking that maybe I could use the Ruby's Rational to extend
my C code .. if you have any pointers for extending C with Ruby, please
let me know.
I'm not sure why you'd want to do Rational arithmetic in Ruby from a C program. There are a couple of decent C libraries for arbitrary-precision integer and rational arithmetic. Try CLN ("Common Lisp Numbers", which is used in GiNaC) and "gmp" (Gnu Multi-Precision).