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!news4.google.com!feeder.news-service.com!94.75.214.39.MISMATCH!aioe.org!.POSTED!not-for-mail From: "Nasser M. Abbasi" Newsgroups: comp.lang.ada Subject: Re: on using array index, vectorized operation Date: Sun, 27 Mar 2011 11:48:01 -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:18506 Date: 2011-03-27T11:48:01-07:00 List-Id: On 3/26/2011 6:31 PM, Nasser M. Abbasi wrote: > Hello; > > Many times I need to operate on an array using an index > by doing some operation on the index itself as a variable > (such as shifting for example). > > This below is a very simple example of what I mean. > > Given an array A, I might want to do > > A(i-1) := A(i) ----------- (1) > > where 'i' above is a variable which contain in it some index > locations to use to index into the array A. > > In Ada, I can do the above, but I am a little clumsy in doing > it. I know I can use array slicing as in A(1..4) := A(2..5), > but wanted to do it when the index itself is a variable, as in (1) > above. Since in other cases, I have to do more operations on the index > itself, and so it needs to be in a variable. > > This is what I tried so far, and my goal to see if I can > shorten this more: > > ------------------------ > procedure t is > i: constant array (1..4) of integer:=(2,3,4,5); > A: array(1..5) of integer := (10,20,30,40,50); > > begin > --A(2..5) := A(1..4); > --A(i-1) := A(i); --error > > A(i(1)-1..i(4)-1) := A(i(1)..i(4)); > end t; > --------------------- > > Now, when I print A, it will show (20,30,40,50,50) > as expected. > > But how can I change the above to do it more directly, as (1)? > > In Fortran, I can do it as follows: > > -------------------- > program t > implicit none > integer :: A(1:5)=(/10,20,30,40,50/) > integer :: i(1:4)=(/2,3,4,5/) > > A(i-1) = A(i) > > end program t > --------------------- > > and in Matlab also > > ---------------------- > EDU>> A=[10 20 30 40 50]; > EDU>> i=2:5; > EDU>> A(i-1)=A(i) > A = > 20 30 40 50 50 > ------------------------- > > I am sure it is possible also in Ada to make this operation > as short in syntax as it is in the other 2 systems I showed above. > I just was not able to figure the syntax yet. ref(me) I've looked more into this. It does not seem Ada support for such 'vectorized' operations is as extensive as with Fortran or Matlab. There is a nice table here I found which summarizes this for many languages http://en.wikipedia.org/wiki/Comparison_of_programming_languages_%28array%29#Slicing For Ada, it says under the column "Vectorized operations" the following: "some, others definable5" Any way. Without such support, I find that I need to make lots of LOOPs for many things now in Ada. I'd rather use vectorized syntax if possible. Most of my Matlab code makes heavy use of vectorized operations on arrays and matrices, which makes the code more compact and also easier to work with. --Nasser