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 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Nasser M. Abbasi" Newsgroups: comp.lang.ada Subject: Re: basic question on adding 2 arrays Date: Sat, 12 Jul 2014 23:09:13 -0500 Organization: Aioe.org NNTP Server Message-ID: References: Reply-To: nma@12000.org NNTP-Posting-Host: +bGw3iKtw0btMD059xlTWg.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 NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:20900 Date: 2014-07-12T23:09:13-05:00 List-Id: On 7/12/2014 10:34 PM, Nasser M. Abbasi wrote: > ------------------------------------------------ > with Ada.Text_IO; use Ada.Text_IO; > > procedure foo is > nPoints : constant Integer := 100; > type grid_type is array (1 .. nPoints) of Float; > now : grid_type := (others => 0.0); > next: grid_type := now; > begin > next(2..now'length-1) := 1/2* > (now(1..now'length-1)+now(3..now'length)); > end foo; > -------------------------------------------------- > > I am basically trying to update an array (this is a finite difference > scheme). The error is that "+" is not defined for the grid_type, > which I understand ofcourse. But wanted to ask if there is a way > to do this in-line without having to define a method for this. > > For example, in Matlab or Fortran something like this works: > > next(2:end-1) = 1/2*(now(1:end-2)+now(3:end)); > This is what I tried. But after making special "+" to add the 2 slices of grid_type, by making new grid_type2 which is 2 elements smaller than grid_type, how does not tell Ada to move this grid_type2 into grid_type since they are of different type? Do I need another loop for this? ---------------------------- with Ada.Text_IO; use Ada.Text_IO; procedure foo is nPoints : constant Integer := 100; type grid_type is array (1 .. nPoints) of Float; type grid_type2 is array (1 .. nPoints - 2) of Float; now : grid_type := (others => 0.0); tmp: grid_type2; -- just to do the slices next : grid_type := (others => 0.0); function "+" (left, right : in grid_type2) return grid_type2 is res : grid_type2; begin for i in grid_type2'Range loop res (i) := left (i) + right (i); end loop; return res; end "+"; begin tmp := grid_type2 (now (2 .. now'Length - 1)) + grid_type2 (now (3 .. now'Length)); next(2..now'length-1) := tmp; -- stuck on this one ;) end foo; ----------------------------- I am sure there must be a better way to do all this, I can't be making new type for each different slice I want to work with? --Nasser