Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to copy complete column (or row) of matrix to another?

314 views
Skip to first unread message

Nasser M. Abbasi

unread,
Sep 5, 2017, 4:41:49 AM9/5/17
to
I have not programmed in Ada for long time. I forgot now if
Ada supports copying whole column or whole row of 2D matrix
in one operation or not?

Here is a toy example. I want to copy one matrix
to another using a loop (to see if this is allowed)

--------------------------
procedure t1 is
type Matrix is array (Integer range <>, Integer range <>) of Integer;
A : Matrix :=
(( 1, 2, 3),
( 4, 5, 6),
( 7, 8, 9));
B: Matrix(1..3,1..3);
begin -- copy A to B one row at a time

FOR I in A'range(1) LOOP
B(I,1..3):=A(I,1..3); -- error at this line
END LOOP;

end t1;
---------------------

I see old thread that slicing is not allowed for matrix?

https://groups.google.com/forum/#!topic/comp.lang.ada/vwPXMabvUR8
"Copying rows in a two dimensional array."

Is this the reason for the error I get or Am I doing something
silly in the above?

Using gnat 2017

thanks
--Nasser

Randy Brukardt

unread,
Sep 5, 2017, 6:45:56 PM9/5/17
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message
news:oolo0a$1djr$1...@gioia.aioe.org...
>I have not programmed in Ada for long time. I forgot now if
> Ada supports copying whole column or whole row of 2D matrix
> in one operation or not?

No, that's not supported. It would be a distributed overhead to allow (that
is, it would make all 2D operations slower), as the components of a slice
would not necessarily be contiguous.

In some cases, it makes more sense to declare a 1D array of a 1D array. Then
you can slice the arrays. (That wouldn't make much sense for a true matrix;
that's not something I use much, so most of my types tend to be arrays of
arrays.)

...
> I see old thread that slicing is not allowed for matrix?
>
> https://groups.google.com/forum/#!topic/comp.lang.ada/vwPXMabvUR8
> "Copying rows in a two dimensional array."
>
> Is this the reason for the error I get or Am I doing something
> silly in the above?

Yup. (Both statements are true. ;-)

Randy.


Nasser M. Abbasi

unread,
Sep 5, 2017, 7:10:32 PM9/5/17
to
Thank you Randy for the answer. Ok. I understand. But
this unfortunately takes Ada out of possible languages
to use for me for now. I am planning to take numerical
course where we have choice to use Fortran or Matlab or
another language, but without being able to do such
common operations on matrices, (without writing
much more code) I will now look at using Fortran
or Matlab for this.

Too bad, because Ada is good language in terms of its
strong typing and other features, which I think will
make numerical software more robust, but it has little
support for many common operations build-in for working
with matrices and vectors as Fortran and Matlab already
has.

Someone should design a solid Ada like language but
with focus on numerical and computational work. That
will be a winner.

--Nasser

far...@gmail.com

unread,
Sep 6, 2017, 1:34:37 AM9/6/17
to
On Tuesday, September 5, 2017 at 4:10:32 PM UTC-7, Nasser M. Abbasi wrote:
>
> Thank you Randy for the answer. Ok. I understand. But
> this unfortunately takes Ada out of possible languages
> to use for me for now. I am planning to take numerical
> course where we have choice to use Fortran or Matlab or
> another language, but without being able to do such
> common operations on matrices, (without writing
> much more code) I will now look at using Fortran
> or Matlab for this.

I agree that this is a serious shortcoming of Ada. But like Randy said, you can have arrays of arrays which probably isn't a great solution if you are going to do lots of numerical stuff (OK for "computer sciencey" stuff I suppose). I think possibly a better solution is to use normal matrices such as you defined in your example and you can write just a few little functions that will do the copying for you; you can make them look fairly readable if not quite as nice as Fortran or Matlab. And don't forget that Ada now has built-in vector and matrix types for real and complex numbers, with overloaded operators.

You can also write your own overloads so that can do essentially "mixed-mode" arithmetic so that you can write mathematical code without considering walking into traffic with your eyes closed because of all the type conversions. Or I can send you mine if you like. I've made overloaded operators for integer, real, imaginary, and complex numbers as well as several array types--there are a lot of them but they are easy to write.

Ada's strong typing makes e.g. the three ways of vector multiplication interesting. I have three "*" operators for vectors and the three kinds of multiplication are made unambiguous because of the return type. Consider

a := b * c;

where b and c are vectors. If a is a scalar, then b * c is the inner or "dot" product. If a is a vector, then b * c is a element-by-element multiplication. And if a is a matrix, then b * c is the outer product. Pretty neat, huh.

Jerry

Simon Wright

unread,
Sep 6, 2017, 3:21:25 AM9/6/17
to
"Nasser M. Abbasi" <n...@12000.org> writes:

> Too bad, because Ada is good language in terms of its
> strong typing and other features, which I think will
> make numerical software more robust, but it has little
> support for many common operations build-in for working
> with matrices and vectors as Fortran and Matlab already
> has.

For example,

function Column
(V : Complex_Matrix; C : Integer) return Complex_Vector
is
begin
return Result : Complex_Vector (V'Range (1)) do
for J in V'Range (1) loop
Result (J) := V (J, C);
end loop;
end return;
end Column;

Dmitry A. Kazakov

unread,
Sep 6, 2017, 3:31:39 AM9/6/17
to
On 06/09/2017 00:45, Randy Brukardt wrote:
> "Nasser M. Abbasi" <n...@12000.org> wrote in message
> news:oolo0a$1djr$1...@gioia.aioe.org...
>> I have not programmed in Ada for long time. I forgot now if
>> Ada supports copying whole column or whole row of 2D matrix
>> in one operation or not?
>
> No, that's not supported. It would be a distributed overhead to allow (that
> is, it would make all 2D operations slower), as the components of a slice
> would not necessarily be contiguous.

If the type of 1D slice of 2D slice could be a type different from but
compatible to the type of 1D slice of the full array the operations
could make use of different representations.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

darek

unread,
Sep 9, 2017, 6:33:40 PM9/9/17
to
It is not Ada but ...
Have a look at this article:
http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=FF8CA42FE18D109BFC4798DF3BB91926?doi=10.1.1.205.9658&rep=rep1&type=pdf

These extensions have been implemented in the ActiveOberon language running under the A2 operating system (which can run natively, under Windows, under Linux). You can get the whole system here:
http://www.ocp.inf.ethz.ch/wiki/Development/Repository
The user forum is here:
http://www.ocp.inf.ethz.ch/forum/

The ActiveOberon language supports COMPLEX/LONGCOMPLEX data types natively.

Regards,
Darek

darek

unread,
Sep 9, 2017, 6:48:05 PM9/9/17
to
On Tuesday, 5 September 2017 10:41:49 UTC+2, Nasser M. Abbasi wrote:
One more link I forgot to add in my previous e-mail:
https://www.inf.ethz.ch/personal/felixf/pdfs/2006_ArrayStructuredOT.pdf

Lucretia

unread,
Sep 12, 2017, 8:57:07 AM9/12/17
to
I agree that multi-dimensional array slices should be supported. One of the tenet's of Ada is to leave the implementation of things up to the compiler, because it knows how best to do it, e.g. parameter passing.

I asked about it here, https://groups.google.com/d/msg/comp.lang.ada/L9XiRrjm3B0/cXfKlQ9-AwAJ and the response was, it's not going to happen due to a slice being a name as Randy mentions, but it's not a request to speed up the assignment, it's just syntactic sugar to aid readability, another of Ada's tenets.

Even the AARM says so here, http://www.ada-auth.org/standards/12aarm/html/AA-4-1-2.html see 8.b.

Johan Söderlind Åström

unread,
Sep 12, 2017, 5:22:29 PM9/12/17
to
Instead of slicing:

A : Real_Matrix (1 .. 3, 1 .. 3) :=
((1.0, 2.0, 3.0),
(4.0, 5.0, 6.0),
(7.0, 8.0, 9.0));
C : Real_Vector (1 .. 3) := (0.0, 0.0, 1.0);
B1 : Real_Vector (1 .. 3) := A * C; -- Extract column 3.
B2 : Real_Vector (1 .. 3) := Transpose (A) * C; -- Extract row 3.

Robert Eachus

unread,
Sep 17, 2017, 8:01:47 AM9/17/17
to
On Tuesday, September 5, 2017 at 4:41:49 AM UTC-4, Nasser M. Abbasi wrote:
> Here is a toy example. I want to copy one matrix
> to another using a loop (to see if this is allowed)
> --------------------------
> procedure t1 is
> type Matrix is array (Integer range <>, Integer range <>) of Integer;
> A : Matrix :=
> (( 1, 2, 3),
> ( 4, 5, 6),
> ( 7, 8, 9));
> B: Matrix(1..3,1..3);
> begin -- copy A to B one row at a time

If you don't insist on a loop, A := B; works just fine.

If you really need to work with Rows and Columns I define:

function Row(M: in Matrix; I: in Index) return Vector;

function Column(M: in Matrix; I: in Index) return Vector;

The reason the language doesn't define these operations is that there are lots of Matrix and Vector types to be dealt with. (I have some algorithms that use slices of arrays of Booleans--and stores them in 64-bit Unsigned types.)

What if you also need to assign to rows or columns without copying the matrix? You could use access types, but I just depend on the compiler being sane and define: procedure Replace_Row (M: in out Matrix; I: in Index; V: in Vector);

I suppose I could provide these as a generic. Would that be useful? Hmmm. Probably should have separate index types for matrices, rows, and columns (a total of four). Or is that too complex, even if it is only really visible in the generic parameters?

Randy Brukardt

unread,
Oct 2, 2017, 7:09:00 PM10/2/17
to
"Lucretia" <lague...@googlemail.com> wrote in message
news:e7eb0977-c449-4d52...@googlegroups.com...
>I agree that multi-dimensional array slices should be supported. One
>of the tenet's of Ada is to leave the implementation of things up to the
>compiler, because it knows how best to do it, e.g. parameter passing.
>
>I asked about it here,
>https://groups.google.com/d/msg/comp.lang.ada/L9XiRrjm3B0/cXfKlQ9-AwAJ
>and the response was, it's not going to happen due to a slice being a name
>as Randy mentions,
>but it's not a request to speed up the assignment, it's just syntactic
>sugar to aid readability,
>another of Ada's tenets.

Sigh.

The problem is that supporting that would slow down ALL array assignments to
parameters, regardless of whether or not the 2-d slices are used. Every such
assignment would have to be element at a time, because there would be no way
to determine (within the subprogram) whether or not some slices were used
that makes the array non-contiguous.

It also would make array indexing operations on such parameters much more
expensive, for the same reason.

Use of matrixes is thought to be common enough that such a performance hit
(on code *not* using slices) is unacceptable. This is known as a
"distributed overhead", where the existence of a feature makes code that
doesn't use it more expensive.

Randy.


Marius Amado-Alves

unread,
Mar 21, 2018, 9:58:46 AM3/21/18
to
> You can also write your own overloads so that can do essentially "mixed-mode" arithmetic so that you can write mathematical code without considering walking into traffic with your eyes closed because of all the type conversions. Or I can send you mine if you like.

I'd love to take a look at your library (to steal ideas for mine:-)

Currently strugling with how to represent the anomalous concepts of "column vector", "row vector", "transpose of a vector" that unfortunately pervade the "mathematical" literature nowadays.

gerdien....@gmail.com

unread,
Mar 30, 2018, 5:25:59 PM3/30/18
to
There are lots of interesting Ada maths packages in here:

https://github.com/janverschelde/PHCpack

enjoy.

j.
0 new messages