Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting a Table requires explicit Evaluate #150

Closed
sn6uv opened this issue Sep 2, 2013 · 7 comments
Closed

Plotting a Table requires explicit Evaluate #150

sn6uv opened this issue Sep 2, 2013 · 7 comments
Labels

Comments

@sn6uv
Copy link
Member

sn6uv commented Sep 2, 2013

fs = Table[x^y, {y, 1, 5}];
Plot[fs, {x, -2, 2}]

Does not work unless used with Evaluate. e.g.

Plot[Evaluate[fs], {x, -2, 2}]

Reported here.

@GitAnt
Copy link
Contributor

GitAnt commented Mar 9, 2014

This problem is caused by the definition on line 140 in mathics/builtin/plot.py, changing it to
attributes = ()
solves it, dunno about the other consequences though. For sure the HoldAll attribute keeps the expression from being evaluated and it's never seen as a list at any point in the plot generation unless an evaluation of it is explicitly requested.

@bnjones
Copy link
Member

bnjones commented Mar 9, 2014

Is this actually a bug? https://reference.wolfram.com/mathematica/ref/Table.html gives an example of Plot with Table, saying "Evaluate is needed to force evaluation of the table before it is fed to Plot". It sounds like we're doing the right thing here.

@GitAnt
Copy link
Contributor

GitAnt commented Mar 9, 2014

The problem is there even if fs is defined as a List, for example fs = {x^2,x^3}. So Plot[fs,{x,xmin,xmax}] gives an empty plot while Plot[{x^2,x^3},{x,xmin,xmax}] gives the expected result. In Mathematica both commands give the same plot. I think I've done some progress on this (without touching the attributes = (HoldAll,) line, I realized that that line must stay like that), I'm waiting for the other pull request to be closed before pushing the changes related to this.

@bnjones
Copy link
Member

bnjones commented Mar 9, 2014

I did a bit of research on this - this might be obvious to people who are more familiar with Mathematica than me, but this question on Mathematica Stack Exchange explains that Plot[{f1, f2}, ...] will plot f1 and f2 as separate functions (in different colours), whereas f = {f1, f2}; Plot[f, ...] will plot f as one function that happens to return multiple values. A MathGroup thread linked from there has some more detail.

If I understand correctly, what this means for Mathics's implementation is that Plot should be able to plot functions that return a list of points.

edit - also, if we're asked to plot something we can't handle, it feels like we should issue a message rather than just return an empty plot!

@GitAnt
Copy link
Contributor

GitAnt commented Mar 9, 2014

From what I've see at the moment Plot in mathics does not handle function arguments, that is

f[x_]:=x^2
Plot[f[x],{x,1,2}]

does not give the expected result but I think it's a different issue respect to the one at hand. For this issue, mathics is able to plot lists of functions of the plot variable (say x) but cannot do this if such a list is "hidden" in a variable. Using Evaluate on the list before plotting is not a good solution because you can get the "wrong" result (i.e. different from mathematica) if you do something like

fs = {x^2,x^3}
x = 5
Plot[Evaluate[fs],{x,xmin,xmax}]

This would plot 2 constant functions at 25 and 125 while in mathematica

Plot[fs,{x,xmin,xmax}]

gives the expected plots since the "x" in fs is treated like a dummy variable. So Plot[Evaluate[fs],{x,xmin,xmax}] (in mathics) and Plot[fs,{x,xmin,xmax}] (in mathematica) are not equivalent.

@bnjones
Copy link
Member

bnjones commented Mar 9, 2014

From what I've see at the moment Plot in mathics does not handle function arguments, that is

f[x_]:=x^2
Plot[f[x],{x,1,2}]

This sounds like a pretty serious problem. Could you file a separate issue for this? I'm not seeing this on current master, so it'd be good to know which git SHA you're running on and the version of Python/SymPy/etc you're using. Thanks!

@GitAnt
Copy link
Contributor

GitAnt commented Mar 9, 2014

Sorry, there was a conflict of definitions in the notebook I was using, stopping the server and restarting it does not reset the definitions (dunno if this is normal) and sometimes I forget to give a Quit[] command in order to start from scratch. The commands

f[x_]:=x^2
Plot[f[x],{x,1,2}]

work, sorry for the false alarm, the rest is confirmed though.

sn6uv added a commit that referenced this issue Mar 17, 2014
Fixes behaviour of plot with defined symbols (addresses #150)
@sn6uv sn6uv closed this as completed Feb 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants