comp.lang.ada
 help / color / mirror / Atom feed
* how to copy complete column (or row) of matrix to another?
@ 2017-09-05  8:41 Nasser M. Abbasi
  2017-09-05 22:45 ` Randy Brukardt
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: Nasser M. Abbasi @ 2017-09-05  8:41 UTC (permalink / raw)


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

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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05  8:41 how to copy complete column (or row) of matrix to another? Nasser M. Abbasi
@ 2017-09-05 22:45 ` Randy Brukardt
  2017-09-05 23:10   ` Nasser M. Abbasi
  2017-09-06  7:31   ` Dmitry A. Kazakov
  2017-09-09 22:33 ` darek
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 14+ messages in thread
From: Randy Brukardt @ 2017-09-05 22:45 UTC (permalink / raw)


"Nasser M. Abbasi" <nma@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.



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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05 22:45 ` Randy Brukardt
@ 2017-09-05 23:10   ` Nasser M. Abbasi
  2017-09-06  5:34     ` faryumg
  2017-09-06  7:17     ` Simon Wright
  2017-09-06  7:31   ` Dmitry A. Kazakov
  1 sibling, 2 replies; 14+ messages in thread
From: Nasser M. Abbasi @ 2017-09-05 23:10 UTC (permalink / raw)


On 9/5/2017 5:45 PM, Randy Brukardt wrote:
> "Nasser M. Abbasi" <nma@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.
> 


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


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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05 23:10   ` Nasser M. Abbasi
@ 2017-09-06  5:34     ` faryumg
  2018-03-21 13:58       ` Marius Amado-Alves
  2017-09-06  7:17     ` Simon Wright
  1 sibling, 1 reply; 14+ messages in thread
From: faryumg @ 2017-09-06  5:34 UTC (permalink / raw)


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

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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05 23:10   ` Nasser M. Abbasi
  2017-09-06  5:34     ` faryumg
@ 2017-09-06  7:17     ` Simon Wright
  1 sibling, 0 replies; 14+ messages in thread
From: Simon Wright @ 2017-09-06  7:17 UTC (permalink / raw)


"Nasser M. Abbasi" <nma@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;

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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05 22:45 ` Randy Brukardt
  2017-09-05 23:10   ` Nasser M. Abbasi
@ 2017-09-06  7:31   ` Dmitry A. Kazakov
  1 sibling, 0 replies; 14+ messages in thread
From: Dmitry A. Kazakov @ 2017-09-06  7:31 UTC (permalink / raw)


On 06/09/2017 00:45, Randy Brukardt wrote:
> "Nasser M. Abbasi" <nma@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

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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05  8:41 how to copy complete column (or row) of matrix to another? Nasser M. Abbasi
  2017-09-05 22:45 ` Randy Brukardt
@ 2017-09-09 22:33 ` darek
  2017-09-09 22:48 ` darek
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: darek @ 2017-09-09 22:33 UTC (permalink / raw)


On Tuesday, 5 September 2017 10:41:49 UTC+2, Nasser M. Abbasi  wrote:
> 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

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 
 

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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05  8:41 how to copy complete column (or row) of matrix to another? Nasser M. Abbasi
  2017-09-05 22:45 ` Randy Brukardt
  2017-09-09 22:33 ` darek
@ 2017-09-09 22:48 ` darek
  2017-09-12 12:57   ` Lucretia
  2017-09-12 21:22 ` Johan Söderlind Åström
  2017-09-17 12:01 ` Robert Eachus
  4 siblings, 1 reply; 14+ messages in thread
From: darek @ 2017-09-09 22:48 UTC (permalink / raw)


On Tuesday, 5 September 2017 10:41:49 UTC+2, Nasser M. Abbasi  wrote:
> 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

One more link I forgot to add in my previous e-mail: 
https://www.inf.ethz.ch/personal/felixf/pdfs/2006_ArrayStructuredOT.pdf

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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-09 22:48 ` darek
@ 2017-09-12 12:57   ` Lucretia
  2017-10-02 23:08     ` Randy Brukardt
  0 siblings, 1 reply; 14+ messages in thread
From: Lucretia @ 2017-09-12 12:57 UTC (permalink / raw)


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.


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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05  8:41 how to copy complete column (or row) of matrix to another? Nasser M. Abbasi
                   ` (2 preceding siblings ...)
  2017-09-09 22:48 ` darek
@ 2017-09-12 21:22 ` Johan Söderlind Åström
  2017-09-17 12:01 ` Robert Eachus
  4 siblings, 0 replies; 14+ messages in thread
From: Johan Söderlind Åström @ 2017-09-12 21:22 UTC (permalink / raw)


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.

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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-05  8:41 how to copy complete column (or row) of matrix to another? Nasser M. Abbasi
                   ` (3 preceding siblings ...)
  2017-09-12 21:22 ` Johan Söderlind Åström
@ 2017-09-17 12:01 ` Robert Eachus
  4 siblings, 0 replies; 14+ messages in thread
From: Robert Eachus @ 2017-09-17 12:01 UTC (permalink / raw)


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?


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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-12 12:57   ` Lucretia
@ 2017-10-02 23:08     ` Randy Brukardt
  0 siblings, 0 replies; 14+ messages in thread
From: Randy Brukardt @ 2017-10-02 23:08 UTC (permalink / raw)


"Lucretia" <laguest9000@googlemail.com> wrote in message 
news:e7eb0977-c449-4d52-8010-72df7c261e0f@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.



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

* Re: how to copy complete column (or row) of matrix to another?
  2017-09-06  5:34     ` faryumg
@ 2018-03-21 13:58       ` Marius Amado-Alves
  2018-03-30 21:25         ` gerdien.de.kruyf
  0 siblings, 1 reply; 14+ messages in thread
From: Marius Amado-Alves @ 2018-03-21 13:58 UTC (permalink / raw)


> 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.


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

* Re: how to copy complete column (or row) of matrix to another?
  2018-03-21 13:58       ` Marius Amado-Alves
@ 2018-03-30 21:25         ` gerdien.de.kruyf
  0 siblings, 0 replies; 14+ messages in thread
From: gerdien.de.kruyf @ 2018-03-30 21:25 UTC (permalink / raw)


On Wednesday, 21 March 2018 15:58:46 UTC+2, Marius Amado-Alves  wrote:
> > 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.

There are lots of interesting Ada maths packages in here:

https://github.com/janverschelde/PHCpack

enjoy.

j.

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

end of thread, other threads:[~2018-03-30 21:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-05  8:41 how to copy complete column (or row) of matrix to another? Nasser M. Abbasi
2017-09-05 22:45 ` Randy Brukardt
2017-09-05 23:10   ` Nasser M. Abbasi
2017-09-06  5:34     ` faryumg
2018-03-21 13:58       ` Marius Amado-Alves
2018-03-30 21:25         ` gerdien.de.kruyf
2017-09-06  7:17     ` Simon Wright
2017-09-06  7:31   ` Dmitry A. Kazakov
2017-09-09 22:33 ` darek
2017-09-09 22:48 ` darek
2017-09-12 12:57   ` Lucretia
2017-10-02 23:08     ` Randy Brukardt
2017-09-12 21:22 ` Johan Söderlind Åström
2017-09-17 12:01 ` Robert Eachus

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