473,666 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boost Rational Number Package for C++

BenTheMan
8 New Member
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.co m.
Nov 9 '06 #1
3 3382
BenTheMan
8 New Member
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 New Member
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
8 New Member
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
2409
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 Content-Type: text/x-rst Created: 16-Dec-2004 Python-Version: 2.5
20
2133
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 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org>
0
2126
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> using namespace std; using namespace boost::python;
2
4020
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 classes. The approach using Loki::Functor looks like this: | class X {
3
5628
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: export PYTHON_ROOT="/user/dtgtools/packages/python/2.5"
25
2287
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 yield correct results): --- $ python
6
4641
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 real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ½ might actually be represented as 0.49998, which may not be good enough for some applications. In this...
1
2855
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 real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ½ might actually be represented as 0.49998, which may not be good enough for some applications. In this...
5
4619
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. but for (2 2 4)&(2 1 4) it gives wrong outputs. now i am unable to find which function is to be corrected and which one not. plz plz help me i have to submit it on 01 dec.
0
8363
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8561
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.