~ubuntu-branches/debian/squeeze/maxima/squeeze

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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- Created on April, 24 2010 by texi2html 1.76 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Many creative people <dev@texi2html.cvshome.org>
Send bugs and suggestions to <users@texi2html.cvshome.org>

-->
<head>
<title>Maxima 5.21.1 Manual: 36. Rules and Patterns</title>

<meta name="description" content="Maxima 5.21.1 Manual: 36. Rules and Patterns">
<meta name="keywords" content="Maxima 5.21.1 Manual: 36. Rules and Patterns">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.76">
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
body
{
    color: black;
    background: white; 
    margin-left: 8%;
    margin-right: 13%;
}

h1
{
    margin-left: +8%;
    font-size: 150%;
    font-family: sans-serif
}

h2
{
    font-size: 125%;
    font-family: sans-serif
}

h3
{
    font-size: 100%;
    font-family: sans-serif
}

h2,h3,h4,h5,h6 { margin-left: +4%; }

div.textbox
{
    border: solid;
    border-width: thin;
    /* width: 100%; */
    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);
    /*background: rgb(200,255,255); */
    /* font-family: fixed */
}

pre.example
{
    border: 1px solid gray;
    padding-top: 1em;
    padding-bottom: 1em;
    padding-left: 1em;
    padding-right: 1em;
    /* background: rgb(247,242,180); */ /* kind of sandy */
    /* background: rgb(200,255,255); */ /* sky blue */
    background-color: #F1F5F9; /* light blue-gray */
    /* font-family: "Lucida Console", monospace */
}

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: 0px;
    padding-bottom: 0px;
    padding-left: 1em;
    padding-right: 1em;
    background: rgb(247,242,220);
}


-->
</style>

<link rel="icon" href="http://maxima.sourceforge.net/favicon.ico"/>
</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">

<a name="Rules-and-Patterns"></a>
<a name="SEC160"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="maxima_35.html#SEC159" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#SEC161" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="maxima_35.html#SEC156" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="maxima.html#SEC_Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="maxima_37.html#SEC163" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="maxima.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="maxima_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="maxima_79.html#SEC331" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="maxima_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h1 class="chapter"> 36. Rules and Patterns </h1>

<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#SEC161">36.1 Introduction to Rules and Patterns</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">  
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC162">36.2 Functions and Variables for Rules and Patterns</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">  
</td></tr>
</table>

<p><a name="Item_003a-Introduction-to-Rules-and-Patterns"></a>
</p><hr size="6">
<a name="Introduction-to-Rules-and-Patterns"></a>
<a name="SEC161"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC160" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#SEC162" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#SEC160" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#SEC160" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="maxima_37.html#SEC163" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="maxima.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="maxima_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="maxima_79.html#SEC331" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="maxima_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 36.1 Introduction to Rules and Patterns </h2>

<p>This section describes user-defined pattern matching and
simplification rules.
There are two groups of functions which implement somewhat different pattern matching schemes.
In one group are <code>tellsimp</code>, <code>tellsimpafter</code>, <code>defmatch</code>, <code>defrule</code>,
<code>apply1</code>, <code>applyb1</code>, and <code>apply2</code>.
In the other group are <code>let</code> and <code>letsimp</code>.
Both schemes define patterns in terms of pattern variables declared by <code>matchdeclare</code>.
</p>
<p>Pattern-matching rules defined by <code>tellsimp</code> and <code>tellsimpafter</code> are applied automatically
by the Maxima simplifier.
Rules defined by <code>defmatch</code>, <code>defrule</code>, and <code>let</code> are applied
by an explicit function call.
</p>
<p>There are additional mechanisms for rules applied to polynomials by <code>tellrat</code>,
and for commutative and noncommutative algebra in <code>affine</code> package. 
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Simplification">Simplification</a>
 &middot;
<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>


<p><a name="Item_003a-Functions-and-Variables-for-Rules-and-Patterns"></a>
</p><hr size="6">
<a name="Functions-and-Variables-for-Rules-and-Patterns"></a>
<a name="SEC162"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC161" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="maxima_37.html#SEC163" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#SEC160" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#SEC160" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="maxima_37.html#SEC163" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="maxima.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="maxima_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="maxima_79.html#SEC331" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="maxima_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 36.2 Functions and Variables for Rules and Patterns </h2>

<p><a name="Item_003a-apply1"></a>
</p><dl>
<dt><u>Function:</u> <b>apply1</b><i> (<var>expr</var>, <var>rule_1</var>, ..., <var>rule_n</var>)</i>
<a name="IDX1270"></a>
</dt>
<dd><p>Repeatedly applies <var>rule_1</var> to
<var>expr</var> until it fails, then repeatedly applies the same rule to all
subexpressions of <var>expr</var>, left to right, until <var>rule_1</var> has failed
on all subexpressions.  Call the result of transforming <var>expr</var> in this
manner <var>expr_2</var>.  Then <var>rule_2</var> is applied in the same fashion
starting at the top of <var>expr_2</var>.  When <var>rule_n</var> fails on the final
subexpression, the result is returned.
</p>
<p><code>maxapplydepth</code> is the depth of the deepest subexpressions processed by
<code>apply1</code> and <code>apply2</code>.
</p>
<p>See also <code>applyb1</code>, <code>apply2</code>, and <code>let</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-apply2"></a>
</p><dl>
<dt><u>Function:</u> <b>apply2</b><i> (<var>expr</var>, <var>rule_1</var>, ..., <var>rule_n</var>)</i>
<a name="IDX1271"></a>
</dt>
<dd><p>If <var>rule_1</var> fails on a given subexpression, then <var>rule_2</var> is
repeatedly applied, etc.  Only if all rules fail on a given
subexpression is the whole set of rules repeatedly applied to the next
subexpression.  If one of the rules succeeds, then the same
subexpression is reprocessed, starting with the first rule.
</p>
<p><code>maxapplydepth</code> is the depth of the deepest subexpressions processed by
<code>apply1</code> and <code>apply2</code>.
</p>
<p>See also <code>apply1</code> and <code>let</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-applyb1"></a>
</p><dl>
<dt><u>Function:</u> <b>applyb1</b><i> (<var>expr</var>, <var>rule_1</var>, ..., <var>rule_n</var>)</i>
<a name="IDX1272"></a>
</dt>
<dd><p>Repeatedly applies <var>rule_1</var> to the deepest subexpression of <var>expr</var> until it fails,
then repeatedly applies the same rule one level higher (i.e., larger subexpressions),
until <var>rule_1</var> has failed on the top-level expression.
Then <var>rule_2</var> is applied in the same fashion to the result of <var>rule_1</var>.
After <var>rule_n</var> has been applied to the top-level expression,
the result is returned.
</p>
<p><code>applyb1</code> is similar to <code>apply1</code> but works from
the bottom up instead of from the top down.
</p>
<p><code>maxapplyheight</code> is the maximum height which <code>applyb1</code> reaches
before giving up.
</p>
<p>See also <code>apply1</code>, <code>apply2</code>, and <code>let</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-current_005flet_005frule_005fpackage"></a>
</p><dl>
<dt><u>Option variable:</u> <b>current_let_rule_package</b>
<a name="IDX1273"></a>
</dt>
<dd><p>Default value: <code>default_let_rule_package</code>
</p>
<p><code>current_let_rule_package</code> is the name of the rule package that is used by
functions in the <code>let</code> package (<code>letsimp</code>, etc.) if no other rule package is specified.
This variable may be assigned the name of any rule package defined
via the <code>let</code> command.
</p>
<p>If a call such as <code>letsimp (expr, rule_pkg_name)</code> is made,
the rule package <code>rule_pkg_name</code> is used for that function call only,
and the value of <code>current_let_rule_package</code> is not changed.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>


</dd></dl>

<p><a name="Item_003a-default_005flet_005frule_005fpackage"></a>
</p><dl>
<dt><u>Option variable:</u> <b>default_let_rule_package</b>
<a name="IDX1274"></a>
</dt>
<dd><p>Default value: <code>default_let_rule_package</code>
</p>
<p><code>default_let_rule_package</code> is the name of the rule package used when one
is not explicitly set by the user with <code>let</code> or by changing the value of
<code>current_let_rule_package</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>


</dd></dl>

<p><a name="Item_003a-defmatch"></a>
</p><dl>
<dt><u>Function:</u> <b>defmatch</b><i> (<var>progname</var>, <var>pattern</var>, <var>x_1</var>, ..., <var>x_n</var>)</i>
<a name="IDX1275"></a>
</dt>
<dt><u>Function:</u> <b>defmatch</b><i> (<var>progname</var>, <var>pattern</var>)</i>
<a name="IDX1276"></a>
</dt>
<dd><p>Defines a function <code><var>progname</var>(<var>expr</var>, <var>x_1</var>, ..., <var>x_n</var>)</code>
which tests <var>expr</var> to see if it matches <var>pattern</var>.
</p>
<p><var>pattern</var> is an expression containing the pattern arguments <var>x_1</var>, ..., <var>x_n</var> (if any)
and some pattern variables (if any).
The pattern arguments are given explicitly as arguments to <code>defmatch</code> while the pattern variables
are declared by the <code>matchdeclare</code> function.
Any variable not declared as a pattern variable in <code>matchdeclare</code>
or as a pattern argument in <code>defmatch</code> matches only itself.
</p>
<p>The first argument to the created function <var>progname</var> is an expression
to be matched against the pattern and the other arguments are the actual arguments
which correspond to the dummy variables <var>x_1</var>, ..., <var>x_n</var> in the pattern.
</p>
<p>If the match is successful, <var>progname</var> returns
a list of equations whose left sides are the
pattern arguments and pattern variables, and whose right sides are the subexpressions
which the pattern arguments and variables matched.
The pattern variables, but not the pattern arguments, are assigned the subexpressions they match.
If the match fails, <var>progname</var> returns <code>false</code>.  
</p>
<p>A literal pattern
(that is, a pattern which contains neither pattern arguments nor pattern variables)
returns <code>true</code> if the match succeeds.
</p>
<p>See also <code>matchdeclare</code>, <code>defrule</code>, <code>tellsimp</code>, and <code>tellsimpafter</code>.
</p>
<p>Examples:
</p>
<p>Define a function <code>linearp(expr, x)</code> which
tests <code>expr</code> to see if it is of the form <code>a*x + b</code>
such that <code>a</code> and <code>b</code> do not contain <code>x</code> and <code>a</code> is nonzero.
This match function matches expressions which are linear in any variable,
because the pattern argument <code>x</code> is given to <code>defmatch</code>.
</p>
<pre class="example">(%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
                    freeof(x));
(%o1)                         done
(%i2) defmatch (linearp, a*x + b, x);
(%o2)                        linearp
(%i3) linearp (3*z + (y + 1)*z + y^2, z);
                         2
(%o3)              [b = y , a = y + 4, x = z]
(%i4) a;
(%o4)                         y + 4
(%i5) b;
                                2
(%o5)                          y
(%i6) x;
(%o6)                           x
</pre>
<p>Define a function <code>linearp(expr)</code> which tests <code>expr</code>
to see if it is of the form <code>a*x + b</code>
such that <code>a</code> and <code>b</code> do not contain <code>x</code> and <code>a</code> is nonzero.
This match function only matches expressions linear in <code>x</code>,
not any other variable, because no pattern argument is given to <code>defmatch</code>.
</p>
<pre class="example">(%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
                    freeof(x));
(%o1)                         done
(%i2) defmatch (linearp, a*x + b);
(%o2)                        linearp
(%i3) linearp (3*z + (y + 1)*z + y^2);
(%o3)                         false
(%i4) linearp (3*x + (y + 1)*x + y^2);
                             2
(%o4)                  [b = y , a = y + 4]
</pre>
<p>Define a function <code>checklimits(expr)</code> which tests <code>expr</code>
to see if it is a definite integral.
</p>
<pre class="example">(%i1) matchdeclare ([a, f], true);
(%o1)                         done
(%i2) constinterval (l, h) := constantp (h - l);
(%o2)        constinterval(l, h) := constantp(h - l)
(%i3) matchdeclare (b, constinterval (a));
(%o3)                         done
(%i4) matchdeclare (x, atom);
(%o4)                         done
(%i5) simp : false;
(%o5)                         false
(%i6) defmatch (checklimits, 'integrate (f, x, a, b));
(%o6)                      checklimits
(%i7) simp : true;
(%o7)                         true
(%i8) 'integrate (sin(t), t, %pi + x, 2*%pi + x);
                       x + 2 %pi
                      /
                      [
(%o8)                 I          sin(t) dt
                      ]
                      /
                       x + %pi
(%i9) checklimits (%);
(%o9)    [b = x + 2 %pi, a = x + %pi, x = t, f = sin(t)]
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-defrule"></a>
</p><dl>
<dt><u>Function:</u> <b>defrule</b><i> (<var>rulename</var>, <var>pattern</var>, <var>replacement</var>)</i>
<a name="IDX1277"></a>
</dt>
<dd><p>Defines and names a
replacement rule for the given pattern.  If the rule named <var>rulename</var> is
applied to an expression (by <code>apply1</code>, <code>applyb1</code>, or <code>apply2</code>), every
subexpression matching the pattern will be replaced by the
replacement. All variables in the replacement which have been
assigned values by the pattern match are assigned those values in the
replacement which is then simplified.
</p>
<p>The rules themselves can be
treated as functions which transform an expression by one
operation of the pattern match and replacement.
If the match fails, the rule function returns <code>false</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-disprule"></a>
</p><dl>
<dt><u>Function:</u> <b>disprule</b><i> (<var>rulename_1</var>, ..., <var>rulename_2</var>)</i>
<a name="IDX1278"></a>
</dt>
<dt><u>Function:</u> <b>disprule</b><i> (all)</i>
<a name="IDX1279"></a>
</dt>
<dd><p>Display rules with the names <var>rulename_1</var>, ..., <var>rulename_n</var>,
as returned by <code>defrule</code>, <code>tellsimp</code>, or <code>tellsimpafter</code>,
or a pattern defined by <code>defmatch</code>.
Each rule is displayed with an intermediate expression label (<code>%t</code>).
</p>
<p><code>disprule (all)</code> displays all rules.
</p>
<p><code>disprule</code> quotes its arguments.
<code>disprule</code> returns the list of intermediate expression labels corresponding to the displayed rules.
</p>
<p>See also <code>letrules</code>, which displays rules defined by <code>let</code>.
</p>
<p>Examples:
</p>
<pre class="example">(%i1) tellsimpafter (foo (x, y), bar (x) + baz (y));
(%o1)                   [foorule1, false]
(%i2) tellsimpafter (x + y, special_add (x, y));
(%o2)                   [+rule1, simplus]
(%i3) defmatch (quux, mumble (x));
(%o3)                         quux
(%i4) disprule (foorule1, &quot;+rule1&quot;, quux);
(%t4)        foorule1 : foo(x, y) -&gt; baz(y) + bar(x)

(%t5)          +rule1 : y + x -&gt; special_add(x, y)

(%t6)                quux : mumble(x) -&gt; []

(%o6)                    [%t4, %t5, %t6]
(%i6) ''%;
(%o6) [foorule1 : foo(x, y) -&gt; baz(y) + bar(x), 
     +rule1 : y + x -&gt; special_add(x, y), quux : mumble(x) -&gt; []]
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
 &middot;
<a href="maxima_95.html#Category_003a-Display-functions">Display functions</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-let"></a>
</p><dl>
<dt><u>Function:</u> <b>let</b><i> (<var>prod</var>, <var>repl</var>, <var>predname</var>, <var>arg_1</var>, ..., <var>arg_n</var>)</i>
<a name="IDX1280"></a>
</dt>
<dt><u>Function:</u> <b>let</b><i> ([<var>prod</var>, <var>repl</var>, <var>predname</var>, <var>arg_1</var>, ..., <var>arg_n</var>], <var>package_name</var>)</i>
<a name="IDX1281"></a>
</dt>
<dd><p>Defines a substitution rule for <code>letsimp</code> such that <var>prod</var> is replaced by <var>repl</var>.
<var>prod</var> is a product of positive or negative powers of the following terms:
</p>
<ul>
<li>
Atoms which <code>letsimp</code> will search for literally unless previous
to calling <code>letsimp</code> the <code>matchdeclare</code> function is used to associate a
predicate with the atom.  In this case <code>letsimp</code> will match the atom to
any term of a product satisfying the predicate.
</li><li>
Kernels such as <code>sin(x)</code>, <code>n!</code>, <code>f(x,y)</code>, etc.  As with atoms above
<code>letsimp</code> will look for a literal match unless <code>matchdeclare</code> is used to
associate a predicate with the argument of the kernel.
</li></ul>

<p>A term to a positive power will only match a term having at least that
power.  A term to a negative power
on the other hand will only match a term with a power at least as
negative.  In the case of negative powers in <var>prod</var> the switch
<code>letrat</code> must be set to <code>true</code>.
See also <code>letrat</code>.
</p>
<p>If a predicate is included in the <code>let</code> function followed by a list of
arguments, a tentative match (i.e. one that would be accepted if the
predicate were omitted) is accepted only if
<code>predname (arg_1', ..., arg_n')</code> evaluates to <code>true</code> where <var>arg_i'</var> is the value
matched to <var>arg_i</var>.  The <var>arg_i</var> may be the name of any atom or the argument
of any kernel appearing in <var>prod</var>.
<var>repl</var> may be any rational expression. If any of the atoms or arguments from <var>prod</var> appear in <var>repl</var> the
appropriate substitutions are made. </p>
<p>The global flag <code>letrat</code> controls the simplification of quotients by <code>letsimp</code>.
When <code>letrat</code> is <code>false</code>,
<code>letsimp</code> simplifies the numerator and
denominator of <var>expr</var> separately, and does not simplify the quotient.
Substitutions such as <code>n!/n</code> goes to <code>(n-1)!</code> then fail.
When <code>letrat</code> is <code>true</code>, then the numerator,
denominator, and the quotient are simplified in that order.
</p>
<p>These substitution functions allow you to work with several rule packages at once.
Each rule package can contain any number of <code>let</code>
rules and is referenced by a user-defined name.
<code>let ([<var>prod</var>, <var>repl</var>, <var>predname</var>, <var>arg_1</var>, ..., <var>arg_n</var>], <var>package_name</var>)</code>
adds the rule <var>predname</var> to the rule package <var>package_name</var>.
<code>letsimp (<var>expr</var>, <var>package_name</var>)</code> 
applies the rules in <var>package_name</var>.
<code>letsimp (<var>expr</var>, <var>package_name1</var>, <var>package_name2</var>, ...)</code>
is equivalent to <code>letsimp (<var>expr</var>, <var>package_name1</var>)</code>
followed by <code>letsimp (%, <var>package_name2</var>)</code>, ....
</p>
<p><code>current_let_rule_package</code> is the name of the rule package that is
presently being used.
This variable may be assigned the name of
any rule package defined via the <code>let</code> command.
Whenever any of the functions comprising the <code>let</code> package are called with no package name,
the package named by <code>current_let_rule_package</code> is used.
If a call such as <code>letsimp (<var>expr</var>, <var>rule_pkg_name</var>)</code> is made,
the rule package <var>rule_pkg_name</var> is used for that <code>letsimp</code> command only,
and <code>current_let_rule_package</code> is not changed.
If not otherwise specified,
<code>current_let_rule_package</code> defaults to <code>default_let_rule_package</code>.
</p>
<pre class="example">(%i1) matchdeclare ([a, a1, a2], true)$
(%i2) oneless (x, y) := is (x = y-1)$
(%i3) let (a1*a2!, a1!, oneless, a2, a1);
(%o3)         a1 a2! --&gt; a1! where oneless(a2, a1)
(%i4) letrat: true$
(%i5) let (a1!/a1, (a1-1)!);
                        a1!
(%o5)                   --- --&gt; (a1 - 1)!
                        a1
(%i6) letsimp (n*m!*(n-1)!/m);
(%o6)                      (m - 1)! n!
(%i7) let (sin(a)^2, 1 - cos(a)^2);
                        2               2
(%o7)                sin (a) --&gt; 1 - cos (a)
(%i8) letsimp (sin(x)^4);
                        4           2
(%o8)                cos (x) - 2 cos (x) + 1
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-letrat"></a>
</p><dl>
<dt><u>Option variable:</u> <b>letrat</b>
<a name="IDX1282"></a>
</dt>
<dd><p>Default value: <code>false</code>
</p>
<p>When <code>letrat</code> is <code>false</code>, <code>letsimp</code> simplifies the
numerator and denominator of a ratio separately,
and does not simplify the quotient.
</p>
<p>When <code>letrat</code> is <code>true</code>,
the numerator, denominator, and their quotient are simplified in that order.
</p>
<pre class="example">(%i1) matchdeclare (n, true)$
(%i2) let (n!/n, (n-1)!);
                         n!
(%o2)                    -- --&gt; (n - 1)!
                         n
(%i3) letrat: false$
(%i4) letsimp (a!/a);
                               a!
(%o4)                          --
                               a
(%i5) letrat: true$
(%i6) letsimp (a!/a);
(%o6)                       (a - 1)!
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>


</dd></dl>

<p><a name="Item_003a-letrules"></a>
</p><dl>
<dt><u>Function:</u> <b>letrules</b><i> ()</i>
<a name="IDX1283"></a>
</dt>
<dt><u>Function:</u> <b>letrules</b><i> (<var>package_name</var>)</i>
<a name="IDX1284"></a>
</dt>
<dd><p>Displays the rules in a rule package.
<code>letrules ()</code> displays the rules in the current rule package.
<code>letrules (<var>package_name</var>)</code> displays the rules in <var>package_name</var>.
</p>
<p>The current rule package is named by <code>current_let_rule_package</code>.
If not otherwise specified, <code>current_let_rule_package</code>
defaults to <code>default_let_rule_package</code>.
</p>
<p>See also <code>disprule</code>, which displays rules defined by <code>tellsimp</code> and <code>tellsimpafter</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-letsimp"></a>
</p><dl>
<dt><u>Function:</u> <b>letsimp</b><i> (<var>expr</var>)</i>
<a name="IDX1285"></a>
</dt>
<dt><u>Function:</u> <b>letsimp</b><i> (<var>expr</var>, <var>package_name</var>)</i>
<a name="IDX1286"></a>
</dt>
<dt><u>Function:</u> <b>letsimp</b><i> (<var>expr</var>, <var>package_name_1</var>, ..., <var>package_name_n</var>)</i>
<a name="IDX1287"></a>
</dt>
<dd><p>Repeatedly applies the substitution rules defined by <code>let</code>
until no further change is made to <var>expr</var>.
</p>
<p><code>letsimp (<var>expr</var>)</code> uses the rules from <code>current_let_rule_package</code>.
</p>
<p><code>letsimp (<var>expr</var>, <var>package_name</var>)</code> uses the rules from <var>package_name</var>
without changing <code>current_let_rule_package</code>.
</p>
<p><code>letsimp (<var>expr</var>, <var>package_name_1</var>, ..., <var>package_name_n</var>)</code>
is equivalent to <code>letsimp (<var>expr</var>, <var>package_name_1</var></code>,
followed by <code>letsimp (%, <var>package_name_2</var>)</code>, and so on.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-let_005frule_005fpackages"></a>
</p><dl>
<dt><u>Option variable:</u> <b>let_rule_packages</b>
<a name="IDX1288"></a>
</dt>
<dd><p>Default value: <code>[default_let_rule_package]</code>
</p>
<p><code>let_rule_packages</code> is a list of all user-defined let rule packages
plus the default package <code>default_let_rule_package</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>


</dd></dl>

<p><a name="Item_003a-matchdeclare"></a>
</p><dl>
<dt><u>Function:</u> <b>matchdeclare</b><i> (<var>a_1</var>, <var>pred_1</var>, ..., <var>a_n</var>, <var>pred_n</var>)</i>
<a name="IDX1289"></a>
</dt>
<dd><p>Associates a predicate <var>pred_k</var> 
with a variable or list of variables <var>a_k</var>
so that <var>a_k</var> matches expressions
for which the predicate returns anything other than <code>false</code>.
</p>
<p>A predicate is the name of a function,
or a lambda expression,
or a function call or lambda call missing the last argument,
or <code>true</code> or <code>all</code>.
Any expression matches <code>true</code> or <code>all</code>.
If the predicate is specified as a function call or lambda call,
the expression to be tested is appended to the list of arguments;
the arguments are evaluated at the time the match is evaluated.
Otherwise, the predicate is specified as a function name or lambda expression,
and the expression to be tested is the sole argument.
A predicate function need not be defined when <code>matchdeclare</code> is called;
the predicate is not evaluated until a match is attempted.
</p>
<p>A predicate may return a Boolean expression as well as <code>true</code> or <code>false</code>.
Boolean expressions are evaluated by <code>is</code> within the constructed rule function,
so it is not necessary to call <code>is</code> within the predicate.
</p>
<p>If an expression satisfies a match predicate,
the match variable is assigned the expression,
except for match variables which are operands of addition <code>+</code> or multiplication <code>*</code>.
Only addition and multiplication are handled specially;
other n-ary operators (both built-in and user-defined) are treated like ordinary functions.
</p>
<p>In the case of addition and multiplication,
the match variable may be assigned a single expression which satisfies the match predicate,
or a sum or product (respectively) of such expressions.
Such multiple-term matching is greedy:
predicates are evaluated in the order in which their associated variables
appear in the match pattern,
and a term which satisfies more than one predicate is taken by the first
predicate which it satisfies.
Each predicate is tested against all operands of the sum or product before the next predicate is evaluated.
In addition,
if 0 or 1 (respectively) satisfies a match predicate,
and there are no other terms which satisfy the predicate,
0 or 1 is assigned to the match variable associated with the predicate.
</p>
<p>The algorithm for processing addition and multiplication patterns makes some match results
(for example, a pattern in which a &quot;match anything&quot; variable appears)
dependent on the ordering of terms in the match pattern and in the expression to be matched.
However,
if all match predicates are mutually exclusive,
the match result is insensitive to ordering,
as one match predicate cannot accept terms matched by another.
</p>
<p>Calling <code>matchdeclare</code> with a variable <var>a</var> as an argument
changes the <code>matchdeclare</code> property for <var>a</var>, if one was already declared;
only the most recent <code>matchdeclare</code> is in effect when a rule is defined,
Later changes to the <code>matchdeclare</code> property
(via <code>matchdeclare</code> or <code>remove</code>)
do not affect existing rules.
</p>
<p><code>propvars (matchdeclare)</code> returns the list of all variables
for which there is a <code>matchdeclare</code> property.
<code>printprops (<var>a</var>, matchdeclare)</code> returns the predicate for variable <code>a</code>.
<code>printprops (all, matchdeclare)</code> returns the list of predicates for all <code>matchdeclare</code> variables.
<code>remove (<var>a</var>, matchdeclare)</code> removes the <code>matchdeclare</code> property from <var>a</var>.
</p>
<p>The functions
<code>defmatch</code>, <code>defrule</code>, <code>tellsimp</code>, <code>tellsimpafter</code>, and <code>let</code>
construct rules which test expressions against patterns.
</p>
<p><code>matchdeclare</code> quotes its arguments.
<code>matchdeclare</code> always returns <code>done</code>.
</p>
<p>Examples:
</p>
<p>A predicate is the name of a function,
or a lambda expression,
or a function call or lambda call missing the last argument,
or <code>true</code> or <code>all</code>.
</p>
<pre class="example">(%i1) matchdeclare (aa, integerp);
(%o1)                         done
(%i2) matchdeclare (bb, lambda ([x], x &gt; 0));
(%o2)                         done
(%i3) matchdeclare (cc, freeof (%e, %pi, %i));
(%o3)                         done
(%i4) matchdeclare (dd, lambda ([x, y], gcd (x, y) = 1) (1728));
(%o4)                         done
(%i5) matchdeclare (ee, true);
(%o5)                         done
(%i6) matchdeclare (ff, all);
(%o6)                         done
</pre>
<p>If an expression satisfies a match predicate,
the match variable is assigned the expression.
</p>
<pre class="example">(%i1) matchdeclare (aa, integerp, bb, atom);
(%o1)                         done
(%i2) defrule (r1, bb^aa, [&quot;integer&quot; = aa, &quot;atom&quot; = bb]);
                    aa
(%o2)        r1 : bb   -&gt; [integer = aa, atom = bb]
(%i3) r1 (%pi^8);
(%o3)               [integer = 8, atom = %pi]
</pre>
<p>In the case of addition and multiplication,
the match variable may be assigned a single expression which satisfies the match predicate,
or a sum or product (respectively) of such expressions.
</p>
<pre class="example">(%i1) matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
(%o1)                         done
(%i2) defrule (r1, aa + bb, [&quot;all atoms&quot; = aa, &quot;all nonatoms&quot; =
               bb]);
bb + aa partitions `sum'
(%o2)  r1 : bb + aa -&gt; [all atoms = aa, all nonatoms = bb]
(%i3) r1 (8 + a*b + sin(x));
(%o3)     [all atoms = 8, all nonatoms = sin(x) + a b]
(%i4) defrule (r2, aa * bb, [&quot;all atoms&quot; = aa, &quot;all nonatoms&quot; =
               bb]);
bb aa partitions `product'
(%o4)   r2 : aa bb -&gt; [all atoms = aa, all nonatoms = bb]
(%i5) r2 (8 * (a + b) * sin(x));
(%o5)    [all atoms = 8, all nonatoms = (b + a) sin(x)]
</pre>
<p>When matching arguments of <code>+</code> and <code>*</code>,
if all match predicates are mutually exclusive,
the match result is insensitive to ordering,
as one match predicate cannot accept terms matched by another.
</p>
<pre class="example">(%i1) matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
(%o1)                         done
(%i2) defrule (r1, aa + bb, [&quot;all atoms&quot; = aa, &quot;all nonatoms&quot; =
               bb]);
bb + aa partitions `sum'
(%o2)  r1 : bb + aa -&gt; [all atoms = aa, all nonatoms = bb]
(%i3) r1 (8 + a*b + %pi + sin(x) - c + 2^n);
                                                     n
(%o3) [all atoms = %pi + 8, all nonatoms = sin(x) + 2  - c + a b]
(%i4) defrule (r2, aa * bb, [&quot;all atoms&quot; = aa, &quot;all nonatoms&quot; =
               bb]);
bb aa partitions `product'
(%o4)   r2 : aa bb -&gt; [all atoms = aa, all nonatoms = bb]
(%i5) r2 (8 * (a + b) * %pi * sin(x) / c * 2^n);
                                                  n
                                         (b + a) 2  sin(x)
(%o5) [all atoms = 8 %pi, all nonatoms = -----------------]
                                                 c
</pre>
<p>The functions <code>propvars</code> and <code>printprops</code> return information about match variables.
</p>
<pre class="example">(%i1) matchdeclare ([aa, bb, cc], atom, [dd, ee], integerp);
(%o1)                         done
(%i2) matchdeclare (ff, floatnump, gg, lambda ([x], x &gt; 100));
(%o2)                         done
(%i3) propvars (matchdeclare);
(%o3)             [aa, bb, cc, dd, ee, ff, gg]
(%i4) printprops (ee, matchdeclare);
(%o4)                    [integerp(ee)]
(%i5) printprops (gg, matchdeclare);
(%o5)              [lambda([x], x &gt; 100, gg)]
(%i6) printprops (all, matchdeclare);
(%o6) [lambda([x], x &gt; 100, gg), floatnump(ff), integerp(ee), 
                      integerp(dd), atom(cc), atom(bb), atom(aa)]
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
 &middot;
<a href="maxima_95.html#Category_003a-Declarations-and-inferences">Declarations and inferences</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-matchfix"></a>
</p><dl>
<dt><u>Function:</u> <b>matchfix</b><i> (<var>ldelimiter</var>, <var>rdelimiter</var>)</i>
<a name="IDX1290"></a>
</dt>
<dt><u>Function:</u> <b>matchfix</b><i> (<var>ldelimiter</var>, <var>rdelimiter</var>, <var>arg_pos</var>, <var>pos</var>)</i>
<a name="IDX1291"></a>
</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 &quot;matchfix&quot; 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><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>&quot;Part of speech&quot;, 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>&quot; &quot;</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>
<ul>
<li>
Delimiters may be almost any strings.
</li></ul>
<pre class="example">(%i1) matchfix (&quot;@@&quot;, &quot;~&quot;);
(%o1)                          @@
(%i2) @@ a, b, c ~;
(%o2)                      @@a, b, c~
(%i3) matchfix (&quot;&gt;&gt;&quot;, &quot;&lt;&lt;&quot;);
(%o3)                          &gt;&gt;
(%i4) &gt;&gt; a, b, c &lt;&lt;;
(%o4)                      &gt;&gt;a, b, c&lt;&lt;
(%i5) matchfix (&quot;foo&quot;, &quot;oof&quot;);
(%o5)                          foo
(%i6) foo a, b, c oof;
(%o6)                     fooa, b, coof
(%i7) &gt;&gt; w + foo x, y oof + z &lt;&lt; / @@ p, q ~;
                     &gt;&gt;z + foox, yoof + w&lt;&lt;
(%o7)                ----------------------
                            @@p, q~
</pre>
<ul>
<li>
Matchfix operators are ordinary user-defined functions.
</li></ul>
<pre class="example">(%i1) matchfix (&quot;!-&quot;, &quot;-!&quot;);
(%o1)                         &quot;!-&quot;
(%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 (&quot;!-&quot; (x, y), x/y - y/x);
                                    x   y
(%o4)                   !-x, y-! := - - -
                                    y   x
(%i5) dispfun (&quot;!-&quot;);
                                    x   y
(%t5)                   !-x, y-! := - - -
                                    y   x

(%o5)                         done
(%i6) !-3, 5-!;
                                16
(%o6)                         - --
                                15
(%i7) &quot;!-&quot; (3, 5);
                                16
(%o7)                         - --
                                15
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Syntax">Syntax</a>
 &middot;
<a href="maxima_95.html#Category_003a-Operators">Operators</a>
</p>
</div>


</dd></dl>

<p><a name="Item_003a-remlet"></a>
</p><dl>
<dt><u>Function:</u> <b>remlet</b><i> (<var>prod</var>, <var>name</var>)</i>
<a name="IDX1292"></a>
</dt>
<dt><u>Function:</u> <b>remlet</b><i> ()</i>
<a name="IDX1293"></a>
</dt>
<dt><u>Function:</u> <b>remlet</b><i> (all)</i>
<a name="IDX1294"></a>
</dt>
<dt><u>Function:</u> <b>remlet</b><i> (all, <var>name</var>)</i>
<a name="IDX1295"></a>
</dt>
<dd><p>Deletes the substitution rule, <var>prod</var> -&gt; repl, most
recently defined by the <code>let</code> function.  If name is supplied the rule is
deleted from the rule package name.
</p>
<p><code>remlet()</code> and <code>remlet(all)</code> delete all substitution rules from the current rule package.
If the name of a rule package is supplied,
e.g. <code>remlet (all, <var>name</var>)</code>, the rule package <var>name</var> is also deleted.
</p>
<p>If a substitution is to be changed using the same
product, <code>remlet</code> need not be called, just redefine the substitution
using the same product (literally) with the <code>let</code> function and the new
replacement and/or predicate name.  Should <code>remlet (<var>prod</var>)</code> now be
called the original substitution rule is revived.
</p>
<p>See also <code>remrule</code>, which removes a rule defined by <code>tellsimp</code> or <code>tellsimpafter</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-remrule"></a>
</p><dl>
<dt><u>Function:</u> <b>remrule</b><i> (<var>op</var>, <var>rulename</var>)</i>
<a name="IDX1296"></a>
</dt>
<dt><u>Function:</u> <b>remrule</b><i> (<var>op</var>, all)</i>
<a name="IDX1297"></a>
</dt>
<dd><p>Removes rules defined by <code>tellsimp</code> or <code>tellsimpafter</code>.
</p>
<p><code>remrule (<var>op</var>, <var>rulename</var>)</code>
removes the rule with the name <var>rulename</var> from the operator <var>op</var>.
When <var>op</var> is a built-in or user-defined operator
(as defined by <code>infix</code>, <code>prefix</code>, etc.),
<var>op</var> and <var>rulename</var> must be enclosed in double quote marks.
</p>
<p><code>remrule (<var>op</var>, all)</code> removes all rules for the operator <var>op</var>.
</p>
<p>See also <code>remlet</code>, which removes a rule defined by <code>let</code>.
</p>
<p>Examples:
</p>
<pre class="example">(%i1) tellsimp (foo (aa, bb), bb - aa);
(%o1)                   [foorule1, false]
(%i2) tellsimpafter (aa + bb, special_add (aa, bb));
(%o2)                   [+rule1, simplus]
(%i3) infix (&quot;@@&quot;);
(%o3)                          @@
(%i4) tellsimp (aa @@ bb, bb/aa);
(%o4)                   [@@rule1, false]
(%i5) tellsimpafter (quux (%pi, %e), %pi - %e);
(%o5)                  [quuxrule1, false]
(%i6) tellsimpafter (quux (%e, %pi), %pi + %e);
(%o6)             [quuxrule2, quuxrule1, false]
(%i7) [foo (aa, bb), aa + bb, aa @@ bb, quux (%pi, %e),
       quux (%e, %pi)];
                                     bb
(%o7) [bb - aa, special_add(aa, bb), --, %pi - %e, %pi + %e]
                                     aa
(%i8) remrule (foo, foorule1);
(%o8)                          foo
(%i9) remrule (&quot;+&quot;, ?\+rule1);
(%o9)                           +
(%i10) remrule (&quot;@@&quot;, ?\@\@rule1);
(%o10)                         @@
(%i11) remrule (quux, all);
(%o11)                        quux
(%i12) [foo (aa, bb), aa + bb, aa @@ bb, quux (%pi, %e),
        quux (%e, %pi)];
(%o12) [foo(aa, bb), bb + aa, aa @@ bb, quux(%pi, %e), 
                                         quux(%e, %pi)]
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-tellsimp"></a>
</p><dl>
<dt><u>Function:</u> <b>tellsimp</b><i> (<var>pattern</var>, <var>replacement</var>)</i>
<a name="IDX1298"></a>
</dt>
<dd><p>is similar to <code>tellsimpafter</code> but places
new information before old so that it is applied before the built-in
simplification rules.
</p>
<p><code>tellsimp</code> is used when it is important to modify
the expression before the simplifier works on it, for instance if the
simplifier &quot;knows&quot; something about the expression, but what it returns
is not to your liking.
If the simplifier &quot;knows&quot; something about the
main operator of the expression, but is simply not doing enough for
you, you probably want to use <code>tellsimpafter</code>.
</p>
<p>The pattern may not be a
sum, product, single variable, or number.
</p>
<p><code>rules</code> is the list of rules defined by
<code>defrule</code>, <code>defmatch</code>, <code>tellsimp</code>, and <code>tellsimpafter</code>.
</p>
<p>Examples:
</p>
<pre class="example">(%i1) matchdeclare (x, freeof (%i));
(%o1)                         done
(%i2) %iargs: false$
(%i3) tellsimp (sin(%i*x), %i*sinh(x));
(%o3)                 [sinrule1, simp-%sin]
(%i4) trigexpand (sin (%i*y + x));
(%o4)         sin(x) cos(%i y) + %i cos(x) sinh(y)
(%i5) %iargs:true$
(%i6) errcatch(0^0);
 0
0  has been generated
(%o6)                          []
(%i7) ev (tellsimp (0^0, 1), simp: false);
(%o7)                  [^rule1, simpexpt]
(%i8) 0^0;
(%o8)                           1
(%i9) remrule (&quot;^&quot;, %th(2)[1]);
(%o9)                           ^
(%i10) tellsimp (sin(x)^2, 1 - cos(x)^2);
(%o10)                 [^rule2, simpexpt]
(%i11) (1 + sin(x))^2;
                                      2
(%o11)                    (sin(x) + 1)
(%i12) expand (%);
                                   2
(%o12)               2 sin(x) - cos (x) + 2
(%i13) sin(x)^2;
                                  2
(%o13)                     1 - cos (x)
(%i14) kill (rules);
(%o14)                        done
(%i15) matchdeclare (a, true);
(%o15)                        done
(%i16) tellsimp (sin(a)^2, 1 - cos(a)^2);
(%o16)                 [^rule3, simpexpt]
(%i17) sin(y)^2;
                                  2
(%o17)                     1 - cos (y)
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-tellsimpafter"></a>
</p><dl>
<dt><u>Function:</u> <b>tellsimpafter</b><i> (<var>pattern</var>, <var>replacement</var>)</i>
<a name="IDX1299"></a>
</dt>
<dd><p>Defines a simplification rule which the Maxima simplifier
applies after built-in simplification rules.
<var>pattern</var> is an expression, comprising pattern variables (declared by <code>matchdeclare</code>)
and other atoms and operators, considered literals for the purpose of pattern matching.
<var>replacement</var> is substituted for an actual expression which matches <var>pattern</var>;
pattern variables in <var>replacement</var> are assigned the values matched in the actual expression.
</p>
<p><var>pattern</var> may be any nonatomic expression
in which the main operator is not a pattern variable;
the simplification rule is associated with the main operator.
The names of functions (with one exception, described below), lists, and arrays
may appear in <var>pattern</var> as the main operator only as literals (not pattern variables);
this rules out expressions such as <code>aa(x)</code> and <code>bb[y]</code> as patterns,
if <code>aa</code> and <code>bb</code> are pattern variables.
Names of functions, lists, and arrays which are pattern variables may appear as operators
other than the main operator in <var>pattern</var>.
</p>
<p>There is one exception to the above rule concerning names of functions.
The name of a subscripted function in an expression such as <code>aa[x](y)</code>
may be a pattern variable,
because the main operator is not <code>aa</code> but rather the Lisp atom <code>mqapply</code>.
This is a consequence of the representation of expressions involving subscripted functions.
</p>

<p>Simplification rules are applied after evaluation 
(if not suppressed through quotation or the flag <code>noeval</code>).
Rules established by <code>tellsimpafter</code> are applied in the order they were defined,
and after any built-in rules.
Rules are applied bottom-up, that is,
applied first to subexpressions before application to the whole expression.
It may be necessary to repeatedly simplify a result
(for example, via the quote-quote operator <code>''</code> or the flag <code>infeval</code>)
to ensure that all rules are applied.
</p>
<p>Pattern variables are treated as local variables in simplification rules.
Once a rule is defined, the value of a pattern variable
does not affect the rule, and is not affected by the rule.
An assignment to a pattern variable which results from a successful rule match
does not affect the current assignment (or lack of it) of the pattern variable.
However,
as with all atoms in Maxima,
the properties of pattern variables (as declared by <code>put</code> and related functions) are global.
</p>
<p>The rule constructed by <code>tellsimpafter</code> is named after the main operator of <var>pattern</var>.
Rules for built-in operators, 
and user-defined operators 
defined by <code>infix</code>, <code>prefix</code>, <code>postfix</code>, <code>matchfix</code>, and <code>nofix</code>,
have names which are Lisp identifiers.
Rules for other functions have names which are Maxima identifiers.
</p>
<p>The treatment of noun and verb forms is slightly confused. If a rule is defined for a noun (or verb) form
and a rule for the corresponding verb (or noun) form already exists, 
the newly-defined rule applies to both forms (noun and verb).
If a rule for the corresponding verb (or noun) form does not exist,
the newly-defined rule applies only to the noun (or verb) form.
</p>
<p>The rule constructed by <code>tellsimpafter</code> is an ordinary Lisp function.
If the name of the rule is <code>$foorule1</code>,
the construct <code>:lisp (trace $foorule1)</code> traces the function,
and <code>:lisp (symbol-function '$foorule1</code> displays its definition.
</p>
<p><code>tellsimpafter</code> quotes its arguments.
<code>tellsimpafter</code> returns the list of rules for the main operator of <var>pattern</var>,
including the newly established rule.
</p>
<p>See also <code>matchdeclare</code>, <code>defmatch</code>, <code>defrule</code>, <code>tellsimp</code>, <code>let</code>,
<code>kill</code>, <code>remrule</code>, and <code>clear_rules</code>.
</p>
<p>Examples:
</p>
<p><var>pattern</var> may be any nonatomic expression in which the 
main operator is not a pattern variable.
</p>
<pre class="example">(%i1) matchdeclare (aa, atom, [ll, mm], listp, xx, true)$
(%i2) tellsimpafter (sin (ll), map (sin, ll));
(%o2)                 [sinrule1, simp-%sin]
(%i3) sin ([1/6, 1/4, 1/3, 1/2, 1]*%pi);
                    1  sqrt(2)  sqrt(3)
(%o3)              [-, -------, -------, 1, 0]
                    2     2        2
(%i4) tellsimpafter (ll^mm, map (&quot;^&quot;, ll, mm));
(%o4)                  [^rule1, simpexpt]
(%i5) [a, b, c]^[1, 2, 3];
                                2   3
(%o5)                      [a, b , c ]
(%i6) tellsimpafter (foo (aa (xx)), aa (foo (xx)));
(%o6)                   [foorule1, false]
(%i7) foo (bar (u - v));
(%o7)                    bar(foo(u - v))
</pre>
<p>Rules are applied in the order they were defined.
If two rules can match an expression,
the rule which was defined first is applied.
</p>
<pre class="example">(%i1) matchdeclare (aa, integerp);
(%o1)                         done
(%i2) tellsimpafter (foo (aa), bar_1 (aa));
(%o2)                   [foorule1, false]
(%i3) tellsimpafter (foo (aa), bar_2 (aa));
(%o3)              [foorule2, foorule1, false]
(%i4) foo (42);
(%o4)                       bar_1(42)
</pre>
<p>Pattern variables are treated as local variables in simplification rules.
(Compare to <code>defmatch</code>, which treats pattern variables as global variables.)
</p>
<pre class="example">(%i1) matchdeclare (aa, integerp, bb, atom);
(%o1)                         done
(%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
(%o2)                   [foorule1, false]
(%i3) bb: 12345;
(%o3)                         12345
(%i4) foo (42, %e);
(%o4)                 bar(aa = 42, bb = %e)
(%i5) bb;
(%o5)                         12345
</pre>
<p>As with all atoms, properties of pattern variables are global even though values are local.
In this example, an assignment property is declared via <code>define_variable</code>.
This is a property of the atom <code>bb</code> throughout Maxima.
</p>
<pre class="example">(%i1) matchdeclare (aa, integerp, bb, atom);
(%o1)                         done
(%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
(%o2)                   [foorule1, false]
(%i3) foo (42, %e);
(%o3)                 bar(aa = 42, bb = %e)
(%i4) define_variable (bb, true, boolean);
(%o4)                         true
(%i5) foo (42, %e);
Error: bb was declared mode boolean, has value: %e
 -- an error.  Quitting.  To debug this try debugmode(true);
</pre>
<p>Rules are named after main operators.
Names of rules for built-in and user-defined operators are Lisp identifiers,
while names for other functions are Maxima identifiers.
</p>
<pre class="example">(%i1) tellsimpafter (foo (%pi + %e), 3*%pi);
(%o1)                   [foorule1, false]
(%i2) tellsimpafter (foo (%pi * %e), 17*%e);
(%o2)              [foorule2, foorule1, false]
(%i3) tellsimpafter (foo (%i ^ %e), -42*%i);
(%o3)         [foorule3, foorule2, foorule1, false]
(%i4) tellsimpafter (foo (9) + foo (13), quux (22));
(%o4)                   [+rule1, simplus]
(%i5) tellsimpafter (foo (9) * foo (13), blurf (22));
(%o5)                  [*rule1, simptimes]
(%i6) tellsimpafter (foo (9) ^ foo (13), mumble (22));
(%o6)                  [^rule1, simpexpt]
(%i7) rules;
(%o7) [foorule1, foorule2, foorule3, +rule1, *rule1, ^rule1]
(%i8) foorule_name: first (%o1);
(%o8)                       foorule1
(%i9) plusrule_name: first (%o4);
(%o9)                        +rule1
(%i10) remrule (foo, foorule1);
(%o10)                         foo
(%i11) remrule (&quot;^&quot;, ?\^rule1);
(%o11)                          ^
(%i12) rules;
(%o12)        [foorule2, foorule3, +rule1, *rule1]
</pre>
<p>A worked example: anticommutative multiplication.
</p>
<pre class="example">(%i1) gt (i, j) := integerp(j) and i &lt; j;
(%o1)           gt(i, j) := integerp(j) and i &lt; j
(%i2) matchdeclare (i, integerp, j, gt(i));
(%o2)                         done
(%i3) tellsimpafter (s[i]^^2, 1);
(%o3)                 [^^rule1, simpncexpt]
(%i4) tellsimpafter (s[i] . s[j], -s[j] . s[i]);
(%o4)                   [.rule1, simpnct]
(%i5) s[1] . (s[1] + s[2]);
(%o5)                    s  . (s  + s )
                          1     2    1
(%i6) expand (%);
(%o6)                      1 - s  . s
                                2    1
(%i7) factor (expand (sum (s[i], i, 0, 9)^^5));
(%o7) 100 (s  + s  + s  + s  + s  + s  + s  + s  + s  + s )
            9    8    7    6    5    4    3    2    1    0
</pre>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-clear_005frules"></a>
</p><dl>
<dt><u>Function:</u> <b>clear_rules</b><i> ()</i>
<a name="IDX1300"></a>
</dt>
<dd><p>Executes <code>kill (rules)</code> and then resets the next rule number to 1
for addition <code>+</code>, multiplication <code>*</code>, and exponentiation <code>^</code>.
</p>
<div class=categorybox>


<p>Categories:&nbsp;&nbsp;<a href="maxima_95.html#Category_003a-Rules-and-patterns">Rules and patterns</a>
</p>
</div>

</dd></dl>

<p><a name="Item_003a-Lists"></a>
</p><hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC160" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="maxima_37.html#SEC163" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="maxima.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="maxima_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="maxima_79.html#SEC331" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="maxima_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
 <font size="-1">
  This document was generated by <em>Robert Dodier</em> on <em>April, 24 2010</em> using <a href="http://texi2html.cvshome.org/"><em>texi2html 1.76</em></a>.
 </font>
 <br>

</p>
</body>
</html>