Ask Your Question

tdstephens3's profile - activity

2022-05-24 19:19:35 +0200 received badge  Famous Question (source)
2017-01-17 14:49:09 +0200 received badge  Notable Question (source)
2013-03-12 13:55:42 +0200 received badge  Popular Question (source)
2012-10-24 02:50:06 +0200 received badge  Famous Question (source)
2012-03-13 12:41:47 +0200 received badge  Famous Question (source)
2012-01-28 04:49:57 +0200 received badge  Notable Question (source)
2011-11-11 14:38:30 +0200 received badge  Notable Question (source)
2011-11-03 23:02:20 +0200 received badge  Popular Question (source)
2011-09-19 10:29:17 +0200 received badge  Popular Question (source)
2011-07-27 21:04:23 +0200 marked best answer callable symbolic expression from python script

I am not sure whether your overall goal is worth pursuing. From the surface, it looks like MyFunc is becoming a clone of sage.symbolic.expression.Expression. Additionally, Sage has a deprecation policy: old code must be supported for at least twelve months with deprecation warnings before Sage can break it. For most purposes, I suggest keeping Sage up to date and continually testing whether your code is using a deprecated interface.

Just for reference, your example can be done in Sage as follows:

Using a symbolic function:

sage: func(x) = function('func', x)
sage: func
x |--> func(x)
sage: func(3)
func(3)
sage: func.differentiate(x)
x |--> D[0](func)(x)
sage: func.differentiate(x)(3)
D[0](func)(3)

Using a concrete function:

sage: func(x) = sinh(x) * sqrt(x)
sage: func
x |--> sqrt(x)*sinh(x)
sage: func(3)
sqrt(3)*sinh(3)
sage: RR(func(3))
17.3514683581443
sage: func.differentiate(x)
x |--> sqrt(x)*cosh(x) + 1/2*sinh(x)/sqrt(x)
sage: RR(func.differentiate(x)(3))
20.3296134831414

As far as I know, the above functionality has been around for a while without any changes, so I would not be too concerned about the stability of Sage's interface.

2011-07-27 21:03:41 +0200 marked best answer sage-python, matplotlib.pyplot: backends tk and pygtk not recognized

Sage is looking for those bindings installed in Sage's copy of python. You can install things in Sage's copy of python by doing "sage -sh" and then whatever you normally would do to install the python bindings.

For Sage's matplotlib, the graphical guis are also turned off by default. Define the environment variable:

export SAGE_MATPLOTLIB_GUI=yes

and then reinstall Sage's matplotlib:

sage -i matplotlib

to compile support for other backends in Sage.

2011-07-27 21:03:21 +0200 received badge  Scholar (source)
2011-07-27 21:03:21 +0200 marked best answer sage-python, import matplotlib: no module named _tkagg

For Sage's matplotlib, the graphical guis are also turned off by default. Define the environment variable:

export SAGE_MATPLOTLIB_GUI=yes

and then reinstall Sage's matplotlib:

sage -i matplotlib

to compile support for other backends in Sage.

2011-06-30 13:57:55 +0200 asked a question sage-python, matplotlib.pyplot: backends tk and pygtk not recognized

I am trying to solve the problem I was having in this question - but since I did a clean sage install since then I figured I would start a new question.

I am writing python modules and running them with sage-python (currently, python-2.6.4.p10). I need some plotting capability, both to the screen and plots written to files. My plan is to use matplotlib.pyplot for this (but I will gladly abandon pyplot if it is appropriate).

The problem is this:

From the bash prompt with matplotlibrc backend specified as GTKAgg:

bash$ sage-python
>>>import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/pyplot.py", line 95,    
in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, 
FigureCanvasGTK,\
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/backend_gtk.py", line 11, in <module>
raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.

From the bash prompt with matplotlibrc backend specified as TkAgg:

bash$ sage-python
>>>import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/pyplot.py", line 95, 
in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/backend_tkagg.py", line 11, in <module>
import matplotlib.backends.tkagg as tkagg
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/backends/tkagg.py", 
line 1, in <module>
import _tkagg
ImportError: No module named _tkagg

Now, I am running Arch Linux and according to my package manager, pacman, I have

extra/pygtk 2.24.0-1 [installed]
Python bindings for the GTK widget set

extra/tcl 8.5.10-1 [installed]
The Tcl scripting language

extra/tk 8.5.10-1 [installed]
A windowing toolkit for use with tcl

I have looked here but I don't see how to get tk8.5-dev since I don't use the Debian repositories (it seems to be available for Ubuntu and Debian).

It may be that I simply have an environment variable defined incorrectly, maybe I need to choose another backend for matplotlib, or need to abandon the pyplot module, what do you think?

2011-06-29 04:38:55 +0200 received badge  Student (source)
2011-06-28 16:41:40 +0200 asked a question sage-python, import matplotlib: no module named _tkagg

EDIT 2: Below is the description of a problem and a subsequent edit, where I thought I had the solution. In fact, what I did was move the problem from one place to another... I am no longer receiving the error No module named _tkagg and I am getting a plot window, but after opening a tk window I get a stack trace that reads:

Exception in Tkinter callback
Traceback (most recent call last):
File "/opt/sage/local/lib/python2.6/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/backend_tkagg.py", line 245, in resize
self.show()
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/backend_tkagg.py", line 249, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/backends/tkagg.py", 
line 18, in blit
_tkagg.tkinit(id(tk), 0)
AttributeError: 'module' object has no attribute 'tkinit'

along with no actual plot on the tk window. This issue remains unresolved.

I am using Sage's python and trying to import matplotlib.pyplot in order to generate some visual output (to the screen for now, to a file at some point). I understand the backend I should use is TkAgg for this, and I have that set in my matplotlibrc file.

When running a short script pleasePlot.py:

# pleasePlot.py

import matplotlib.pyplot as plt 
def main():
    x = [0,1,2,3,4,5]
    y = [1,6,4,4,2,7]
    plt.plot(x,y)
    plt.show()
main()

as

bash$ python pleasePlot.py

I am greeted with absolutely no output. When examining this closer, by running a sage-python (python2.6.4.p10) shell I get the following:

>>>import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/pyplot.py", line 95,          
in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/opt/sage/local/lib/python2.6/site-    
packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/opt/sage/local/lib/python2.6/site-    
packages/matplotlib/backends/backend_tkagg.py", line 11, in <module>
import matplotlib.backends.tkagg as tkagg
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/backends/tkagg.py",    
line 1, in <module>
import _tkagg
ImportError: No module named _tkagg

I have tried

sage -f python-2.6.4.p10
sage -f matplotlib-1.0.1

but this does not magically fix the problem. As well, Google seems to not be much help. What do you think?

EDIT 1: FIXED! I am able to get a plot window from either the sage-python prompt -or- by running this 'program' from the bash shell. What I did was to track down the _tkagg call (by reading the stack trace more carefully) coming from the file

/opt/sage/local/lib/python2.6/site-packages/matplotlib/backends/tkagg.py

the file starts out as:

import _tkagg
yada yada yada

so I went searching around ... (more)

2011-06-27 11:35:48 +0200 commented answer How do I get sage to honor my PYTHONPATH environmental variable?

Emerson, these are directives specified in the .bashrc or .bash_profile file sitting in your home directory. The best thing for you to do is to learn about the .bashrc file (and the .bash_profile file) in general, then look into what are known as environment variables, which are these PATH things they are talking about.

2011-06-14 17:16:16 +0200 commented answer How do I Pass a tuple as an argument for a multivariate polynomial?

What about the efficiency of each/any of the above methods? In particular, is unpacking the dictionary quicker than unpacking the list?

2011-06-01 17:33:50 +0200 received badge  Supporter (source)
2011-06-01 17:15:37 +0200 commented answer callable symbolic expression from python script

I agree that MyFunc is a clone of sage.symbolic.expression.Expression, but I *think* that is what I want here. I am trying to have these symbolic expressions available to a larger system written in python... I am rewriting a large amount of OO Matlab code in python. For better or worse, I am being guided by my earlier design patterns. Is there a deprecation issue with the code I have written?

2011-06-01 16:48:54 +0200 received badge  Editor (source)
2011-06-01 16:48:26 +0200 answered a question callable symbolic expression from python script

Alright, I guess it works this way sometimes (ask question, five minutes later figure out the answer!)

Using the information here (Sage tutorial), we can build a CallableSymbolicExpression. Here's what I did for this specific example:

import sage.all as sage

class myFunc:

   def __init__(self):
      sage.var('x')
      expr = x+1
      self.expr = expr.function(x)

   def __call__(self,value):
      return self.expr(value)

   def diff(self):
      return self.expr.diff(x)

This uses the Sage function(...) method as explained in the link. It is clean and I like it.

2011-06-01 16:21:15 +0200 asked a question callable symbolic expression from python script

I am trying to obtain a callable symbolic expression from a python script and having a hard time of it.

In my python script I would like to have something along the lines of:

func = myFunc()
func_evaluated_at_3 = myFunc(3)

func_dx = func.differentiate()
func_dx_evaluated_at_3 = func_dx(3)

In the module defining myFunc I was hoping to have something along the lines of:

import sage.all as sage

class myFunc:

   def __init__(self):

      sage.var('x')
      self.expression = x + 1
      # maybe this should be: self.expression = sage.preparse("f(x) = x + 1")?

   def __call__(self,value):
       return self.expression(value)

   def differentiate(self):
      return self.expression.diff(x)

The goal is to keep the client code that constructs, calls, and manipulates objects of type myFunc from knowing about Sage. Perhaps I am misguided, any help would be appreciated. (also, help with the appropriate tags would be, well, helpful)