![]() |
Blitz Support : |
From: Ben McLean \(finlaylabs\) (bmclean_at_[hidden])
Date: 2003-11-11 19:52:46
Thanks Julian for your detailed reply! I am at home at the moment but about
to go to work (other side of the world to you), and will work thru your
comments there and try and get things going. My desired outcome would be to
construct a zero-size array when the Data class is created - ie have Data
own the array as a member (I do know type and dimension at complile time,
but finding the size of the array is a later Data-class function). Then
after resize()ing the array (also in Data) I need to use it within other
classes (eg Functions) and maybe within main() itself.
In this case, could I move
Array<double,2> myArray; //or blitz::Array<double,2> myArray;
to within Data.h along with other data members declarations like
double *anotherarray;
double xstep, ystep, zstep;
Then resize the array within Data.cpp (should be easy enough)
Then use the array within other classes as like
cout << "myArray = " << Data.myArray << endl;
?
Thanks for the effort,
Ben.
----- Original Message -----
From: "Julian Cummings" <cummings_at_[hidden]>
To: "Support list for Blitz++" <blitz-support_at_[hidden]>
Sent: Wednesday, November 12, 2003 6:08 AM
Subject: Re: [Blitz-support] how to make instances of blitz arrays
> Hello Ben,
>
> It seems like you are looking to construct an Array in main() but
> actually initialize
> the Array somewhere else. That is perfectly fine as long as the
> initializing function
> can refer to the Array object somehow. The problem with your code below
> is that
> the blitz Array object test2D initialized in Data::initializeArrays() is
> not the same
> as the object myArray back in main().
>
> There are several ways of dealing with this, depending on who should own
> the Array.
> If you want to define the Array as a global static variable before the
> main() function,
> as you did below, then you would need an extern declaration of the same
> Array that
> was visible in the Data class definition. That way, Data could refer to
> the Array object
> in order to initialize it. Generally speaking, such global static
> objects are deprecated
> in C++, since they violate the concept of encapsulation. If you declare
> an Array object
> within the main() function, then it really makes sense to pass the Array
> to the Data class
> in order to have it be initialized. You could do this in the Data
> constructor or in the
> call to Data::initializeArrays(). A third way to go would be to have
> the Data class actually
> own the Array as a data member. Then the Array would get constructed
> when the Data
> class was created.
>
> Another important thing you should be aware of is the resize() method of
> the blitz Array.
> If you construct a blitz Array with no constructor arguments, you get an
> Array with the
> default storage mechanism (C-style array storage) and zero size. You
> can then call the
> resize() method on this Array to set the extents of the array, using
> either a set of ints, a set of blitz Range objects, or a blitz
> TinyVector of ints. This is the approach to use when you do
> not know at Array construction time what size of Array you want. (You
> do have to set the
> element type and array dimension at compile time, of course.) Given
> this, you might change
> your example code as follows:
>
> // main()
> using namespace std;
>
> #include "Data.h"
> #include "blitz/Array.h"
>
> Data *myData;
> Array<double,2> myArray;
>
> int main(inyt argc, char *argv[]) {
> // construct Data object pointed to by myData somehow ...
>
> myData->initializeArrays();
> cout << "Array myArray = " << myArray << endl;
>
> return 0;
> }
>
> // Data.cpp
> #include "Data.h"
> #include "blitz/array.h"
> extern Array<double,2> myArray;
>
> int Data::initializeArrays() {
> int nx = ny = 3;
> myArray.resize(nx,ny);
> myArray = 0.0;
> return 0;
> }
>
>
> I hope this example clears things up for you. By the way, the issue of
> the blitz namespace has nothing to do with your current problem. When
> you say "using namespace blitz", this
> merely brings all the types and methods defined within namespace blitz
> into the current
> scope. It does not create any Array objects or anything like that.
>
> Regards, Julian C.
>
>
> Ben McLean (finlaylabs) wrote:
>
> > Hi all,
> >
> > after downloading basic cygwin and adding in gcc, make, etc, I have
> > been able to (error-free) make Blitz. Very impressive! A bit of
> > jiggling in Dev-C++ and it is a useable tool already. Well done and
> > thanks.
> >
> > All the docs and examples use
> > using namespace blitz;
> >
> > As I am (trying to) use blitz as part of a larger multi-class project
> > i do not want to use namespace blitz, rather i would prefer to use a
> > constructor to create instances of Array or whatever that I can use in
> > main or from any other class, especially if i actually create arrays
> > in a class functiuon outside of main. What is the syntax for doing
> > this? My best guesses do not work, an example guess that may clarify
> > what i am trying to do is below. I would appreciate edits / comments
> > on the basic example below from anyone who can show me how to do this.
> >
> > Thanks, Ben.
> > ____________________________________________
> > // main()
> > using namespace std;
> >
> > #include "Data.h"
> > #include "blitz/array.h"
> >
> > Data *myData;
> > Array myArray; //or use Blitz::Array myArray; or blitz myBlitz;,
> > something... need help here
> >
> > int main(int argc, char *argv[]){
> >
> > Data::initializeArrays();
> >
> > cout << "Array test2D = " << myArray.test2D << endl;//access an
> > array created
> >
> > // within another class
> >
> > return(0);
> > }
> > _____________________________________________
> > // Data.cpp
> > #include "Data.h"
> > #include "blitz/array.h"
> >
> > int initializeArrays(){
> > int nx=ny = 3;
> > blitz::Array<double,2> test2D(nx,ny);//made the array here,
> > // but want to access it from main() or elsewhere
> > test2D = 0.0;
> > return(0);
> > }
> > _____________________________________________
> > // Data.h
> > #include "blitz/array.h"
> >
> > class Data{
> > public:
> > Data(Params *myParams);//constructor
> > ~Data();//destructor
> >
> > int initializeArrays();
> > };
> >
> >
> >------------------------------------------------------------------------
> >
> >_______________________________________________
> >Blitz-support mailing list
> >Blitz-support_at_[hidden]
> >http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
> >
> >
>
> --
> Dr. Julian C. Cummings E-mail:
cummings_at_[hidden]
> California Institute of Technology Phone: 626-395-2543
> 1200 E. California Blvd., Mail Code 158-79 Fax: 626-584-5917
> Pasadena, CA 91125
>
>
>
> _______________________________________________
> Blitz-support mailing list
> Blitz-support_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>