diff options
author | bnewbold <bnewbold@eta.mit.edu> | 2009-01-22 03:46:57 -0500 |
---|---|---|
committer | bnewbold <bnewbold@eta.mit.edu> | 2009-01-22 03:46:57 -0500 |
commit | b00bae3f32ebc3427ed7933a2fda4b830c094497 (patch) | |
tree | 4a06e1f6e7bb9eae5ab1796a852c36c643d122e2 /other | |
parent | 1d18259d031a2bbbb6e99f883f26de1919bda511 (diff) | |
download | 8thesis-b00bae3f32ebc3427ed7933a2fda4b830c094497.tar.gz 8thesis-b00bae3f32ebc3427ed7933a2fda4b830c094497.zip |
journal edit, notes, mitscheme in sage
Diffstat (limited to 'other')
-rw-r--r-- | other/all.py | 43 | ||||
-rw-r--r-- | other/mitscheme.py | 353 | ||||
-rw-r--r-- | other/sage-scheme-notes.txt | 37 |
3 files changed, 429 insertions, 4 deletions
diff --git a/other/all.py b/other/all.py new file mode 100644 index 0000000..33e2717 --- /dev/null +++ b/other/all.py @@ -0,0 +1,43 @@ +# interfaces to other interpreters + +from frobby import frobby +from axiom import Axiom, axiom, axiom_console + +from expect import is_ExpectElement +from gap import gap, gap_reset_workspace, gap_console, gap_version, is_GapElement, Gap +from genus2reduction import genus2reduction, Genus2reduction +from gfan import gfan, Gfan +from gp import gp, gp_console, gp_version, is_GpElement, Gp +from gnuplot import gnuplot, gnuplot_console +from kash import kash, kash_console, kash_version, is_KashElement, Kash +from lisp import lisp, lisp_console, Lisp +from magma import magma, magma_console, magma_version, Magma, is_MagmaElement +from magma_free import magma_free +from macaulay2 import macaulay2, macaulay2_console, Macaulay2 +from maple import maple, maple_console, Maple +from maxima import maxima, maxima_console, is_MaximaElement, Maxima +from mathematica import mathematica, mathematica_console, Mathematica +from matlab import matlab, matlab_console, matlab_version, Matlab +from mitscheme import mitscheme, mitscheme_console, MitScheme +from mupad import mupad, mupad_console, Mupad # NOT functional yet +from mwrank import mwrank, Mwrank, mwrank_console +from octave import octave, octave_console, octave_version, Octave +from qepcad import qepcad, qepcad_console, qepcad_version, qepcad_formula +from qsieve import qsieve +from scm import scm, scm_console, SCM +from singular import singular, singular_console, singular_version, is_SingularElement, Singular +from sage0 import sage0 as sage0, sage0_console, sage0_version, Sage +from tachyon import tachyon_rt +from psage import PSage +from ecm import ECM, ecm +from povray import povray +from lie import lie, lie_console, LiE +from r import r, r_console, R, r_version, is_RElement + +# signal handling +from get_sigs import * + +interfaces = ['gap', 'gp', 'mathematica', 'gnuplot', \ + 'kash', 'magma', 'macaulay2', 'maple', 'maxima', \ + 'mathematica', 'mitscheme', 'mwrank', 'octave', 'r', \ + 'singular', 'sage0', 'sage', 'scm'] diff --git a/other/mitscheme.py b/other/mitscheme.py new file mode 100644 index 0000000..826f26e --- /dev/null +++ b/other/mitscheme.py @@ -0,0 +1,353 @@ +r"""nodoctest [[remove the nodoctest, so doctests will run]] +Interface Template + +[[Describe the math software you are interfacing with here.]] + +[[Replace this by something relevant to your system.]] + Type \code{gp.[tab]} for a list of all the functions available + from your Gp install. Type \code{gp.[tab]?} for Gp's + help about a given function. Type \code{gp(...)} to create + a new Gp object, and \code{gp.eval(...)} to run a string + using Gp (and get the result back as a string). + + +EXAMPLES: + +[[Go through a standard tutorial for your software package +and do it via your SAGE interface.]] + +AUTHORS: + -- William Stein (template) +""" + +########################################################################## +# +# Copyright (C) 2006 William Stein <wstein@gmail.com> +# +# Distributed under the terms of the GNU General Public License (GPL) +# +# http://www.gnu.org/licenses/ +# +########################################################################## + +from __future__ import with_statement +from expect import Expect, ExpectElement, ExpectFunction, FunctionElement, gc_disabled +from sage.misc.misc import verbose + +class MitScheme(Expect): + """ + [[Some basic help about your system. This is what + will be displayed when somebody write mitscheme?.]] + """ + def __init__(self, + maxread=100000, script_subdirectory=None, + logfile=None, + server=None, + server_tmpdir=None): + Expect.__init__(self, + + # The capitalized version of this is used for printing. + name = 'MIT/GNU Scheme', + + # This is regexp of the input prompt. If you can change + # it to be very obfuscated that would be better. Even + # better is to use sequence numbers. + prompt = '\d? \]=> ', + + # This is the command that starts up your program + command = "/usr/local/scmutils/mit-scheme/bin/scheme --library /usr/local/scmutils/mit-scheme/lib/ --heap 12000 --band edwin-mechanics.com", + #command = "mit-scheme", + + maxread = maxread, + + server=server, + server_tmpdir=server_tmpdir, + + script_subdirectory = script_subdirectory, + + # If this is true, then whenever the user presses Control-C to + # interrupt a calculation, the whole interface is restarted. + restart_on_ctrlc = True, + + # If true, print out a message when starting + # up the command when you first send a command + # to this interface. + verbose_start = True, + + logfile=logfile, + + # If an input is longer than this number of characters, then + # try to switch to outputing to a file. + eval_using_file_cutoff=1024) + + self.__seq = 0 + self.eval('(set! standard-error-hook (lambda (x) (begin (warn x) (cmdl-interrupt/abort-top-level))))') + + def chdir(self, dir): + raise NotImplemented + + def eval(self, code, strip=True, synchronize=False, **kwds): + """ + For Scheme, don't listen for prompt except for the last line + Also, for MIT/GNU Scheme strip the ;Value: part of results + + INPUT: + code -- text to evaluate + strip -- bool; whether to strip output prompts, etc. + (ignored in the base class). + **kwds -- All other arguments are passed onto the _eval_line method. + An often useful example is reformat=False. + """ + if synchronize: + try: + self._synchronize() + except AttributeError: + pass + + if strip: + try: + code = self._strip_prompt(code) + except AttributeError: + pass + + if not isinstance(code, basestring): + raise TypeError, 'input code must be a string.' + + #Remove extra whitespace + code = code.strip() + + try: + with gc_disabled(): + return self._strip_prompt(self._eval_line(code, **kwds)) + except KeyboardInterrupt: + # DO NOT CATCH KeyboardInterrupt, as it is being caught + # by _eval_line + # In particular, do NOT call self._keyboard_interrupt() + raise + except TypeError, s: + raise TypeError, 'error evaluating "%s":\n%s'%(code,s) + + def _strip_prompt(self, code): + return code.strip().replace(';Value: ','') + + def _repr_(self): + return 'MIT/GNU Scheme Interpreter' + + def __reduce__(self): + return reduce_load_mitscheme, tuple([]) + + def __getattr__(self, attrname): + if attrname[:1] == "_": + raise AttributeError + return MitSchemeFunction(self, attrname) + + def _quit_string(self): + return '(quite)' + + def _read_in_file_command(self, filename): + raise NotImplementedError + + def trait_names(self): + ## [[implement giving a list of all functions and identifiers in the system]] + raise NotImplementedError + + def read(self, filename): + # [[implement loading of the contents of filename into the system]] + raise NotImplementedError + + + def kill(self, var): + # [[send code that kills the variable with given name in the system.]] + pass + + def console(self): + # run the console command (defined below). + mitscheme_console() + + def version(self): + # run the version command (defined below) + pass + + def _object_class(self): + return MitSchemeElement + + def _left_list_delim(self): + raise NotImplementedError + + def _right_list_delim(self): + raise NotImplementedError + + def _assign_symbol(self): + raise NotImplementedError + + def _true_symbol(self): + # return the string rep of truth, i.e., what the system outputs + # when you type 1==1. + return '#t' + + def _false_symbol(self): + # return the string rep of truth, i.e., what the system outputs + # when you type 1==2. + return '#f' + + def _equality_symbol(self): + # return the symbol for checking equality, e.g., == or eq. + raise NotImplementedError + + def help(self, command): + # return help on a given command. + raise NotImplementedError + + def set(self, var, value): + """ + Set the variable var to the given value. + """ + cmd = '(define %s %s)'%(var, value) + self.eval(cmd) + + def get(self, var): + """ + Get the value of the variable var. + """ + cmd = '%s'%var + return self.eval(var) + + def function_call(self, function, args=[], kwds={}): + """ + EXAMPLES: + sage: maxima.quad_qags(x, x, 0, 1, epsrel=1e-4) + [0.5,5.5511151231257...E-15,21,0] + sage: maxima.function_call('quad_qags', [x, x, 0, 1], {'epsrel':'1e-4'}) + [0.5,5.5511151231257...E-15,21,0] + """ + if function == '': + raise ValueError, "function name must be nonempty" + if function[:2] == "__": + raise AttributeError + if not isinstance(args, list): + args = [args] + for i in range(len(args)): + if not isinstance(args[i], ExpectElement): + args[i] = self.new(args[i]) + for key, value in kwds.iteritems(): + kwds[key] = self.new(value) + + return self.new("(%s %s)"%(function, " ".join([s.name() for s in args]+ + ['%s'%(key,value.name()) for key, value in kwds.items()]))) + + +class MitSchemeElement(ExpectElement): + """ + Describe elements of your system here. + """ + def trait_names(self): + # This is if your system doesn't really have types. If you have types + # this function should only return the relevant methods that take self + # as their first argument. + return self.parent().trait_names() + + def __cmp__(self, other): + P = self.parent() + if P.eval("(eq? %s %s)"%(self.name(), other.name())) == P._true_symbol(): + return 0 + elif P.eval("(< %s %s)"%(self.name(), other.name())) == P._true_symbol(): + return -1 + elif P.eval("(> %s %s)"%(self.name(), other.name())) == P._true_symbol(): + return 1 + + # everything is supposed to be comparable in Python, so we define + # the comparison thus when no comparable in interfaced system. + if (hash(self) < hash(other)): + return -1 + else: + return 1 + + def hasattr(self, attrname): + """ + Scheme things don't have attributes (unless SOS...) + """ + raise NotImplementedError + + def attribute(self, attrname): + """ + Scheme things don't have attributes (unless SOS...) + """ + raise NotImplementedError + + def __getitem__(self, n): + """ + Scheme things don't have attributes (unless SOS...) + """ + raise NotImplementedError + + def bool(self): + P = self.parent() + t = P._true_symbol() + cmd = '%s'%self._name + return P.eval(cmd) == t + + def __nonzero__(self): + """ + EXAMPLES: + sage: bool(mitscheme('#t')) + True + sage: bool(mitscheme('#f')) + False + """ + P = self.parent() + t = P._true_symbol() + cmd = '(zero? %s)'%self._name + return P.eval(cmd) == t + + def _operation(self, operation, right): + P = self._check_valid() + try: + return P.new('(%s %s %s)'%(operation, self._name, right._name)) + except Exception, msg: + raise TypeError, msg + + def __pow__(self, n): + """ + EXAMPLES: + sage: a = maxima('2') + sage: a^(3/4) + 2^(3/4) + """ + P = self._check_valid() + if not hasattr(n, 'parent') or P is not n.parent(): + n = P(n) + return self._operation("expt", n) + + +class MitSchemeFunctionElement(FunctionElement): + def _sage_doc_(self): + M = self._obj.parent() + return M.help(self._name) + + +class MitSchemeFunction(ExpectFunction): + def _sage_doc_(self): + M = self._parent + return M.help(self._name) + + +def is_MitSchemeElement(x): + return isinstance(x, MitSchemeElement) + +# An instance +mitscheme = MitScheme() + +def reduce_load_MitScheme(): + return mitscheme + +import os +def mitscheme_console(): + # This will only spawn local processes + os.system('mitscheme') + + +def mitscheme_version(): + """ + UGH hard to implement + """ + raise NotImplemented diff --git a/other/sage-scheme-notes.txt b/other/sage-scheme-notes.txt index 0b88a90..b366683 100644 --- a/other/sage-scheme-notes.txt +++ b/other/sage-scheme-notes.txt @@ -1,9 +1,38 @@ +scm or MIT/GNU Scheme in Sage +======================================= -in sage-###/devel/sage/sage/interfaces/, copy template.py to mitscheme.py -edit all.py and add appropriate objects, strings +First install scm, MIT/GNU Scheme, or scmutils on your system. If you're doing +scmutils do it in /usr/local/ as recommended. -then compile everything: -./sage -b +Then copy mitscheme.py and/or scm.py to SAGE_ROOT/devel/sage/sage/interfaces/ +and edit all.py to include the lines: + from mitscheme import mitscheme, mitscheme_console, MitScheme + from scm import scm, scm_console, SCM +and edit the list of interfaces to include 'scm' and 'mitscheme'. For notebook +cell use, edit SAGE_ROOT/devel/sage/server/notebook/notebook.py and add +'scm (optional)' and 'scmutils (optional)' to the list of interfaces. +If you want plain mit-scheme instead of all the scmutils packages, edit +mitscheme.py and replace the long 'command' declaration near the top with the +short commented out declaration. + +Then return to your root sage directory (SAGE_ROOT) and do: + + $ ./sage -b + +See if it works, eg: + + sage: m = MitScheme() + Starting MIT/GNU Scheme Interpreter + sage: m.eval('(+ 1 1)') + '2' + sage: one = mitscheme(1) + '1' + sage: two = mitscheme(2) + '2' + sage: one + two + '3' + +Much to do! |