Jeremy Howard wrote:
> $a = sum(@b*@c+@d)
I'm not strong in math, but I do remember a bit about row and column
vectors. Isn't @b*@c ambiguous? Shouldn't it normally be interpreted
as a dot product, i.e. treat all vectors the same?
> The normal problem with this type of structure is that the previous
> statement would create 2 array copies, and 3 loops for most compilers. In
> perl speak, it might look like:
> $dummy1[$_] = $b[$_]*$c[$_] for (0..$#b-1);
> $dummy2[$_] = $d[$_]+$dummy1[$_] for (0..$#dummy1-1);
> $sum+=$_ for (@dummy2);
A simpler rule would be to treat an array as an implicit stream
whenever it's used in a scalar+reduce context. Then the following
code would be generated:
$dummy1 = stream @b;
$dummy2 = stream @c;
$dummy3 = stream @d;
while ($dummy4 = $dummy1->next * $dummy2->next + $dummy3->next) {
$sum += $dummy4;
}
> $sum+=$b[$_]*$c[$_]+$d[$_] for (0..$#b-1);
Shouldn't that be
$sum+=$b[$_]*$c[$_]+$d[$_] for (0..min($#b, $#c, $#d));
> Without this optimisation, array semantics become next to useless for
> numeric programming, because their overhead is just so high.
I think talking about array semantics instead of vector and matrix
semantics is next to useless too, but I'm not a math guy.
- Ken