Changeset 7718:ab7a62ccfb10


Ignore:
Timestamp:
12/11/07 12:10:14 (6 years ago)
Author:
William Stein <wstein@…>
Branch:
default
Message:

Trac #1461 -- sub expressions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/calculus/calculus.py

    r7717 r7718  
    28212821         
    28222822    ################################################################### 
     2823    # Expression substitution 
     2824    ################################################################### 
     2825    def subs_expr(self, *equations): 
     2826        """ 
     2827        Given a dictionary of key:value pairs, substitute all occurences 
     2828        of key for value in self. 
     2829 
     2830        WARNING: This is a formal pattern substitution, which may or 
     2831        may not have any mathematical meaning.  The exact rules used 
     2832        at present in Sage are determined by Maxima's subst command. 
     2833        Sometimes patterns are replaced even though one would think 
     2834        they should be -- see examples below. 
     2835 
     2836        EXAMPLES: 
     2837            sage: f = x^2 + 1 
     2838            sage: f.subs_expr(x^2 == x) 
     2839            x + 1 
     2840 
     2841            sage: var('x,y,z'); f = x^3 + y^2 + z 
     2842            (x, y, z) 
     2843            sage: f.subs_expr(x^3 == y^2, z == 1) 
     2844            2*y^2 + 1 
     2845             
     2846            sage: f = x^2 + x^4 
     2847            sage: f.subs_expr(x^2 == x) 
     2848            x^4 + x 
     2849            sage: f = cos(x^2) + sin(x^2) 
     2850            sage: f.subs_expr(x^2 == x) 
     2851            sin(x) + cos(x) 
     2852 
     2853            sage: f(x,y,t) = cos(x) + sin(y) + x^2 + y^2 + t 
     2854            sage: f.subs_expr(y^2 == t) 
     2855            (x, y, t) |--> sin(y) + cos(x) + x^2 + 2*t 
     2856 
     2857        The following seems really weird, but it *is* what maple does: 
     2858            sage: f.subs_expr(x^2 + y^2 == t) 
     2859            (x, y, t) |--> sin(y) + y^2 + cos(x) + x^2 + t         
     2860            sage: maple.eval('subs(x^2 + y^2 = t, cos(x) + sin(y) + x^2 + y^2 + t)') 
     2861            'cos(x)+sin(y)+x^2+y^2+t' 
     2862            sage: maxima.eval('cos(x) + sin(y) + x^2 + y^2 + t, x^2 + y^2 = t') 
     2863            'sin(y)+y^2+cos(x)+x^2+t' 
     2864 
     2865        Actually Mathematica does something that makes more sense: 
     2866            sage: mathematica.eval('Cos[x] + Sin[y] + x^2 + y^2 + t /. x^2 + y^2 -> t') 
     2867            2 t + Cos[x] + Sin[y] 
     2868        """ 
     2869        for x in equations: 
     2870            if not isinstance(x, SymbolicEquation): 
     2871                raise TypeError, "each expression must be an equation" 
     2872        R = self.parent() 
     2873        v = ','.join(['%s=%s'%(x.lhs()._maxima_init_(), x.rhs()._maxima_init_()) \ 
     2874                      for x in equations]) 
     2875        return R(self._maxima_().subst(v)) 
     2876 
     2877    ################################################################### 
    28232878    # Real and imaginary parts 
    28242879    ################################################################### 
Note: See TracChangeset for help on using the changeset viewer.