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

Presentation and help with class containing two valarrays

P: n/a
I'm a Phd student in statistics, and I'm writing computer code for the
simulation of stochastic differential equations.
Little background about my knowledge: I know C programming decently,
and my main reference for C++ programming is the book: "C++ and Object-
oriented Numeric Computing for scientists and Engineers" which I
studied chapter to chapter 1 year ago and I'm in the process of
studying it again (I forget things easily....).

I would like to define a class to store the simulated path of the
stochastic process:

#include <iostream>
#include <valarray>
using namespace std;

class Path {
static long int maxsize;

public:
valarray<doublev; // values of the path
valarray<doublet; // at these times
long size; // actual lenght of the path
double x0, delta, T; // starting point , delta of
discretization, ending time
Path(long int n);
Path(); //default constructor
};

The problem is that now I don't know how to define the default
constructor.
I want to initialize v and t as follow:
valarray<doublev(maxsize);
valarray<doublet(maxsize);

Is there an easy way to do it?
Or do I have to revert to the usual pointer do data approach?
Thank you

StephQ

Feb 23 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 23 Feb 2007 07:38:52 -0800, askmeofit wrote:
I'm a Phd student in statistics, and I'm writing computer code for the
simulation of stochastic differential equations.
Little background about my knowledge: I know C programming decently,
and my main reference for C++ programming is the book: "C++ and Object-
oriented Numeric Computing for scientists and Engineers" which I
studied chapter to chapter 1 year ago and I'm in the process of
studying it again (I forget things easily....).

I would like to define a class to store the simulated path of the
stochastic process:

#include <iostream>
#include <valarray>
using namespace std;

class Path {
static long int maxsize;

public:
valarray<doublev; // values of the path
valarray<doublet; // at these times
long size; // actual lenght of the path
double x0, delta, T; // starting point , delta of
discretization, ending time
Path(long int n);
Path(); //default constructor
};

The problem is that now I don't know how to define the default
constructor.
I want to initialize v and t as follow:
valarray<doublev(maxsize);
valarray<doublet(maxsize);

Is there an easy way to do it?
Path::Path() : v(maxsize), t(maxsize)
{
...
}

and you probably also want:

Path::Path(long int n) : v(n), t(n)
{
...
}

--
Lionel B
Feb 23 '07 #2

P: n/a
On Feb 23, 10:38 am, askmeo...@mailinator.com wrote:
I'm a Phd student in statistics, and I'm writing computer code for the
simulation of stochastic differential equations.
Little background about my knowledge: I know C programming decently,
and my main reference for C++ programming is the book: "C++ and Object-
oriented Numeric Computing for scientists and Engineers" which I
studied chapter to chapter 1 year ago and I'm in the process of
studying it again (I forget things easily....).

I would like to define a class to store the simulated path of the
stochastic process:

#include <iostream>
#include <valarray>
using namespace std;

class Path {
static long int maxsize;

public:
valarray<doublev; // values of the path
valarray<doublet; // at these times
You could use a std::pair<double,doubleor your own struct so that
you only have one valarray. This might simplify some things since
you'll always have a time for each value.
long size; // actual lenght of the path
Is this the same as v.size()? If so, why even keep it?
double x0, delta, T; // starting point , delta of
discretization, ending time
Path(long int n);
Path(); //default constructor

};

The problem is that now I don't know how to define the default
constructor.
I want to initialize v and t as follow:
valarray<doublev(maxsize);
valarray<doublet(maxsize);

Is there an easy way to do it?
Or do I have to revert to the usual pointer do data approach?
See Lionel B's response and this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-10.6

Cheers! --M

Feb 23 '07 #3

P: n/a
Thank to both of you for the replies!
And...
>
You could use a std::pair<double,doubleor your own struct so that
you only have one valarray. This might simplify some things since
you'll always have a time for each value.
Do you mean std::pair<valarray<double>,valarray<double>?
As it's my first "real life" proj in c++ I'm trying to keep things as
easy as possible...
>
long size; // actual lenght of the path

Is this the same as v.size()? If so, why even keep it?
No it is not, sorry if I didn't specify this.
The point is that for some schemes also the number of elements in the
valarrays is random, so you just choose these valarrays big enough
(maxsize).
http://www.parashift.com/c++-faq-lit....html#faq-10.6
Very interesting.
I knew of the member list init syntax, but the faq is very good. I'm
printing it now :)
>
Cheers! --M
Cheers!
StephQ

Feb 23 '07 #4

P: n/a
On Feb 23, 12:14 pm, askmeo...@mailinator.com wrote:
[snip]
Do you mean std::pair<valarray<double>,valarray<double>?
I'm thinking probably the other way around.

valarray<pair<double,double

(plus appropo uses of std:: that I'm too lazy to put in).

Also, be careful about the on the end, and put a space
between them. Otherwise your compiler may get confused with
the >operator.

For anybody who cares to answer: When would valarray be
preferred over vector or one of the other standard
containers?
Socks

Feb 23 '07 #5

P: n/a
On Fri, 23 Feb 2007 09:35:52 -0800, Puppet_Sock wrote:
On Feb 23, 12:14 pm, askmeo...@mailinator.com wrote:
[snip]
>Do you mean std::pair<valarray<double>,valarray<double>?

I'm thinking probably the other way around.

valarray<pair<double,double
Actually, my suspicion is that you _don't_ want this, as it might make
maths with your arrays trickier and less efficient. For instance, you
can do some nice stuff with valarray's like applying functions:
valarray<doublex = ...
valarray<doubley = log(x);

or algorithms:

valarray<doublex = ...
double mean = x.sum()/x.size();
double stdev = sqrt(((x-mean)*(x-mean)).sum()/(x.size()-1));

and so on.
(plus appropo uses of std:: that I'm too lazy to put in).

Also, be careful about the on the end, and put a space between them.
Otherwise your compiler may get confused with the >operator.

For anybody who cares to answer: When would valarray be preferred over
vector or one of the other standard containers?
Actually valarray is not a very "standard" container, as it fails some of
the criteria to be one (please don't ask me which...). Its syntax is kind
of flaky too.

One of the main reasons to use valarray is that it is potentially more
optimisable than (say) std::vector, since it is guaranteed not to be
"aliased".

Another is that it is designed to be able to implement efficient
multi-dimensional arrays via "slices".

--
Lionel B
Feb 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.