array operations enhancements
Don
nospam at nospam.com
Fri Aug 20 00:52:01 PDT 2010
F. Almeida wrote:
> == Quote from KennyTM~ (kennytm at gmail.com)'s article
>> On Aug 18, 10 14:41, F. Almeida wrote:
>>> What if the compiler was able to introduce them in the
> assignment
>>> loop, provided that the functions pass ref double (in this
> case)?
>>> double anyfun(ref double x) { ... }
>>>
>>> c = anyfun(a[]) + 2.0*b[];
>> import std.algorithm;
>> import std.stdio;
>> import std.array;
>> auto vectorize(alias f, U)(U a) {
>> return array(map!f(a));
>> }
>> double square(double x) {
>> return x*x;
>> }
>> void main () {
>> auto a = [1.0, 2.0, 3.0, 4.0];
>> auto b = [5.0, 6.0, 7.0, 8.0];
>> auto c = [0.0, 0.0, 0.0, 0.0];
>> c[] = vectorize!square(a)[] + 2.0 * b[];
>> writeln(">", c);
>> }
>
> This doesn't help, as vectorize!() is being called to create an
> array before passing it to the array expression (one additional
> assignment loop). Additional functions passed through vectorize!()
> would lead to N+1 loops per operation, with N the number of
> vectorize!() calls, instead of the intended single loop. What is
> intended is to pass as many functions as possible, and allow the
> assignment to occur within a single loop.
>
> The best alternative so far is to allow the compiler to take
> initiative whenever a pure function accepting one argument (value,
> not ref) of the same type exists.
>
> pure double myf(double x) { ... }
>
> c[] = myf(a[]) + 2.0*myf(b[]);
>
> Is such an optimization possible?
Definitely. I'm just a bit paranoid about what could happen with
user-defined types.
I would hope, btw, that we could do:
Complex!double[] a;
double [] r = a[].re; // grab the real part
I see abs, sqrt, re, and im as the most important use cases.
My feeling is that it might be necessary to restrict this to:
c[] = myf(a[])[] + 2.0 * myf(b[])[];
That may be paranoia on my part. I'm not certain that
More information about the Digitalmars-d
mailing list