473,327 Members | 1,979 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,327 software developers and data experts.

Presentation and help with class containing two valarrays

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
5 1645
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike C. Fletcher | last post by:
Slides from my PyGTA presentation on Tuesday, focusing mostly on why/where you would want to use meta-classes, are available in PDF format: ...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
7
by: Silver | last post by:
First of all...merry christmas everyone! Now, I have to write a program. Among other things, I need to overload the '*' operator, so that it returns a pointer-to-ponter of type int, which is...
3
by: Sebastian Thiebes | last post by:
Hello all, I am using valarrays of double in my code, but for using certain functions I need to pass a normal array of double. What I did was write something like this: valarray<double> vad;...
0
by: Larry Baxter | last post by:
We have some computationally intense code that would greatly benefit from any floating point performance enhancement we could find. We have tests valarrays vs vectors at all optimization levels...
13
by: Michelle | last post by:
Hi all... I could use a little TLC here for understanding and not for solving a specific problem... sorry if I've got the wrong group, but I'm using VB so I figured this was the most appropriate...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: Clark Simpson | last post by:
Hi, Apologies if this is such a simple query but if I have an ASP/.NET Application which I normally access via www.foo.com/myapp on Machine A can I access this application layer machine via a...
20
by: d.s. | last post by:
I've got an app with two classes, and one class (InventoryInfoClass) is an object within the other class (InventoryItem). I'm running into problems with trying to access (get/set) a private...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.