comp.lang.ada
 help / color / mirror / Atom feed
From: "Nasser M. Abbasi" <nma@12000.org>
Subject: Re: on using array index, vectorized operation
Date: Mon, 28 Mar 2011 21:55:03 -0700
Date: 2011-03-28T21:55:03-07:00	[thread overview]
Message-ID: <imron7$bep$1@speranza.aioe.org> (raw)
In-Reply-To: imrhe3$2m5$1@munin.nbi.dk

On 3/28/2011 7:50 PM, Randy Brukardt wrote:
> "Nasser M. Abbasi"<nma@12000.org>  wrote in message


>> Does not look like it either, not directly any way:
>>
>> ---------------------
>> procedure foo2 is
>>
>> type array_t is  array(natural range<>) of integer;
>> u : array_t(1..10) := (others=>0);
>>
>> begin
>>     u := u + 1;
>> end foo2;
>


> You can do this for float values by using Generic_Real_Arrays:
>
> with Ada.Numerics.Generic_Real_Arrays;
> procedure foo3 is
>
>     package My_Vectors is new Ada.Numerics.Generic_Real_Arrays (Float);
>     use My_Vectors;
>
>     subtype  array_t is Real_Vector;
>
>     u : array_t(1..10) := (others=>0.0);
>
> begin
>      u := u + (others =>  1.0);
>      u := u * 1.0;
> end foo3;
>
> I'd suggest writing your own package similar to Generic Real_Arrays to
> define your own vector types, complete with overloaded operators. You only
> have to do that once, and the uses can be much simpler in your code. That's
> a much better option than writing loads of loops in your code that ruin the
> readability.
>
> Just because something isn't provided for free with Ada doesn't mean that
> you can't write a similar replacement...
>
>                                     Randy.
>
>

Thanks Randy !  That really helped. I am a newbie at Ada and did
not know about such package. (but strange it is only for float?
But I suppose one can duplicate it for other types).

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:

-------------
       subtype r is Natural range o.u'First + 1 .. o.u'Last - 1;
       subtype r_plus_1 is Natural range r'First + 1 .. r'Last + 1;
       subtype r_minus_1 is Integer range r'First - 1 .. r'Last - 1;

    begin

       u (r) := u (r) -
                (a / 2.0) * (u (r_plus_1) - u (r_minus_1)) +
                (a ** 2) / 2.0 *
                (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

-----------------------------------------
       u (i) := 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) )
----------------------------------------------

Where 'i' is an array of the index range I am interested in.

It would be great if there is another 'trick' to allow
me to write it as such in Ada. I am still not too happy
with having to do this, but this is a very minor thing now
for me and I can life with it.

Removing the loop is a major improvement from what I started
with. I like Ada now again :) since I am able to write
the code in a much close form as I am used to.

Fyi, I've updated my small Ada Lax-Wendroff package here which
I used to practice on.

http://12000.org/my_notes/solve_advection_1D_in_Ada/index.htm

thanks again for all who helped.

--Nasser



  reply	other threads:[~2011-03-29  4:55 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-27  1:31 on using array index, vectorized operation Nasser M. Abbasi
2011-03-27 18:48 ` Nasser M. Abbasi
2011-03-27 20:01 ` Cyrille
2011-03-27 20:44   ` Nasser M. Abbasi
2011-03-27 21:02     ` Pascal Obry
2011-03-27 21:30     ` Shark8
2011-03-27 22:00       ` Nasser M. Abbasi
2011-03-27 22:37         ` Phil Clayton
2011-03-27 22:43           ` Nasser M. Abbasi
2011-03-27 22:59             ` Phil Clayton
2011-03-27 22:09     ` Phil Clayton
2011-03-27 22:12       ` Nasser M. Abbasi
2011-03-27 22:23       ` Nasser M. Abbasi
2011-03-29  2:50         ` Randy Brukardt
2011-03-29  4:55           ` Nasser M. Abbasi [this message]
2011-03-29 20:11             ` Phil Clayton
2011-03-29 21:17               ` Nasser M. Abbasi
2011-03-29 22:49                 ` Phil Clayton
2011-03-29 23:47                   ` Nasser M. Abbasi
2011-03-30 13:00                     ` Robert A Duff
replies disabled

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