def _sage_(self): """ EXAMPLES: sage: m = lie('[[1,0,3,3],[12,4,-4,7],[-1,9,8,0],[3,-5,-2,9]]') # optional - lie sage: m.sage() # optional - lie [ 1 0 3 3] [12 4 -4 7] [-1 9 8 0] [ 3 -5 -2 9] """ t = self.type() if t == "grp": raise ValueError, "cannot convert Lie groups to native Sage objects" elif t == "mat": import sage.matrix.constructor return sage.matrix.constructor.matrix(eval(str(self).replace("\n", "").strip())) elif t == "pol": import sage.misc.misc from sage.rings.all import PolynomialRing, QQ # Figure out the number of variables s = str(self) open_bracket = s.find("[") close_bracket = s.find("]") nvars = len(s[open_bracket:close_bracket].split(",")) # create the polynomial ring R = PolynomialRing(QQ, nvars, "x") x = R.gens() pol = R(0) # Split up the polynomials into terms terms = [] for termgrp in s.split(" - "): # The first entry in termgrp has # a negative coefficient termgrp = "-" + termgrp.strip() terms += termgrp.split("+") # Make sure we don't accidentally add a negative # sign to the first monomial if s[0] != "-": terms[0] = terms[0][1:] # go through all the terms in s for term in terms: xpos = term.find("X") coef = eval(term[:xpos].strip()) exps = eval(term[xpos + 1 :].strip()) monomial = sage.misc.misc.prod(map(lambda i: x[i] ** exps[i], range(nvars))) pol += coef * monomial return pol elif t == "tex": return repr(self) elif t == "vid": return None else: return ExpectElement._sage_(self)
def _sage_(self): """ EXAMPLES: sage: m = lie('[[1,0,3,3],[12,4,-4,7],[-1,9,8,0],[3,-5,-2,9]]') # optional - lie sage: m.sage() # optional - lie [ 1 0 3 3] [12 4 -4 7] [-1 9 8 0] [ 3 -5 -2 9] """ t = self.type() if t == 'grp': raise ValueError, "cannot convert Lie groups to native Sage objects" elif t == 'mat': import sage.matrix.constructor return sage.matrix.constructor.matrix( eval( str(self).replace('\n','').strip()) ) elif t == 'pol': import sage.misc.misc from sage.rings.all import PolynomialRing, QQ #Figure out the number of variables s = str(self) open_bracket = s.find('[') close_bracket = s.find(']') nvars = len(s[open_bracket:close_bracket].split(',')) #create the polynomial ring R = PolynomialRing(QQ, nvars, 'x') x = R.gens() pol = R(0) #Split up the polynomials into terms terms = [] for termgrp in s.split(' - '): #The first entry in termgrp has #a negative coefficient termgrp = "-"+termgrp.strip() terms += termgrp.split('+') #Make sure we don't accidentally add a negative #sign to the first monomial if s[0] != "-": terms[0] = terms[0][1:] #go through all the terms in s for term in terms: xpos = term.find('X') coef = eval(term[:xpos].strip()) exps = eval(term[xpos+1:].strip()) monomial = sage.misc.misc.prod(map(lambda i: x[i]**exps[i] , range(nvars))) pol += coef * monomial return pol elif t == 'tex': return repr(self) elif t == 'vid': return None else: return ExpectElement._sage_(self)
def _sage_(self): """ EXAMPLES:: sage: m = lie('[[1,0,3,3],[12,4,-4,7],[-1,9,8,0],[3,-5,-2,9]]') # optional - lie sage: m.sage() # optional - lie [ 1 0 3 3] [12 4 -4 7] [-1 9 8 0] [ 3 -5 -2 9] """ t = self.type() if t == 'grp': raise ValueError("cannot convert Lie groups to native Sage objects") elif t == 'mat': import sage.matrix.constructor return sage.matrix.constructor.matrix( eval( str(self).replace('\n','').strip()) ) elif t == 'pol': from sage.rings.all import PolynomialRing, QQ #Figure out the number of variables s = str(self) open_bracket = s.find('[') close_bracket = s.find(']') nvars = len(s[open_bracket:close_bracket].split(',')) #create the polynomial ring R = PolynomialRing(QQ, nvars, 'x') x = R.gens() pol = R(0) #Split up the polynomials into terms terms = [] for termgrp in s.split(' - '): #The first entry in termgrp has #a negative coefficient termgrp = "-"+termgrp.strip() terms += termgrp.split('+') #Make sure we don't accidentally add a negative #sign to the first monomial if s[0] != "-": terms[0] = terms[0][1:] #go through all the terms in s for term in terms: xpos = term.find('X') coef = eval(term[:xpos].strip()) exps = eval(term[xpos+1:].strip()) monomial = prod([x[i]**exps[i] for i in range(nvars)]) pol += coef * monomial return pol elif t == 'tex': return repr(self) elif t == 'vid': return None else: return ExpectElement._sage_(self)
def __repr__(self): """ EXAMPLES:: sage: gap(2) 2 """ s = ExpectElement.__repr__(self) if s.find('must have a value') != -1: raise RuntimeError, "An error occurred creating an object in %s from:\n'%s'\n%s"%(self.parent().name(), self._create, s) return s
def __init__(self, parent, value, is_name=False, name=None): """ Create a Maxima element. See ``MaximaElement`` for full documentation. EXAMPLES:: sage: maxima(zeta(7)) zeta(7) TESTS:: sage: from sage.interfaces.maxima import MaximaElement sage: loads(dumps(MaximaElement))==MaximaElement True sage: a = maxima(5) sage: type(a) <class 'sage.interfaces.maxima.MaximaElement'> sage: loads(dumps(a))==a True """ ExpectElement.__init__(self, parent, value, is_name=False, name=None)