1/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
2/* [ Created with wxMaxima version 20.03.1-DevelopmentSnapshot ] */
3/* [wxMaxima: title   start ]
4Fast list access
5   [wxMaxima: title   end   ] */
6
7
8/* [wxMaxima: section start ]
9Lists and Arrays
10   [wxMaxima: section end   ] */
11
12
13/* [wxMaxima: comment start ]
14If one wants to store a bunch of items there is basically two methods:
15 * Arrays: If you know in advance that you want to store n items and that all have the same size m it is easy to allocate m*n bytes of memory. Determining the address the k-th element starts is easy in this case.
16 * Lists: Memory for each list element is allocated the moment a new element is needed and each element knows where in the computer's memory the next list element is stored.
17   [wxMaxima: comment end   ] */
18
19
20/* [wxMaxima: comment start ]
21Maxima loves Lists: It is easy to append items to a lists at any time. It is easy to delete list items. List items can be re-ordered just by swapping a few pointers. Maxima contains fast methods to sort lists, to eliminate double elements. But determining the length of a list or the address the nth list item is stored at means having to iterate through the list querying each item where the next list item can be found.
22   [wxMaxima: comment end   ] */
23
24
25/* [wxMaxima: section start ]
26Fast access to all list elements
27   [wxMaxima: section end   ] */
28
29
30/* [wxMaxima: comment start ]
31Accessing a random item of a list involves a time-consuming iterating over the list. But if one plans to iterate through the whole list, anyway (as one normally has to) this work isn't much of an overhead - and maxima offers several functions for this purpose:
32   [wxMaxima: comment end   ] */
33
34
35/* [wxMaxima: subsect start ]
36makelist
37   [wxMaxima: subsect end   ] */
38
39
40/* [wxMaxima: comment start ]
41Makelist is a quite universal swiss-army knife, if what you want to generate is shaped like a list.
42The most widely known form of this command is:
43   [wxMaxima: comment end   ] */
44
45
46/* [wxMaxima: input   start ] */
47list1:makelist(i,i,1,10);
48list2:makelist(i,i,0,9/10,1/10);
49/* [wxMaxima: input   end   ] */
50
51
52/* [wxMaxima: comment start ]
53But you can also tell makelist to iterate over a list - which is quite fast:
54   [wxMaxima: comment end   ] */
55
56
57/* [wxMaxima: input   start ] */
58list3:makelist(sin(i),i,list2);
59/* [wxMaxima: input   end   ] */
60
61
62/* [wxMaxima: subsect start ]
63for
64   [wxMaxima: subsect end   ] */
65
66
67/* [wxMaxima: comment start ]
68For does do nearly the same as makelist except that it doesn't return a list.:
69   [wxMaxima: comment end   ] */
70
71
72/* [wxMaxima: input   start ] */
73for i in list3 do disp(asin(i));
74/* [wxMaxima: input   end   ] */
75
76
77/* [wxMaxima: subsect start ]
78pop
79   [wxMaxima: subsect end   ] */
80
81
82/* [wxMaxima: comment start ]
83It is possible to access all list elements by accessing and removing the first element repeatedly:
84   [wxMaxima: comment end   ] */
85
86
87/* [wxMaxima: input   start ] */
88while not emptyp(list3) do
89    disp(pop(list3));
90/* [wxMaxima: input   end   ] */
91
92
93/* [wxMaxima: subsect start ]
94map
95   [wxMaxima: subsect end   ] */
96
97
98/* [wxMaxima: comment start ]
99map runs the same function on every single list element:
100   [wxMaxima: comment end   ] */
101
102
103/* [wxMaxima: input   start ] */
104list2;
105/* [wxMaxima: input   end   ] */
106
107
108/* [wxMaxima: input   start ] */
109map(sin,list2);
110/* [wxMaxima: input   end   ] */
111
112
113/* [wxMaxima: comment start ]
114If the function one wants to run on each list element is only used once and one doesn't want to give this one-time-function a name one can ask the lambda() command to generate a name-less function with one argument: Just passlambda() first the list of the arguments the new command shall have and the contents of that command:
115   [wxMaxima: comment end   ] */
116
117
118/* [wxMaxima: input   start ] */
119map(
120    lambda(
121        [x],
122        sin(cos(x))
123    ),
124    list2
125);
126/* [wxMaxima: input   end   ] */
127
128
129/* [wxMaxima: section start ]
130Iterating through two lists at the same time
131   [wxMaxima: section end   ] */
132
133
134/* [wxMaxima: comment start ]
135One way to do so efficiently is to use the pop() method on both lists. The other is to make a list of lists:
136   [wxMaxima: comment end   ] */
137
138
139/* [wxMaxima: input   start ] */
140M1:matrix(list1,list2);
141/* [wxMaxima: input   end   ] */
142
143
144/* [wxMaxima: input   start ] */
145M2:transpose(M1);
146/* [wxMaxima: input   end   ] */
147
148
149/* [wxMaxima: comment start ]
150Now we extract the list of arguments to the matrix() command:
151   [wxMaxima: comment end   ] */
152
153
154/* [wxMaxima: input   start ] */
155list4:args(M2);
156/* [wxMaxima: input   end   ] */
157
158
159/* [wxMaxima: comment start ]
160Of course all of these commands can be executed in one long command line:
161   [wxMaxima: comment end   ] */
162
163
164/* [wxMaxima: input   start ] */
165list5:args(transpose(matrix(list1,list2)));
166/* [wxMaxima: input   end   ] */
167
168
169/* [wxMaxima: comment start ]
170...and over this list we can iterate efficiently again:
171   [wxMaxima: comment end   ] */
172
173
174/* [wxMaxima: input   start ] */
175makelist(f(i[1])=i[2],i,list4);
176/* [wxMaxima: input   end   ] */
177
178
179
180/* Old versions of Maxima abort on loading files that end in a comment. */
181"Created with wxMaxima 20.03.1-DevelopmentSnapshot"$
182