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,f3405cf75879a8dc X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!fu-berlin.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.ada Subject: Re: basic questions on using Ada arrays Date: Wed, 06 Oct 2010 18:55:13 -0700 Organization: None to speak of Message-ID: References: <871v838f5x.fsf@hugsarin.sparre-andersen.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Thu, 7 Oct 2010 01:55:03 +0000 (UTC) Injection-Info: mx01.eternal-september.org; posting-host="9nFTuw1iclElqUG5+//YDQ"; logging-data="10201"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PCpc05f5P3H4BXRlIx6bk" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:h9ElfivC0PWEPCaGtmL2gRoI7tA= sha1:C9A3JTOmFYBCiVrOFrffrP9uv6Y= Xref: g2news1.google.com comp.lang.ada:14420 Date: 2010-10-06T18:55:13-07:00 List-Id: Jacob Sparre Andersen writes: > Nasser M. Abbasi wrote: > >> I am really rusty with Ada. Wanted to find if I apply a function to an >> array in one call without having to loop calling the function for each >> entry in the array? > > Only if the function/operation is declared for the array type. > > The operator "and" is automatically declared for arrays of Booleans. > >> Suppose I have an array of values, and I want to take the sin() of >> each value in the array? > > The function "Sin" is not declared automatically for arrays of Floats. > You will have to declare it yourself: > > function Sin (Item : in The_Array_Type) return The_Array_Type is > begin > return Result : The_Array_Type do > for I in Item'Range loop > Result (I) := Sin (Item (I)); > end loop; > end return; > end Sin; A more "generic" solution (though it doesn't use generics) is: with Ada.Text_IO; use Ada.Text_IO; with Ada.Numerics; use Ada.Numerics; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; with Ada.Float_Text_IO; use Ada.Float_Text_IO; procedure Test2 is type Func_Ptr is access function(F: Float) return Float; type Float_Array is array(positive range <>) of Float; function Apply(Func: Func_Ptr; Arr: Float_Array) return Float_Array is Result: Float_Array(Arr'Range); begin for I in Result'Range loop Result(I) := Func(Arr(I)); end loop; return Result; end Apply; function Init return Float_Array is nPoints : constant := 100; del : constant Float := 2.0*Pi/Float(nPoints-1); Result: Float_Array(1 .. nPoints); begin for I in Result'Range loop Result(I) := Float(I - 1) * del; end loop; return result; end Init; X: constant Float_Array := Init; Y: constant Float_Array := Apply(sin'Access, X); begin for I in X'Range loop Put(X(I), Exp => 0); Put(" => "); Put(Y(I), Exp => 0); New_Line; end loop; end Test2; [...] Note that the initialization function multiplies by "del" for each entry rather than adding "del"; the latter can cause cumulative errors. For numeric work, consider using Long_Float rather than Float; it's more precise (well, at least as precise) and very often no more expensive. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"