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
|
<!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: User defined operators</title>
<meta name="description" content="Maxima 5.47.0 Manual: User defined operators">
<meta name="keywords" content="Maxima 5.47.0 Manual: User defined 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_42.html#Evaluation" rel="next" title="Evaluation">
<link href="maxima_40.html#Assignment-operators" rel="previous" title="Assignment operators">
<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="User-defined-operators"></a>
<div class="header">
<p>
Previous: <a href="maxima_40.html#Assignment-operators" accesskey="p" rel="previous">Assignment operators</a>, Up: <a href="maxima_34.html#Operators" accesskey="u" rel="up">Operators</a> [<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="User-defined-operators-1"></a>
<h3 class="section">7.7 User defined operators</h3>
<a name="infix"></a><a name="Item_003a-Operators_002fdeffn_002finfix"></a><dl>
<dt><a name="index-infix"></a>Function: <strong>infix</strong> <em><br> <tt>infix</tt> (<var>op</var>) <br> <tt>infix</tt> (<var>op</var>, <var>lbp</var>, <var>rbp</var>) <br> <tt>infix</tt> (<var>op</var>, <var>lbp</var>, <var>rbp</var>, <var>lpos</var>, <var>rpos</var>, <var>pos</var>)</em></dt>
<dd>
<p>Declares <var>op</var> to be an infix operator. An infix operator is a function of
two arguments, with the name of the function written between the arguments.
For example, the subtraction operator <code>-</code> is an infix operator.
</p>
<p><code>infix (<var>op</var>)</code> declares <var>op</var> to be an infix operator with default
binding powers (left and right both equal to 180) and parts of speech (left and
right both equal to <code>any</code>).
</p>
<p><code>infix (<var>op</var>, <var>lbp</var>, <var>rbp</var>)</code> declares <var>op</var> to be an infix
operator with stated left and right binding powers and default parts of speech
(left and right both equal to <code>any</code>).
</p>
<p><code>infix (<var>op</var>, <var>lbp</var>, <var>rbp</var>, <var>lpos</var>, <var>rpos</var>, <var>pos</var>)</code>
declares <var>op</var> to be an infix operator with stated left and right binding
powers and parts of speech <var>lpos</var>, <var>rpos</var>, and <var>pos</var> for the left
operand, the right operand, and the operator result, respectively.
</p>
<p>"Part of speech", in reference to operator declarations, means expression type.
Three types are recognized: <code>expr</code>, <code>clause</code>, and <code>any</code>,
indicating an algebraic expression, a Boolean expression, or any kind of
expression, respectively. Maxima can detect some syntax errors by comparing the
declared part of speech to an actual expression.
</p>
<p>The precedence of <var>op</var> with respect to other operators derives from the left
and right binding powers of the operators in question. If the left and right
binding powers of <var>op</var> are both greater the left and right binding powers of
some other operator, then <var>op</var> takes precedence over the other operator.
If the binding powers are not both greater or less, some more complicated
relation holds.
</p>
<p>The associativity of <var>op</var> depends on its binding powers. Greater left
binding power (<var>lbp</var>) implies an instance of <var>op</var> is evaluated before
other operators to its left in an expression, while greater right binding power
(<var>rbp</var>) implies an instance of <var>op</var> is evaluated before other operators
to its right in an expression. Thus greater <var>lbp</var> makes <var>op</var>
right-associative, while greater <var>rbp</var> makes <var>op</var> left-associative.
If <var>lbp</var> is equal to <var>rbp</var>, <var>op</var> is left-associative.
</p>
<p>See also <a href="maxima_35.html#Introduction-to-operators">Introduction to operators</a>.
</p>
<p>Examples:
</p>
<p>If the left and right binding powers of <var>op</var> are both greater
the left and right binding powers of some other operator,
then <var>op</var> takes precedence over the other operator.
</p>
<div class="example">
<pre class="example">(%i1) :lisp (get '$+ 'lbp)
100
(%i1) :lisp (get '$+ 'rbp)
100
(%i1) infix ("##", 101, 101);
(%o1) ##
(%i2) "##"(a, b) := sconcat("(", a, ",", b, ")");
(%o2) (a ## b) := sconcat("(", a, ",", b, ")")
(%i3) 1 + a ## b + 2;
(%o3) (a,b) + 3
(%i4) infix ("##", 99, 99);
(%o4) ##
(%i5) 1 + a ## b + 2;
(%o5) (a+1,b+2)
</pre></div>
<p>Greater <var>lbp</var> makes <var>op</var> right-associative,
while greater <var>rbp</var> makes <var>op</var> left-associative.
</p>
<div class="example">
<pre class="example">(%i1) infix ("##", 100, 99);
(%o1) ##
(%i2) "##"(a, b) := sconcat("(", a, ",", b, ")")$
(%i3) foo ## bar ## baz;
(%o3) (foo,(bar,baz))
(%i4) infix ("##", 100, 101);
(%o4) ##
(%i5) foo ## bar ## baz;
(%o5) ((foo,bar),baz)
</pre></div>
<p>Maxima can detect some syntax errors by comparing the
declared part of speech to an actual expression.
</p>
<div class="example">
<pre class="example">(%i1) infix ("##", 100, 99, expr, expr, expr);
(%o1) ##
(%i2) if x ## y then 1 else 0;
Incorrect syntax: Found algebraic expression where logical
expression expected
if x ## y then
^
(%i2) infix ("##", 100, 99, expr, expr, clause);
(%o2) ##
(%i3) if x ## y then 1 else 0;
(%o3) if x ## y then 1 else 0
</pre></div>
<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Operators">Operators</a>
·<a href="maxima_424.html#Category_003a-Declarations-and-inferences">Declarations and inferences</a>
·<a href="maxima_424.html#Category_003a-Syntax">Syntax</a>
·</div></dd></dl>
<a name="matchfix"></a><a name="Item_003a-Operators_002fdeffn_002fmatchfix"></a><dl>
<dt><a name="index-matchfix"></a>Function: <strong>matchfix</strong> <em><br> <tt>matchfix</tt> (<var>ldelimiter</var>, <var>rdelimiter</var>) <br> <tt>matchfix</tt> (<var>ldelimiter</var>, <var>rdelimiter</var>, <var>arg_pos</var>, <var>pos</var>)</em></dt>
<dd>
<p>Declares a matchfix operator with left and right delimiters <var>ldelimiter</var>
and <var>rdelimiter</var>. The delimiters are specified as strings.
</p>
<p>A "matchfix" operator is a function of any number of arguments,
such that the arguments occur between matching left and right delimiters.
The delimiters may be any strings, so long as the parser can
distinguish the delimiters from the operands
and other expressions and operators.
In practice this rules out unparseable delimiters such as
<code>%</code>, <code>,</code>, <code>$</code> and <code>;</code>,
and may require isolating the delimiters with white space.
The right delimiter can be the same or different from the left delimiter.
</p>
<p>A left delimiter can be associated with only one right delimiter;
two different matchfix operators cannot have the same left delimiter.
</p>
<p>An existing operator may be redeclared as a matchfix operator
without changing its other properties.
In particular, built-in operators such as addition <code>+</code> can
be declared matchfix,
but operator functions cannot be defined for built-in operators.
</p>
<p>The command <code>matchfix (<var>ldelimiter</var>, <var>rdelimiter</var>, <var>arg_pos</var>,
<var>pos</var>)</code> declares the argument part-of-speech <var>arg_pos</var> and result
part-of-speech <var>pos</var>, and the delimiters <var>ldelimiter</var> and
<var>rdelimiter</var>.
</p>
<p>"Part of speech", in reference to operator declarations, means expression type.
Three types are recognized: <code>expr</code>, <code>clause</code>, and <code>any</code>,
indicating an algebraic expression, a Boolean expression, or any kind of
expression, respectively.
Maxima can detect some syntax errors by comparing the
declared part of speech to an actual expression.
</p>
<p>The function to carry out a matchfix operation is an ordinary
user-defined function.
The operator function is defined
in the usual way
with the function definition operator <code>:=</code> or <code>define</code>.
The arguments may be written between the delimiters,
or with the left delimiter as a quoted string and the arguments
following in parentheses.
<code>dispfun (<var>ldelimiter</var>)</code> displays the function definition.
</p>
<p>The only built-in matchfix operator is the list constructor <code>[ ]</code>.
Parentheses <code>( )</code> and double-quotes <code>" "</code>
act like matchfix operators,
but are not treated as such by the Maxima parser.
</p>
<p><code>matchfix</code> evaluates its arguments.
<code>matchfix</code> returns its first argument, <var>ldelimiter</var>.
</p>
<p>Examples:
</p>
<p>Delimiters may be almost any strings.
</p>
<div class="example">
<pre class="example">(%i1) matchfix ("@@", "~");
(%o1) @@
(%i2) @@ a, b, c ~;
(%o2) @@a, b, c~
(%i3) matchfix (">>", "<<");
(%o3) >>
(%i4) >> a, b, c <<;
(%o4) >>a, b, c<<
(%i5) matchfix ("foo", "oof");
(%o5) foo
(%i6) foo a, b, c oof;
(%o6) fooa, b, coof
(%i7) >> w + foo x, y oof + z << / @@ p, q ~;
>>z + foox, yoof + w<<
(%o7) ----------------------
@@p, q~
</pre></div>
<p>Matchfix operators are ordinary user-defined functions.
</p>
<div class="example">
<pre class="example">(%i1) matchfix ("!-", "-!");
(%o1) "!-"
(%i2) !- x, y -! := x/y - y/x;
x y
(%o2) !-x, y-! := - - -
y x
(%i3) define (!-x, y-!, x/y - y/x);
x y
(%o3) !-x, y-! := - - -
y x
(%i4) define ("!-" (x, y), x/y - y/x);
x y
(%o4) !-x, y-! := - - -
y x
(%i5) dispfun ("!-");
x y
(%t5) !-x, y-! := - - -
y x
(%o5) done
(%i6) !-3, 5-!;
16
(%o6) - --
15
(%i7) "!-" (3, 5);
16
(%o7) - --
15
</pre></div>
<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Syntax">Syntax</a>
·<a href="maxima_424.html#Category_003a-Operators">Operators</a>
·</div></dd></dl>
<a name="function_005fnary"></a><a name="Item_003a-Operators_002fdeffn_002fnary"></a><dl>
<dt><a name="index-nary"></a>Function: <strong>nary</strong> <em><br> <tt>nary</tt> (<var>op</var>) <br> <tt>nary</tt> (<var>op</var>, <var>bp</var>, <var>arg_pos</var>, <var>pos</var>)</em></dt>
<dd>
<p>An <code>nary</code> operator is used to denote a function of any number of arguments,
each of which is separated by an occurrence of the operator, e.g. A+B or A+B+C.
The <code>nary("x")</code> function is a syntax extension function to declare <code>x</code>
to be an <code>nary</code> operator. Functions may be declared to be <code>nary</code>. If
<code>declare(j,nary);</code> is done, this tells the simplifier to simplify, e.g.
<code>j(j(a,b),j(c,d))</code> to <code>j(a, b, c, d)</code>.
</p>
<p>See also <a href="maxima_35.html#Introduction-to-operators">Introduction to operators</a>.
</p>
<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Operators">Operators</a>
·<a href="maxima_424.html#Category_003a-Syntax">Syntax</a>
·</div></dd></dl>
<a name="nofix"></a><a name="Item_003a-Operators_002fdeffn_002fnofix"></a><dl>
<dt><a name="index-nofix"></a>Function: <strong>nofix</strong> <em><br> <tt>nofix</tt> (<var>op</var>) <br> <tt>nofix</tt> (<var>op</var>, <var>pos</var>)</em></dt>
<dd>
<p><code>nofix</code> operators are used to denote functions of no arguments.
The mere presence of such an operator in a command will cause the
corresponding function to be evaluated. For example, when one types
"exit;" to exit from a Maxima break, "exit" is behaving similar to a
<code>nofix</code> operator. The function <code>nofix("x")</code> is a syntax extension
function which declares <code>x</code> to be a <code>nofix</code> operator.
</p>
<p>See also <a href="maxima_35.html#Introduction-to-operators">Introduction to operators</a>.
</p>
<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Operators">Operators</a>
·<a href="maxima_424.html#Category_003a-Syntax">Syntax</a>
·</div></dd></dl>
<a name="postfix"></a><a name="Item_003a-Operators_002fdeffn_002fpostfix"></a><dl>
<dt><a name="index-postfix"></a>Function: <strong>postfix</strong> <em><br> <tt>postfix</tt> (<var>op</var>) <br> <tt>postfix</tt> (<var>op</var>, <var>lbp</var>, <var>lpos</var>, <var>pos</var>)</em></dt>
<dd>
<p><code>postfix</code> operators like the <code>prefix</code> variety denote functions of a
single argument, but in this case the argument immediately precedes an
occurrence of the operator in the input string, e.g. 3!. The
<code>postfix("x")</code> function is a syntax extension function to declare <code>x</code>
to be a <code>postfix</code> operator.
</p>
<p>See also <a href="maxima_35.html#Introduction-to-operators">Introduction to operators</a>.
</p>
<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Operators">Operators</a>
·<a href="maxima_424.html#Category_003a-Syntax">Syntax</a>
·</div></dd></dl>
<a name="prefix"></a><a name="Item_003a-Operators_002fdeffn_002fprefix"></a><dl>
<dt><a name="index-prefix"></a>Function: <strong>prefix</strong> <em><br> <tt>prefix</tt> (<var>op</var>) <br> <tt>prefix</tt> (<var>op</var>, <var>rbp</var>, <var>rpos</var>, <var>pos</var>)</em></dt>
<dd>
<p>A <code>prefix</code> operator is one which signifies a function of one argument,
which argument immediately follows an occurrence of the operator.
<code>prefix("x")</code> is a syntax extension function to declare <code>x</code> to be a
<code>prefix</code> operator.
</p>
<p>See also <a href="maxima_35.html#Introduction-to-operators">Introduction to operators</a>.
</p>
<div class=categorybox>
Categories:<a href="maxima_424.html#Category_003a-Operators">Operators</a>
·<a href="maxima_424.html#Category_003a-Syntax">Syntax</a>
·</div></dd></dl>
<hr>
<div class="header">
<p>
Previous: <a href="maxima_40.html#Assignment-operators" accesskey="p" rel="previous">Assignment operators</a>, Up: <a href="maxima_34.html#Operators" accesskey="u" rel="up">Operators</a> [<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>
|