473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Boost Rational Number Package for C++

BenTheMan
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
3 3356
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
bram
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
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

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

Similar topics

21
by: Mike Meyer | last post by:
PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org> Status: Draft Type: Staqndards...
20
by: Mike Meyer | last post by:
This version includes the input from various and sundry people. Thanks to everyone who contributed. <mike PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $...
0
by: Li Daobing | last post by:
I can't use .def(str(self)) I write a simple example, without `str', I can build it well, but with this one, I can't build //Rational.cpp #include <boost/python.hpp> #include <iostream> ...
2
by: Martin Herbert Dietze | last post by:
Hi, in a project I am using callbacks which are called like ordinary functions but in fact are Loki::Functor's encapsulating calls to non-static member functions on instances of different...
3
by: TonyHa | last post by:
Hello I try to install boost_1_33_1 on RedHat Linux. I have downloaded boost into /user/dtgtools/tmp and I try to install it into /user/dtgtools/packages/boost. I use the following commands:...
25
by: Martin Manns | last post by:
Hi, I am starting to use rationals and since I found no batteries included, I tried out the mxNumber package. However, I get strange warnings on comparison operations (which however seem to...
6
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
1
by: ben kipkorir | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
5
by: anumliaqat | last post by:
hello!! i hav a problem.my program is giving correct outputs for some inputs but wrong results for others. for example if u give (4 2 2)&(2 1 2) to it,it shows all results correct....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.