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-Thread: 103376,4692663255b51613 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!k30g2000yqb.googlegroups.com!not-for-mail From: Phil Clayton Newsgroups: comp.lang.ada Subject: Re: on using array index, vectorized operation Date: Tue, 29 Mar 2011 13:11:36 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 91.110.254.39 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1301429496 2638 127.0.0.1 (29 Mar 2011 20:11:36 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 29 Mar 2011 20:11:36 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k30g2000yqb.googlegroups.com; posting-host=91.110.254.39; posting-account=v7gx3AoAAABfjb9m5b7l_Lt2KVEgQBIe User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.1.9) Gecko/20100330 Fedora/3.5.9-1.fc12 Firefox/3.5.9,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:19556 Date: 2011-03-29T13:11:36-07:00 List-Id: On Mar 29, 5:55=A0am, "Nasser M. Abbasi" wrote: > 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: > > ------------- > =A0 =A0 =A0 =A0subtype r is Natural range o.u'First + 1 .. o.u'Last - 1; > =A0 =A0 =A0 =A0subtype r_plus_1 is Natural range r'First + 1 .. r'Last + = 1; > =A0 =A0 =A0 =A0subtype r_minus_1 is Integer range r'First - 1 .. r'Last -= 1; > > =A0 =A0 begin > > =A0 =A0 =A0 =A0u (r) :=3D u (r) - > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (a / 2.0) * (u (r_plus_1) - u (r_minus_1)= ) + > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (a ** 2) / 2.0 * > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (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 > > ----------------------------------------- > =A0 =A0 =A0 =A0u (i) :=3D u (i) - > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (a / 2.0) * (u (i+1) - u (i-1)) + > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (a ** 2) / 2.0 * > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (u (i-1) - 2.0 * u (i) + u(i+1) ) > ---------------------------------------------- > > Where 'i' is an array of the index range I am interested in. It's worth observing that you're using vectorized subscripting, not just vectorized arithmetic. It should be possible to write a function that does the vectorized subscript, e.g. Sub(A, X) =3D B such that B(i) =3D A(X(i)). Unfortunately, if you use real arrays for the i+1 in your example, you'll have real subscripts. That may be acceptable when modelling e.g. in Matlab but not in an implementation. So you may need + and - on arrays of your index type. It all depends what you intend to use your code for. Phil P.S. I still think Ada should allow us to write something like u (2 .. 5) :=3D (for i in 2 .. 5 =3D> 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))); (which avoids the temporary variable that the equivalent loop requires). This enables on-the-fly vectorization by promoting scalar operations inline in expressions. For example, if you didn't have a version of some function F vectorized in its second argument, you could write (for I in B'Range =3D> F(A, B(I), C))