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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4692663255b51613 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!goblin3!goblin.stu.neva.ru!gegeweb.org!aioe.org!.POSTED!not-for-mail From: "Nasser M. Abbasi" Newsgroups: comp.lang.ada Subject: Re: on using array index, vectorized operation Date: Mon, 28 Mar 2011 21:55:03 -0700 Organization: Aioe.org NNTP Server Message-ID: References: Reply-To: nma@12000.org NNTP-Posting-Host: tUYQ4Ty9mMw9Pdc8TJRFQA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 X-Notice: Filtered by postfilter v. 0.8.2 Xref: g2news1.google.com comp.lang.ada:18570 Date: 2011-03-28T21:55:03-07:00 List-Id: On 3/28/2011 7:50 PM, Randy Brukardt wrote: > "Nasser M. Abbasi" wrote in message >> Does not look like it either, not directly any way: >> >> --------------------- >> procedure foo2 is >> >> type array_t is array(natural range<>) of integer; >> u : array_t(1..10) := (others=>0); >> >> begin >> u := u + 1; >> end foo2; > > You can do this for float values by using Generic_Real_Arrays: > > with Ada.Numerics.Generic_Real_Arrays; > procedure foo3 is > > package My_Vectors is new Ada.Numerics.Generic_Real_Arrays (Float); > use My_Vectors; > > subtype array_t is Real_Vector; > > u : array_t(1..10) := (others=>0.0); > > begin > u := u + (others => 1.0); > u := u * 1.0; > end foo3; > > I'd suggest writing your own package similar to Generic Real_Arrays to > define your own vector types, complete with overloaded operators. You only > have to do that once, and the uses can be much simpler in your code. That's > a much better option than writing loads of loops in your code that ruin the > readability. > > Just because something isn't provided for free with Ada doesn't mean that > you can't write a similar replacement... > > Randy. > > Thanks Randy ! That really helped. I am a newbie at Ada and did not know about such package. (but strange it is only for float? But I suppose one can duplicate it for other types). I rewrote the procedure using this package (I am using float, so it worked for me), and was really surprised that now I able to remove the loop and use vectorized statement: ------------- subtype r is Natural range o.u'First + 1 .. o.u'Last - 1; subtype r_plus_1 is Natural range r'First + 1 .. r'Last + 1; subtype r_minus_1 is Integer range r'First - 1 .. r'Last - 1; begin u (r) := u (r) - (a / 2.0) * (u (r_plus_1) - u (r_minus_1)) + (a ** 2) / 2.0 * (u (r_minus_1) - 2.0 * u (r) + u(r_plus_1)); ------------- It is now almost the same form as in Fortran and Matlab ! Only extra thing is the need to define those subtype's for the ranges. (r, r_plus_1, r_minus_1) . In Fortran, I would have written the above as ----------------------------------------- u (i) := u (i) - (a / 2.0) * (u (i+1) - u (i-1)) + (a ** 2) / 2.0 * (u (i-1) - 2.0 * u (i) + u(i+1) ) ---------------------------------------------- Where 'i' is an array of the index range I am interested in. It would be great if there is another 'trick' to allow me to write it as such in Ada. I am still not too happy with having to do this, but this is a very minor thing now for me and I can life with it. Removing the loop is a major improvement from what I started with. I like Ada now again :) since I am able to write the code in a much close form as I am used to. Fyi, I've updated my small Ada Lax-Wendroff package here which I used to practice on. http://12000.org/my_notes/solve_advection_1D_in_Ada/index.htm thanks again for all who helped. --Nasser