By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,347 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,347 IT Pros & Developers. It's quick & easy.

Boost Rational Number Package for C++

BenTheMan
P: 8
Hello all. I will probably be frequenting these discussions in the future. I am a graduate student in physics learning C++ on the fly.

This is probably an easy quesiton, but my background in this subject is very limited. I have tried a few permutations of what I think SHOULD be right, but the compiler is giving me very cryptic errors (like 'template argument is invalid'). I cannot find any examples of what I want to do on line, and I am tied to the Boost/Rational class, as that is what my collaborators are using.

I want to construct a matrix using the vector class. This is not a problem, but all of the entries in that matrix should be rational numbers. For example, I can declare a 2d vector using

vector <vector< int> > vec;

Is the corresponding statement for the rational class

vector <vector< rational<int> > > vec;

or

vector< vector< rational> >

(or none of these)?

Secondly, I wish to write a function that returns a rational number, or a matrix of rational numbers. When I declare that function at the onset of my program, how should I do so? These functions take in and spit out vectors and matrices. I have tried something like

rational<int> Length( const vector<rational>& vec );

but I get an error that says "expected constructor, destructor, or type conversion before < token".

Any help in these matters would be appreciated---feel free to direct me to a website or some other resource.

PS---If anyone needs help in physics or math, I am a moderator at livephysics.com and frequent the boards at ilovephysics.com.
Nov 9 '06 #1
Share this Question
Share on Google+
3 Replies


BenTheMan
P: 8
I have decoded the declaring things part, I still don't know how to PASS vectors of rational numbers (or even rational numbers) between functions.

Thank again in advance.
Nov 9 '06 #2

P: 1
While you can definitely do what you want with vector<T>, I might suggest for math-intensive operations to stay away from it. While its name is vector, and would make you think it's mathy, it's really bent more on the purposes of dynamic arrays.

If you really want to go through and use them, here is some untested code:
Expand|Select|Wrap|Line Numbers
  1. vector<int> dotproduct(const vector<vector<int> > &m, vector<int> &v) {
  2.   vector <int> r;
  3.   results.resize(m.size());
  4.   for (int i = 0; i < m.size(); i++) {
  5.     r[i] = 0;
  6.     for (int j = 0; j < v.size(); j++) {
  7.       r[i] += m[i][j]*v[j];
  8.     }
  9.    return r;
  10. }
  11.  
  12. int main(int argc, char **argv) {
  13.   vector<vector<int> > m;
  14.   vector<int> v;
  15.  
  16.   m.resize(10);
  17.   for (int i = 0; i < m.size(); i++) m[i].resize(10);
  18.  
  19.   vector<int> r = dotproduct(m, v);
  20. }
  21.  
Of course, not only is it untested, it's 1:30, so I most likely got the algorithm wrong. But the syntax should all be good. Whenever you pass an instance of a class, you should always pass it as a reference (the '&'). Otherwise, the instance will go out of scope, and the destructor will be called.

I hope I also got across how unwieldly it is to use the vector class for this type of thing. It's painful.

There's a bunch of free libraries out there to do exactly what you want. For example, there is Numerical Recipes in C++, you can use the GSL (GNU science Libraries) and wrap them in c++ if you'd like. If you just google for 'c++ matrix' you'll find a bunch of hits, like:

http://math.nist.gov/sparselib++/

Another question is, how strongly tied are you to c++? I see a lot of posts here with 'I need to use c++', but without any good reasons given. Python has an excellant library called matplotlib and numarray. Together they can not only do really fast matrix operations (it's based on BLAS, which is essentially what Matlab uses), but it can make the pretty pictures too.

In addition, if you still like the idea of using c++, there is boost::python that let's you easily extend Python with c++ and embed Python into c++.
Nov 10 '06 #3

BenTheMan
P: 8
Thank you for your reply.

First, I am pretty tied to using vectors and rational numbers, for now. I am getting started in a research program, and the code is currently written using these classes. In the future, once I've my feet under me, I will certainly look into these things. We will be dealing with large sets of data, and optimization is definitely a concern in the future.

Second, I am still confused about how to pass vectors of rational numbers. If I want a function to return a vector full of integers, I can use

Expand|Select|Wrap|Line Numbers
  1. vector< vector<int> > Function ( const vector< vector<int> >&x )
etc. I have no idea how to tell the function I want it to return a vector full of rational numbers. When I use

Expand|Select|Wrap|Line Numbers
  1. vector<vector<rational> > Function ( const vector< vector<rational> > )
I get errors like "Rational not declared in this scope" (in the function template) and again when I define the function, along with "template argument invalid".

I don't have any problems declaring vectors of rational numbers in my functions, I can use

Expand|Select|Wrap|Line Numbers
  1. typedef boost::rational<int> Fraction;
  2.  
  3. vector<Fraction>  LotsOfFractions;
  4.  
How do I pass these things in and out of functions?

Thanks again!
Nov 10 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.