comp.lang.ada
 help / color / mirror / Atom feed
From: Keith Thompson <kst-u@mib.org>
Subject: Re: basic questions on using Ada arrays
Date: Wed, 06 Oct 2010 18:55:13 -0700
Date: 2010-10-06T18:55:13-07:00	[thread overview]
Message-ID: <ln8w2apy0u.fsf@nuthaus.mib.org> (raw)
In-Reply-To: 871v838f5x.fsf@hugsarin.sparre-andersen.dk

Jacob Sparre Andersen <sparre@nbi.dk> 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  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"



  reply	other threads:[~2010-10-07  1:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-06 15:43 basic questions on using Ada arrays Nasser M. Abbasi
2010-10-06 16:17 ` Pascal Obry
2010-10-06 16:22 ` Jacob Sparre Andersen
2010-10-07  1:55   ` Keith Thompson [this message]
2010-10-08  0:04     ` Randy Brukardt
2010-10-08 14:47       ` Jacob Sparre Andersen
2010-10-09  6:35         ` Randy Brukardt
2010-10-26  2:20           ` Yannick Duchêne (Hibou57)
2010-10-06 17:03 ` Jeffrey Carter
2010-10-06 19:54 ` Simon Wright
2010-10-08  8:02 ` Alex Mentis
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox