Example #1
0
 def __init__(self,
              path,
              mode='launch',
              timeout=1,
              debug=False,
              plot_dir=None,
              plot_size=None,
              plot_format='png',
              output_prompt=False,
              input_prompt=False):
     self._env = _ml.env()
     mathLinkPath = path.encode('latin1')
     sys.argv.extend(['-linkname', "" + mathLinkPath + "  -mathlink"])
     self.kernel = self._env.openargv(sys.argv)
     self.kernel.connect()
     self.debug = debug
     self.plot_dir = plot_dir
     self.plot_num = 0
     self.last_python_result = None
     self.last_str_result = None
     self.plot_size = plot_size
     self.plot_format = plot_format
     self.output_prompt = output_prompt
     self.input_prompt = input_prompt
     self.last_error = None
     _time.sleep(timeout)
     if not self.kernel.ready():
         raise PythonicaException("Unable to Start Mathematica Kernel")
     else:
         packet = self.kernel.nextpacket()
         if self.debug:
             print _ml.packetdescriptiondictionary[packet]
         if packet == _ml.INPUTNAMEPKT:
             self.kernel.getstring()
 def __init__(self,
              name='math -mathlink',
              mode='launch',
              timeout=1,
              debug=False,
              plot_dir=None,
              plot_size=None,
              plot_format='png',
              output_prompt=False,
              input_prompt=False):
     self._env = _ml.env()
     self.kernel = self._env.open(name,mode=mode)
     self.kernel.connect()
     self.debug=debug
     self.plot_dir = plot_dir
     self.plot_num = 0
     self.last_python_result=None
     self.last_str_result=None
     self.plot_size = plot_size
     self.plot_format = plot_format
     self.output_prompt = output_prompt
     self.input_prompt = input_prompt
     self.last_error = None
     _time.sleep(timeout)
     if not self.kernel.ready():
         raise PythonicaException("Unable to Start Mathematica Kernel")
     else:
         packet = self.kernel.nextpacket()
         if self.debug:
             print _ml.packetdescriptiondictionary[packet]
         if packet == _ml.INPUTNAMEPKT:
             self.kernel.getstring()
Example #3
0
 def __init__(self,
              path,
              mode='launch',
              timeout=5,
              debug=False,
              plot_dir=None,
              plot_size=None,
              plot_format='png',
              output_prompt=False,
              input_prompt=False):
     self._env = _ml.env()
     mathLinkPath = path.encode('latin1')
     sys.argv.extend(['-linkname', "" + mathLinkPath +"  -mathlink"])
     self.kernel = self._env.openargv(sys.argv)
     self.kernel.connect()
     self.debug=debug
     self.plot_dir = plot_dir
     self.plot_num = 0
     self.last_python_result=None
     self.last_str_result=None
     self.plot_size = plot_size
     self.plot_format = plot_format
     self.output_prompt = output_prompt
     self.input_prompt = input_prompt
     self.last_error = None
     _time.sleep(timeout)
     if not self.kernel.ready():
         raise PythonicaException("Unable to Start Mathematica Kernel")
     else:
         packet = self.kernel.nextpacket()
         if self.debug:
             print _ml.packetdescriptiondictionary[packet]
         if packet == _ml.INPUTNAMEPKT:
             self.kernel.getstring()
Example #4
0
    def __init__(self, **kwds):
        raise NotImplementedError("this should be upgraded for doing parallel computations")

        self.env = mathlink.env()

        # FIXME: i can probably use this same class to run/manage mathlink instances in parallel
        argv0 = kwds.pop('argv0', '<none>')
        if 'create' in kwds:
            name = kwds.pop('create')
            argv = [argv0, "-mathlink", "-linkcreate", "-linkname", name]
        elif 'connect' in kwds:
            name = kwds.pop('connect')
            argv = [argv0, "-mathlink", "-linkconnect", "-linkname", name]
        else:
            raise UserError("%s : please specify the 'create' or 'connect' keywords"%(self.__class__.__module__, self.__class__.__name__))
        self.link = self.env.openargv(argv)
Example #5
0
from fractions import Fraction
import mathlink
e = mathlink.env()
link = e.open('-linkcreate -linkname hehe')

from fractions import Fraction

class Symbol(str):
    def __repr__(self):
        return self

#List = Symbol('List')

class Expr(tuple):
    def __repr__(self):
        head = self[0]
        listq = type(head) == Symbol and head == 'List'
        if listq:
            r = '['
        else:
            r = head.__repr__() + '('
        if len(self) > 1:
            r += self[1].__repr__()
            for i in self[2:]:
                r += ', ' + i.__repr__()
        return r + (']' if listq else ')')
        
def getreal(self):
    t = self.gettype()
    if t == mathlink.MLTKINT:
        return self.getlong()