Presentation and help with class containing two valarrays

A

askmeofit

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<double> v; // values of the path
valarray<double> t; // 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<double> v(maxsize);
valarray<double> t(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
 
L

Lionel B

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<double> v; // values of the path
valarray<double> t; // 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<double> v(maxsize);
valarray<double> t(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)
{
...
}
 
M

mlimber

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<double> v; // values of the path
valarray<double> t; // at these times

You could use a std::pair<double,double> or 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<double> v(maxsize);
valarray<double> t(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-lite/ctors.html#faq-10.6

Cheers! --M
 
A

askmeofit

Thank to both of you for the replies!
And...
You could use a std::pair<double,double> or 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...
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).
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
 
P

Puppet_Sock

On Feb 23, 12:14 pm, (e-mail address removed) 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
 
L

Lionel B

On Feb 23, 12:14 pm, (e-mail address removed) 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<double> x = ...
valarray<double> y = log(x);

or algorithms:

valarray<double> x = ...
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top