A few days ago I spent a couple of hours getting PyObjC working with  
sage.  The objective was to be able to write little sage-based GUI  
applications.  (Though it could easily turn into a full-blown sage.app  
IDE, that was outside the scope of 2 hours playing around.)

The basic notion of PyObjC is that you can write your program in  
python, inheriting python objects from objective-c classes, and making  
your python data members accessible to objective-c.  That makes the  
Cocoa libraries directly callable from sage.

I wrote a little app to demonstrate one possibility.  It takes from  
GUI fields a function, parameter names and a variable, and  
differentiates the function.

It has three source files, two of which are boilerplate (one is a  
python file that sets up the path and initialises sage, the other is  
an objective-c file that starts the application).

The third file is below with a couple of comments.

A GUI screenshot is at
http://alliance.anu.edu.au/access/content/group/hpv/public/differentiator.png

#---------------- Code for GUI controller ----------------
#import the relevant parts of Cocoa
from Foundation import *

#import the relevant parts of Sage
from sage.calculus import *
from sage.calculus.var import *
from sage.calculus.calculus import log, sin, cos

#"controller" is the class that talks to both the GUI and Sage
class controller(NSObject):
     # the three input fields are wired to the GUI text input fields
     functionInput = objc.ivar(u"functionInput")
     variableInput = objc.ivar(u"variableInput")
     parametersInput = objc.ivar(u"parametersInput")

     # the output field is wired to a GUI label
     calculatedDerivative = objc.ivar(u"calculatedDerivative")

     # the GUI "derivative" button triggers this method
     def doCalculation(self):
         # standard sage code, assuming that the inputs
         # are strings.

         # declare the symbolic parameters
         var(self.parametersInput)
         x = var(self.variableInput)

         # declare the function to differentiate
         f = eval(self.functionInput)

         # differentiate (this illustrates how 'standard sage' is  
available)
         f = f.derivative(x)

         # return the result to the GUI.
         self.calculatedDerivative = f.__repr__()

#---------------- End of GUI controller code ----------------

About half of the 'couple' of hours was reading through the sage  
initialisation code and setting up the boilerplate files I referred  
above.  The other half was because I suck at python...

The weakness of this approach is that sage is running the main  
application loop.  I.e. if f.derivative() takes its time, then there's  
a spinning beachball.  I assume the big M's overcome this by  
separating their GUIs from their kernels.

The big strength is much the same.  Because sage is running the main  
application loop, you don't have to think about message passing of any  
sort.  It should be easy enough to subclass NSView or something to  
display plots.  Etc.

If anyone's interested in kicking around ideas or explaining why this  
is a dumb idea, please go for it.  If you want the boilerplate code  
you are welcome, but it was mostly hard hacks to get things working,  
and is utterly specific to my system.

D


==================================
David J Philp
Postdoctoral Fellow
National Centre for Epidemiology and Population Health
Building 62, cnr Mills Rd & Eggleston Rd
The Australian National University
Canberra ACT 0200 Australia

T: +61 2 6125 8260
F: +61 2 6125 0740
M: 0423 535 397
W: http://nceph.anu.edu.au/

CRICOS Provider #00120C



--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to