* Slice from a matrix?
@ 2003-05-23 9:14 Harald Schmidt
2003-05-23 10:52 ` Dale Stanbrough
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Harald Schmidt @ 2003-05-23 9:14 UTC (permalink / raw)
Hi,
I got a generic matrix / vector package like this:
Generic
rows : positive;
cols : positive;
item_type is digits <>;
Package matrix is
type vector_type is array (positive range <>) of item_type;
type matrix_type is array (positive range <>,
positive range <>) of item_type;
subtype col_vector is vector_type(1..cols);
subtype row_vector is vector_type(1..rows);
subtype matrix is matrix_type(1..rows, 1..cols);
...
End matrix;
Then I have three Put procedures, one for matrix, one for row_vector, and
one for col_vector.
My question is how to get a slice from a matrix which I can convert to a
col_vector which does the printing?
Things like...
PUT (col_vector(M (1, 1..3)));
doesn't work.
I don't want to copy code for the printing routines.
Thanks for any help,
Harald
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 9:14 Slice from a matrix? Harald Schmidt
@ 2003-05-23 10:52 ` Dale Stanbrough
2003-05-23 11:23 ` David C. Hoos
2003-05-23 17:56 ` Stephen Leake
2 siblings, 0 replies; 9+ messages in thread
From: Dale Stanbrough @ 2003-05-23 10:52 UTC (permalink / raw)
Harald Schmidt <harald.schmidt@anobject.net> wrote:
> My question is how to get a slice from a matrix which I can convert to a
> col_vector which does the printing?
>
> Things like...
>
> PUT (col_vector(M (1, 1..3)));
>
> doesn't work.
>
You can only take slices from 1d arrays.
dale
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 9:14 Slice from a matrix? Harald Schmidt
2003-05-23 10:52 ` Dale Stanbrough
@ 2003-05-23 11:23 ` David C. Hoos
2003-05-23 14:15 ` P S Norby
2003-05-23 18:13 ` Georg Bauhaus
2003-05-23 17:56 ` Stephen Leake
2 siblings, 2 replies; 9+ messages in thread
From: David C. Hoos @ 2003-05-23 11:23 UTC (permalink / raw)
"Harald Schmidt" <harald.schmidt@anobject.net> wrote in message
news:BAF3B30D.3023%harald.schmidt@anobject.net...
> Hi,
>
> I got a generic matrix / vector package like this:
>
> Generic
> rows : positive;
> cols : positive;
> item_type is digits <>;
> Package matrix is
> type vector_type is array (positive range <>) of item_type;
> type matrix_type is array (positive range <>,
> positive range <>) of item_type;
> subtype col_vector is vector_type(1..cols);
> subtype row_vector is vector_type(1..rows);
> subtype matrix is matrix_type(1..rows, 1..cols);
> ...
> End matrix;
>
> Then I have three Put procedures, one for matrix, one for row_vector, and
> one for col_vector.
>
> My question is how to get a slice from a matrix which I can convert to a
> col_vector which does the printing?
>
> Things like...
>
> PUT (col_vector(M (1, 1..3)));
>
> doesn't work.
>
> I don't want to copy code for the printing routines.
Since slices in ada can only be taken from one-dimensional arrays, you
would need to declare your matrix as an array of vectors.
Individual elements are then accessed by matrix (m}{n}.
Incidentally, shouldn't a vector which is indexed by row be a column vector,
and a vector which is indexed by column be a row vector?
>
> Thanks for any help,
>
> Harald
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 11:23 ` David C. Hoos
@ 2003-05-23 14:15 ` P S Norby
2003-05-24 23:00 ` Robert I. Eachus
2003-05-23 18:13 ` Georg Bauhaus
1 sibling, 1 reply; 9+ messages in thread
From: P S Norby @ 2003-05-23 14:15 UTC (permalink / raw)
"David C. Hoos" <david.c.hoos.sr@ada95.com> wrote in message
news:Cmnza.38775$l66.3829@fe10.atl2.webusenet.com...
>
> "Harald Schmidt" <harald.schmidt@anobject.net> wrote in message
> news:BAF3B30D.3023%harald.schmidt@anobject.net...
> > Hi,
> >
> > I got a generic matrix / vector package like this:
> >
> > Generic
> > rows : positive;
> > cols : positive;
> > item_type is digits <>;
> > Package matrix is
> > type vector_type is array (positive range <>) of item_type;
> > type matrix_type is array (positive range <>,
> > positive range <>) of item_type;
> > subtype col_vector is vector_type(1..cols);
> > subtype row_vector is vector_type(1..rows);
> > subtype matrix is matrix_type(1..rows, 1..cols);
> > ...
> > End matrix;
> >
> > Then I have three Put procedures, one for matrix, one for row_vector,
and
> > one for col_vector.
> >
> > My question is how to get a slice from a matrix which I can convert to a
> > col_vector which does the printing?
> >
> > Things like...
> >
> > PUT (col_vector(M (1, 1..3)));
> >
> > doesn't work.
> >
> > I don't want to copy code for the printing routines.
> Since slices in ada can only be taken from one-dimensional arrays, you
> would need to declare your matrix as an array of vectors.
> Individual elements are then accessed by matrix (m}{n}.
>
The trouble then is that you can either have rows of columns, in which case
you can take a slice of a column, but not a slice of a row... or you can
have columns of rows, in which case you can take a slice of a row, but not a
slice of a column. The col_vector and row_vector are not subtypes of
matrix_type.
> Incidentally, shouldn't a vector which is indexed by row be a column
vector,
> and a vector which is indexed by column be a row vector?
Yes. Might want to make them type-safe.
My simple solution would be to have a "Put_Item (item : in item_type);"
routine (add other parameters as desired), then declare something like
procedure Put_Column (Mat : in Matrix_type; in_col : in cols;
from_row, to_row: in rows);
and similalry to print rows or matrix. Your "PUT (col_vector(M (1,
1..3)));" becomes
Put_Column (M, 1, 1, 3);
or Put_Column (Mat => M, in_col => 1, From_row =>1, to_row => 3); if
you prefer named notation.
These "put" routines could be in your generic or elsewhere.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 9:14 Slice from a matrix? Harald Schmidt
2003-05-23 10:52 ` Dale Stanbrough
2003-05-23 11:23 ` David C. Hoos
@ 2003-05-23 17:56 ` Stephen Leake
2003-05-23 20:03 ` Simon Wright
2 siblings, 1 reply; 9+ messages in thread
From: Stephen Leake @ 2003-05-23 17:56 UTC (permalink / raw)
Harald Schmidt <harald.schmidt@anobject.net> writes:
> I got a generic matrix / vector package like this:
>
> <snip>
>
> Then I have three Put procedures, one for matrix, one for row_vector, and
> one for col_vector.
>
> My question is how to get a slice from a matrix which I can convert to a
> col_vector which does the printing?
If you change your matrix to be an array of arrays, you can use the
Generic_Array_Text_IO package from Auto_Text_IO at
http://www.toadmail.com/~ada_wizard/auto_text_io.html.
It supports both Put and Get, with optional named association.
--
-- Stephe
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 11:23 ` David C. Hoos
2003-05-23 14:15 ` P S Norby
@ 2003-05-23 18:13 ` Georg Bauhaus
1 sibling, 0 replies; 9+ messages in thread
From: Georg Bauhaus @ 2003-05-23 18:13 UTC (permalink / raw)
David C. Hoos <david.c.hoos.sr@ada95.com> wrote:
: "Harald Schmidt" <harald.schmidt@anobject.net> wrote in message
:> [...]
:> Package matrix is
:> type vector_type is array (positive range <>) of item_type;
:> type matrix_type is array (positive range <>,
:> positive range <>) of item_type;
:> subtype col_vector is vector_type(1..cols);
:> subtype row_vector is vector_type(1..rows);
:> subtype matrix is matrix_type(1..rows, 1..cols);
:> ...
:> End matrix;
:> My question is how to get a slice from a matrix which I can convert to a
:> col_vector which does the printing?
:>
: Since slices in ada can only be taken from one-dimensional arrays, you
: would need to declare your matrix as an array of vectors.
: Individual elements are then accessed by matrix (m}{n}.
Wouldn't this be a good opportunity to hide the implementation of
both vector_type and matrix_type, declare the needed subprograms
in the matrix spec, and use whatever combination of data "layout"
and algorithms seem appropriate to implement access to columns,
rows, and cells?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 17:56 ` Stephen Leake
@ 2003-05-23 20:03 ` Simon Wright
2003-05-23 21:06 ` Stephen Leake
0 siblings, 1 reply; 9+ messages in thread
From: Simon Wright @ 2003-05-23 20:03 UTC (permalink / raw)
Stephen Leake <Stephe.Leake@nasa.gov> writes:
> If you change your matrix to be an array of arrays, you can use the
> Generic_Array_Text_IO package from Auto_Text_IO at
> http://www.toadmail.com/~ada_wizard/auto_text_io.html.
http://www.toadmail.com/~ada_wizard/ada/auto_text_io.html
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 20:03 ` Simon Wright
@ 2003-05-23 21:06 ` Stephen Leake
0 siblings, 0 replies; 9+ messages in thread
From: Stephen Leake @ 2003-05-23 21:06 UTC (permalink / raw)
Simon Wright <simon@pushface.org> writes:
> Stephen Leake <Stephe.Leake@nasa.gov> writes:
>
> > If you change your matrix to be an array of arrays, you can use the
> > Generic_Array_Text_IO package from Auto_Text_IO at
> > http://www.toadmail.com/~ada_wizard/auto_text_io.html.
>
> http://www.toadmail.com/~ada_wizard/ada/auto_text_io.html
Arrgh. Sorry. "always check the link", same as "always compile the
example" :).
--
-- Stephe
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Slice from a matrix?
2003-05-23 14:15 ` P S Norby
@ 2003-05-24 23:00 ` Robert I. Eachus
0 siblings, 0 replies; 9+ messages in thread
From: Robert I. Eachus @ 2003-05-24 23:00 UTC (permalink / raw)
P S Norby wrote:
> The trouble then is that you can either have rows of columns, in which case
> you can take a slice of a column, but not a slice of a row... or you can
> have columns of rows, in which case you can take a slice of a row, but not a
> slice of a column. The col_vector and row_vector are not subtypes of
> matrix_type.
Not that I would ever throw this at a compiler but this is not true.
Let's define three types, a "conventional" Ada two-dimensional array, a
derived type from that type which uses Fortran (row major) layout.
Finally a vector of vectors type that can be sliced, and which can be
mapped to the other two types using Unchecked_Conversion.
Will it work in GNAT? I think so.
Do I think it is a good idea? Hell no.
I'd much rather define Row and Column functions for the two dimensional
array type. Three or four lines of code each, and a good optimizer
might figure out to actually return a fat pointer to the slice for the
Row operation.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2003-05-24 23:00 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-23 9:14 Slice from a matrix? Harald Schmidt
2003-05-23 10:52 ` Dale Stanbrough
2003-05-23 11:23 ` David C. Hoos
2003-05-23 14:15 ` P S Norby
2003-05-24 23:00 ` Robert I. Eachus
2003-05-23 18:13 ` Georg Bauhaus
2003-05-23 17:56 ` Stephen Leake
2003-05-23 20:03 ` Simon Wright
2003-05-23 21:06 ` Stephen Leake
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox