comp.lang.ada
 help / color / mirror / Atom feed
* unconstrained array type problems
@ 2004-07-21 15:04 zork
  2004-07-21 15:09 ` Ludovic Brenta
  0 siblings, 1 reply; 10+ messages in thread
From: zork @ 2004-07-21 15:04 UTC (permalink / raw)


Hi, i am trying to read a matrix from a text file. For instance if i want to
read in a 2 x 3 matrix, my text file is set up as follows:

-----
2
3
1.0
2.4
3.5
1.2
1.1
2.4
....

I was hoping i could do something like the following (I thought an
unconstrained array is the way to go since i do not know the dimensions of
the array until reading the text file):

------------------
procedure Matrix is

    type Matrix_Type is array ( Integer range <>, Integer range <> ) of
Float;
    ..
    ..
begin
    ..
    Get ( File, number_rows ); -- read the number of rows from file
    Get ( File, number_columns ); -- read the number of columns from file
    array1 : Matrix_Type ( 1 .. number_rows, 1 .. number_columns );
    ..
end Matrix;
------------------

I cant seem to define my array1 in the begin / end block. This is a bit
frustrating. How does one get around this? Is an unconstrained array the
best choice?

Any help most appreciated!

thanks,
zork





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

* Re: unconstrained array type problems
  2004-07-21 15:04 unconstrained array type problems zork
@ 2004-07-21 15:09 ` Ludovic Brenta
  2004-07-21 15:33   ` zork
  0 siblings, 1 reply; 10+ messages in thread
From: Ludovic Brenta @ 2004-07-21 15:09 UTC (permalink / raw)


"zork" writes:
> procedure Matrix is
>
>     type Matrix_Type is array ( Integer range <>, Integer range <> ) of
> Float;
>     ..
>     ..
> begin
>     ..
>     Get ( File, number_rows ); -- read the number of rows from file
>     Get ( File, number_columns ); -- read the number of columns from file
>     array1 : Matrix_Type ( 1 .. number_rows, 1 .. number_columns );
>     ..
> end Matrix;
> ------------------
>
> I cant seem to define my array1 in the begin / end block. This is a bit
> frustrating. How does one get around this? Is an unconstrained array the
> best choice?
>
> Any help most appreciated!

You can do it with a declare block:

begin
   Get (File, number_rows);
   Get (File, number_columns);
   declare
      array1: Matrix_Type (1 .. number_rows, 1 .. number_columns);
   begin
      -- process array1
   end;
end;

But if your processing is long, consider changing the declare block to
a subprogram, and passing number_columns and number_rows to it.

-- 
Ludovic Brenta.



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

* Re: unconstrained array type problems
  2004-07-21 15:09 ` Ludovic Brenta
@ 2004-07-21 15:33   ` zork
  2004-07-21 15:54     ` Marius Amado Alves
  2004-07-22  7:57     ` Martin Krischik
  0 siblings, 2 replies; 10+ messages in thread
From: zork @ 2004-07-21 15:33 UTC (permalink / raw)


Hi thanks for that!

Yes that works for one array (just tried it) thanks!. What i forgot to
mention was that i need to read in a number of arrays from the text file,
and perform various operations between the arrays (multiplication). If i
define a 'declare' then each matrix will only be visible to itself ( i.e.
wont be visible to one another), hence the following wont work?

begin
   Get (File, number_rows);
   Get (File, number_columns);
   declare
      array1: Matrix_Type (1 .. number_rows, 1 .. number_columns);
   begin
      -- read in array1
   end;

   Get (File, number_rows);
   Get (File, number_columns);
   declare
      array2: Matrix_Type (1 .. number_rows, 1 .. number_columns);
   begin
      -- read in array2
   end;

  -- multiply array1 by array2 (array multiplication)

end;

How can i make them visible to one another? This is so easy to do in
c++/java. ADD so tight.

Thanks again!
zork

"Ludovic Brenta" <ludovic.brenta@insalien.org> wrote in message
news:87zn5tl7n1.fsf@insalien.org...
> "zork" writes:
> > procedure Matrix is
> >
> >     type Matrix_Type is array ( Integer range <>, Integer range <> ) of
> > Float;
> >     ..
> >     ..
> > begin
> >     ..
> >     Get ( File, number_rows ); -- read the number of rows from file
> >     Get ( File, number_columns ); -- read the number of columns from
file
> >     array1 : Matrix_Type ( 1 .. number_rows, 1 .. number_columns );
> >     ..
> > end Matrix;
> > ------------------
> >
> > I cant seem to define my array1 in the begin / end block. This is a bit
> > frustrating. How does one get around this? Is an unconstrained array the
> > best choice?
> >
> > Any help most appreciated!
>
> You can do it with a declare block:
>
> begin
>    Get (File, number_rows);
>    Get (File, number_columns);
>    declare
>       array1: Matrix_Type (1 .. number_rows, 1 .. number_columns);
>    begin
>       -- process array1
>    end;
> end;
>
> But if your processing is long, consider changing the declare block to
> a subprogram, and passing number_columns and number_rows to it.
>
> -- 
> Ludovic Brenta.





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

* Re: unconstrained array type problems
  2004-07-21 15:33   ` zork
@ 2004-07-21 15:54     ` Marius Amado Alves
  2004-07-21 16:24       ` Ludovic Brenta
  2004-07-22  7:57     ` Martin Krischik
  1 sibling, 1 reply; 10+ messages in thread
From: Marius Amado Alves @ 2004-07-21 15:54 UTC (permalink / raw)
  To: comp.lang.ada

> How can i make them visible to one another? This is so easy to do in
> c++/java.

This is extremely easy in Ada with the proper design, namely with these 
two entities:

    type Matrix is array (Positive range <>, Positive range <>) of Real;
    function Read_File (Name : String) return Matrix;

Then read all the matrices you want in the same place:

    Matrix_1 : Matrix := Read_Matrix ("One");
    Matrix_2 : Matrix := Read_Matrix ("Two");
    ...






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

* Re: unconstrained array type problems
  2004-07-21 15:54     ` Marius Amado Alves
@ 2004-07-21 16:24       ` Ludovic Brenta
  2004-07-21 17:33         ` Georg Bauhaus
  2004-07-21 17:35         ` zork
  0 siblings, 2 replies; 10+ messages in thread
From: Ludovic Brenta @ 2004-07-21 16:24 UTC (permalink / raw)


Marius Amado Alves writes:
>> How can i make them visible to one another? This is so easy to do in
>> c++/java.
>
> This is extremely easy in Ada with the proper design, namely with
> these two entities:
>
>     type Matrix is array (Positive range <>, Positive range <>) of Real;
>     function Read_File (Name : String) return Matrix;
>
> Then read all the matrices you want in the same place:
>
>     Matrix_1 : Matrix := Read_Matrix ("One");
>     Matrix_2 : Matrix := Read_Matrix ("Two");
>     ...

Yes, this is as I suggested, move the declare block to its own
subprogram.  But to answer more directly to the OP's question:

begin
   Get (File, number_rows_1);
   Get (File, number_columns_1);
   Get (File, number_rows_2);
   Get (File, number_columns_2);

   declare
      array1: Matrix_Type (1 .. number_rows, 1 .. number_columns);
      array2: Matrix_Type (1 .. number_rows, 1 .. number_columns);
   begin
      -- read in array1
      -- read in array2
      -- multiply array1 by array2 (array multiplication)
   end;
end;

-- 
Ludovic Brenta.



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

* Re: unconstrained array type problems
  2004-07-21 16:24       ` Ludovic Brenta
@ 2004-07-21 17:33         ` Georg Bauhaus
  2004-07-21 17:35         ` zork
  1 sibling, 0 replies; 10+ messages in thread
From: Georg Bauhaus @ 2004-07-21 17:33 UTC (permalink / raw)


Ludovic Brenta <ludovic.brenta@insalien.org> wrote:
: 
: begin
:   Get (File, number_rows_1);
:   Get (File, number_columns_1);
:   Get (File, number_rows_2);
:   Get (File, number_columns_2);
: 
:   declare
:      array1: Matrix_Type (1 .. number_rows, 1 .. number_columns);
:      array2: Matrix_Type (1 .. number_rows, 1 .. number_columns);
:   begin
:      -- read in array1
:      -- read in array2
:      -- multiply array1 by array2 (array multiplication)
:   end;
: end;

Maybe things can be made a bit more separate:


   subtype Realistic_Index is Positive range 1 .. 50;
   --   declared womewhere or "generically"

   type Matrix_Data is
     array(Realistic_Index range <>, Realistic_Index range <>)
     of Value;

   type Matrix(rows, columns: Realistic_Index) is limited  -- !
      record
         item: Matrix_Data(1 .. rows, 1 .. columns);
      end record;

   procedure Read(filename: String; m: in out Matrix);


This way a Matrix object can be declared and constrained
before cell items are read. Copying is avoided if I'm not
mistaken?


-- Georg



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

* Re: unconstrained array type problems
  2004-07-21 16:24       ` Ludovic Brenta
  2004-07-21 17:33         ` Georg Bauhaus
@ 2004-07-21 17:35         ` zork
  2004-07-21 18:30           ` tmoran
  1 sibling, 1 reply; 10+ messages in thread
From: zork @ 2004-07-21 17:35 UTC (permalink / raw)



"> begin
>    Get (File, number_rows_1);
>    Get (File, number_columns_1);
>    Get (File, number_rows_2);
>    Get (File, number_columns_2);
>
>    declare
>       array1: Matrix_Type (1 .. number_rows, 1 .. number_columns);
>       array2: Matrix_Type (1 .. number_rows, 1 .. number_columns);
>    begin
>       -- read in array1
>       -- read in array2
>       -- multiply array1 by array2 (array multiplication)
>    end;
> end;

Thanks, yes that would work if the arrays each had its own data file. But
what i find difficult to grasp is that what if the data is contained in one
file:

-----
row1
column1
elements or array1
row2
column2
elements of array2
..
-----

I need to:

---
open (file)

read in dimensions of array1
create the array1
fill the array1

read in dimensions of array2
create the array2
fill the array2

close (file)

allow the arrays to interact with one another
---

I cannot see a way around this.

thanks
zork





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

* Re: unconstrained array type problems
  2004-07-21 17:35         ` zork
@ 2004-07-21 18:30           ` tmoran
  2004-07-21 23:11             ` zork
  0 siblings, 1 reply; 10+ messages in thread
From: tmoran @ 2004-07-21 18:30 UTC (permalink / raw)


>open (file)
>
>read in dimensions of array1
>create the array1
>fill the array1
>
>read in dimensions of array2
>create the array2
>fill the array2
>
>close (file)
>
>allow the arrays to interact with one another
>---
>
>I cannot see a way around this.


Repeating and expanding on Marius Amado Alves message:
> > How can i make them visible to one another? This is so easy to do in
> > c++/java.
>
> This is extremely easy in Ada with the proper design, namely with these
> two entities:
>
>     type Matrix is array (Positive range <>, Positive range <>) of Real;
    function Read_Matrix(File : File_Type) return Matrix is
      Row_Count, Column_Count : Positive;
    begin
      Get (File, Row_Count);
      Get (File, Column_Count);
      declare
        Result: Matrix_Type (1 .. Row_Count, 1 .. Column_Count);
      begin
        -- read in matrix data
        return Result;
      end;
    end Read_Matrix;
>
> Then read all the matrices you want in the same place:
>
    procedure Do_Matrices is
>     Matrix_1 : Matrix := Read_Matrix(File);
>     Matrix_2 : Matrix := Read_Matrix(File);
      Product  : Matrix(Matrix_1'range(1), Matrix_2'range(2)); -- for example
    begin
      -- do stuff with Matrix_1 and Matrix_2
    end Do_Matrices;



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

* Re: unconstrained array type problems
  2004-07-21 18:30           ` tmoran
@ 2004-07-21 23:11             ` zork
  0 siblings, 0 replies; 10+ messages in thread
From: zork @ 2004-07-21 23:11 UTC (permalink / raw)


Thanks for the help everyone! The transition from c++ to ADA doesnt seem
that smooth. But im getting there.

thanks!
zork

<tmoran@acm.org> wrote in message news:kDyLc.157648$XM6.36144@attbi_s53...
> >open (file)
> >
> >read in dimensions of array1
> >create the array1
> >fill the array1
> >
> >read in dimensions of array2
> >create the array2
> >fill the array2
> >
> >close (file)
> >
> >allow the arrays to interact with one another
> >---
> >
> >I cannot see a way around this.
>
>
> Repeating and expanding on Marius Amado Alves message:
> > > How can i make them visible to one another? This is so easy to do in
> > > c++/java.
> >
> > This is extremely easy in Ada with the proper design, namely with these
> > two entities:
> >
> >     type Matrix is array (Positive range <>, Positive range <>) of Real;
>     function Read_Matrix(File : File_Type) return Matrix is
>       Row_Count, Column_Count : Positive;
>     begin
>       Get (File, Row_Count);
>       Get (File, Column_Count);
>       declare
>         Result: Matrix_Type (1 .. Row_Count, 1 .. Column_Count);
>       begin
>         -- read in matrix data
>         return Result;
>       end;
>     end Read_Matrix;
> >
> > Then read all the matrices you want in the same place:
> >
>     procedure Do_Matrices is
> >     Matrix_1 : Matrix := Read_Matrix(File);
> >     Matrix_2 : Matrix := Read_Matrix(File);
>       Product  : Matrix(Matrix_1'range(1), Matrix_2'range(2)); -- for
example
>     begin
>       -- do stuff with Matrix_1 and Matrix_2
>     end Do_Matrices;





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

* Re: unconstrained array type problems
  2004-07-21 15:33   ` zork
  2004-07-21 15:54     ` Marius Amado Alves
@ 2004-07-22  7:57     ` Martin Krischik
  1 sibling, 0 replies; 10+ messages in thread
From: Martin Krischik @ 2004-07-22  7:57 UTC (permalink / raw)


zork wrote:

> How can i make them visible to one another? This is so easy to do in
> c++/java. ADD so tight.

You can the same as in C++ or Java: Use a container library and/or dynamic
memory.

Honestly: Try your example in C++ or Java without vector<>/ java.util.Vector
and/or the operator new things become quite difficult there too.

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

end of thread, other threads:[~2004-07-22  7:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-21 15:04 unconstrained array type problems zork
2004-07-21 15:09 ` Ludovic Brenta
2004-07-21 15:33   ` zork
2004-07-21 15:54     ` Marius Amado Alves
2004-07-21 16:24       ` Ludovic Brenta
2004-07-21 17:33         ` Georg Bauhaus
2004-07-21 17:35         ` zork
2004-07-21 18:30           ` tmoran
2004-07-21 23:11             ` zork
2004-07-22  7:57     ` Martin Krischik

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