Annotation of OpenXM/src/asir-doc/parts/asir.texi, Revision 1.4
1.4 ! noro 1: @comment $OpenXM: OpenXM/src/asir-doc/parts/asir.texi,v 1.3 1999/12/21 02:47:31 noro Exp $
1.3 noro 2: \BJP
1.1 noro 3: @node $B%f!<%68@8l(B Asir,,, Top
4: @chapter $B%f!<%68@8l(B Asir
1.3 noro 5: \E
6: \BEG
7: @node User language Asir,,, Top
8: @chapter User language @b{Asir}
9: \E
1.1 noro 10:
11: @noindent
1.3 noro 12: \BJP
1.1 noro 13: @b{Asir} $B$NAH$_9~$_H!?t$O(B, $B0x?tJ,2r(B, GCD $B$J$I$N7W;;$r9T$&$b$N(B, $B%U%!(B
14: $B%$%kF~=PNO$r9T$&$b$N(B, $B$"$k$$$O?t<0$N0lIt$r<h$j=P$9$b$N$J$I$5$^$6$^$J$b$N(B
15: $B$,MQ0U$5$l$F$$$k$,(B, $B%f!<%6$,<B:]$K9T$$$?$$$3$H$r<B9T$5$;$k$?$a$K$O0l(B
16: $BHL$K$O%f!<%68@8l$K$h$k%W%m%0%i%`$r=q$/I,MW$,$"$k(B. $B%f!<%68@8l$b(B
17: @b{Asir} $B$H8F$P$l$k(B. $B0J2<$G$O(B, $B%f!<%68@8l$NJ8K!5,B'$*$h$S<B:]$N%f!<(B
18: $B%68@8l%W%m%0%i%`$rNc$H$7$?%W%m%0%i%`$N=q$-J}$K$D$$$F=R$Y$k(B.
1.3 noro 19: \E
20: \BEG
21: @b{Asir} provides many built-in functions, which perform algebraic
22: computations, e.g., factorization and GCD computation, file I/O,
23: extract a part of an algebraic expression, etc.
24: In practice, you will often encounter a specific problem for which
25: @b{Asir} does not provide a direct solution. For such cases, you have
26: to write a program in a certain user language. The user language for
27: @b{Asir} is also called @b{Asir}. In the following, we describe the
28: Syntax and then show how to write a user program by several examples.
29: \E
1.1 noro 30:
31: @menu
1.3 noro 32: \BJP
1.1 noro 33: * $BJ8K!(B (C $B8@8l$H$N0c$$(B)::
34: * $B%f!<%6Dj5AH!?t$N=q$-J}(B::
1.3 noro 35: \E
36: \BEG
37: * Syntax (Difference from C language)::
38: * Writing user defined functions::
39: \E
1.1 noro 40: @end menu
41:
42:
1.3 noro 43: \BJP
1.1 noro 44: @node $BJ8K!(B (C $B8@8l$H$N0c$$(B),,, $B%f!<%68@8l(B Asir
45: @section $BJ8K!(B (C $B8@8l$H$N0c$$(B)
1.3 noro 46: \E
47: \BEG
48: @node Syntax (Difference from C language),,, User language Asir
49: @section Syntax --- Difference from C language
50: \E
1.1 noro 51:
52: @noindent
1.3 noro 53: \BJP
1.1 noro 54: @b{Asir} $B$NJ8K!$O(B C $B8@8l$K=`5r$7$F$$$k(B.
55: $B$*$b$JAj0cE@$O<!$NDL$j$G$"$k(B. $B0J2<$G(B, $BJQ?t$H$O(B @b{Asir} $B$K$*$1$k(B
56: $B%W%m%0%i%`MQ$NJQ?t(B, $B$9$J$o$ABgJ8;z$G;O$^$kJ8;zNs$r0UL#$9$k$3$H$H$9$k(B.
1.3 noro 57: \E
58: \BEG
59: The syntax of @b{Asir} is based on C language.
60: Main differences are as follows.
61: In this section, a variable does not mean an indeterminate, but
62: a program variable which is written by a string which begins with a
63: capital alphabetical letter in @b{Asir}.
64: \E
1.1 noro 65:
66: @itemize @bullet
67: @item
1.3 noro 68: \JP $BJQ?t$N7?$,$J$$(B.
69: \EG No types for variables.
1.4 ! noro 70: @*
1.3 noro 71: \BJP
1.1 noro 72: $B4{$K@bL@$7$?$H$*$j(B, @b{Asir} $B$G07$o$l$kBP>]<+?H$OA4$F2?$i$+$N7?(B
73: $B$r;}$C$F$$$k(B. $B$7$+$7(B, $B%W%m%0%i%`JQ?t<+BN$O(B, $B$I$N$h$&$JBP>]$G$b(B
74: $BBeF~$G$-$k$H$$$&0UL#$G7?$,$J$$$N$G$"$k(B.
1.3 noro 75: \E
76: \BEG
77: As is already mentioned, any object in @b{Asir} has their respective
78: types. A program variable, however, is type-less, that is, any typed
79: object can be assigned to it.
80: \E
1.1 noro 81:
82: @example
83: [0] A = 1;
84: 1
85: [1] type(A);
86: 1
87: [2] A = [1,2,3];
88: [1,2,3]
89: [3] type(A);
90: 4
91: @end example
92:
93: @item
1.3 noro 94: \BJP
1.1 noro 95: $BH!?tFb$NJQ?t$O(B, $B%G%U%)%k%H$G$O2>0z?t$r$3$a$F$9$Y$F6I=jJQ?t(B.
1.4 ! noro 96: @*
1.1 noro 97: $B$?$@$7(B, @code{extern} $B@k8@$5$l$?JQ?t$O(B, $B%H%C%W%l%Y%k$K$*$1$kBg0hJQ?t$H$J$k(B.
98: $B$9$J$o$A(B, $BJQ?t$N%9%3!<%W$OBg0hJQ?t$H6I=jJQ?t$N(B 2 $B<oN`$KC1=c2=$5$l$F$$$k(B.
99: $B%H%C%W%l%Y%k(B, $B$9$J$o$A%W%m%s%W%H$KBP$7$FF~NO$5$l$?JQ?t$OA4$FBg0hJQ?t(B
100: $B$H$7$FEPO?$5$l$k(B. $B$^$?H!?tFb$G$O<!$N$$$:$l$+$H$J$k(B.
1.3 noro 101: \E
102: \BEG
103: Variables, together with formal parameters, in a function (procedure)
104: are all local to the function by default.
1.4 ! noro 105: @*
1.3 noro 106: Variables can be global at the top level,
107: if they are declared with the key word @code{extern}.
108: Thus, the scope rule of @b{Asir} is very simple.
109: There are only two types of variables: global variables and local
110: variables.
111: A name that is input to the @b{Asir}'s prompt at the top level
112: is denotes a global variable commonly accessed at the top level.
113: In a function (procedure) the following rules are applied.
114: \E
1.1 noro 115:
116: @enumerate
117: @item
1.3 noro 118: \BJP
1.1 noro 119: $BH!?t$,Dj5A$5$l$k%U%!%$%k$K$*$$$F(B, $B$=$NH!?tDj5A0JA0$K(B, $B$"$k(B
120: $BJQ?t$,(B @code{extern} $B@k8@$5$l$F$$$k>l9g(B, $BH!?tFb$N$=$NJQ?t$bBg0hJQ?t(B
121: $B$H$7$F07$o$l$k(B.
1.3 noro 122: \E
123: \BEG
124: If a variable is declared as global by an @code{extern} statement in
125: a function, the variable used in that function denotes a global variable
126: at the top level.
127: Furthermore, if a variable in a function is preceded by an @code{extern}
128: declaration outside the function but in a file where the function is
129: defined, all the appearance of that variable in the same file denote
130: commonly a global variable at the top level.
131: \E
132:
133: @item
134: \JP @code{extern} $B@k8@$5$l$F$$$J$$JQ?t$O$=$NH!?t$K6I=jE*$H$J$k(B.
135: \BEG
136: A variable in a function is local to that function, if it is not declared
137: as global by an @code{extern} declaration.
138: \E
1.1 noro 139: @end enumerate
140:
141: @example
142: % cat afo
143: def afo() @{ return A;@}
144: extern A$
145: def bfo() @{ return A;@}
146: end$
147: % asir
148: [0] load("afo")$
149: [5] A = 1;
150: 1
151: [6] afo();
152: 0
153: [7] bfo();
154: 1
155: @end example
156:
157: @item
1.3 noro 158: \JP $B%W%m%0%i%`JQ?t$OBgJ8;z$G;O$^$j(B, $BITDj85(B, $BH!?t$O>.J8;z$G;O$^$k(B.
159: \EG Program variables and algebraic indeterminates are distinguished in @b{Asir}.
1.4 ! noro 160: @*
1.3 noro 161: \BJP
1.1 noro 162: $B$3$NE@$O(B, $B4{B8$N?t<0=hM}%7%9%F%`$N$[$H$s$I$H0[$J$kE@$G$"$k(B. @b{Asir}
163: $B$,$3$N;EMM$r:NMQ$7$?$N$O(B, $B%f!<%6$,ITDj85$N$D$b$j$G;HMQ$7$?JQ?t$K(B
164: $B$J$s$i$+$NCM$,BeF~$5$l$F$$$?>l9g$K:.Mp$r>7$/(B, $B$H$$$&(B, $B4{B8$N(B
165: $B%7%9%F%`$K$"$j$,$A$J>u67$rHr$1$k$?$a$G$"$k(B.
1.3 noro 166: \E
167: \BEG
168: The names of program variables must begin with a capital letter;
169: while the names of indeterminates and functions must begin with
170: a small letter.
171:
172: This is an unique point that differs from almost all other existing
173: computer algebra systems. The distinction between program variables
174: and indeterminates is adopted to avoid the possible and usual confusion
175: that may arise in a situation where a name is used as an indeterminate
176: but, as it was, the name has been already assigned some value.
177: To use different type of letters, capital and small, was a matter of
178: syntactical convention like Prolog, but it is convenient to distinguish
179: variables and indeterminates in a program.
180: \E
181:
182: @item
183: \JP @code{switch} $BJ8(B, @code{goto} $B$,$J$$(B.
184: \EG No @code{switch} statements, and @code{goto} statements.
1.4 ! noro 185: @*
1.3 noro 186: \JP @code{goto} $B$,$J$$$?$a(B, $BB?=E%k!<%W$r0lEY$KH4$1$k$N$,$d$dJ#;($K$J$k>l9g$,$"$k(B.
187: \EG Lack of @code{goto} statement makes it rather bothering to exit from within multiple loops.
188:
189: @item
190: \BJP
191: $B%3%s%^<0$O(B, @code{for (A;B;C)} $B$^$?$O(B, @code{while(A)} $B$N(B @code{A},
192: @code{B}, @code{C} $B$K$N$_;H$&$3$H$,$G$-$k(B.
193: \E
194: \BEG
195: Comma expressions are allowed only in @code{A}, @code{B} and @code{C}
196: of the constructs @code{for (A;B;C)} or @code{while(A)}.
197: \E
1.4 ! noro 198: @*
1.3 noro 199: \JP $B$3$l$O(B, $B%j%9%H$r@5<0$J%*%V%8%'%/%H$H$7$F2C$($?$3$H$K$h$k(B.
200: \EG This limitation came from adopting lists as legal data objects for @b{Asir}.
1.1 noro 201:
202: @end itemize
203:
204: @noindent
1.3 noro 205: \JP $B0J>e$O@)8B$G$"$k$,(B, $B3HD%$H$7$F$O<!$NE@$,5s$2$i$l$k(B.
206: \EG The above are limitations; extensions are listed as follows.
1.1 noro 207:
208: @itemize @bullet
209: @item
1.3 noro 210: \JP $BM-M}<0$KBP$9$k7W;;$r(B, $BDL>o$N(B C $B$K$*$1$k7W;;$HF1MM$K$G$-$k(B.
211: \BEG
212: Arithmetic for rational expressions can be done in the
213: same manner as is done for numbers in C language.
214: \E
1.1 noro 215:
216: @item
1.3 noro 217: \JP $B%j%9%H$,07$($k(B.
218: \EG Lists are available for data objects.
1.1 noro 219:
1.3 noro 220: \BJP
1.1 noro 221: $B9=B$BN$rMQ$$$k$^$G$b$J$$MWAG$N=89gBN$r(B, $B%j%9%H$GI=$9$3$H$,$G$-(B,
222: C $B$GD>@\=q$/>l9g$KHf3S$7$F%W%m%0%i%`$,C;$/(B, $BFI$_$d$9$/=q$1$k(B.
1.3 noro 223: \E
224: \BEG
225: Lists are conveniently used to represent a certain collection of objects.
226: Use of lists enables to write programs more easily, shorter and more
227: comprehensible than use of structure like C programs.
228: \E
1.2 noro 229:
230: @item
1.3 noro 231: \JP $B%f!<%6Dj5AH!?t$K$*$1$k%*%W%7%g%s;XDj(B.
232: \EG Options can be specified in calling user defined functions.
1.2 noro 233:
1.3 noro 234: \JP $B$3$l$K4X$7$F$O(B, @xref{$B%*%W%7%g%s;XDj(B}.
235: \EG @xref{option}.
1.1 noro 236: @end itemize
237:
1.3 noro 238: \BJP
1.1 noro 239: @node $B%f!<%6Dj5AH!?t$N=q$-J}(B,,, $B%f!<%68@8l(B Asir
240: @section $B%f!<%6Dj5AH!?t$N=q$-J}(B
1.3 noro 241: \E
242: \BEG
243: @node Writing user defined functions,,, User language Asir
244: @section Writing user defined functions
245: \E
1.1 noro 246:
247: @menu
1.3 noro 248: \BJP
1.1 noro 249: * $B%f!<%6Dj5AH!?t(B::
250: * $BJQ?t$*$h$SITDj85(B::
251: * $B0z?t(B::
252: * $B%3%a%s%H(B::
253: * $BJ8(B::
254: * return $BJ8(B::
255: * if $BJ8(B::
256: * $B%k!<%W(B break return continue::
257: * $B$5$^$6$^$J<0(B::
258: * $B%W%j%W%m%;%C%5(B::
1.2 noro 259: * $B%*%W%7%g%s;XDj(B::
1.3 noro 260: \E
261: \BEG
262: * User defined functions::
263: * variables and indeterminates::
264: * parameters and arguments::
265: * comments::
266: * statements::
267: * return statement::
268: * if statement::
269: * loop break return continue::
270: * various expressions::
271: * preprocessor::
272: * option::
273: \E
1.1 noro 274: @end menu
275:
1.3 noro 276: \BJP
1.1 noro 277: @node $B%f!<%6Dj5AH!?t(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
278: @subsection $B%f!<%6Dj5AH!?t(B
1.3 noro 279: \E
280: \BEG
281: @node User defined functions,,, Writing user defined functions
282: @subsection User defined functions
283: \E
1.1 noro 284:
285: @noindent
1.3 noro 286: \BJP
1.1 noro 287: $B%f!<%6$K$h$kH!?t$NDj5A$O(B @samp{def} $BJ8$G9T$&(B. $BJ8K!%(%i!<$OFI$_9~$_;~$K(B
288: $B$"$kDxEY%A%'%C%/$5$l(B, $B$*$*$h$=$N>l=j$,I=<($5$l$k(B.
289: $B4{$K(B($B0z?t$N8D?t$K4X78$J$/(B)$BF1L>$NH!?t$,Dj5A$5$l$F$$$k>l9g$K$O(B,
290: $B$=$NH!?t$O:FDj5A$5$l$k(B. @code{ctrl()} $BH!?t$K$h$j(B @code{verbose} $B%U%i%0(B
291: $B$,(B on $B$K$J$C$F$$$k>l9g(B,
1.3 noro 292: \E
293: \BEG
294: To define functions by an user himself, @samp{def} statement must be used.
295: Syntactical errors are detected in the parsing phase
296: of @b{Asir}, and notified with an indication of where @b{Asir} found the error.
297: If a function with the same name is already defined (regardless to
298: its arity,) the new definition will override the old one, and the user
299: will be told by a message,
300: \E
1.1 noro 301:
302: @example
303: afo() redefined.
304: @end example
305:
306: @noindent
1.3 noro 307: \BJP
1.1 noro 308: $B$H$$$&%a%C%;!<%8$,I=<($5$l$k(B. $B$"$kH!?t$NDj5A$K$*$$$F(B, $B$^$@L$Dj5A$NH!?t(B
309: $B$r8F$S=P$7$F$$$F$b(B, $BDj5A;~$K$O%(%i!<$K$J$i$J$$(B. $B<B9T;~$KL$Dj5A$NH!?t(B
310: $B$r8F$S=P$=$&$H$7$?>l9g$K%(%i!<$H$J$k(B.
1.3 noro 311: \E
312: \BEG
313: on the screen when a flag @code{verbose} is set to a non-zero value by
314: @code{ctrl()}.
315: Recursive definition, and of course, recursive use of functions are
316: available.
317: A call for an yet undefined function in a function definition is not
318: detected as an error. An error will be detected at execution of the
319: call of that yet undefined function.
320: \E
1.1 noro 321: @example
322: @tex
323: /* $X!$ */
324: @end tex
325:
326: def f(X) @{
327: if ( !X )
328: return 1;
329: else
330: return X * f(X-1);
331: @}
332:
333: @tex
1.3 noro 334: \JP /* ${_i}C_j ( 0 \le i \le N, 0 \le j \le i )$ */
335: \EG /* ${_i}C_j ( 0 \le i \le N, 0 \le j \le i )$ */
1.1 noro 336: @end tex
337:
338: def c(N)
339: @{
340: A = newvect(N+1); A[0] = B = newvect(1); B[0] = 1;
341: for ( K = 1; K <= N; K++ ) @{
342: A[K] = B = newvect(K+1); B[0] = B[K] = 1;
343: for ( P = A[K-1], J = 1; J < K; J++ )
344: B[J] = P[J-1]+P[J];
345: @}
346: return A;
347: @}
348: @end example
349:
350: @noindent
1.3 noro 351: \BJP
1.1 noro 352: 2 $B$DL\$NNc$G$O(B, $BD9$5(B @code{N+1} $B$N%Y%/%H%k(B (@code{A}$B$H$9$k(B) $B$,JV$5$l$k(B.
353: @code{A[I]} $B$OD9$5(B @code{I+1} $B$NG[Ns$G$"$j(B, $B$=$N$=$l$>$l$NMWAG$,(B
1.3 noro 354: \E
355: \BEG
356: In the second example, @code{c(N)} returns a vector, say @code{A}, of length
357: @code{N+1}. @code{A[I]} is a vector of length @code{I+1}, and
358: each element is again a vector which contains
359: \E
1.1 noro 360: @iftex
361: @tex
362: ${_I}C_J$
363: @end tex
364: @end iftex
365: @ifinfo
366: ICJ
367: @end ifinfo
1.3 noro 368: \JP $B$rMWAG$H$9$kG[Ns$G$"$k(B.
369: \EG as its elements.
1.1 noro 370:
371: @noindent
1.3 noro 372: \BJP
1.1 noro 373: $B0J2<$G$O(B, C $B$K$h$k%W%m%0%i%_%s%0$N7P83$,$J$$?M$N$?$a$K(B, @b{Asir} $B8@8l(B
374: $B$K$h$k%W%m%0%i%`$N=q$-J}$r2r@b$9$k(B.
1.3 noro 375: \E
376: \BEG
377: In the following, the manner of writing @b{Asir} programs is exhibited
378: for those who have no experience in writing C programs.
379: \E
1.1 noro 380:
1.3 noro 381: \BJP
1.1 noro 382: @node $BJQ?t$*$h$SITDj85(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
383: @subsection $BJQ?t$*$h$SITDj85(B
1.3 noro 384: \E
1.1 noro 385:
386: @noindent
1.3 noro 387: \BJP
1.1 noro 388: $B4{$K=R$Y$?DL$j(B, @b{Asir} $B$K$*$$$F$O%W%m%0%i%`JQ?t$HITDj85$rL@3N$K(B
389: $B6hJL$7$F$$$k(B.
1.3 noro 390: \E
391: \BEG
392: @node variables and indeterminates,,, Writing user defined functions
393: @subsection variables and indeterminates
394: \E
1.1 noro 395:
396: @table @b
1.3 noro 397: \BJP
1.1 noro 398: @item $BJQ?t(B
399: $BBgJ8;z$G;O$^$j(B, $B%"%k%U%!%Y%C%H(B, $B?t;z(B, @samp{_} $B$+$i$J$kJ8;zNs(B
1.3 noro 400: \E
401: \BEG
402: @item variables (program variables)
403: A program variable is a string that begins with a capital
404: alphabetical letter followed by any numbers of alphabetical letters,
405: digits and @samp{_}.
406: \E
1.1 noro 407:
1.3 noro 408: \BJP
1.1 noro 409: $BJQ?t$"$k$$$O%W%m%0%i%`JQ?t$H$O(B, @b{Asir} $B$N$5$^$6$^$J7?$NFbIt7A<0$r(B
410: $B3JG<$9$k$?$a$NH"$G$"$j(B, $B3JG<$5$l$?FbIt7A<0$,(B, $B$3$NJQ?t$NCM$G$"$k(B. $BJQ(B
411: $B?t$,<0$NMWAG$H$7$FI>2A$5$l$k;~$O(B, $B$=$3$K<}$a$i$l$?CM$KCV$-49$($i$l$k(B.
412: $B$9$J$o$A(B, $BFbIt7A<0$NCf$K$O%W%m%0%i%`JQ?t$O8=$l$J$$(B. $BJQ?t$OA4$F(B 0 $B$G(B
413: $B=i4|2=$5$l$F$$$k(B.
1.3 noro 414: \E
415: \BEG
416: A program variable is thought of a box (a carrier) which can contain
417: @b{Asir} objects of various types. The content is called the `value'
418: of that variable. When an expression in a program is to be evaluated,
419: the variable appearing in the expression is first replaced by its value
420: and then the expression is evaluated to some value and stored in
421: the memory. Thus, no program variable appears in objects in the
422: internal form.
423: All the program variables are initialized to the value 0.
424: \E
1.1 noro 425:
426: @example
427: [0] X^2+X+1;
428: 1
429: [1] X=2;
430: 2
431: [2] X^2+X+1;
432: 7
433: @end example
434:
1.3 noro 435: \BJP
1.1 noro 436: @item $BITDj85(B
437: $B>.J8;z$G;O$^$j(B, $B%"%k%U%!%Y%C%H(B, $B?t;z(B, @samp{_} $B$+$i$J$kJ8;zNs(B
438:
439: $BITDj85$H$O(B, $BB?9`<04D$r9=@.$9$k:]$KE:2C$5$l$kJQ?t$r$$$&(B. @b{Asir} $B$K(B
440: $B$*$$$F$O(B, $BITDj85$OCM$r$b$?$J$$D61[E*$J85$G$"$j(B, $BITDj85$X$NCM$NBeF~$O(B
441: $B5v$5$l$J$$(B.
1.3 noro 442: \E
443: \BEG
444: @item indeterminates
445: An indeterminate is a string that begins with a small alphabetical letter
446: followed by any numbers of alphabetical letters, digits and @samp{_}.
447:
448: An indeterminate is a transcendental element, so-called variable,
449: which is used to construct polynomial rings.
450: An indeterminate cannot have any value. No assignment is allowed to it.
451: \E
1.1 noro 452:
453: @example
454: [3] X=x;
455: x
456: [4] X^2+X+1;
457: x^2+x+1
458: @end example
459: @end table
460:
1.3 noro 461: \BJP
1.1 noro 462: @node $B0z?t(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
463: @subsection $B0z?t(B
1.3 noro 464: \E
465: \BEG
466: @node parameters and arguments,,, Writing user defined functions
467: @subsection parameters and arguments
468: \E
1.1 noro 469:
470: @example
471: def sum(N) @{
472: for ( I = 1, S = 0; I <= N; I++ )
473: S += I;
474: return S;
475: @}
476: @end example
477:
478: @noindent
1.3 noro 479: \BJP
1.1 noro 480: $B$3$l$O(B, 1 $B$+$i(B @code{N} $B$^$G$N<+A3?t$NOB$r5a$a$kH!?t(B @code{sum()} $B$N(B
481: $BDj5A$G$"$k(B. $B$3$NNc$K$*$1$k(B @code{sum(N)} $B$N(B @code{N} $B$,0z?t$G$"$k(B.
482: $B$3$NNc$O(B, 1 $B0z?tH!?t$NNc$G$"$k$,(B, $B0lHL$K0z?t$N8D?t$OG$0U$G$"$j(B,
483: $BI,MW$J$@$1$N8D?t$r(B @samp{,} $B$G6h@Z$C$F;XDj$9$k$3$H$,$G$-$k(B. $B0z?t$O(B
484: $BCM$,EO$5$l$k(B. $B$9$J$o$A(B, $B0z?t$r<u$1$H$C$?B&$,(B, $B$=$N0z?t$NCM$rJQ99$7$F(B
485: $B$b(B, $BEO$7$?B&$NJQ?t$OJQ2=$7$J$$(B. $B$?$@$7(B, $BNc30$,$"$k(B. $B$=$l$O(B, $B%Y%/%H%k(B,
486: $B9TNs$r0z?t$KEO$7$?>l9g$G$"$k(B. $B$3$N>l9g$b(B, $BEO$5$l$?JQ?t$=$N$b$N$r=q$-(B
487: $BBX$($k$3$H$O(B, $B$=$NH!?t$K6I=jE*$JA`:n$G$"$k$,(B, $BMWAG$r=q$-49$($?>l9g(B,
488: $B$=$l$O(B, $B8F$S=P$7B&$N%Y%/%H%k(B, $B9TNs$NMWAG$r=q$-49$($k$3$H$K$J$k(B.
1.3 noro 489: \E
490: \BEG
491: This is an example definition of a function that sums up integers
492: from 1 to @code{N}. The @code{N} in @code{sum(N)} is called the
493: (formal) parameter of @code{sum(N)}.
494: The example shows a function of the single argument.
495: In general, any number of parameters can be specified by separating
496: by commas (@samp{,}).
497: A (formal) parameter accepts a value given as an argument (or an actual
498: parameter) at a function call of the function.
499: Since the value of the argument is given to the formal parameter,
500: any modification to the parameter does not usually affect the argument
501: (or actual parameter). However, there are a few exceptions: vector
502: arguments and matrix arguments.
503:
504: Let @code{A} be a program variable and assigned to a vector value
505: @code{[ a, b ]}.
506: If A is given as an actual parameter to a formal parameter, say @code{V},
507: of a function, then an assignment in the function to the vector element
508: designator @code{V[1]}, say @code{V[1]=c;}, causes modification of the
509: actual parameter @code{A} resulting @code{A} to have an altered value
510: @code{[ a c ]}. Thus, if a vector is given to a formal parameter of
511: a function, then its element (and subsequently the vector itself) in
512: the calling side is modified through modification of the formal parameter
513: by a vector element designator in the called function.
514: The same applies to a matrix argument.
515: Note that, even in such case where a vector (or a matrix) is given to
516: a formal parameter, the assignment to the whole parameter itself has
517: only a local effect within the function.
518: \E
1.1 noro 519:
520: @example
521: def clear_vector(M) @{
522: /* M is expected to be a vector */
523: L = size(M)[0];
524: for ( I = 0; I < L; I++ )
525: M[I] = 0;
526: @}
527: @end example
528:
529: @noindent
1.3 noro 530: \BJP
1.1 noro 531: $B$3$NH!?t$O(B, $B0z?t$N%Y%/%H%k$r(B 0 $B%Y%/%H%k$K=i4|2=$9$k$?$a$NH!?t$G$"$k(B.
532: $B$^$?(B, $B%Y%/%H%k$r0z?t$KEO$9$3$H$K$h$j(B, $BJ#?t$N7k2L$r0z?t$N%Y%/%H%k$K(B
533: $B<}G<$7$FJV$9$3$H$,$G$-$k(B. $B<B:]$K$O(B, $B$3$N$h$&$J>l9g$K$O(B, $B7k2L$r%j%9%H(B
534: $B$K$7$FJV$9$3$H$b$G$-$k(B. $B>u67$K1~$8$F;H$$$o$1$9$k$3$H$,K>$^$7$$(B.
1.3 noro 535: \E
536: \BEG
537: This function will clear off the vector given as its argument to the
538: formal parameter @code{M} and return a 0 vector.
539:
540: Passing a vector as an argument to a function enables returning
541: multiple results by packing each result in a vector element.
542: Another alternative to return multiple results is to use a list.
543: Which to use depends on cases.
544: \E
1.1 noro 545:
1.3 noro 546: \BJP
1.1 noro 547: @node $B%3%a%s%H(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
548: @subsection $B%3%a%s%H(B
1.3 noro 549: \E
550: \BEG
551: @node comments,,, Writing user defined functions
552: @subsection comments
553: \E
1.1 noro 554:
555: @noindent
1.3 noro 556: \JP C $B$HF1MM(B @samp{/*} $B$H(B @samp{*/} $B$G0O$^$l$?ItJ,$O%3%a%s%H$H$7$F07$o$l$k(B.
557: \BEG
558: The text enclosed by @samp{/*} and @samp{*/} (containing @samp{/*} and
559: @samp{*/}) is treated as a comment and has no effect to the program
560: execution as in C programs.
561: \E
1.1 noro 562:
563: @example
564: /*
565: * This is a comment.
566: */
567:
568: def afo(X) @{
569: @end example
570:
571: @noindent
1.3 noro 572: \BJP
1.1 noro 573: $B%3%a%s%H$OJ#?t9T$KEO$C$F$b9=$o$J$$$,(B, $BF~$l;R$K$9$k$3$H$O$G$-$J$$(B.
574: @samp{/*} $B$,$$$/$D$"$C$F$b:G=i$N$b$N$N$_$,M-8z$H$J$j(B, $B:G=i$K8=$l$?(B
575: @samp{*/} $B$G%3%a%s%H$O=*N;$7$?$H8+$J$5$l$k(B. $B%W%m%0%i%`$J$I$G(B, $B%3%a%s%H(B
576: $B$r4^$`2DG=@-$,$"$kItJ,$r%3%a%s%H%"%&%H$7$?>l9g$K$O(B, @code{#if 0},
577: @code{#endif}$B$r;H$($P$h$$(B. (@xref{$B%W%j%W%m%;%C%5(B})
1.3 noro 578: \E
579: \BEG
580: A comment can span to several lines, but it cannot be nested.
581: Only the first @samp{/*} is effective no matter how many @samp{/*}'s
582: in the subsequent text exist, and the comment terminates at the first
583: @samp{*/}.
584:
585: In order to comment out a program part that may contain comments in it,
586: use the pair, @code{#if 0} and @code{#endif}. (@xref{preprocessor})
587: \E
1.1 noro 588:
589: @example
590: #if 0
591: def bfo(X) @{
592: /* empty */
593: @}
594: #endif
595: @end example
596:
1.3 noro 597: \BJP
1.1 noro 598: @node $BJ8(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
599: @subsection $BJ8(B
1.3 noro 600: \E
601: \BEG
602: @node statements,,, Writing user defined functions
603: @subsection statements
604: \E
1.1 noro 605:
606: @noindent
1.3 noro 607: \BJP
1.1 noro 608: @b{Asir} $B$N%f!<%6H!?t$O(B,
609:
610: @example
611: def $BL>A0(B($B0z?t(B,$B0z?t(B,...,$B0z?t(B) @{
612: $BJ8(B
613: $BJ8(B
614: ...
615: $BJ8(B
616: @}
1.3 noro 617: \E
618: \BEG
619: An user function of @b{Asir} is defined in the following form.
620:
621: @example
622: def name(parameter, parameter,...,parameter) @{
623: statement
624: statement
625: ...
626: statement
627: @}
628: \E
1.1 noro 629: @end example
630:
631: @noindent
1.3 noro 632: \BJP
1.1 noro 633: $B$H$$$&7A$GDj5A$5$l$k(B. $B$3$N$h$&$K(B, $BJ8$OH!?t$N4pK\E*9=@.MWAG$G$"$j(B, $B%W%m(B
634: $B%0%i%`$r=q$/$?$a$K$O(B, $BJ8$,$I$N$h$&$J$b$N$G$"$k$+CN$i$J$1$l$P$J$i$J$$(B.
635: $B:G$bC1=c$JJ8$H$7$F(B, $BC1J8$,$"$k(B. $B$3$l$O(B,
1.3 noro 636: \E
637: \BEG
638: As you can see, the statement is a fundamental element of the
639: function.
640: Therefore, in order to write a program, you have to learn what
641: the statement is. The simplest statement is the simple statement.
642: One example is an expression with a terminator (@samp{;} or @samp{$}.)
643: \E
1.1 noro 644:
645: @example
646: S = sum(N);
647: @end example
648:
649: @noindent
1.3 noro 650: \BJP
1.1 noro 651: $B$N$h$&$K(B, $B<0$K=*C<5-9f(B (@samp{;} $B$^$?$O(B @samp{$}) $B$r$D$1$?$b$N$G$"$k(B.
652: $B$3$NC1J85Z$SN`;w$N(B @code{return} $BJ8(B, @code{break} $BJ8$J$I$,J8$N:G>.9=@.(B
653: $BC10L$H$J$k(B. @code{if} $BJ8$d(B @code{for} $BJ8$NDj5A(B (@xref{$BJ8K!$N>\:Y(B}) $B$r8+$l(B
654: $B$P$o$+$kDL$j(B, $B$=$l$i$NK\BN$O(B, $BC1$J$k0l$D$NJ8$H$7$FDj5A$5$l$F$$$k(B. $BDL>o(B
655: $B$O(B, $BK\BN$K$OJ#?t$NJ8$,=q$1$k$3$H$,I,MW$H$J$k(B. $B$3$N$h$&$J>l9g(B,
656: @samp{@{} $B$H(B @samp{@}} $B$GJ8$NJB$S$r3g$C$F(B, $B0l$D$NJ8$H$7$F07$&$3$H$,$G(B
657: $B$-$k(B. $B$3$l$rJ#J8$H8F$V(B.
1.3 noro 658: \E
659: \BEG
660: A `@code{return} statement' and `@code{break} statement' are also
661: primitives to construct `statements.'
662: As you can see the syntactic definition of `@code{if} statement' and
663: `@code{for} statement', each of their bodies consists of a single
664: `statement.' Usually, you need several statements in such a body.
665: To solve this contradictory requirement, you may use the `compound
666: statement.' A `compound statement' is a sequence of `statement's
667: enclosed by a left brace @samp{@{} and a right brace @samp{@}}.
668: Thus, you can use multiple statement as if it were a single statement.
669: \E
1.1 noro 670:
671: @example
672: if ( I == 0 ) @{
673: J = 1;
674: K = 2;
675: L = 3;
676: @}
677: @end example
678:
679: @noindent
1.3 noro 680: \BJP
1.1 noro 681: @samp{@}} $B$N8e$m$K$O=*C<5-9f$OI,MW$J$$(B. $B$J$<$J$i(B, @samp{@{} $BJ8JB$S(B
682: @samp{@}}$B$,4{$KJ8$H$J$C$F$$$F(B, @code{if} $BJ8$NMW@A$rK~$?$7$F$$$k$+$i$G(B
683: $B$"$k(B.
1.3 noro 684: \E
685: \BEG
686: No terminator symbol is necessary after @samp{@}},
687: because @samp{@{} statement sequence @samp{@}} already forms a statement,
688: and it satisfies the syntactical requirement of the
689: `@code{if} statement.'
690: \E
1.1 noro 691:
1.3 noro 692: \BJP
1.1 noro 693: @node return $BJ8(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
694: @subsection @code{return} $BJ8(B
1.3 noro 695: \E
696: \BEG
697: @node return statement,,, Writing user defined functions
698: @subsection @code{return} statement
699: \E
1.1 noro 700:
701: @noindent
1.3 noro 702: \JP @code{return} $BJ8$O(B,
703: \EG There are two forms of @code{return} statement.
1.1 noro 704:
705: @example
1.3 noro 706: \JP return $B<0(B;
707: \EG return expression;
1.1 noro 708:
709: return;
710: @end example
711:
712: @noindent
1.3 noro 713: \BJP
1.1 noro 714: $B$N(B 2 $B$D$N7A<0$,$"$k(B. $B$$$:$l$bH!?t$+$iH4$1$k$?$a$NJ8$G$"$k(B. $BA0<T$O(B
715: $BH!?t$NCM$H$7$F(B $B<0(B $B$rJV$9(B. $B8e<T$G$O(B, $BH!?t$NCM$H$7$F2?$,JV$5$l$k$+(B
716: $B$O$o$+$i$J$$(B.
1.3 noro 717: \E
718: \BEG
719: Both forms are used for exiting from a function.
720: The former returns the value of the expression as a function value.
721: The function value of the latter is not defined.
722: \E
1.1 noro 723:
1.3 noro 724: \BJP
1.1 noro 725: @node if $BJ8(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
726: @subsection @code{if} $BJ8(B
1.3 noro 727: \E
728: \BEG
729: @node if statement,,, Writing user defined functions
730: @subsection @code{if} statement
731: \E
1.1 noro 732:
733: @noindent
1.3 noro 734: \JP @code{if} $BJ8$K$O(B
735: \EG There are two forms of @code{if} statement.
1.1 noro 736:
737: @example
1.3 noro 738: \BJP
1.1 noro 739: if ( $B<0(B ) if ( $B<0(B )
740: $BJ8(B $B5Z$S(B $BJ8(B
741: else
742: $BJ8(B
1.3 noro 743: \E
744: \BEG
745: if ( expression ) if ( expression )
746: statement and statement
747: else
748: statement
749: \E
1.1 noro 750: @end example
751:
752: @noindent
1.3 noro 753: \BJP
1.1 noro 754: $B$N(B 2 $B<oN`$,$"$k(B. $B$3$l$i$NF0:n$OL@$i$+$G$"$k$,(B, $BJ8$N0LCV$K(B @code{if} $BJ8(B
755: $B$,Mh$?>l9g$KCm0U$rMW$9$k(B. $B<!$NNc$r9M$($F$_$h$&(B.
1.3 noro 756: \E
757: \BEG
758: The interpretation of these forms are obvious. However, be careful
759: when another @code{if} statement comes at the place for `statement'.
760: Let us examine the following example.
761: \E
1.1 noro 762:
763: @example
1.3 noro 764: \BJP
1.1 noro 765: if ( $B<0(B )
766: if ( $B<0(B ) $BJ8(B
767: else
768: $BJ8(B
1.3 noro 769: \E
770: \BEG
771: if ( expression1 )
772: if ( expression2 ) statement1
773: else
774: statement2
775: \E
1.1 noro 776: @end example
777:
778: @noindent
1.3 noro 779: \BJP
1.1 noro 780: $B$3$N>l9g(B, $B;z2<$2$+$i$O(B, @code{else} $B0J2<$O(B, $B:G=i$N(B @code{if} $B$KBP1~$9$k(B
781: $B$h$&$K8+$($k$,(B, $B%Q!<%6$O(B, $B<+F0E*$K(B 2 $BHVL\$N(B @code{if} $B$KBP1~$9$k$HH=CG$9$k(B.
782: $B$9$J$o$A(B, 2 $B<oN`$N(B @code{if} $BJ8$r5v$7$?$?$a$K(B, $BJ8K!$K[#Kf@-$,8=$l(B, $B$=$l$r(B
783: $B2r>C$9$k$?$a$K(B, @code{else} $B0J2<$O(B, $B:G$b6a$$(B @code{if} $B$KBP1~$9$k$H(B
784: $B$$$&5,B'$,E,MQ$5$l$k$N$G$"$k(B. $B=>$C$F(B, $B$3$NNc$O(B,
1.3 noro 785: \E
786: \BEG
787: One might guess @code{statement2} after @code{else} corresponds with the
788: first @code{if ( expression1 )} by its appearance of indentation.
789: But, as a matter of fact, the @code{Asir} parser decides that it
790: correspond with the second @code{if ( expression2 )}.
791: Ambiguity due to such two kinds of forms of @code{if} statement is
792: thus solved by introducing a rule that a statement preceded by an
793: @code{else} matches to the nearest preceding @code{if}.
794:
795: Therefore, rearrangement of the above example for improving readability
796: according to the actual interpretation gives the following.
797: \E
1.1 noro 798:
799: @example
1.3 noro 800: \BJP
1.1 noro 801: if ( $B<0(B ) @{
802: if ( $B<0(B ) $BJ8(B else $BJ8(B
803: @}
1.3 noro 804: \E
805: \BEG
806: if ( expression1 ) @{
807: if ( expression2 ) statement1 else statement2
808: @}
809: \E
1.1 noro 810: @end example
811:
812: @noindent
1.3 noro 813: \JP $B$H$$$&0UL#$H$J$k(B. $B;z2<$2$KBP1~$5$;$k$?$a$K$O(B,
814: \BEG
815: On the other hand, in order to reflect the indentation, it must be
816: written as the following.
817: \E
1.1 noro 818:
819: @example
1.3 noro 820: \BJP
1.1 noro 821: if ( $B<0(B ) @{
822: if ( $B<0(B ) $BJ8(B
823: @} else
824: $BJ8(B
1.3 noro 825: \E
826: \BEG
827: if ( expression1 ) @{
828: if ( expression2 ) statement1
829: @} else
830: statement2
831: \E
1.1 noro 832: @end example
833:
834: @noindent
1.3 noro 835: \JP $B$H$7$J$1$l$P$J$i$J$$(B.
1.1 noro 836:
1.3 noro 837: \BJP
1.1 noro 838: @node $B%k!<%W(B break return continue,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
839: @subsection $B%k!<%W(B, @code{break}, @code{return}, @code{continue}
1.3 noro 840: \E
841: \BEG
842: @node loop break return continue,,, Writing user defined functions
843: @subsection @code{loop}, @code{break}, @code{return}, @code{continue}
844: \E
1.1 noro 845:
846: @noindent
1.3 noro 847: \BJP
1.1 noro 848: $B%k!<%W$r9=@.$9$kJ8$O(B, @code{while} $BJ8(B, @code{for} $BJ8(B, @code{do} $BJ8(B
849: $B$N(B 3 $B<oN`$,$"$k(B.
1.3 noro 850: \E
851: \BEG
852: There are three kinds of statements for loops (repetitions):
853: the @code{while} statement, the @code{for} statement, and the
854: @code{do} statement.
855: \E
1.1 noro 856:
857: @itemize @bullet
858: @item
1.3 noro 859: \JP @code{while} $BJ8(B
860: \EG @code{while} statement
1.4 ! noro 861: @*
1.3 noro 862: \JP $B7A<0$O(B,
863: \EG It has the following form.
1.1 noro 864:
865: @example
1.3 noro 866: \JP while ( $B<0(B ) $BJ8(B
867: \EG while ( expression ) statement
1.1 noro 868: @end example
869:
870: @noindent
1.3 noro 871: \BJP
1.1 noro 872: $B$G(B, $B$3$l$O(B, $B<0(B $B$rI>2A$7$F(B, $B$=$NCM$,(B 0 $B$G$J$$8B$j(B $BJ8(B $B$r<B9T$9$k$H$$$&(B
873: $B0UL#$H$J$k(B. $B$?$H$($P(B $B<0(B $B$,(B 1 $B$J$i$P(B, $BC1=c$JL58B%k!<%W$H$J$k(B.
1.3 noro 874: \E
875: \BEG
876: This statement specifies that @code{statement} is repeatedly evaluated
877: as far as the @code{expression} evaluates to a non-zero value.
878: If the expression 1 is given to the @code{expression}, it forms an
879: infinite loop.
880: \E
1.1 noro 881:
882: @item
1.3 noro 883: \JP @code{for} $BJ8(B
884: \EG @code{for} statement
1.4 ! noro 885: @*
1.3 noro 886: \JP $B7A<0$O(B,
887: \EG It has the following form.
1.1 noro 888:
889: @example
1.3 noro 890: \JP for ( $B<0JB$S(B-1; $B<0(B; $B<0JB$S(B-2 ) $BJ8(B
891: \EG for ( expression list-1; expression; expression list-2 ) statement
1.1 noro 892: @end example
893:
1.3 noro 894: \JP $B$G(B, $B$3$l$O(B
895: \EG This is equivalent to the program
1.1 noro 896:
897: @example
1.3 noro 898: \BJP
1.1 noro 899: $B<0JB$S(B-1 ($B$rC1J8JB$S$K$7$?$b$N(B)
900: while ( $B<0(B ) @{
901: $BJ8(B
902: $B<0JB$S(B-2 ($B$rC1J8JB$S$K$7$?$b$N(B)
903: @}
1.3 noro 904: \E
905: \BEG
906: expression list-1 (transformed into a sequence of simple statement)
907: while ( expression ) @{
908: statement
909: expression list-2 (transformed into a sequence of simple statement)
910: @}
911: \E
1.1 noro 912: @end example
913:
1.3 noro 914: \JP $B$HEy2A$G$"$k(B.
1.1 noro 915:
916: @item
1.3 noro 917: \JP @code{do} $BJ8(B
918: \EG @code{do} statement
1.4 ! noro 919: @*
1.1 noro 920: @example
1.3 noro 921: \BJP
1.1 noro 922: do @{
923: $BJ8(B
924: @} while ( $B<0(B )
1.3 noro 925: \E
926: \BEG
927: do @{
928: statement
929: @} while ( expression )
930: \E
1.1 noro 931: @end example
932:
1.3 noro 933: \BJP
1.1 noro 934: $B$O(B, $B@h$K(B $BJ8$r<B9T$7$F$+$i>r7o<0$K$h$kH=Dj$r9T$&=j$,(B @code{while} $BJ8(B
935: $B$H0[$J$C$F$$$k(B.
1.3 noro 936: \E
937: \BEG
938: This statement differs from @code{while} statement by the location of
939: the termination condition: This statement first execute the
940: @code{statement} and then check the condition, whereas @code{while}
941: statement does it in the reverse order.
942: \E
1.1 noro 943: @end itemize
944:
945: @noindent
1.3 noro 946: \BJP
1.1 noro 947: $B%k!<%W$rH4$1=P$9<jCJ$H$7$F(B,
948: @code{break} $BJ85Z$S(B @code{return} $BJ8$,$"$k(B. $B$^$?(B, $B%k!<%W$N@)8f$r(B
949: $B$"$k0LCV$K0\$9<jCJ$H$7$F(B @code{continue} $BJ8$,$"$k(B.
1.3 noro 950: \E
951: \BEG
952: As means for exiting from loops, there are @code{break} statement and
953: @code{return} statement. The @code{continue} statement allows to move
954: the control to a certain point of the loop.
955: \E
1.1 noro 956: @itemize @bullet
957:
958: @item
959: @code{break}
1.4 ! noro 960: @*
1.3 noro 961: \JP @code{break} $BJ8$O(B, $B$=$l$r0O$`%k!<%W$r0l$D$@$1H4$1$k(B.
962: \EG The @code{break} statement is used to exit the inner most loop.
1.1 noro 963: @item
964: @code{return}
1.4 ! noro 965: @*
1.3 noro 966: \BJP
1.1 noro 967: @code{return} $BJ8$O(B, $B0lHL$KH!?t$+$iH4$1$k$?$a$NJ8$G$"$j(B,
968: $B%k!<%W$NCf$+$i$G$bM-8z$G$"$k(B.
1.3 noro 969: \E
970: \BEG
971: The @code{return} statement is usually used to exit from a function call
972: and it is also effective in a loop.
973: \E
1.1 noro 974:
975: @item
976: @code{continue}
1.4 ! noro 977: @*
1.3 noro 978: \BJP
1.1 noro 979: @code{continue} $BJ8$O(B, $B%k!<%W$NK\BN$NJ8$NKvC<$K@)8f$r0\$9(B.
980: $BNc$($P(B @code{for} $BJ8$G$O(B, $B:G8e$N<0JB$S$N<B9T$r9T$$(B, @code{while}
981: $BJ8$G$O>r7o<0$NH=Dj$K0\$k(B.
1.3 noro 982: \E
983: \BEG
984: The @code{continue} statement is used to move the control to the end
985: point of the loop body.
986: For example, the last expression list will be evaluated in a @code{for}
987: statement, and the termination condition will be evaluated in a
988: @code{while} statement.
989: \E
1.1 noro 990: @end itemize
991:
1.3 noro 992: \BJP
1.1 noro 993: @node $B$5$^$6$^$J<0(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
994: @subsection $B$5$^$6$^$J<0(B
1.3 noro 995: \E
996: \BEG
997: @node various expressions,,, Writing user defined functions
998: @subsection various expressions
999: \E
1.1 noro 1000:
1001: @noindent
1.3 noro 1002: \JP $B<g$J<0$N9=@.MWAG$H$7$F$O(B, $B<!$N$h$&$J$b$N$,$"$k(B.
1003: \EG Major elements to construct expressions are the following:
1.1 noro 1004:
1005: @itemize @bullet
1006: @item
1.3 noro 1007: \JP $B2C8:>h=|(B, $BQQ(B
1008: \EG addition, subtraction, multiplication, division, exponentiation
1.4 ! noro 1009: @*
1.3 noro 1010: \BJP
1.1 noro 1011: $BQQ$O(B, @samp{^} $B$K$h$jI=$9(B. $B=|;;(B @samp{/} $B$O(B, $BBN$H$7$F$N1i;;$KMQ$$$k(B.
1012: $BNc$($P(B, @code{2/3} $B$OM-M}?t$N(B @code{2/3} $B$rI=$9(B.
1013: $B@0?t=|;;(B, $BB?9`<0=|;;(B ($B>jM>$r4^$`1i;;(B) $B$K$OJLESAH$_9~$_H!?t$,MQ0U$5$l$F$$$k(B.
1.3 noro 1014: \E
1015: \BEG
1016: The exponentiation is denoted by @samp{^}. (This differs from C language.)
1017: Division denoted by @samp{/} is used to operate in a field, for example,
1018: @code{2/3} results in a rational number @code{2/3}.
1019: For integer division and polynomial division, both including remainder
1020: operation, built-in functions are provided.
1021: \E
1.1 noro 1022:
1023: @example
1024: x+1 A^2*B*afo X/3
1025: @end example
1026:
1027: @item
1.3 noro 1028: \JP $B%$%s%G%C%/%9$D$-$NJQ?t(B
1029: \EG programming variables with indices
1.4 ! noro 1030: @*
1.3 noro 1031: \BJP
1.1 noro 1032: $B%Y%/%H%k(B, $B9TNs(B, $B%j%9%H$NMWAG$O%$%s%G%C%/%9$rMQ$$$k$3$H$K$h$j<h$j=P$;$k(B.
1033: $B%$%s%G%C%/%9$O(B 0 $B$+$i;O$^$k$3$H$KCm0U$9$k(B. $B<h$j=P$7$?MWAG$,%Y%/%H%k(B,
1034: $B9TNs(B, $B%j%9%H$J$i(B, $B$5$i$K%$%s%G%C%/%9$r$D$1$k$3$H$bM-8z$G$"$k(B.
1.3 noro 1035: \E
1036: \BEG
1037: An element of a vector, a matrix or a list can be referred to by
1038: indexing.
1039: Note that the indices begin with number 0. When the referred element
1040: is again a vector, a matrix or a list, repeated indexing is also
1041: effective.
1042: \E
1.1 noro 1043:
1044: @example
1045: V[0] M[1][2]
1046: @end example
1047:
1048: @item
1.3 noro 1049: \JP $BHf3S1i;;(B
1050: \EG comparison operation
1.4 ! noro 1051: @*
1.3 noro 1052: \BJP
1.1 noro 1053: $BEy$7$$(B (@samp{==}), $BEy$7$/$J$$(B (@samp{!=}), $BBg>.(B (@samp{>}, @samp{<},
1054: @samp{>=}, @samp{<=}) $B$N(B 2 $B9`1i;;$,$"$k(B. $B??$J$i$PM-M}?t$N(B 1, $B56$J$i$P(B
1055: 0 $B$rCM$K;}$D(B.
1.3 noro 1056: \E
1057: \BEG
1058: There are comparison operations
1059: @samp{==} for equivalence, @samp{!=} for non-equivalence,
1060: @samp{>}, @samp{<},@samp{>=}, and @samp{<=} for larger or smaller.
1061: The results of these operations are either value 1 for the truth,
1062: or 0 for the false.
1063: \E
1.1 noro 1064:
1065: @item
1.3 noro 1066: \JP $BO@M}<0(B
1067: \EG logical expression
1.4 ! noro 1068: @*
1.3 noro 1069: \BJP
1.1 noro 1070: $BO@M}@Q(B (@samp{&&}), $BO@M}OB(B (@samp{||}) $B$N(B 2 $B9`1i;;$H(B, $BH]Dj(B (@samp{!})
1071: $B$,MQ0U$5$l$F$$$k(B. $BCM$O$d$O$j(B 1, 0 $B$G$"$k(B.
1.3 noro 1072: \E
1073: \BEG
1074: There are two binary logical operations
1075: @samp{&&} for logical @samp{conjunction}(and),
1076: @samp{||} for logical @samp{disjunction}(or),
1077: and one unary logical operation @samp{!} for logical @samp{negation}(not).
1078: The results of these operations are either value 1 for the truth,
1079: and 0 for the false.
1080: \E
1.1 noro 1081:
1082: @item
1.3 noro 1083: \JP $BBeF~(B
1084: \EG assignment
1.4 ! noro 1085: @*
1.3 noro 1086: \BJP
1.1 noro 1087: $BDL>o$NBeF~$O(B @samp{=} $B$G9T$&(B. $B$3$N$[$+(B, $B;;=Q1i;;;R$HAH$_9g$o$;$F(B
1088: $BFC<l$JBeF~$r9T$&$3$H$b$G$-$k(B.
1.3 noro 1089: \E
1090: \BEG
1091: Value assignment of a program variable is usually done by @samp{=}.
1092: There are special assignments combined with arithmetic operations.
1093: \E
1.1 noro 1094: (@samp{+=}, @samp{-=}, @samp{*=}, @samp{/=}, @samp{^=})
1095:
1096: @example
1.3 noro 1097: \JP A = 2 A *= 3 ($B$3$l$O(B A = A*3 $B$HF1$8(B; $B$=$NB>$N1i;;;R$bF1MM(B)
1098: \EG A = 2 A *= 3 (the same as A = A*3; The other combination are alike.)
1.1 noro 1099: @end example
1100: @item
1.3 noro 1101: \JP $BH!?t8F$S=P$7(B
1102: \EG function call
1.4 ! noro 1103: @*
1.3 noro 1104: \JP $BH!?t8F$S=P$7$b<0$N0l<o$G$"$k(B.
1105: \EG A function call is also an expression.
1.1 noro 1106: @item
1107: @samp{++}, @samp{--}
1.4 ! noro 1108: @*
1.3 noro 1109: \JP $B$3$l$i$O(B, $BJQ?t$NA08e$K$D$$$F(B, $B$=$l$>$l<!$N$h$&$JA`:n(B, $BCM$rI=$9(B.
1110: \BEG
1111: These operators are attached to or before a program variable,
1112: and denote special operations and values.
1113: \E
1.1 noro 1114: @example
1.3 noro 1115: \BJP
1.1 noro 1116: A++ $BCM$O85$N(B A $B$NCM(B, A = A+1
1117: A-- $BCM$O85$N(B A $B$NCM(B, A = A-1
1118: ++A A = A+1, $BCM$OJQ2=8e$NCM(B
1119: --A A = A-1, $BCM$OJQ2=8e$NCM(B
1.3 noro 1120: \E
1121: \BEG
1122: A++ the expression value is the previous value of A, and A = A+1
1123: A-- the expression value is the previous value of A, and A = A-1
1124: ++A A = A+1, and the expression value is the value after increment of A
1125: --A A = A-1, and the expression value is the value after decrement of A
1126: \E
1.1 noro 1127: @end example
1128:
1129: @end itemize
1130:
1.3 noro 1131: \BJP
1.1 noro 1132: @node $B%W%j%W%m%;%C%5(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
1133: @subsection $B%W%j%W%m%;%C%5(B
1.3 noro 1134: \E
1135: \BEG
1136: @node preprocessor,,, Writing user defined functions
1137: @subsection preprocessor
1138: \E
1.1 noro 1139:
1140: @noindent
1.3 noro 1141: \BJP
1.1 noro 1142: @b{Asir} $B$N%f!<%68@8l$O(B C $B8@8l$rLO$7$?$b$N$G$"$k(B. C $B$NFCD'$H$7$F(B,
1143: $B%W%j%W%m%;%C%5(B @code{cpp} $B$K$h$k%^%/%mE83+(B, $B%U%!%$%k$N%$%s%/%k!<%I(B
1144: $B$,$"$k$,(B, @b{Asir} $B$K$*$$$F$b%f!<%68@8l%U%!%$%k$NFI$_9~$_$N:](B
1145: @code{cpp} $B$rDL$7$F$+$iFI$_9~$`$3$H$H$7$?(B. $B$3$l$K$h$j%f!<%68@8l(B
1146: $B%U%!%$%kCf$G(B @code{#include}, @code{#define}, @code{#if} $B$J$I$,;H$($k(B.
1.3 noro 1147: \E
1148: \BEG
1149: he @b{Asir} user language imitates C language. A typical features of
1150: C language include macro expansion and file inclusion by the
1151: preprocessor @code{cpp}. Also, @b{Asir} read in user program files
1152: through @code{cpp}. This enables @b{Asir} user to use
1153: @code{#include}, @code{#define}, @code{#if} etc. in his programs.
1154: \E
1.1 noro 1155:
1156: @itemize @bullet
1157: @item
1158: @code{#include}
1.4 ! noro 1159: @*
1.3 noro 1160: \BJP
1.1 noro 1161: @code{cpp} $B$KFC$K0z?t$rEO$5$J$$$?$a(B, $B%$%s%/%k!<%I%U%!%$%k$O(B,
1162: @code{#include} $B$,=q$+$l$F$$$k%U%!%$%k$HF1$8%G%#%l%/%H%j$G%5!<%A$5$l$k(B.
1.3 noro 1163: \E
1164: \BEG
1165: Include files are searched within the same directory as the file
1166: containing @code{#include} so that no arguments are passed to @code{cpp}.
1167: \E
1.1 noro 1168:
1169: @item
1170: @code{#define}
1.4 ! noro 1171: @*
1.3 noro 1172: \JP $B$3$l$O(B, C $B$K$*$1$k$N$HA4$/F1MM$KMQ$$$k$3$H$,$G$-$k(B.
1173: \EG This can be used just as in C language.
1.1 noro 1174:
1175: @item
1176: @code{#if}
1.4 ! noro 1177: @*
1.3 noro 1178: \BJP
1.1 noro 1179: @code{/*}, @code{*/} $B$K$h$k%3%a%s%H$OF~$l;R$K$G$-$J$$$N$G(B, $B%W%m%0%i%`(B
1180: $B$NBg$-$JItJ,$r%3%a%s%H%"%&%H$9$k:]$K(B, @code{#if 0}, @code{#endif}
1181: $B$r;H$&$HJXMx$G$"$k(B.
1.3 noro 1182: \E
1183: \BEG
1184: This is conveniently used to comment out a large part of a user program
1185: that may contain comments by @code{/*} and @code{*/},
1186: because such comments cannot be nested.
1187: \E
1.1 noro 1188: @end itemize
1189:
1190: @noindent
1.3 noro 1191: \JP $B<!$NNc$O(B, @samp{defs.h} $B$K$"$k%^%/%mDj5A$G$"$k(B.
1192: \EG the following are the macro definitions in @samp{defs.h}.
1.1 noro 1193:
1194: @example
1195: #define ZERO 0
1196: #define NUM 1
1197: #define POLY 2
1198: #define RAT 3
1199: #define LIST 4
1200: #define VECT 5
1201: #define MAT 6
1202: #define STR 7
1203: #define N_Q 0
1204: #define N_R 1
1205: #define N_A 2
1206: #define N_B 3
1207: #define N_C 4
1208: #define V_IND 0
1209: #define V_UC 1
1210: #define V_PF 2
1211: #define V_SR 3
1212: #define isnum(a) (type(a)==NUM)
1213: #define ispoly(a) (type(a)==POLY)
1214: #define israt(a) (type(a)==RAT)
1215: #define islist(a) (type(a)==LIST)
1216: #define isvect(a) (type(a)==VECT)
1217: #define ismat(a) (type(a)==MAT)
1218: #define isstr(a) (type(a)==STR)
1219: #define FIRST(L) (car(L))
1220: #define SECOND(L) (car(cdr(L)))
1221: #define THIRD(L) (car(cdr(cdr(L))))
1222: #define FOURTH(L) (car(cdr(cdr(cdr(L)))))
1223: #define DEG(a) deg(a,var(a))
1224: #define LCOEF(a) coef(a,deg(a,var(a)))
1225: #define LTERM(a) coef(a,deg(a,var(a)))*var(a)^deg(a,var(a))
1226: #define TT(a) car(car(a))
1227: #define TS(a) car(cdr(car(a)))
1228: #define MAX(a,b) ((a)>(b)?(a):(b))
1229: @end example
1.2 noro 1230:
1231:
1.3 noro 1232: \BJP
1.2 noro 1233: @node $B%*%W%7%g%s;XDj(B,,, $B%f!<%6Dj5AH!?t$N=q$-J}(B
1234: @subsection $B%*%W%7%g%s;XDj(B
1.3 noro 1235: \E
1236: \BEG
1237: @node option,,, Writing user defined functions
1238: @subsection option
1239: \E
1.2 noro 1240:
1.3 noro 1241: \BJP
1.2 noro 1242: $B%f!<%6Dj5A4X?t$,(B @var{N} $BJQ?t$G@k8@$5$l$?>l9g(B, $B$=$N4X?t$O(B, @var{N}
1243: $BJQ?t$G$N8F$S=P$7$N$_$,5v$5$l$k(B.
1.3 noro 1244: \E
1245: \BEG
1246: If a user defined function is declared with @var{N} arguments,
1247: then the function is callable with @var{N} arguments only.
1248: \E
1.2 noro 1249:
1250: @example
1251: [0] def factor(A) @{ return fctr(A); @}
1252: [1] factor(x^5-1,3);
1253: evalf : argument mismatch in factor()
1254: return to toplevel
1255: @end example
1256:
1.3 noro 1257: \BJP
1.2 noro 1258: $BITDj8D0z?t$N4X?t$r%f!<%68@8l$G5-=R$7$?$$>l9g(B, $B%j%9%H(B, $BG[Ns$rMQ$$$k$3$H$G(B
1259: $B2DG=$H$J$k$,(B, $B<!$N$h$&$J$h$jJ,$+$j$d$9$$J}K!$b2DG=$G$"$k(B.
1.3 noro 1260: \E
1261: \BEG
1262: A function with indefinite number of arguments can be realized by
1263: using a list or an array as its argument. Another method is available
1264: as follows:
1265: \E
1.2 noro 1266:
1267: @example
1268: % cat factor
1269: def factor(F)
1270: @{
1271: Mod = getopt(mod);
1272: ModType = type(Mod);
1273: if ( ModType == 1 ) /* 'mod' is not specified. */
1274: return fctr(F);
1275: else if ( ModType == 0 ) /* 'mod' is a number */
1276: return modfctr(F,Mod);
1277: @}
1278: @end example
1279:
1280: @example
1281: [0] load("factor")$
1282: [1] factor(x^5-1);
1283: [[1,1],[x-1,1],[x^4+x^3+x^2+x+1,1]]
1284: [2] factor(x^5-1|mod=11);
1285: [[1,1],[x+6,1],[x+2,1],[x+10,1],[x+7,1],[x+8,1]]
1286: @end example
1287:
1.3 noro 1288: \BJP
1.2 noro 1289: 2 $BHVL\$N(B @code{factor()} $B$N8F$S=P$7$K$*$$$F(B, $B4X?tDj5A$N:]$K@k8@$5$l$?0z(B
1.3 noro 1290: $B?t(B @code{x^5-1}$B$N8e$m$K(B @code{|mod=11} $B$,CV$+$l$F$$$k(B. $B$3$l$O(B, $B4X?t<B9T;~(B
1291: $B$K(B, @code{mod} $B$H$$$&(B keyword $B$KBP$7$F(B @code{11} $B$H$$$&CM$r3d$jEv$F$k$3$H(B
1.2 noro 1292: $B$r;XDj$7$F$$$k(B. $B$3$l$r%*%W%7%g%s;XDj$H8F$V$3$H$K$9$k(B. $B$3$NCM$O(B
1293: @code{getopt(mod)} $B$G<h$j=P$9$3$H$,$G$-$k(B. 1 $BHVL\$N8F$S=P$7$N$h$&$K(B
1.3 noro 1294: @code{mod} $B$KBP$9$k%*%W%7%g%s;XDj$,$J$$>l9g$K$O(B, @code{getopt(mod)} $B$O7?(B
1.2 noro 1295: $B<1JL;R(B -1 $B$N%*%V%8%'%/%H$rJV$9(B. $B$3$l$K$h$j(B, $B;XDj$,$J$$>l9g$NF0:n$r(B if $BJ8(B
1296: $B$K$h$j5-=R$G$-$k(B. @samp{|} $B$N8e$m$K$O(B, $BG$0U8D$N%*%W%7%g%s$r(B, @samp{,}
1297: $B$G6h@Z$C$F;XDj$9$k$3$H$,$G$-$k(B.
1.3 noro 1298: \E
1299: \BEG
1300: In the second call of @code{factor()}, @code{|mod=11} is placed
1301: after the argument @code{x^5-1}, which appears in the declaration of
1302: @code{factor()}. This means that the value @code{11} is assigned to
1303: the keyword @code{mod} when the function is executed. The value
1304: can be retrieved by @code{getopt(mod)}. We call such machinery
1305: @var{option}. If the option for @var{mod} is not specified,
1306: @code{getopt(mod)} returns an object whose type is -1. By this
1307: feature, one can describe the behaviour of the function when
1308: the option is not specified by @var{if} statements.
1309: After @samp{|} one can append any number of options seperated by @samp{,}.
1310: \E
1.2 noro 1311: @example
1312: [100] xxx(1,2,x^2-1,[1,2,3]|proc=1,index=5);
1313: @end example
1314:
1.1 noro 1315:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>