comp.lang.ada
 help / color / mirror / Atom feed
* array of matrices
@ 1996-05-02  0:00 Boaz Chow
  1996-05-03  0:00 ` John Herro
  0 siblings, 1 reply; 5+ messages in thread
From: Boaz Chow @ 1996-05-02  0:00 UTC (permalink / raw)



Hi there, 
 
I need to do matrix multiplication with arrays : 
 
A1 5x15 
A2 15x7 
A3 7X25 
 
I want to set up a variable for array (1..N) of Matrix; 
 
type Matrix is array (integer range <>, integer range <>) of integer; 
 
My question is how can I setup an array for that? 
 
I want something like this : 
 
A(1) := ( (1,2,3,4,5), 
               (2,3,4,5,6), 
                etc... 
             ); 
 
and A(2) := ( (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), 
                      etc... 
                    ); 
 
I know this won't work : 
 
A : array (1..N) of Matrix; 
 
please email me asap. 
 
Thanks! 
 
 
 
 
 
   ______   Meow 
   \ OO /  _/  
  :(__ =) 
    U \\ 
http://www.sci.csupomona.edu/~cchow




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array of matrices
  1996-05-02  0:00 array of matrices Boaz Chow
@ 1996-05-03  0:00 ` John Herro
  1996-05-04  0:00   ` Boaz Chow
  1996-05-10  0:00   ` Ron J Theriault
  0 siblings, 2 replies; 5+ messages in thread
From: John Herro @ 1996-05-03  0:00 UTC (permalink / raw)



boaz@usa.pipeline.com(Boaz Chow) writes:
> type Matrix is array (integer range <>, integer range <>) of integer; 
> I want to set up a variable for array (1..N) of Matrix; 
> I know this won't work:  A : array (1..N) of Matrix;
     You're right.  You can't directly set up an array of an unconstarined
type.  There are two things you can do.  One is to allow for the largest
possible matrix for every element in the array, and keep track of the
number of rows and columns actually used:

type Square15 is array(1 .. 15, 1 .. 15) of Integer;
type Matrix is
   record
      Data    : Square15;
      Rows    : Integer;
      Columns : Integer;
   end record;
...
A : array(1 .. N) of Matrix;
...
A(1).Rows    := 5;
A(1).Columns := 15;
A(2).Rows    := 15;
A(2).Columns := 7;
...

This has the disadvantage of using more memory than you actually need, but
is perhaps a bit simpler than the second way.  The second way is to
declare an array of *pointers*.  Each pointer can point to a different
size matrix:

type Matrix is array(Integer range <>, Integer range <>) of Integer;
type Ptr is access Matrix;
...
A : array(1 .. N) of Ptr;
...
A(1) := new Matrix(1 .. 5, 1 .. 15);
A(2) := new Matrix(1 .. 15, 1 .. 7);
...

I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array of matrices
  1996-05-03  0:00 ` John Herro
@ 1996-05-04  0:00   ` Boaz Chow
  1996-05-04  0:00     ` John Herro
  1996-05-10  0:00   ` Ron J Theriault
  1 sibling, 1 reply; 5+ messages in thread
From: Boaz Chow @ 1996-05-04  0:00 UTC (permalink / raw)



I have figured out the first method. 
 
This is what I have done : 
 
type Matrix_Size is array (1..50, 1..50) of integer; 
type Matrix is record 
    I : integer; 
   J : integer; 
   Info : Matrix_Size; 
end record; 
type test_array is (integer range <>) of Matrix; 
A : test_array(6); 
 
Your second method looks very good.  But how can I retrive the size (or
dimension) of the matrix? 
 
On May 03, 1996 14:30:18 in article <Re: array of matrices>,
'johnherro@aol.com (John Herro)' wrote: 
 
 
>boaz@usa.pipeline.com(Boaz Chow) writes: 
>> type Matrix is array (integer range <>, integer range <>) of integer;  
>> I want to set up a variable for array (1..N) of Matrix;  
>> I know this won't work:  A : array (1..N) of Matrix; 
>You're right.  You can't directly set up an array of an unconstarined 
>type.  There are two things you can do.  One is to allow for the largest 
>possible matrix for every element in the array, and keep track of the 
>number of rows and columns actually used: 
> 
>type Square15 is array(1 .. 15, 1 .. 15) of Integer; 
>type Matrix is 
>record 
>Data    : Square15; 
>Rows    : Integer; 
>Columns : Integer; 
>end record; 
>... 
>A : array(1 .. N) of Matrix; 
>... 
>A(1).Rows    := 5; 
>A(1).Columns := 15; 
>A(2).Rows    := 15; 
>A(2).Columns := 7; 
>... 
> 
>This has the disadvantage of using more memory than you actually need, but

>is perhaps a bit simpler than the second way.  The second way is to 
>declare an array of *pointers*.  Each pointer can point to a different 
>size matrix: 
> 
>type Matrix is array(Integer range <>, Integer range <>) of Integer; 
>type Ptr is access Matrix; 
>... 
>A : array(1 .. N) of Ptr; 
>... 
>A(1) := new Matrix(1 .. 5, 1 .. 15); 
>A(2) := new Matrix(1 .. 15, 1 .. 7); 
>... 
> 
>I hope this helps. 
>- John Herro 
>Software Innovations Technology 
>http://members.aol.com/AdaTutor 
>ftp://members.aol.com/AdaTutor 
-- 
   ______   Meow 
   \ OO /  _/  
  :(__ =) 
    U \\ 
http://www.sci.csupomona.edu/~cchow




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array of matrices
  1996-05-04  0:00   ` Boaz Chow
@ 1996-05-04  0:00     ` John Herro
  0 siblings, 0 replies; 5+ messages in thread
From: John Herro @ 1996-05-04  0:00 UTC (permalink / raw)



boaz@usa.pipeline.com(Boaz Chow) writes:
> type Matrix is array (integer range <>, integer range <>) of integer;
> Your second method [creating an array of
> pointers to type Matirx] looks very good.
> But how can I retrive the size (or dimension)
> of the matrix?
     On the matrix pointed to, use the attribute 'Last with a subscript. 
For example, if we have

type Matrix is array(Integer range <>, Integer range <>) of Integer; 
type Ptr is access Matrix; 
... 
A : array(1 .. N) of Ptr; 
... 
A(1) := new Matrix(1 .. 5, 1 .. 15); 
A(2) := new Matrix(1 .. 15, 1 .. 7); 

then the upper bound of the FIRST dimension of the matrix pointed to by
A(2) is
A(2).all'Last(1),
              -
which is 15, and the upper bound of the SECOND dimension of the matrix
pointed to by A(2) is
A(2).all'Last(2),
              -
which is 7.  Similarly, 'First retrieves the lower bound, so in this
example A(2).all'First(1) and A(2).all'First(2) are both 1.  For one last
example, using A(1) instead of A(2), A(1).all'Last(1) is 5.
     The .all may be omitted in all of these, so A(1)'Last(1) means
A(1).all'Last(1).
     I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array of matrices
  1996-05-03  0:00 ` John Herro
  1996-05-04  0:00   ` Boaz Chow
@ 1996-05-10  0:00   ` Ron J Theriault
  1 sibling, 0 replies; 5+ messages in thread
From: Ron J Theriault @ 1996-05-10  0:00 UTC (permalink / raw)



I'm surprised nobody has jumped in on this but...
To represent a 2-D matrix, the best type to use is probably
a discriminated record.  Something like:

type  elements_2 is array(positive range <>, positive range <>) of integer;
type matrix_2 (rows: positive; columns: positive) is
   record
      data : elements_2 (1..rows, 1..columns);
   end record;

   obj: matrix_2 (2,3) := (2,3, ((1,2), (3,6), (77,89)));
   ...


Ron Theriault              |   
CS Department              |   In a democracy, you only have to fool
Texas A&M Univ.            |   most of the people, most of the time.
ron@cs.tamu.edu            |   




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1996-05-10  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-05-02  0:00 array of matrices Boaz Chow
1996-05-03  0:00 ` John Herro
1996-05-04  0:00   ` Boaz Chow
1996-05-04  0:00     ` John Herro
1996-05-10  0:00   ` Ron J Theriault

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox