~ubuntu-branches/ubuntu/karmic/maxima/karmic

« back to all changes in this revision

Viewing changes to share/contrib/opsubst.lisp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-07-06 17:04:52 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060706170452-j9ypoqc1kjfnz221
Tags: 5.9.3-1ubuntu1
* Re-sync with Debian
* Comment out backward-delete-char-untabify in maxima.el (Closes Malone #5273)
* debian/control: build-dep automake -> automake1.9 (Closes BTS: #374663)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#|
 
2
Usage: The function 'opsubst' is similar to the function 'subst', except that
 
3
'opsubst' only makes substitutions for the operators in an expression. Specifically,
 
4
 
 
5
 opsubst(f,g,e) --> When 'f' is an operator in the expression e, substitute 'g' 
 
6
                    for 'f' in the expression 'e'. 
 
7
 opsubst(g=f,e) --> opsubst(f,g,e).
 
8
 opsubst([],e) --> e.
 
9
 opsubst([g1=f1, g2=f2, ..., gn=fn],e) --> opsubst([g2=f2,...,gn=fn], opsubst(f1=f1, e)).
 
10
 
 
11
 Examples:
 
12
 
 
13
(%i1) opsubst(f,g,g(g(x)));
 
14
(%o1) f(f(x))
 
15
(%i2) opsubst(f,g,g(g));
 
16
(%o2) f(g)
 
17
(%i3) opsubst(f,g[x],g[x](z));
 
18
(%o3) f(z)
 
19
(%i4) opsubst(g[x],f, f(z));
 
20
(%o4) g[x](z)
 
21
(%i5) opsubst(tan, sin, sin(sin));
 
22
(%o5) tan(SIN)
 
23
(%i6) opsubst([f=g,g=h],f(x));
 
24
(%o6) h(x)
 
25
 
 
26
To determine the operator, 'opsubst' sets 'inflag' to true. This means 
 
27
'opsubst' substitutes for the internal, not the displayed, operator
 
28
in the expression. Internally, Maxima does not use the unary negation,
 
29
division, or the subtraction operators; thus:
 
30
 
 
31
(%i1) opsubst("+","-",a-b);
 
32
(%o1) a-b
 
33
(%i2) opsubst("f","-",-a);
 
34
(%o2) -a
 
35
(%i3) opsubst("^^","//",a/b);
 
36
(%o3) a/b
 
37
 
 
38
The internal representation of -a*b is *(-1,a,b); thus
 
39
 
 
40
(%i4) opsubst("[","*", -a*b);
 
41
(%o4) [-1,a,b]
 
42
 
 
43
 
 
44
When either operator isn't a Maxima symbol, generally some other function
 
45
will signal an error:
 
46
 
 
47
(%i5) opsubst(a+b,f, f(x));
 
48
Improper name or value in functional position:b+a
 
49
 
 
50
However, subscripted operators are allowed:
 
51
 
 
52
(%i6) opsubst(g[5],f, f(x));
 
53
(%o6) g[5](x)
 
54
 
 
55
|#
 
56
 
 
57
(defun $opsubst (&rest q)
 
58
  (let ((e))
 
59
    (cond ((= 3 (length q)) (apply 'op-subst q))
 
60
          ((= 2 (length q))
 
61
           (setq e (second q))
 
62
           (setq q (if ($listp (first q)) (margs (first q)) (list (first q))))
 
63
           (dolist (qi q e)
 
64
             (if (op-equalp qi 'mequal) (setq e (op-subst ($rhs qi) ($lhs qi) e))
 
65
               (merror "Expected an expression of the form `a = b'; instead found ~:M" qi))))
 
66
          (t (wna-err '$opsubst)))))
 
67
 
 
68
(defun op-subst (f g e)
 
69
  (let (($inflag t))
 
70
    (if ($mapatom e) e
 
71
      (mfuncall '$apply (if (like g ($op e)) f ($op e))
 
72
                (cons '(mlist) (mapcar #'(lambda (s) (op-subst f g s)) (margs ($args e))))))))
 
73