Again $table function, speed/functionality improved



On 13. May, 2009, at 3:32 PM, Richard Fateman wrote:

> How about other versions that might do the following:
>
> (a) produce an array instead of a list
> (b) use a different combining form. Instead of cons or append.  For  
> example, plus, times,  or any binary operation.
>
>  table(i, iterator, target=array)    --- for Maxima maybe need to  
> say  table(i,iterator,type=array, name=A) since arrays are not  
> first-class object in Maxima.
Arrays and list must be handled differently sadly, unless the  
function produces an array of arrays. What I mean:
table( i+j , [i,5], [j,i]) can't be a 2d array, only array of arrays  
(1d)..
Similar function for arrays (multi-dimensional) would have to treat  
iterators differently - iterators cannot depend on previous iterators  
anymore. Each iterator must produce strictly defined number of  
elements (without seeing other iterators)... so it is not a good  
solution I fear, unless we settle for 1d arrays of 1d arrays (think  
Mathematica :)).

>  table(i,iterator, combiner="+")    like apply("+",table(..))  but  
> faster.  See if it is faster than sum ..

It can't be. It would be exactly the same unless the expression  
always evaluates to (for instance) a floating point number..
If you have general maxima expressions in a list (as far as I  
understand maxima), then
apply("+", list) takes the same time as if you used 'add*' in your  
loop when constructing that list - perhaps even more.
Only when you know the type of result of expression, you can use that  
to improve speed (of addition, multiplication)... But the expression  
in table can be anything and can evaluates to a general maxima  
expression, nothing can be assumed. So apply("+", table() ) is  
(roughly) the same as if you used maximas summation inside table.  
Nothing gained...

What Mathematica does to get great speed is
1) arrays for lists
2) special array types (hidden from user): 2*{1.0,2.0,3.0,....,4.0}  
is calculated differently than 2*{1.0,Sin[x],3.0,4.0,...,4.0}, the  
first case utilizes numerical libraries (BLAS), while the second is  
calculated by mathematica, since BLAS cant handle non numeric arrays.
If your arrays/list have a type, then you can use 'fast' summing,  
multiplying...
But having types complicates the code (changing a list element can  
mean copying (probably cheap-copy) the whole list in Mathematica).

Regards,
Ziga Lenarcic