Changeset 7446:437ecc2db8e8


Ignore:
Timestamp:
11/30/07 22:51:34 (5 years ago)
Author:
Mike Hansen <mhansen@…>
Branch:
default
Message:

Fixed #644

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/calculus/calculus.py

    r7431 r7446  
    241241import operator 
    242242from sage.misc.latex import latex, latex_variable_name 
     243from sage.misc.misc import uniq as unique 
    243244from sage.structure.sage_object import SageObject 
    244245 
     
    32353236            self._r_assoc = True 
    32363237 
     3238    def __call__(self, *args, **kwargs): 
     3239        """ 
     3240        EXAMPLES: 
     3241            sage: x,y,z=var('x,y,z') 
     3242             
     3243            sage: h = sin + cos 
     3244            sage: h(1) 
     3245            sin(1) + cos(1) 
     3246            sage: h(x) 
     3247            sin(x) + cos(x) 
     3248            sage: h = 3*sin 
     3249            sage: h(1) 
     3250            3*sin(1) 
     3251            sage: h(x) 
     3252            3*sin(x) 
     3253 
     3254            sage: (sin+cos)(1) 
     3255            sin(1) + cos(1) 
     3256            sage: (sin+1)(1) 
     3257            sin(1) + 1 
     3258            sage: (x+sin)(5) 
     3259            sin(5) + 5 
     3260            sage: (y+sin)(5) 
     3261            sin(5) + 5 
     3262            sage: (x+y+sin)(5) 
     3263            y + sin(5) + 5 
     3264 
     3265 
     3266            sage: f = x + 2*y + 3*z 
     3267            sage: f(1) 
     3268            3*z + 2*y + 1 
     3269            sage: f(0,1) 
     3270            3*z + 2 
     3271            sage: f(0,0,1) 
     3272            3 
     3273 
     3274 
     3275        """ 
     3276 
     3277        if kwargs and args: 
     3278            raise ValueError, "args and kwargs cannot both be specified" 
     3279 
     3280        if len(args) == 1 and isinstance(args[0], dict): 
     3281            kwargs = dict(map(lambda x: (repr(x[0]), x[1]), args[0].iteritems())) 
     3282 
     3283        if kwargs: 
     3284            #Handle the case where kwargs are specified 
     3285            x = var('x') 
     3286             
     3287            new_ops = [] 
     3288            for op in self._operands: 
     3289                try: 
     3290                    new_ops.append( op(**kwargs) ) 
     3291                except ValueError: 
     3292                    new_ops.append(op) 
     3293 
     3294             
     3295 
     3296        else: 
     3297            #Handle the case where args are specified 
     3298 
     3299            #Get all the variables 
     3300            variables = list( self.variables() ) 
     3301 
     3302            if len(args) > len(variables) and len(args) > 1: 
     3303                raise ValueError, "the number of arguments must be less than or equal to %s"%len(variables) 
     3304             
     3305            new_ops = [] 
     3306            for op in self._operands: 
     3307                try: 
     3308                    op_vars = op.variables() 
     3309                    if len(op_vars) == 0: 
     3310                        new_ops.append( op(args[0]) ) 
     3311                        continue 
     3312                    else: 
     3313                        indices = filter(lambda i: i < len(args), map(variables.index, op_vars)) 
     3314                        if len(indices) == 0: 
     3315                            new_ops.append( op ) 
     3316                        else: 
     3317                            new_ops.append( op(*[args[i] for i in indices]) ) 
     3318                except ValueError: 
     3319                    new_ops.append( op ) 
     3320 
     3321        #Check to see if all of the new_ops are symbolic constants 
     3322        #If so, then we should return a symbolic constant. 
     3323        new_ops = map(SR, new_ops) 
     3324        is_constant = all(map(lambda x: isinstance(x, SymbolicConstant), new_ops)) 
     3325        if is_constant: 
     3326            return SymbolicConstant( self._operator(*map(lambda x: x._obj, new_ops)) ) 
     3327        else: 
     3328            return self._operator(*new_ops) 
     3329 
     3330 
    32373331    def _recursive_sub(self, kwds): 
    32383332        """ 
     
    36823776    def _coerce_impl(self, x): 
    36833777        if isinstance(x, SymbolicExpression): 
     3778            if isinstance(x, PrimitiveFunction): 
     3779                return CallableSymbolicExpression(self, x( *self._args )) 
    36843780            if isinstance(x, CallableSymbolicExpression): 
    36853781                x = x._expr 
     
    38783974            sage: g + f 
    38793975            (x, n, m, y, z) |--> z^m + y^m + 2*x^n 
     3976 
     3977            sage: f(x) = x^2 
     3978            sage: f+sin 
     3979            x |--> sin(x) + x^2 
     3980            sage: g(y) = y^2 
     3981            sage: g+sin 
     3982            y |--> sin(y) + y^2 
     3983            sage: h = g+sin 
     3984            sage: h(2) 
     3985            sin(2) + 4 
    38803986        """ 
    38813987        if isinstance(right, CallableSymbolicExpression): 
Note: See TracChangeset for help on using the changeset viewer.