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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b375f07e05d12c7a X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-14 01:17:27 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!193.174.75.178!news-fra1.dfn.de!news.rwth-aachen.de!not-for-mail From: "Alexander Boucke" Newsgroups: comp.lang.ada Subject: Re: efficient vector/matrix operations in Ada Date: Tue, 14 Aug 2001 10:17:22 +0200 Organization: Lehr- und Forschungsgebiet f. Mechanik Message-ID: <9lamqm$ehp$1@nets3.rz.RWTH-Aachen.DE> References: <3B6F40C1.FD215D1D@sneakemail.com> <3B6F5ABC.3C40E189@sneakemail.com> <9f6e2b77.0108100854.66b084b4@posting.google.com> <3B783732.A3A1EB82@mindspring.com> NNTP-Posting-Host: 134.130.177.111 X-Trace: nets3.rz.RWTH-Aachen.DE 997777046 14905 134.130.177.111 (14 Aug 2001 08:17:26 GMT) X-Complaints-To: abuse@rwth-aachen.de NNTP-Posting-Date: 14 Aug 2001 08:17:26 GMT X-Newsreader: Microsoft Outlook Express Unix 5.00.2013.1312 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2013.1312 Xref: archiver1.google.com comp.lang.ada:11895 Date: 2001-08-14T08:17:26+00:00 List-Id: > > Here's how you write "A = B + C + D;" efficiently: > > > > A = B; > > A += C; > > A += D; > > > > This requires no temporary matrices or passing by value. The original > > form (A=B+C+D) is fine for rapid prototyping, but the efficient form > > is preferable for production code. As far as I am concerned, Ada > > really needs arithmetic assignment operators. Sure, you can use > > procedures, but they're intent and effect is not as obvious to the > > reader. There is a way to do the same (regarding efficiency) in Ada 95 using the A:= B + C + D form. For my work I've written a Matrix-Vector package, that encapsulates the Arrays using access-types. The only temporary objects you get now are access-types. There would have been a problem with the RAM slowly filling up, so I used controlled types to do reference-counting. Together with a temporary vector holding all results from sums, this leads to equivalent efficiency as using the += operators. You have to deal with the overhead due to the garbage collection using controlled types, but that is negligible when using large vectors/matrices as in typical finite element programs. How this could look (simplified): package vectors is ... type Vector is new Ada.Finalization.Controlled with private; function "+" (left,right : vector) return vector; ... private type value_array is array (<>) of real; type handled_vector (first,last : integer) is record value : value_array(first, last); count : natural; -- for reference counting end record; type handled_vector_access is access handled_vector; type vector is new ada.finalization.controlled with record the_vector : handled_vector_access; end record; end vectors; Regards, Alexander Boucke