plot3d and expression evaluation

6 views
Skip to first unread message

stefan

unread,
Mar 12, 2010, 11:39:41 AM3/12/10
to sage-support
Hello group,

I have encountered a somewhat strange problem in plotting a simple
function. It seems to be related to the issues described in the
tutorial section "Some Common Issues with Functions", but the lambda-
function trick does not work here.

I have uploaded a worksheet with what I have been trying so far here:
http://www.sagenb.org/home/steja/1/

Basically, I am trying to plot this expression:

#sage> fermi(x,y,d,L) = 1 - 1/( exp( ( max(abs(x),abs(y))-L) /d) + 1)

as a function of (x,y) for some different values of d and L (nothing
too useful, I invented it as a toy project to get familiar with Sage).
I tried the following commands, but none of them works:

#sage> plot3d(lambda x,y: fermi(x,y,0.2,1),(x,-2,2),(y,-2,2))

#sage> def plfermi(x,y): return fermi(x,y,0.2,1)
#sage> plot3d(plfermi,(-2,2),(-2,2))

Both of these commands do plot something, but the result is wrong. I
also tried this:

#sage> pf(x,y) = fermi(x,y,0.2,1.0)
#sage> plot(pf,(-2,2),(-2,2))

Here, the result is an error message (which looks like Klingon
language to me).

Could somebody send some light down my way? I am almost dying to get
rid of Mathematica in favor of an open source CAS (because of their #*!
§ license that always seems to fail when one desperately needs it),
but I'm afraid I've still got a long way to go with Sage when I'm not
even able to plot a simple function...

Harald Schilly

unread,
Mar 12, 2010, 11:47:04 AM3/12/10
to sage-support
On Mar 12, 5:39 pm, stefan <stejane...@gmail.com> wrote:
> Basically, I am trying to plot this expression:
>
> #sage> fermi(x,y,d,L) = 1 - 1/( exp( ( max(abs(x),abs(y))-L) /d)  + 1)


I'm not sure but there might be a bug or problem evaluating the
expression. Anyways, going the "pure" python way works:

sage: def fermi(x,y,d,L): return 1 - 1/( exp( ( max(abs(x),abs(y))-
L) /d) + 1)

sage: plot3d(lambda x,y : fermi(x,y,0.2,1), (x,-2,2), (y,-2,2))

Does this look fine?

H

Harald Schilly

unread,
Mar 12, 2010, 11:49:11 AM3/12/10
to sage-support
On Mar 12, 5:39 pm, stefan <stejane...@gmail.com> wrote:
> I have encountered a somewhat strange problem ...

Ah, I got it, the max function is evaluated during the definition of
the function, look:

sage: fermi(x,y,d,L) = 1 - 1/( exp( ( max(abs(x),abs(y))-L) /d) + 1)
sage: fermi
(x, y, d, L) |--> -1/(e^(-(L - abs(x))/d) + 1) + 1

especially:
sage: max(abs(x),abs(y))
abs(x)

There is no y any more!

H

stefan

unread,
Mar 12, 2010, 12:11:12 PM3/12/10
to sage-support

On Mar 12, 5:47 pm, Harald Schilly <harald.schi...@gmail.com> wrote:
> I'm not sure but there might be a bug or problem evaluating the
> expression. Anyways, going the "pure" python way works:
>
> sage: def fermi(x,y,d,L): return  1 - 1/( exp( ( max(abs(x),abs(y))-
> L) /d)  + 1)
>
> sage: plot3d(lambda x,y : fermi(x,y,0.2,1), (x,-2,2), (y,-2,2))
>
> Does this look fine?

Yes, the "pure python" way works. But defining the function that way,
I loose the ability to perform symbolic manipulations with it, don't I?

stefan

unread,
Mar 12, 2010, 12:29:11 PM3/12/10
to sage-support
I think I've got a vague idea what happens here... The same thing also
happens when
I define the max() function myself:

sage: def my_max(x,y):
sage: if(x>y): return x
sage: else: return y

sage: fermi2(x,y,d,L) = 1 - 1/( exp( ( my_max(abs(x),abs(y))-L) /d) +
1)

sage: fermi2
(x, y, d, L) |--> -1/(e^(-(L - abs(y))/d) + 1) + 1

This looks a bit like max(x,y) gets passed the the objects 'abs(x)'
and 'abs(y)'
and decides, for whatever reason, that 'abs(x)' is not greater than
'abs(y)', so
the else-clause gets executed and we are left with abs(y). Does this
make sense to
you? (I'm just thinking aloud, I got no idea how sage actually does
it)

Most likely the internal "max" function is defined in rather the same
way (it
just returns abs(x) instead of abs(y). )

The bottom line seems to be that it is a very dangerous game to mix
symbolic expressions and python functions. Is there some way to
control
when (and how) Sage evaluates expressions and invokes python
functions?
Something like saying "first evaluate abs(x) and abs(y) and then
invoke the
python function max() with the result of the evaluation"?

Reply all
Reply to author
Forward
0 new messages