Ticket #10246 (closed defect: fixed)

Opened 3 years ago

Last modified 2 years ago

Can't get symbol from callable function x |--> x

Reported by: kcrisman Owned by: burcin
Priority: critical Milestone: sage-4.7
Component: symbolics Keywords: plot, beginner
Cc: jason Work issues:
Report Upstream: N/A Reviewers: Mike Hansen
Authors: Karl-Dieter Crisman Merged in: sage-4.7.alpha4
Dependencies: Stopgaps:

Description (last modified by kcrisman) (diff)

From  this ask.sagemath question:

I was plotting sin(x)/x and also wanted a seperate plot of sin(x) and x so I could visualize those components as well. However I get an error when plotting x saying "free variable x -> x". If I add a coefficient to h(x) such as h(x) = 1.0001*x then it works fine.

This is the code I am using:

f(x) = sin(x)/x
g(x) = sin(x)
h(x) = x

fig1 = plot(f,-10,10,rgbcolor='green')
fig2 = plot(g,-10,10,rgbcolor='purple')
fig3 = plot(h,-10,10,rgbcolor='red')

figure = fig1 + fig2 + fig3
show(figure,xmin=-10,xmax=10,ymin=-10,ymax=10,figsize=[8,8],fontsize=20)

The underlying problem is

The exception arises in `sage.symbolic.expression_conversions.FastFloatConverter.symbol`:

            try:
                return self.ff.fast_float_constant(float(ex))
            except TypeError:
                raise ValueError, "free variable: %s" % repr(ex)

so that it assumes if a callable expression doesn't have the variable, it must be a constant.  However, it before this asks for `if name in vars` and `if name in svars`, but here the `name` is `'x |--> x'`.

So what we need to do is fix it so that if the operator is `None` in the previous traceback in your error message, we check for this case explicitly, where the name isn't just one of the variables.

Apply attachment:trac_10246-callable-x.patch Download

Attachments

trac_10246-callable-x.patch Download (2.0 KB) - added by kcrisman 2 years ago.
Based on 4.7.alpha1

Change History

comment:1 Changed 3 years ago by jason

  • Cc jason added

comment:2 Changed 2 years ago by kcrisman

  • Keywords plot, beginner added; plot removed
  • Priority changed from major to critical

See also  this ask.sagemath.org question where exactly the same thing comes up.

I'm putting this as 'beginner', and moving to 'critical', though that is pretty meaningless... Anyway, it's really dumb that we can't plot x.

Changed 2 years ago by kcrisman

Based on 4.7.alpha1

comment:3 Changed 2 years ago by kcrisman

  • Status changed from new to needs_review
  • Description modified (diff)
  • Authors set to Karl-Dieter Crisman

Ready for review. This should fix the problem, and I tried to do it in a relatively elegant way. Unfortunately, there really is no such elegant way, since symbol shouldn't be relying on strings anyway, right? Oh well.

comment:4 Changed 2 years ago by mhansen

  • Status changed from needs_review to positive_review
  • Reviewers set to Mike Hansen

Looks good to me.

comment:5 Changed 2 years ago by jdemeyer

  • Status changed from positive_review to closed
  • Resolution set to fixed
  • Merged in set to sage-4.7.alpha4
Note: See TracTickets for help on using tickets.