plot3d using real-valued real-parameter functions but involving complex numbers as intermediates

162 views
Skip to first unread message

GaryMak

unread,
Apr 16, 2013, 9:57:40 AM4/16/13
to sage-s...@googlegroups.com
Hi

I am trying to use plot functions for the first time in sage - apologies if this is a dumb question for a change.

I have a square matrix M of fixed complex numbers which are then all multiplied by a different phase depending on which column they are in. (In the 2x2 example below M=[a,b;c,d] and column 1 is multiplied by exp(i*theta) and column 2 is multiplied by exp(i*phi) ). I then take another fixed matrix A away from M and take f=det(M-A).abs() as my output. I have explicitly written this formula for A = [1,1;0,0] below. So the function I would like to plot is f as a function of theta and phi (in this case).

a = 1/2
b = - i/3
c = 1/4
d = i/5
theta, phi = var('theta,phi')
f(theta,phi) = ( (a*exp(i*theta)-1)*d*exp(i*phi) - (b*exp(i*phi)-1)*c*exp(i*theta) ) . abs()
plot3d(f, (theta,0,pi), (phi,0,pi))

No matter how I try to do this, even if I explicitly write out the complex conjugates of each element, I get what for me is a record-length screed of error stuff ending in 'TypeError: unable to coerce to a real number', originating in the command 'plot3d(...etc...)'. I have attached a text file with the full output of one of my attempts.

I can see that in a way I am asking a lot of the interpreter; but then again I feel I have explicitly made the thing real by the time it gets to be an input for the plot3d function.

Is there any way around this, or do I have to start writing complexes as a+bi etc and do it the hard way?

Many thanks

Gary

ploterror.txt

P Purkayastha

unread,
Apr 16, 2013, 8:54:35 PM4/16/13
to sage-s...@googlegroups.com
The problem is actually in the conversion of your function to a
fast_float() version of the function. Apparently, it fails for symbolic
"complex" functions (which are not really complex) even in this trivial
example:

f(theta, phi) = (theta + i * phi).abs()
ff = fast_float(f, 'theta', 'phi')

One trivial workaround is to define your function to be a python
function. Somehow, it works. :)

def f(theta,phi): return ( (a*exp(i*theta)-1)*d*exp(i*phi) -
(b*exp(i*phi)-1)*c*exp(i*theta) ) . abs()

plot3d(f, (theta, 0, pi), (phi, 0, phi))


kcrisman

unread,
Apr 16, 2013, 9:59:31 PM4/16/13
to sage-s...@googlegroups.com

ppurka, can you try the patch at #13355 to see if that helps in this case?

GaryMak

unread,
Apr 17, 2013, 4:39:27 AM4/17/13
to sage-s...@googlegroups.com
Hi @ppurka - thank you very much as I would never have thought of that! - that certainly fixes the crashing problem - but I now have an even more bizarre problem, which is that plot3d does literally nothing! I have re-booted everything but it makes no difference. Here is my code:

s = 4
a=1/s
b=1/s
c=1/s
d=i/s
theta, phi = var('theta,phi')
def f(theta,phi): return ( (a*exp(i*theta)-1)*d*exp(i*phi) - (b*exp(i*phi)-1)*c*exp(i*theta) ) . abs()
plot3d(f,(theta,0,pi),(phi,0,pi))

I know that plot3d works in the same notebook() session say on examples from the SAGE Reference manual, and the function f as you defined it also works fine on individual (pairs of) angles ... does this code work on your computer?

Thanks again

Gary McConnell

unread,
Apr 17, 2013, 5:11:53 AM4/17/13
to sage-s...@googlegroups.com
Sorry - at ease ... 

If I name the plot PP or something then put just 'PP' in a different cell from its definition, it shows up fine.

Thanks again for the help

Kind regards

Gary


--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/3mekDq5Stvk/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gary McConnell

unread,
Apr 17, 2013, 7:05:11 AM4/17/13
to sage-s...@googlegroups.com
OK I have now uncovered another weird sage-python problem. I think I should be the go-to guy to wreck otherwise perfectly healthy code :).

If you try to use the function minimize() with the python function @ppurka defined above then you get the error

TypeError: g() takes exactly 2 arguments (1 given)

whereas if I try to do the minimize using a SAGE function as per my original message, we get back to the same "coercion to real" problem. Here is some dumb code to show the problem:

vars = var('x y')
def gg(x,y): return sin(x) + cos(y)
minimize(gg,[0,0])

gives the above error; whereas:

vars = var('x y')
gg = sin(x) + cos(y)
minimize(gg,[0,0])

works fine .... I cannot see anywhere in the reference manual where the syntax for minimize() for python functions, should be different from that for SAGE functions right?!

Kind regards

Gary

PS using SAGE 5.7 on VM version 4.2.10 on Windows 7 on an HP notebook

P Purkayastha

unread,
Apr 17, 2013, 7:09:43 AM4/17/13
to sage-s...@googlegroups.com
On 04/17/2013 07:05 PM, Gary McConnell wrote:
> OK I have now uncovered another weird sage-python problem. I think I
> should be the go-to guy to wreck otherwise perfectly healthy code :).
>
> If you try to use the function minimize() with the python function
> @ppurka defined above then you get the error
>
> TypeError: g() takes exactly 2 arguments (1 given)
>
>
> whereas if I try to do the minimize using a SAGE function as per my
> original message, we get back to the same "coercion to real" problem.
> Here is some dumb code to show the problem:
>
> vars = var('x y')
> def gg(x,y): return sin(x) + cos(y)
> minimize(gg,[0,0])
>
> gives the above error; whereas:
>
> vars = var('x y')
> gg = sin(x) + cos(y)
> minimize(gg,[0,0])
>
> works fine .... I cannot see anywhere in the reference manual where the
> syntax for minimize() for python functions, should be different from
> that for SAGE functions right?!
>
> Kind regards
>
> Gary

Hello Gary,

The minimize function takes in a function which has only *one*
argument. That argument itself can be tuple or list. So, this very
simple modification to your code works:

sage: def gg(x): return sin(x[0]) + cos(x[1])
sage: minimize(gg,[0,0])
Optimization terminated successfully.
Current function value: -2.000000
Iterations: 72
Function evaluations: 139
(-1.5707636272, 3.14159570731)

basu.

Gary McConnell

unread,
Apr 17, 2013, 7:16:39 AM4/17/13
to sage-s...@googlegroups.com
Ah I see now that this is implicit in the docs example ... thank you ... should we perhaps point that out explicitly, since the very same function takes two rather different syntaxes? I am happy to write a small amendment to the page.


--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/3mekDq5Stvk/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to sage-support+unsubscribe@googlegroups.com.

P Purkayastha

unread,
Apr 17, 2013, 9:42:55 AM4/17/13
to sage-s...@googlegroups.com
On 04/17/2013 07:16 PM, Gary McConnell wrote:
> Ah I see now that this is implicit in the docs example ... thank you ...
> should we perhaps point that out explicitly, since the very same
> function takes two rather different syntaxes? I am happy to write a
> small amendment to the page.

You are welcome to improve the docs. If it was confusing to you it will
be confusing to someone else too in the future. So, if you have some
ideas how to fix/enhance the documentation, please go ahead and submit a
patch. :)


P Purkayastha

unread,
Apr 17, 2013, 10:42:22 AM4/17/13
to sage-s...@googlegroups.com
On 04/17/2013 09:59 AM, kcrisman wrote:
>
> ppurka, can you try the patch at #13355 to see if that helps in this case?

No, that doesn't work either. :(


Gary McConnell

unread,
Apr 17, 2013, 3:13:12 PM4/17/13
to sage-s...@googlegroups.com
I am very keen to help - my problem is utter incompetence at following the high-level instructions in the manual for developers. Also I am on the VM and I cannot access the sage directories directly. Is there a way you could send me a manual file that I could modify and send back to you? Sorry :( I promise I will learn this one day ...

the page I was referring to is

Also there are presumably many functions to which this syntax difference applies ... how can I find them?

Thanks!


P Purkayastha

unread,
Apr 20, 2013, 1:00:34 PM4/20/13
to sage-s...@googlegroups.com
On 04/18/2013 03:13 AM, Gary McConnell wrote:
> I am very keen to help - my problem is utter incompetence at following
> the high-level instructions in the manual for developers. Also I am on
> the VM and I cannot access the sage directories directly. Is there a way
> you could send me a manual file that I could modify and send back to
> you? Sorry :( I promise I will learn this one day ...
>
> the page I was referring to is
> http://www.sagemath.org/doc/reference/numerical/sage/numerical/optimize.html#sage.numerical.optimize.minimize
>
> Also there are presumably many functions to which this syntax difference
> applies ... how can I find them?
>
> Thanks!

I failed to notice this reply earlier.

I think it is better if you can submit a patch to trac. The patch is
generated by mercurial and it can automatically populate the author
attribution and other information. These information is used by a
patchbot that automatically tests the patches for validity and performs
other tests.

To generate the patch, you can either install the sage library code.

If you don't want to install the sage from source, you can download the
sage spkg and work with that. This is OK since you intend to do only
documentation changes. In this case, follow the steps below:

1. download the sage spkg from
http://www.sagemath.org/packages/standard/sage-5.8.spkg
This contains the source code.

2. Create a file in $HOME/.hgrc containing your name as mentioned here:
http://www.sagemath.org/doc/developer/producing_patches.html

3. Open a ticket in trac so that you can get the ticket number. Let's
say the number is 12345.

4. Use the inbuilt mercurial inside Sage to create the patch.
i) First change directory to the unpacked sage/sage directory
ii) Next, run the command (where /path/to/sage is where the sage script
is present and 12345 is the ticket number)
/path/to/sage -hg qnew 12345_documentation.patch
iii) Hack hack hack and make your changes :)
You will need to follow the developer documentation for syntax and
formatting.
iv) Now that you are satisfied with your changes, run
/path/to/sage -hg qref -m "a one line description of your changes"
v) Make hg generate the patch:
/path/to/sage -hg export qtip > ~/$(/path/to/sage -hg qtop)

5. Go back to the ticket and upload the patch that is present at
~/12345_documentation.patch

Hopefully, this procedure will become much simpler after the move to
git, since trivial changes can be made directly in github (if the
project remains hosted in github).

Gary McConnell

unread,
Apr 21, 2013, 7:14:57 PM4/21/13
to sage-s...@googlegroups.com
Dear @ppurka

thanks so much - i am now finally back at home with my trusty Macs and hopefully will be able to do the above, eventually .... :)

For me this is entering an exciting new phase! Thanks again.

Best regards

Gary


GaryMak

unread,
Apr 25, 2013, 10:20:27 AM4/25/13
to sage-s...@googlegroups.com
Hi again

the following is driving me absolutely nuts. I thought i had implemented all of the previous lessons you guys have taught me, but no matter how I re-wire this I cannot get it to work. The function f works fine btw, as a standalone function. But the minimize thing gives me all sorts of errors depending on how i attempt to use it:

vs = VectorSpace(CC,2)
u = vs([1+I,1-I])
v = vs([3-2*I,6])
vars = var('t')
def f(t): return (vs(u-exp(I*t)*v)).norm(2)
minimize( f , [0] )

Sorry as always if I am missing something obvious but I really don't get this!!

Thanks, as ever

Gary

Gary McConnell

unread,
Apr 26, 2013, 9:05:12 AM4/26/13
to sage-s...@googlegroups.com
Hi

OK I have successfully raised trac tkt 14493, but that's about it. I'm afraid I am so unused to this stuff that I cannot even follow the instructions above. I tried to download 5.8 from that link, but it failed to install. But I'm not even sure I was meant to install it! ie maybe I just need the code stored somewhere - not sure what you meant ... (I already have 5.7 working in this machine). I set up the file from step 2, and then from step 4 I changed directory to Applications/sage but Applications/sage/sage is a file not a directory. So I ran "./sage -hg qnew 14493_documentation.patch" and it quietly blinked for a second and then gave me a new command line - apparently doing absolutely nothing! So when you say hack hack hack etc :) I am not sure what I am supposed to be hacking!!

Sorry if this is moronic, but I'll get there eventually ....


P Purkayastha

unread,
Apr 26, 2013, 11:10:11 PM4/26/13
to sage-s...@googlegroups.com
On 04/26/2013 09:05 PM, Gary McConnell wrote:
> Hi
>
> OK I have successfully raised trac tkt 14493, but that's about it. I'm
> afraid I am so unused to this stuff that I cannot even follow the
> instructions above. I tried to download 5.8 from that link, but it
> failed to install. But I'm not even sure I was meant to install it! ie
> maybe I just need the code stored somewhere - not sure what you meant
> ... (I already have 5.7 working in this machine). I set up the file from
> step 2, and then from step 4 I changed directory to Applications/sage
> but Applications/sage/sage is a file not a directory. So I ran "./sage
> -hg qnew 14493_documentation.patch" and it quietly blinked for a second
> and then gave me a new command line - apparently doing absolutely
> nothing! So when you say hack hack hack etc :) I am not sure what I am
> supposed to be hacking!!
>
> Sorry if this is moronic, but I'll get there eventually ....

Hello,
No, I outlined the instructions below if you don't want to install
the sage source. After the step 4 ii) you will get no output. But you
can check that the patch file has been created by using the command

/path/to/sage -hg qser

Note that I wrote /path/to/sage because it is supposed to be the sage
script from your existing sage installation, NOT the unpacked sage-5.8
directory.

By "hack hack", I meant that you just carry on with the changes you want
to perform on the actual files. The hg will keep track of the changes.
You can see a diff of the changes when you run the command

/path/to/sage -hg qdiff

Hope that helps,
basu.

>
> If you don't want to install the sage from source, you can download
> the sage spkg and work with that. This is OK since you intend to do
> only documentation changes. In this case, follow the steps below:
>
> 1. download the sage spkg from
> http://www.sagemath.org/__packages/standard/sage-5.8.__spkg
> <http://www.sagemath.org/packages/standard/sage-5.8.spkg>
> This contains the source code.
>
> 2. Create a file in $HOME/.hgrc containing your name as mentioned here:
> http://www.sagemath.org/doc/__developer/producing_patches.__html
> <http://www.sagemath.org/doc/developer/producing_patches.html>
>
> 3. Open a ticket in trac so that you can get the ticket number.
> Let's say the number is 12345.
>
> 4. Use the inbuilt mercurial inside Sage to create the patch.
> i) First change directory to the unpacked sage/sage directory
> ii) Next, run the command (where /path/to/sage is where the sage
> script is present and 12345 is the ticket number)
> /path/to/sage -hg qnew 12345_documentation.patch
> iii) Hack hack hack and make your changes :)
> You will need to follow the developer documentation for syntax and
> formatting.
> iv) Now that you are satisfied with your changes, run
> /path/to/sage -hg qref -m "a one line description of your changes"
> v) Make hg generate the patch:
> /path/to/sage -hg export qtip > ~/$(/path/to/sage -hg qtop)
>
> 5. Go back to the ticket and upload the patch that is present at
> ~/12345_documentation.patch
>
> Hopefully, this procedure will become much simpler after the move to
> git, since trivial changes can be made directly in github (if the
> project remains hosted in github).
>
>
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "sage-support" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/__topic/sage-support/__3mekDq5Stvk/unsubscribe?hl=en
> <https://groups.google.com/d/topic/sage-support/3mekDq5Stvk/unsubscribe?hl=en>.
> To unsubscribe from this group and all its topics, send an email to
> sage-support+unsubscribe@__googlegroups.com
> <mailto:sage-support%2Bunsu...@googlegroups.com>.
> To post to this group, send email to sage-s...@googlegroups.com
> <mailto:sage-s...@googlegroups.com>.
> Visit this group at
> http://groups.google.com/__group/sage-support?hl=en
> <http://groups.google.com/group/sage-support?hl=en>.
> For more options, visit https://groups.google.com/__groups/opt_out
> <https://groups.google.com/groups/opt_out>.
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sage-support...@googlegroups.com.

Gary McConnell

unread,
May 2, 2013, 3:14:30 PM5/2/13
to sage-s...@googlegroups.com
hi Basu

really sorry about this - I've just gotten around to trying this again - but I am afraid I simply don't understand the most basic things of what I have to do. You are right that indeed the patch file thingo seems to have been created. So I went in search of these mysterious files and tried to unpack the 5.8 stuff, but all it does is try to install it, which crashes (please see attached screenshot) ... and obviously if I try to open the .spkg thing itself inside an editor I just get utter gibberish ...

Also it has been over 20 years since I last used vi or emacs ... is there a window-based OS X text editor for morons like me please? eg Can I just use TextEdit? :)

I hope I'm not depressing you with this as much as I am myself ....

Thanks again for all your kind help

Gary



    Visit this group at
    http://groups.google.com/__group/sage-support?hl=en
    <http://groups.google.com/group/sage-support?hl=en>.
    For more options, visit https://groups.google.com/__groups/opt_out
    <https://groups.google.com/groups/opt_out>.



--
You received this message because you are subscribed to the Google
Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send

To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/3mekDq5Stvk/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to sage-support+unsubscribe@googlegroups.com.
Screen shot 2013-05-02 at 20.05.52.png

P Purkayastha

unread,
May 2, 2013, 4:46:06 PM5/2/13
to sage-s...@googlegroups.com, garym...@googlemail.com


On Friday, May 3, 2013 3:14:30 AM UTC+8, Gary McConnell wrote:
hi Basu

really sorry about this - I've just gotten around to trying this again - but I am afraid I simply don't understand the most basic things of what I have to do. You are right that indeed the patch file thingo seems to have been created. So I went in search of these mysterious files and tried to unpack the 5.8 stuff, but all it does is try to install it, which crashes (please see attached screenshot) ... and obviously if I try to open the .spkg thing itself inside an editor I just get utter gibberish ...

Oh. I never asked you to *install* it. I asked you to simply *unpack* it. Run the following command from outside your sage installation directory.

tar xvf /path/to/sage-5.8.spkg

Then you will see a sage-5.8 directory in the directory you are in.
 

    Visit this group at
    http://groups.google.com/__group/sage-support?hl=en
    <http://groups.google.com/group/sage-support?hl=en>.
    For more options, visit https://groups.google.com/__groups/opt_out
    <https://groups.google.com/groups/opt_out>.



--
You received this message because you are subscribed to the Google
Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send

To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/3mekDq5Stvk/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to sage-support...@googlegroups.com.

kcrisman

unread,
Apr 21, 2014, 9:45:21 AM4/21/14
to sage-s...@googlegroups.com, garym...@googlemail.com
Hi, I've updated the ticket you created (http://trac.sagemath.org/ticket/14493) with some example.  Let us know if they correctly say the problems you had.  Also thanks also for the feedback on the devel guide - it's not "good" to hear, but it is good to know that even people who do know what vi and emacs are don't always find it easy to follow, so that we can keep trying to improve it.

- kcrisman


On Wednesday, April 17, 2013 3:13:12 PM UTC-4, Gary McConnell wrote:
I am very keen to help - my problem is utter incompetence at following the high-level instructions in the manual for developers. Also I am on the VM and I cannot access the sage directories directly. Is there a way you could send me a manual file that I could modify and send back to you? Sorry :( I promise I will learn this one day ...

the page I was referring to is

Also there are presumably many functions to which this syntax difference applies ... how can I find them?

Thanks!
On Wed, Apr 17, 2013 at 3:42 PM, P Purkayastha <ppu...@gmail.com> wrote:
On 04/17/2013 07:16 PM, Gary McConnell wrote:
Ah I see now that this is implicit in the docs example ... thank you ...
should we perhaps point that out explicitly, since the very same
function takes two rather different syntaxes? I am happy to write a
small amendment to the page.

You are welcome to improve the docs. If it was confusing to you it will be confusing to someone else too in the future. So, if you have some ideas how to fix/enhance the documentation, please go ahead and submit a patch. :)



--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/3mekDq5Stvk/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to sage-support...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages