From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.36.84.212 with SMTP id t203mr8573212ita.36.1497787600799; Sun, 18 Jun 2017 05:06:40 -0700 (PDT) X-Received: by 10.157.51.139 with SMTP id u11mr252679otc.16.1497787600773; Sun, 18 Jun 2017 05:06:40 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!185no1481163itv.0!news-out.google.com!k7ni1000itk.0!nntp.google.com!f20no1480872itb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 18 Jun 2017 05:06:40 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:191:8303:2100:5985:2c17:9409:aa9c; posting-account=fdRd8woAAADTIlxCu9FgvDrUK4wPzvy3 NNTP-Posting-Host: 2601:191:8303:2100:5985:2c17:9409:aa9c References: <35c6ac5a-3295-4fa4-8545-ca76c113dde4@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Arrays in Ada 2020 From: Robert Eachus Injection-Date: Sun, 18 Jun 2017 12:06:40 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:46982 Date: 2017-06-18T05:06:40-07:00 List-Id: 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: > >=20 > > FYI Ada 2020 is about to bring some improvements: > >=20 > > http://www.ada-auth.org/standards/2xrm/html/RM-4-3-3.html > >=20 > >> G : constant Matrix :=3D > >> (for I in 1 .. 4 =3D> > >> (for J in 1 .. 4 =3D> > >> (if I =3D J then 1.0 else 0.0))); -- Identity matrix > >=20 > > Best Regards, > > Ivan Levashev, > > Barnaul >=20 > 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 :=3D Unit_Matrix(4);=20 begin null; end Main; On a different but related subject, the body of Unit_Matrix should look lik= e: function Unit_Matrix (Order : Positive; First_1, First_2 : Integer :=3D 1) return Real_Matrix is Result: Real_Matrix(First_1..First_1+Order, First_2..First_2+Order) :=3D (others =3D>=20 (others =3D> 0.0)); begin for I in First_1..First_1+Order loop Result(I, I-First_1+First_2) :=3D 1.0; end loop; return Result; end Unit_Matrix; On modern virtual memory machines, new space (as opposed to reused stack sp= ace) 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 matr= ices, 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 squar= ed 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 bac= k. The fact that the view of the object that Unit_Matrix is assigned to ma= y be constant won't affect things, the view of Result inside Unix_Matrix is= not constant.