Ticket #11974: trac_11974.patch

File trac_11974.patch, 3.6 KB (added by jason, 10 years ago)
  • sage/plot/plot3d/plot3d.py

    # HG changeset patch
    # User Jason Grout <jason.grout@drake.edu>
    # Date 1320159619 18000
    # Node ID b39c30aacf5437cfb459d0835b716e9034e30245
    # Parent  816af9579407b6bc1bcf7061c446feaf6e633fb8
    Convert transformed functions to float before trying to substitute into symbolic expressions.
    
    This takes care of cases where the function might return an object that is not a float, but can be cast into a float.  For example, see http://ask.sagemath.org/question/858/2d-interpolating-function-from-numpy-arrays-to and the new doctest.
    
    diff --git a/sage/plot/plot3d/plot3d.py b/sage/plot/plot3d/plot3d.py
    a b  
    171171            sage: T.to_cartesian(f, [x, y])
    172172            (x + y, x - y, 2*x + y)
    173173            sage: [h(1,2) for h in T.to_cartesian(lambda x,y: 2*x+y)]
    174             [3, -1, 4]
     174            [3.0, -1.0, 4.0]
    175175           
    176176        We try to return a function having the same variable names as
    177177        the function passed in::
     
    208208            sage: inspect.getargspec(t1)
    209209            ArgSpec(args=['u', 'v'], varargs=None, keywords=None, defaults=None)
    210210            sage: [h(1,2) for h in T.to_cartesian(operator.mul)]
    211             [3, -1, 2]
     211            [3.0, -1.0, 2.0]
    212212            sage: [h(u=1,v=2) for h in T.to_cartesian(operator.mul)]
    213             [3, -1, 2]
     213            [3.0, -1.0, 2.0]
     214
     215        The output of the function `func` is coerced to a float when
     216        it is evaluated if the function is something like a lambda or
     217        python callable. This takes care of situations like f returning a
     218        singleton numpy array, for example.
     219
     220            sage: from numpy import array
     221            sage: v_phi=array([ 0.,  1.57079637,  3.14159274, 4.71238911,  6.28318548])
     222            sage: v_theta=array([ 0.,  0.78539819,  1.57079637,  2.35619456,  3.14159274])
     223            sage: m_r=array([[ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 0.55282422],
     224            ... [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 0.55282422],
     225            ... [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 0.55282422],
     226            ... [ 0.16763356,  0.19993708,  0.31403568,  0.47359696, 0.55282422],
     227            ... [ 0.16763356,  0.25683223,  0.16649297,  0.10594339, 0.55282422]])
     228            sage: import scipy.interpolate
     229            sage: f=scipy.interpolate.RectBivariateSpline(v_phi,v_theta,m_r)
     230            sage: spherical_plot3d(f,(0,2*pi),(0,pi))
    214231
    215232        """
    216233        from sage.symbolic.expression import is_Expression
     
    244261                # We use eval so that the lambda function has the same
    245262                # variable names as the original function
    246263                ll="""lambda {x},{y}: t.subs({{
    247                     dep_var_dummy: func({x}, {y}),
    248                     indep_var_dummies[0]: {x},
    249                     indep_var_dummies[1]: {y}
     264                    dep_var_dummy: float(func({x}, {y})),
     265                    indep_var_dummies[0]: float({x}),
     266                    indep_var_dummies[1]: float({y})
    250267                }})""".format(x=params[0], y=params[1])
    251268                return eval(ll,dict(t=t, func=func, dep_var_dummy=dep_var_dummy,
    252269                                    indep_var_dummies=indep_var_dummies))
     
    351368            sage: T.to_cartesian(f, [x, y])               
    352369            (x + y, x - y, 2*x + y)
    353370            sage: [h(1,2) for h in T.to_cartesian(lambda x,y: 2*x+y)]
    354             [3, -1, 4]
     371            [3.0, -1.0, 4.0]
    355372        """
    356373        self.dep_var = str(dep_var)
    357374        self.indep_vars = [str(i) for i in indep_vars]