2 question about arrays



--Alt-Boundary-1209.53062149
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Mail message body

Am 8 Feb 2006 um 15:32 hat Stavros Macrakis geschrieben:

> I'm afraid Maxima's system of arrays is rather messy.  There are three
> kinds of arrays: named Maxima arrays, anonymous Lisp arrays, and
> lists/matrices used as 1/2-dimensional arrays.
> 
> The array function creates a named array and adds it to the 'arrays' list.
> 
> The make_array function creates an *anonymous* array, which is not
> added to the 'arrays' list.  In your example above, you have not named
> the array, you have assigned an array value to a variable.
> 
that makes it clear

> Personally, I prefer the list/matrix solution because they are
> first-class objects in the Maxima world, though they are slower -- but
> that doesn't matter for many applications.

I did time consuming computations with 2-dim-lists, so I am interested to know, if arrays 
are faster. let's see ...
> 
> > foo works when not compiled, but not when compiled. can someone explain?
> 
> I'm not sure, but the compiler probably assumes that array is a named array.

named arrays do not seem be a solution. the compiled function does not see the 
values.

(%i1) array(test,2,3);
(%o1) 				     test
(%i2) arrays;
(%o2) 				    [test]
(%i3) for r:0 thru 2 do
	for c:0 thru 3 do
		test[r,c]: random(10)$
(%i4) printArray(array,rows,cols):= block(
	[ row ],
	for r:0 thru rows do (
		row:[],
		for c:cols thru 0 step -1 do row:cons(array[r,c],row),
		apply('print,row) ) )$
(%i5) printArray(test,2,3);
2 2 4 5 
4 1 9 5 
8 3 5 5 
(%o5) 				     done
(%i6) compile(printArray);
Compiling gazonk0.lsp.
End of Pass 1.  
End of Pass 2.  
OPTIMIZE levels: Safety=2, Space=3, Speed=2
Finished compiling gazonk0.lsp.
(%o6) 				 [printArray]
(%i7) printArray(test,2,3);
test     test	  test	   test	    
     0, 0      0, 1       0, 2        0, 3
test     test	  test	   test	    
     1, 0      1, 1       1, 2       1, 3
test     test	  test	   test	    
     2, 0      2, 1       2, 2       2, 3
(%o7) 				     done

Volker
 _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
> 



--Alt-Boundary-1209.53062149
Content-type: text/html; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Mail message body

<?xml  version="1.0" ?><html>
<head>
<title></title>
</head>
<body>
<div align="left"><font face="Arial"><span style="font-size:10pt">Am 8 Feb 2006 um 15:32 hat Stavros Macrakis geschrieben:</span></font></div>
<div align="left"><br/>
</div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; I'm afraid Maxima's system of arrays is rather messy.&#160; There are three</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; kinds of arrays: named Maxima arrays, anonymous Lisp arrays, and</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; lists/matrices used as 1/2-dimensional arrays.</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; The array function creates a named array and adds it to the 'arrays' list.</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; The make_array function creates an *anonymous* array, which is not</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; added to the 'arrays' list.&#160; In your example above, you have not named</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; the array, you have assigned an array value to a variable.</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">that makes it clear</span></font></div>
<div align="left"><br/>
</div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; Personally, I prefer the list/matrix solution because they are</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; first-class objects in the Maxima world, though they are slower -- but</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; that doesn't matter for many applications.</span></font></div>
<div align="left"><br/>
</div>
<div align="left"><font face="Arial"><span style="font-size:10pt">I did time consuming computations with 2-dim-lists, so I am interested to know, if arrays </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">are faster. let's see ...</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; &gt; foo works when not compiled, but not when compiled. can someone explain?</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; I'm not sure, but the compiler probably assumes that array is a named array.</span></font></div>
<div align="left"><br/>
</div>
<div align="left"><font face="Arial"><span style="font-size:10pt">named arrays do not seem be a solution. the compiled function does not see the </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">values.</span></font></div>
<div align="left"><br/>
</div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%i1) array(test,2,3);</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%o1) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
test</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%i2) arrays;</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%o2) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
[test]</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%i3) for r:0 thru 2 do</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for c:0 thru 3 do</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;test[r,c]: 
random(10)$</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%i4) printArray(array,rows,cols):= block(</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[ row ],</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for r:0 thru rows do (</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;row:[],</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for 
c:cols thru 0 step -1 do row:cons(array[r,c],row),</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;apply('print,row) 
) )$</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%i5) printArray(test,2,3);</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">2 2 4 5 </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">4 1 9 5 </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">8 3 5 5 </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%o5) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
done</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%i6) compile(printArray);</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">Compiling gazonk0.lsp.</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">End of Pass 1.&#160; </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">End of Pass 2.&#160; </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">OPTIMIZE levels: Safety=2, Space=3, Speed=2</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">Finished compiling gazonk0.lsp.</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%o6) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
[printArray]</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%i7) printArray(test,2,3);</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">test&#160;&#160;&#160;&#160; test&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; test&#160;&#160;&#160;&#160;&#160;&#160; 
test&#160;&#160;&#160;&#160;&#160;&#160; </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160; 0, 0&#160;&#160;&#160;&#160;&#160; 0, 1&#160;&#160;&#160;&#160;&#160;&#160; 0, 2&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
0, 3</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">test&#160;&#160;&#160;&#160; test&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; test&#160;&#160;&#160;&#160;&#160;&#160; 
test&#160;&#160;&#160;&#160;&#160;&#160; </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160; 1, 0&#160;&#160;&#160;&#160;&#160; 1, 1&#160;&#160;&#160;&#160;&#160;&#160; 1, 2&#160;&#160;&#160;&#160;&#160;&#160; 
1, 3</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">test&#160;&#160;&#160;&#160; test&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; test&#160;&#160;&#160;&#160;&#160;&#160; 
test&#160;&#160;&#160;&#160;&#160;&#160; </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">&#160;&#160;&#160;&#160; 2, 0&#160;&#160;&#160;&#160;&#160; 2, 1&#160;&#160;&#160;&#160;&#160;&#160; 2, 2&#160;&#160;&#160;&#160;&#160;&#160; 
2, 3</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt">(%o7) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
done</span></font></div>
<div align="left"><br/>
</div>
<div align="left"><font face="Arial"><span style="font-size:10pt">Volker</span></font></div>
<div align="left"><font face="Arial"><span style="font-size:10pt"> _______________________________________________</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; Maxima mailing list</span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font><font face="Arial" color="#008000"><span style="font-size:10pt"><u>Maxima at math.utexas.edu</u></span></font></div>
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font><font face="Arial" color="#008000"><span style="font-size:10pt"><u>http://www.math.utexas.edu/mailman/listinfo/maxima</u></span></font></div>;
<div align="left"><font face="Arial" color="#7f0000"><span style="font-size:10pt">&gt; </span></font></div>
<div align="left"><br/></div>
<div align="left"></div>
</body>
</html>

--Alt-Boundary-1209.53062149--