File: maxima_40.html

package info (click to toggle)
maxima 5.47.0-9
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 193,104 kB
  • sloc: lisp: 434,678; fortran: 14,665; tcl: 10,990; sh: 4,577; makefile: 2,763; ansic: 447; java: 328; python: 262; perl: 201; xml: 60; awk: 28; sed: 15; javascript: 2
file content (465 lines) | stat: -rw-r--r-- 20,242 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Maxima 5.47.0 Manual: Assignment operators</title>

<meta name="description" content="Maxima 5.47.0 Manual: Assignment operators">
<meta name="keywords" content="Maxima 5.47.0 Manual: Assignment operators">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="maxima_toc.html#Top" rel="start" title="Top">
<link href="maxima_423.html#Function-and-Variable-Index" rel="index" title="Function and Variable Index">
<link href="maxima_toc.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="maxima_34.html#Operators" rel="up" title="Operators">
<link href="maxima_41.html#User-defined-operators" rel="next" title="User defined operators">
<link href="maxima_39.html#Operators-for-Equations" rel="previous" title="Operators for Equations">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
body {color: black; background: white;  margin-left: 8%; margin-right: 13%;
      font-family: "FreeSans", sans-serif}
h1 {font-size: 150%; font-family: "FreeSans", sans-serif}
h2 {font-size: 125%; font-family: "FreeSans", sans-serif}
h3 {font-size: 100%; font-family: "FreeSans", sans-serif}
a[href] {color: rgb(0,0,255); text-decoration: none;}
a[href]:hover {background: rgb(220,220,220);}
div.textbox {border: solid; border-width: thin; padding-top: 1em;
    padding-bottom: 1em; padding-left: 2em; padding-right: 2em}
div.titlebox {border: none; padding-top: 1em; padding-bottom: 1em;
    padding-left: 2em; padding-right: 2em; background: rgb(200,255,255);
    font-family: sans-serif}
div.synopsisbox {
    border: none; padding-top: 1em; padding-bottom: 1em; padding-left: 2em;
    padding-right: 2em; background: rgb(255,220,255);}
pre.example {border: 1px solid rgb(180,180,180); padding-top: 1em;
    padding-bottom: 1em; padding-left: 1em; padding-right: 1em;
    background-color: rgb(238,238,255)}
div.spacerbox {border: none; padding-top: 2em; padding-bottom: 2em}
div.image {margin: 0; padding: 1em; text-align: center}
div.categorybox {border: 1px solid gray; padding-top: 1em; padding-bottom: 1em;
    padding-left: 1em; padding-right: 1em; background: rgb(247,242,220)}
img {max-width:80%; max-height: 80%; display: block; margin-left: auto; margin-right: auto}

-->
</style>

<link rel="icon" href="figures/favicon.ico">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6>"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Assignment-operators"></a>
<div class="header">
<p>
Next: <a href="maxima_41.html#User-defined-operators" accesskey="n" rel="next">User defined operators</a>, Previous: <a href="maxima_39.html#Operators-for-Equations" accesskey="p" rel="previous">Operators for Equations</a>, Up: <a href="maxima_34.html#Operators" accesskey="u" rel="up">Operators</a> &nbsp; [<a href="maxima_toc.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="maxima_423.html#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Assignment-operators-1"></a>
<h3 class="section">7.6 Assignment operators</h3>

<a name="g_t_003a"></a><a name="Item_003a-Operators_002fdeffn_002f_003a"></a><dl>
<dt><a name="index-_003a"></a>Operator: <strong>:</strong></dt>
<dd><a name="index-Assignment-operator"></a>

<p>Assignment operator.
</p>
<p>When the left-hand side is a simple variable (not subscripted), <code>:</code>
evaluates its right-hand side and associates that value with the left-hand side.
</p>
<p>When the left-hand side is a subscripted element of a list, matrix, declared
Maxima array, or Lisp array, the right-hand side is assigned to that element.
The subscript must name an existing element; such objects cannot be extended by
naming nonexistent elements.
</p>
<p>When the left-hand side is a subscripted element of a <code><a href="maxima_23.html#hashed-array">hashed array</a></code>,
the right-hand side is assigned to that element, if it already exists,
or a new element is allocated, if it does not already exist.
</p>
<p>When the left-hand side is a list of simple and/or subscripted variables, the
right-hand side must evaluate to a list, and the elements of the right-hand
side are assigned to the elements of the left-hand side, in parallel.
</p>
<p>See also <code><a href="maxima_8.html#kill">kill</a></code> and <code><a href="maxima_159.html#remvalue">remvalue</a></code>, which undo the association between
the left-hand side and its value.
</p>
<p>Examples:
</p>
<p>Assignment to a simple variable.
</p>
<div class="example">
<pre class="example">(%i1) a;
(%o1)                           a
(%i2) a : 123;
(%o2)                          123
(%i3) a;
(%o3)                          123
</pre></div>

<p>Assignment to an element of a list.
</p>
<div class="example">
<pre class="example">(%i1) b : [1, 2, 3];
(%o1)                       [1, 2, 3]
(%i2) b[3] : 456;
(%o2)                          456
(%i3) b;
(%o3)                      [1, 2, 456]
</pre></div>

<p>Assignment to a variable that neither is the name of a list nor of an array
creates a <code><a href="maxima_23.html#hashed-array">hashed array</a></code>.
</p>
<div class="example">
<pre class="example">(%i1) c[99] : 789;
(%o1)                          789
(%i2) c[99];
(%o2)                          789
(%i3) c;
(%o3)                           c
(%i4) arrayinfo (c);
(%o4)                   [hashed, 1, [99]]
(%i5) listarray (c);
(%o5)                         [789]
</pre></div>

<p>Multiple assignment.
</p>
<div class="example">
<pre class="example">(%i1) [a, b, c] : [45, 67, 89];
(%o1)                     [45, 67, 89]
(%i2) a;
(%o2)                          45
(%i3) b;
(%o3)                          67
(%i4) c;
(%o4)                          89
</pre></div>

<p>Multiple assignment is carried out in parallel.
The values of <code>a</code> and <code>b</code> are exchanged in this example.
</p>
<div class="example">
<pre class="example">(%i1) [a, b] : [33, 55];
(%o1)                       [33, 55]
(%i2) [a, b] : [b, a];
(%o2)                       [55, 33]
(%i3) a;
(%o3)                          55
(%i4) b;
(%o4)                          33
</pre></div>

<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Evaluation">Evaluation</a>
&middot;<a href="maxima_424.html#Category_003a-Operators">Operators</a>
&middot;</div></dd></dl>

<a name="g_t_003a_003a"></a><a name="Item_003a-Operators_002fdeffn_002f_003a_003a"></a><dl>
<dt><a name="index-_003a_003a"></a>Operator: <strong>::</strong></dt>
<dd><a name="index-Assignment-operator-_0028evaluates-left_002dhand-side_0029"></a>

<p>Assignment operator.
</p>
<p><code>::</code> is the same as <code><a href="#g_t_003a">:</a></code> (which see) except that <code>::</code> evaluates
its left-hand side as well as its right-hand side.
</p>
<p>Examples:
</p>
<div class="example">
<pre class="example">(%i1) x : 'foo;
(%o1)                          foo
(%i2) x :: 123;
(%o2)                          123
(%i3) foo;
(%o3)                          123
(%i4) x : '[a, b, c];
(%o4)                       [a, b, c]
(%i5) x :: [11, 22, 33];
(%o5)                     [11, 22, 33]
(%i6) a;
(%o6)                          11
(%i7) b;
(%o7)                          22
(%i8) c;
(%o8)                          33
</pre></div>

<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Evaluation">Evaluation</a>
&middot;<a href="maxima_424.html#Category_003a-Operators">Operators</a>
&middot;</div></dd></dl>

<a name="g_t_003a_003a_003d"></a><a name="Item_003a-Operators_002fdeffn_002f_003a_003a_003d"></a><dl>
<dt><a name="index-_003a_003a_003d"></a>Operator: <strong>::=</strong></dt>
<dd><a name="index-Macro-function-definition-operator"></a>

<p>Macro function definition operator.
<code>::=</code> defines a function (called a &quot;macro&quot; for historical reasons) which
quotes its arguments, and the expression which it returns (called the &quot;macro
expansion&quot;) is evaluated in the context from which the macro was called.
A macro function is otherwise the same as an ordinary function.
</p>
<p><code><a href="maxima_169.html#macroexpand">macroexpand</a></code> returns a macro expansion (without evaluating it).
<code>macroexpand (foo (x))</code> followed by <code>''%</code> is equivalent to
<code>foo (x)</code> when <code>foo</code> is a macro function.
</p>
<p><code>::=</code> puts the name of the new macro function onto the global list
<code><a href="maxima_169.html#macros">macros</a></code>.  <code><a href="maxima_8.html#kill">kill</a></code>, <code><a href="maxima_62.html#remove">remove</a></code>, and <code><a href="maxima_170.html#remfunction">remfunction</a></code><!-- /@w -->
unbind macro function definitions and remove names from <code>macros</code>.
</p>
<p><code><a href="maxima_170.html#fundef">fundef</a></code> or <code><a href="maxima_170.html#dispfun">dispfun</a></code> return a macro function definition or assign it
to a label, respectively.
</p>
<p>Macro functions commonly contain <code><a href="maxima_169.html#buildq">buildq</a></code> and <code><a href="maxima_169.html#splice">splice</a></code> expressions to
construct an expression, which is then evaluated.
</p>
<p>Examples
</p>
<p>A macro function quotes its arguments, so message (1) shows <code>y - z</code>, not
the value of <code>y - z</code>.  The macro expansion (the quoted expression
<code>'(print (&quot;(2) x is equal to&quot;, x))</code>) is evaluated in the context from which
the macro was called, printing message (2).
</p>
<div class="example">
<pre class="example">(%i1) x: %pi$
(%i2) y: 1234$
(%i3) z: 1729 * w$
(%i4) printq1 (x) ::= block (print (&quot;(1) x is equal to&quot;, x),
      '(print (&quot;(2) x is equal to&quot;, x)))$
(%i5) printq1 (y - z);
(1) x is equal to y - z
(2) x is equal to %pi
(%o5)                                 %pi
</pre></div>

<p>An ordinary function evaluates its arguments, so message (1) shows the value of
<code>y - z</code>.  The return value is not evaluated, so message (2) is not printed
until the explicit evaluation <code>''%</code>.
</p>
<div class="example">
<pre class="example">(%i1) x: %pi$
(%i2) y: 1234$
(%i3) z: 1729 * w$
(%i4) printe1 (x) := block (print (&quot;(1) x is equal to&quot;, x),
      '(print (&quot;(2) x is equal to&quot;, x)))$
(%i5) printe1 (y - z);
(1) x is equal to 1234 - 1729 w
(%o5)                     print((2) x is equal to, x)
(%i6) ''%;
(2) x is equal to %pi
(%o6)                                 %pi
</pre></div>

<p><code>macroexpand</code> returns a macro expansion.
<code>macroexpand (foo (x))</code> followed by <code>''%</code> is equivalent to
<code>foo (x)</code> when <code>foo</code> is a macro function.
</p>
<div class="example">
<pre class="example">(%i1) x: %pi$
(%i2) y: 1234$
(%i3) z: 1729 * w$
(%i4) g (x) ::= buildq ([x], print (&quot;x is equal to&quot;, x))$
(%i5) macroexpand (g (y - z));
(%o5)                     print(x is equal to, y - z)
(%i6) ''%;
x is equal to 1234 - 1729 w
(%o6)                            1234 - 1729 w
(%i7) g (y - z);
x is equal to 1234 - 1729 w
(%o7)                            1234 - 1729 w
</pre></div>

<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Function-definition">Function definition</a>
&middot;<a href="maxima_424.html#Category_003a-Operators">Operators</a>
&middot;</div></dd></dl>

<a name="g_t_003a_003d"></a><a name="Item_003a-Operators_002fdeffn_002f_003a_003d"></a><dl>
<dt><a name="index-_003a_003d"></a>Operator: <strong>:=</strong></dt>
<dd><a name="index-Function-definition-operator"></a>

<p>The function definition operator.
</p>
<p><code><var>f</var>(<var>x_1</var>, ..., <var>x_n</var>) := <var>expr</var></code> defines a function named
<var>f</var> with arguments <var>x_1</var>, &hellip;, <var>x_n</var> and function body
<var>expr</var>.  <code>:=</code> never evaluates the function body (unless explicitly
evaluated by quote-quote <code>'<!-- /@w -->'</code>).
The function body is evaluated every time the function is called.
</p>
<p><code><var>f</var>[<var>x_1</var>, ..., <var>x_n</var>] := <var>expr</var></code> defines a so-called
<code><a href="maxima_168.html#memoizing-function">memoizing function</a></code>.
Its function body is evaluated just once for each distinct value of its arguments,
and that value is returned, without evaluating the function body,
whenever the arguments have those values again.
(A function of this kind is also known as a &ldquo;array function&rdquo;.)
</p>
<p><code><var>f</var>[<var>x_1</var>, ..., <var>x_n</var>](<var>y_1</var>, ..., <var>y_m</var>) := <var>expr</var></code>
is a special case of a <code><a href="maxima_168.html#memoizing-function">memoizing function</a></code>.
<code><var>f</var>[<var>x_1</var>, ..., <var>x_n</var>]</code> is a <code><a href="maxima_168.html#memoizing-function">memoizing function</a></code> which returns a lambda expression
with arguments <code><var>y_1</var>, ..., <var>y_m</var></code>.
The function body is evaluated once for each distinct value of <code><var>x_1</var>, ..., <var>x_n</var></code>,
and the body of the lambda expression is that value.
</p>
<p>When the last or only function argument <var>x_n</var> is a list of one element, the
function defined by <code>:=</code> accepts a variable number of arguments.  Actual
arguments are assigned one-to-one to formal arguments <var>x_1</var>, &hellip;,
<var>x_(n - 1)</var>, and any further actual arguments, if present, are assigned to
<var>x_n</var> as a list.
</p>
<p>All function definitions appear in the same namespace; defining a function
<code>f</code> within another function <code>g</code> does not automatically limit the scope
of <code>f</code> to <code>g</code>.  However, <code>local(f)</code> makes the definition of
function <code>f</code> effective only within the block or other compound expression
in which <code><a href="maxima_170.html#local">local</a></code> appears.
</p>
<p>If some formal argument <var>x_k</var> is a quoted symbol, the function defined by
<code>:=</code> does not evaluate the corresponding actual argument.  Otherwise all
actual arguments are evaluated.
</p>
<p>See also <code><a href="maxima_170.html#define">define</a></code> and <code><a href="#g_t_003a_003a_003d">::=</a></code>.
</p>
<p>Examples:
</p>
<p><code>:=</code> never evaluates the function body (unless explicitly evaluated by
quote-quote).
</p>
<div class="example">
<pre class="example">(%i1) expr : cos(y) - sin(x);
(%o1)                    cos(y) - sin(x)
(%i2) F1 (x, y) := expr;
(%o2)                   F1(x, y) := expr
(%i3) F1 (a, b);
(%o3)                    cos(y) - sin(x)
(%i4) F2 (x, y) := ''expr;
(%o4)              F2(x, y) := cos(y) - sin(x)
(%i5) F2 (a, b);
(%o5)                    cos(b) - sin(a)
</pre></div>

<p><code>f(<var>x_1</var>, ..., <var>x_n</var>) := ...</code> defines an ordinary function.
</p>
<div class="example">
<pre class="example">(%i1) G1(x, y) := (print (&quot;Evaluating G1 for x=&quot;, x, &quot;and y=&quot;, y),
 x.y - y.x);
(%o1) G1(x, y) := (print(&quot;Evaluating G1 for x=&quot;, x, &quot;and y=&quot;, 
                                               y), x . y - y . x)
(%i2) G1([1, a], [2, b]);
Evaluating G1 for x= [1, a] and y= [2, b] 
(%o2)                           0
(%i3) G1([1, a], [2, b]);
Evaluating G1 for x= [1, a] and y= [2, b] 
(%o3)                           0
</pre></div>

<p><code>f[<var>x_1</var>, ..., <var>x_n</var>] := ...</code> defines a <code><a href="maxima_168.html#memoizing-function">memoizing function</a></code>.
</p>
<div class="example">
<pre class="example">(%i1) G2[a] := (print (&quot;Evaluating G2 for a=&quot;, a), a^2);
                                                     2
(%o1)     G2  := (print(&quot;Evaluating G2 for a=&quot;, a), a )
            a
(%i2) G2[1234];
Evaluating G2 for a= 1234 
(%o2)                        1522756
(%i3) G2[1234];
(%o3)                        1522756
(%i4) G2[2345];
Evaluating G2 for a= 2345 
(%o4)                        5499025
(%i5) arrayinfo (G2);
(%o5)              [hashed, 1, [1234], [2345]]
(%i6) listarray (G2);
(%o6)                  [1522756, 5499025]
</pre></div>

<p><code><var>f</var>[<var>x_1</var>, ..., <var>x_n</var>](<var>y_1</var>, ..., <var>y_m</var>) := <var>expr</var></code>
is a special case of a <code><a href="maxima_168.html#memoizing-function">memoizing function</a></code>.
</p>
<div class="example">
<pre class="example">(%i1) G3[n](x) := (print (&quot;Evaluating G3 for n=&quot;, n), diff (sin(x)^2,
 x, n));
(%o1) G3 (x) := (print(&quot;Evaluating G3 for n=&quot;, n), 
        n
                                                     2
                                             diff(sin (x), x, n))
(%i2) G3[2];
Evaluating G3 for n= 2 
                                2           2
(%o2)          lambda([x], 2 cos (x) - 2 sin (x))
(%i3) G3[2];
                                2           2
(%o3)          lambda([x], 2 cos (x) - 2 sin (x))
(%i4) G3[2](1);
                           2           2
(%o4)                 2 cos (1) - 2 sin (1)
(%i5) arrayinfo (G3);
(%o5)                   [hashed, 1, [2]]
(%i6) listarray (G3);
                                2           2
(%o6)         [lambda([x], 2 cos (x) - 2 sin (x))]
</pre></div>

<p>When the last or only function argument <var>x_n</var> is a list of one element,
the function defined by <code>:=</code> accepts a variable number of arguments.
</p>
<div class="example">
<pre class="example">(%i1) H ([L]) := apply (&quot;+&quot;, L);
(%o1)                H([L]) := apply(&quot;+&quot;, L)
(%i2) H (a, b, c);
(%o2)                       c + b + a
</pre></div>

<p><code>local</code> makes a local function definition.
</p>
<div class="example">
<pre class="example">(%i1) foo (x) := 1 - x;
(%o1)                    foo(x) := 1 - x
(%i2) foo (100);
(%o2)                         - 99
(%i3) block (local (foo), foo (x) := 2 * x, foo (100));
(%o3)                          200
(%i4) foo (100);
(%o4)                         - 99
</pre></div>

<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Function-definition">Function definition</a>
&middot;<a href="maxima_424.html#Category_003a-Operators">Operators</a>
&middot;</div></dd></dl>

<a name="Item_003a-Operators_002fnode_002fUser-defined-operators"></a><hr>
<div class="header">
<p>
Next: <a href="maxima_41.html#User-defined-operators" accesskey="n" rel="next">User defined operators</a>, Previous: <a href="maxima_39.html#Operators-for-Equations" accesskey="p" rel="previous">Operators for Equations</a>, Up: <a href="maxima_34.html#Operators" accesskey="u" rel="up">Operators</a> &nbsp; [<a href="maxima_toc.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="maxima_423.html#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>