comp.lang.ada
 help / color / mirror / Atom feed
From: Robert Eachus <rieachus@comcast.net>
Subject: Re: Arrays in Ada 2020
Date: Sun, 18 Jun 2017 05:06:40 -0700 (PDT)
Date: 2017-06-18T05:06:40-07:00	[thread overview]
Message-ID: <aca80457-8fb0-49de-a6a0-a2812c3cbeee@googlegroups.com> (raw)
In-Reply-To: <oi4qbt$evo$1@gioia.aioe.org>

On Saturday, June 17, 2017 at 11:00:15 PM UTC-4, Nasser M. Abbasi wrote:
> On 6/17/2017 9:14 PM, Ivan Levashev wrote:
> > 
> > FYI Ada 2020 is about to bring some improvements:
> > 
> > http://www.ada-auth.org/standards/2xrm/html/RM-4-3-3.html
> > 
> >> G : constant Matrix :=
> >>      (for I in 1 .. 4 =>
> >>         (for J in 1 .. 4 =>
> >>            (if I = J then 1.0 else 0.0))); -- Identity matrix
> > 
> > Best Regards,
> > Ivan Levashev,
> > Barnaul
> 
> That is nice. But still too much code for such common operation.

What about:

with Ada.Numerics.Generic_Real_Arrays;
procedure Main is
   package My_Arrays is new Ada.Numerics.Generic_Real_Arrays(Long_Float);
   use My_Arrays;
   Identity_4: Real_Matrix := Unit_Matrix(4); 
begin null; end Main;

On a different but related subject, the body of Unit_Matrix should look like:
    function Unit_Matrix (Order           : Positive;
                         First_1, First_2 : Integer := 1)
                                            return Real_Matrix is
      Result: Real_Matrix(First_1..First_1+Order,
                     First_2..First_2+Order) := (others => 
                                                (others => 0.0));
   begin
     for I in First_1..First_1+Order loop
        Result(I, I-First_1+First_2) := 1.0;
     end loop;
     return Result;
   end Unit_Matrix;

On modern virtual memory machines, new space (as opposed to reused stack space) is cleared to zeros and the compiler knows it.  If you are allocating a large chunk of memory (in theory on the stack), the compiler can grab new memory and put a pointer on the stack.  If you are working with large matrices, you either expect this to happen or handle it yourself.  Setting the diagonal values to 1.0 is an operation repeated Order times not Order squared times, so let the compiler do the zeros as fast as possible, then worry about the ones.

Oh, and the compiler should create Result in place, rather than copying back.  The fact that the view of the object that Unit_Matrix is assigned to may be constant won't affect things, the view of Result inside Unix_Matrix is not constant.

  reply	other threads:[~2017-06-18 12:06 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-06 11:47 Arrays in Ada 2012 Anatoly Chernyshev
2017-06-06 12:17 ` Dmitry A. Kazakov
2017-06-06 13:17   ` Simon Wright
2017-06-06 13:57     ` Dmitry A. Kazakov
2017-06-06 21:59       ` Simon Wright
2017-06-06 13:19 ` AdaMagica
2017-06-06 20:56   ` Randy Brukardt
2017-06-07  7:06     ` Dmitry A. Kazakov
2017-06-07 11:47       ` Anatoly Chernyshev
2017-06-07 12:15         ` Dmitry A. Kazakov
2017-06-08  0:55         ` Randy Brukardt
2017-06-08  0:51       ` Randy Brukardt
2017-06-08  7:07         ` Dmitry A. Kazakov
2017-06-09  3:23           ` Randy Brukardt
2017-06-18  2:14 ` Arrays in Ada 2020 Ivan Levashev
2017-06-18  3:00   ` Nasser M. Abbasi
2017-06-18 12:06     ` Robert Eachus [this message]
2017-06-18 20:15       ` Simon Wright
2017-06-20 13:33         ` Robert Eachus
2017-06-19  6:36       ` Ivan Levashev
2017-06-19 12:06         ` AdaMagica
2017-06-23  1:17           ` Randy Brukardt
2017-06-18 20:15     ` Pascal Obry
replies disabled

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