diff --git a/src/sage/interfaces/axiom.py b/src/sage/interfaces/axiom.py index 5df8d2e184a..51b3a26f90c 100644 --- a/src/sage/interfaces/axiom.py +++ b/src/sage/interfaces/axiom.py @@ -317,8 +317,7 @@ def _commands(self): i = s.find(start) end = "To get more information about" j = s.find(end) - s = s[i+len(start):j].split() - return s + return s[i + len(start):j].split() def _tab_completion(self, verbose=True, use_disk_cache=True): """ @@ -357,15 +356,15 @@ def _tab_completion(self, verbose=True, use_disk_cache=True): print("To force rebuild later, delete %s." % self._COMMANDS_CACHE) v = self._commands() - #Process we now need process the commands to strip out things which - #are not valid Python identifiers. + # Process we now need process the commands to strip out things which + # are not valid Python identifiers. valid = re.compile('[^a-zA-Z0-9_]+') names = [x for x in v if valid.search(x) is None] - #Change everything that ends with ? to _q and - #everything that ends with ! to _e - names += [x[:-1]+"_q" for x in v if x.endswith("?")] - names += [x[:-1]+"_e" for x in v if x.endswith("!")] + # Change everything that ends with ? to _q and + # everything that ends with ! to _e + names += [x[:-1] + "_q" for x in v if x.endswith("?")] + names += [x[:-1] + "_e" for x in v if x.endswith("!")] self.__tab_completion = names if len(v) > 200: @@ -430,7 +429,7 @@ def _eval_line(self, line, reformat=True, allow_use_file=False, if self._expect is None: self._start() if allow_use_file and self.__eval_using_file_cutoff and \ - len(line) > self.__eval_using_file_cutoff: + len(line) > self.__eval_using_file_cutoff: return self._eval_line_using_file(line) try: E = self._expect @@ -465,7 +464,7 @@ def _eval_line(self, line, reformat=True, allow_use_file=False, line = line.rstrip() if line[:4] == ' (': i = line.find('(') - i += line[i:].find(')')+1 + i += line[i:].find(')') + 1 if line[i:] == "": i = 0 outs = outs[1:] @@ -598,14 +597,13 @@ def _richcmp_(self, other, op): sage: f = axiom('sin(x)'); g = axiom('cos(x)') #optional - axiom sage: f == g #optional - axiom False - """ P = self.parent() - if 'true' in P.eval("(%s = %s) :: Boolean" % (self.name(),other.name())): + if 'true' in P.eval("(%s = %s) :: Boolean" % (self.name(), other.name())): return rich_to_bool(op, 0) elif 'true' in P.eval("(%s < %s) :: Boolean" % (self.name(), other.name())): return rich_to_bool(op, -1) - elif 'true' in P.eval("(%s > %s) :: Boolean" % (self.name(),other.name())): + elif 'true' in P.eval("(%s > %s) :: Boolean" % (self.name(), other.name())): return rich_to_bool(op, 1) return NotImplemented @@ -637,7 +635,7 @@ def __len__(self): P = self._check_valid() s = P.eval('# %s ' % self.name()) i = s.rfind('Type') - return int(s[:i-1]) + return int(s[:i - 1]) def __getitem__(self, n): r""" @@ -709,12 +707,12 @@ def _latex_(self): i = s.find('$$') j = s.rfind('$$') s = s[i + 2:j] - s = multiple_replace({'\r':'', '\n':' ', - ' \\sp ':'^', - '\\arcsin ':'\\sin^{-1} ', - '\\arccos ':'\\cos^{-1} ', - '\\arctan ':'\\tan^{-1} '}, - re.sub(r'\\leqno\(.*?\)','',s)) # no eq number! + s = multiple_replace({'\r': '', '\n': ' ', + ' \\sp ': '^', + '\\arcsin ': '\\sin^{-1} ', + '\\arccos ': '\\cos^{-1} ', + '\\arctan ': '\\tan^{-1} '}, + re.sub(r'\\leqno\(.*?\)', '', s)) # no eq number! return s def as_type(self, type): @@ -753,16 +751,13 @@ def unparsed_input_form(self): s = P.eval('unparse(%s::InputForm)' % self._name) if 'translation error' in s or 'Cannot convert' in s: raise NotImplementedError - s = multiple_replace({'\r\n':'', # fix stupid Fortran-ish - 'DSIN(':'sin(', - 'DCOS(':'cos(', - 'DTAN(':'tan(', - 'DSINH(':'sinh('}, s) - r = re.search(r'"(.*)"',s) - if r: - return r.groups(0)[0] - else: - return s + s = multiple_replace({'\r\n': '', # fix stupid Fortran-ish + 'DSIN(': 'sin(', + 'DCOS(': 'cos(', + 'DTAN(': 'tan(', + 'DSINH(': 'sinh('}, s) + r = re.search(r'"(.*)"', s) + return r.groups(0)[0] if r else s def _sage_(self): """ @@ -835,8 +830,8 @@ def _sage_(self): from sage.rings.integer_ring import ZZ prec = max(self.mantissa().length()._sage_(), 53) R = RealField(prec) - x,e,b = self.unparsed_input_form().lstrip('float(').rstrip(')').split(',') - return R(ZZ(x)*ZZ(b)**ZZ(e)) + x, e, b = self.unparsed_input_form().lstrip('float(').rstrip(')').split(',') + return R(ZZ(x) * ZZ(b)**ZZ(e)) elif type == "DoubleFloat": from sage.rings.real_double import RDF return RDF(repr(self)) @@ -850,16 +845,18 @@ def _sage_(self): R = PolynomialRing(base_ring, vars) return R(self.unparsed_input_form()) elif type.startswith('Fraction'): - return self.numer().sage()/self.denom().sage() + return self.numer().sage() / self.denom().sage() - #If all else fails, try using the unparsed input form + # If all else fails, try using the unparsed input form try: import sage.misc.sage_eval vars = sage.symbolic.ring.var(str(self.variables())[1:-1]) - if isinstance(vars,tuple): - return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(), locals={str(x):x for x in vars}) + if isinstance(vars, tuple): + return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(), + locals={str(x): x for x in vars}) else: - return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(), locals={str(vars):vars}) + return sage.misc.sage_eval.sage_eval(self.unparsed_input_form(), + locals={str(vars): vars}) except Exception: raise NotImplementedError @@ -961,9 +958,10 @@ def is_AxiomElement(x): return isinstance(x, AxiomElement) -#Instances +# Instances axiom = Axiom(name='axiom') + def reduce_load_Axiom(): """ Returns the Axiom interface object defined in @@ -977,6 +975,7 @@ def reduce_load_Axiom(): """ return axiom + def axiom_console(): """ Spawn a new Axiom command-line session. diff --git a/src/sage/interfaces/sympy.py b/src/sage/interfaces/sympy.py index a334c241127..001b7bc687f 100644 --- a/src/sage/interfaces/sympy.py +++ b/src/sage/interfaces/sympy.py @@ -51,8 +51,10 @@ ################################################################ # Distributed under GNU GPL3, see www.gnu.org ################################################################ +from sage.misc.misc import run_once + -################# numbers and constants ############## +# ################ numbers and constants ############## def _sympysage_float(self): """ @@ -65,6 +67,7 @@ def _sympysage_float(self): from sage.rings.real_mpfr import create_RealNumber return create_RealNumber(str(self)) + def _sympysage_integer_ring(self): r""" EXAMPLES:: @@ -76,6 +79,7 @@ def _sympysage_integer_ring(self): from sage.rings.integer_ring import ZZ return ZZ + def _sympysage_integer(self): """ EXAMPLES:: @@ -89,6 +93,7 @@ def _sympysage_integer(self): from sage.rings.integer import Integer return Integer(self.p) + def _sympysage_rational(self): """ EXAMPLES:: @@ -101,6 +106,7 @@ def _sympysage_rational(self): from sage.rings.rational import Rational return Rational((Integer(self.p), Integer(self.q))) + def _sympysage_rational_field(self): r""" EXAMPLES:: @@ -112,6 +118,7 @@ def _sympysage_rational_field(self): from sage.rings.rational_field import QQ return QQ + def _sympysage_real_interval(self): r""" EXAMPLES:: @@ -138,6 +145,7 @@ def _sympysage_real_interval(self): domain = self.dom._sage_().fraction_field() return RIF(domain(self.a)).union(RIF(domain(self.b))) + def _sympysage_complex_interval(self): r""" EXAMPLES:: @@ -164,6 +172,7 @@ def _sympysage_complex_interval(self): domain = self.dom._sage_().fraction_field() return CIF(domain(self.ax), domain(self.ay)).union(CIF(domain(self.bx), domain(self.by))) + def _sympysage_polynomial_ring(self): r""" EXAMPLES:: @@ -184,6 +193,7 @@ def _sympysage_polynomial_ring(self): variables = ','.join(map(str, self.gens)) return base_ring[variables] + def _sympysage_polynomial(self): r""" EXAMPLES:: @@ -216,6 +226,7 @@ def _sympysage_polynomial(self): R = base_ring[variables] return R.sum(base_ring(coeff) * R.monomial(*exp) for exp, coeff in self.rep.terms(order=None)) + def _sympysage_pinfty(self): """ EXAMPLES:: @@ -227,6 +238,7 @@ def _sympysage_pinfty(self): from sage.rings.infinity import PlusInfinity return PlusInfinity() + def _sympysage_ninfty(self): """ EXAMPLES:: @@ -238,6 +250,7 @@ def _sympysage_ninfty(self): from sage.rings.infinity import MinusInfinity return MinusInfinity() + def _sympysage_uinfty(self): """ EXAMPLES:: @@ -249,6 +262,7 @@ def _sympysage_uinfty(self): from sage.rings.infinity import unsigned_infinity return unsigned_infinity + def _sympysage_nan(self): """ EXAMPLES:: @@ -260,6 +274,7 @@ def _sympysage_nan(self): from sage.symbolic.constants import NaN return NaN + def _sympysage_e(self): """ EXAMPLES:: @@ -271,6 +286,7 @@ def _sympysage_e(self): from sage.symbolic.constants import e return e + def _sympysage_pi(self): """ EXAMPLES:: @@ -282,6 +298,7 @@ def _sympysage_pi(self): from sage.symbolic.constants import pi return pi + def _sympysage_golden_ratio(self): """ EXAMPLES:: @@ -293,6 +310,7 @@ def _sympysage_golden_ratio(self): from sage.symbolic.constants import golden_ratio return golden_ratio + def _sympysage_eulerg(self): """ EXAMPLES:: @@ -304,6 +322,7 @@ def _sympysage_eulerg(self): from sage.symbolic.constants import euler_gamma return euler_gamma + def _sympysage_catalan(self): """ EXAMPLES:: @@ -315,6 +334,7 @@ def _sympysage_catalan(self): from sage.symbolic.constants import catalan return catalan + def _sympysage_i(self): """ EXAMPLES:: @@ -326,7 +346,8 @@ def _sympysage_i(self): from sage.symbolic.constants import I return I -################## basic operators ############## + +# ################# basic operators ############## def _sympysage_add(self): """ @@ -342,6 +363,7 @@ def _sympysage_add(self): s += x._sage_() return s + def _sympysage_mul(self): """ EXAMPLES:: @@ -356,6 +378,7 @@ def _sympysage_mul(self): s *= x._sage_() return s + def _sympysage_pow(self): """ EXAMPLES:: @@ -367,6 +390,7 @@ def _sympysage_pow(self): """ return self.args[0]._sage_()**self.args[1]._sage_() + def _sympysage_symbol(self): """ EXAMPLES:: @@ -398,7 +422,7 @@ def _sympysage_Subs(self): return args[0]._sage_().subs(substi) -############## functions ############### +# ############# functions ############### def _sympysage_function_by_name(fname): """ @@ -431,6 +455,7 @@ def _sympysage_function_by_name(fname): raise AttributeError return func + # the convoluted class structure with metaclasses and stuff sympy uses # to implement undefined functions makes things a bit harder for us # here @@ -451,7 +476,8 @@ def __get__(self, ins, typ): return lambda: _sympysage_function_by_name(typ.__name__) else: args = [arg._sage_() for arg in ins.args] - return lambda : _sympysage_function_by_name(ins.__class__.__name__)(*args) + return lambda: _sympysage_function_by_name(ins.__class__.__name__)(*args) + def _sympysage_function(self): """ @@ -484,6 +510,7 @@ def _sympysage_function(self): return func(*args) + def _sympysage_integral(self): """ EXAMPLES:: @@ -509,6 +536,7 @@ def _sympysage_integral(self): f = integral(f, (x._sage_(), a._sage_(), b._sage_()), hold=True) return f + def _sympysage_derivative(self): """ EXAMPLES:: @@ -546,6 +574,7 @@ def _sympysage_derivative(self): for a in (arg if isinstance(arg, (tuple, Tuple)) else [arg])] return derivative(f, *args) + def _sympysage_order(self): """ EXAMPLES:: @@ -558,6 +587,7 @@ def _sympysage_order(self): from sage.functions.other import Order return Order(self.args[0])._sage_() + def _sympysage_lambertw(self): """ EXAMPLES:: @@ -569,6 +599,7 @@ def _sympysage_lambertw(self): from sage.functions.log import lambert_w return lambert_w(self.args[0]._sage_()) + def _sympysage_rf(self): """ EXAMPLES:: @@ -582,6 +613,7 @@ def _sympysage_rf(self): from sage.arith.misc import rising_factorial return rising_factorial(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_ff(self): """ EXAMPLES:: @@ -595,6 +627,7 @@ def _sympysage_ff(self): from sage.arith.misc import falling_factorial return falling_factorial(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_lgamma(self): """ EXAMPLES:: @@ -606,6 +639,7 @@ def _sympysage_lgamma(self): from sage.functions.gamma import log_gamma return log_gamma(self.args[0]._sage_()) + def _sympysage_polygamma(self): """ EXAMPLES:: @@ -621,7 +655,8 @@ def _sympysage_polygamma(self): integrate(psi(x), x) """ from sage.functions.gamma import psi - return psi(self.args[0]._sage_(),self.args[1]._sage_()) + return psi(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_dirac_delta(self): """ @@ -634,6 +669,7 @@ def _sympysage_dirac_delta(self): from sage.functions.generalized import dirac_delta return dirac_delta(self.args[0]._sage_()) + def _sympysage_heaviside(self): """ EXAMPLES:: @@ -645,6 +681,7 @@ def _sympysage_heaviside(self): from sage.functions.generalized import heaviside return heaviside(self.args[0]._sage_()) + def _sympysage_expint(self): """ EXAMPLES:: @@ -658,6 +695,7 @@ def _sympysage_expint(self): from sage.functions.exp_integral import exp_integral_e return exp_integral_e(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_hyp(self): """ EXAMPLES:: @@ -673,6 +711,7 @@ def _sympysage_hyp(self): bq = [arg._sage_() for arg in self.args[1]] return hypergeometric(ap, bq, self.argument._sage_()) + def _sympysage_elliptic_k(self): """ EXAMPLES:: @@ -684,6 +723,7 @@ def _sympysage_elliptic_k(self): from sage.functions.special import elliptic_kc return elliptic_kc(self.args[0]._sage_()) + def _sympysage_kronecker_delta(self): """ EXAMPLES:: @@ -697,6 +737,7 @@ def _sympysage_kronecker_delta(self): from sage.functions.generalized import kronecker_delta return kronecker_delta(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_ceiling(self): """ EXAMPLES:: @@ -710,6 +751,7 @@ def _sympysage_ceiling(self): from sage.functions.other import ceil return ceil(self.args[0]._sage_()) + def _sympysage_piecewise(self): """ EXAMPLES:: @@ -726,7 +768,8 @@ def _sympysage_piecewise(self): -y*z + cases(((log(x) != 0, x^y/log(x)), (1, y))) """ from sage.functions.other import cases - return cases([(p.cond._sage_(),p.expr._sage_()) for p in self.args]) + return cases([(p.cond._sage_(), p.expr._sage_()) for p in self.args]) + def _sympysage_fresnels(self): """ @@ -742,6 +785,7 @@ def _sympysage_fresnels(self): from sage.functions.error import fresnel_sin return fresnel_sin(self.args[0]._sage_()) + def _sympysage_fresnelc(self): """ EXAMPLES:: @@ -756,6 +800,7 @@ def _sympysage_fresnelc(self): from sage.functions.error import fresnel_cos return fresnel_cos(self.args[0]._sage_()) + def _sympysage_besselj(self): """ EXAMPLES:: @@ -769,6 +814,7 @@ def _sympysage_besselj(self): from sage.functions.bessel import bessel_J return bessel_J(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_bessely(self): """ EXAMPLES:: @@ -782,6 +828,7 @@ def _sympysage_bessely(self): from sage.functions.bessel import bessel_Y return bessel_Y(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_besseli(self): """ EXAMPLES:: @@ -795,6 +842,7 @@ def _sympysage_besseli(self): from sage.functions.bessel import bessel_I return bessel_I(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_besselk(self): """ EXAMPLES:: @@ -808,6 +856,7 @@ def _sympysage_besselk(self): from sage.functions.bessel import bessel_K return bessel_K(self.args[0]._sage_(), self.args[1]._sage_()) + def _sympysage_ynm(self): """ EXAMPLES:: @@ -824,6 +873,7 @@ def _sympysage_ynm(self): self.args[2]._sage_(), self.args[3]._sage_()) + def _sympysage_re(self): """ EXAMPLES:: @@ -835,6 +885,7 @@ def _sympysage_re(self): from sage.functions.other import real_part return real_part(self.args[0]._sage_()) + def _sympysage_im(self): """ EXAMPLES:: @@ -846,6 +897,7 @@ def _sympysage_im(self): from sage.functions.other import imag_part return imag_part(self.args[0]._sage_()) + def _sympysage_abs(self): """ EXAMPLES:: @@ -857,6 +909,7 @@ def _sympysage_abs(self): from sage.functions.other import abs_symbolic return abs_symbolic(self.args[0]._sage_()) + def _sympysage_crootof(self): """ EXAMPLES:: @@ -875,6 +928,7 @@ def _sympysage_crootof(self): from sage.symbolic.ring import SR return complex_root_of(self.args[0]._sage_(), SR(self.args[1])) + def _sympysage_matrix(self): """ Convert SymPy matrix ``self`` to Sage. @@ -939,7 +993,6 @@ def _sympysage_matrix(self): [x - 1 1] sage: M == MutatedM False - """ try: return self._sage_object @@ -959,7 +1012,7 @@ def _sympysage_matrix(self): coercion_model = get_coercion_model() try: base_ring = coercion_model.common_parent(*d.values()) - except TypeError: # no common canonical parent + except TypeError: # no common canonical parent base_ring = SR result = matrix(base_ring, rows, cols, d, sparse=isinstance(self, SparseMatrix), @@ -968,6 +1021,7 @@ def _sympysage_matrix(self): self._sage_object = result return result + def _sympysage_relational(self): """ EXAMPLES:: @@ -989,9 +1043,10 @@ def _sympysage_relational(self): """ from operator import eq, ne, gt, lt, ge, le from sympy import Eq, Ne, Gt, Ge, Lt, Le - ops = {Eq : eq, Ne : ne, Gt : gt, Lt : lt, Ge : ge, Le : le} + ops = {Eq: eq, Ne: ne, Gt: gt, Lt: lt, Ge: ge, Le: le} return ops.get(self.func)(self.lhs._sage_(), self.rhs._sage_()) + def _sympysage_false(self): """ EXAMPLES:: @@ -1003,6 +1058,7 @@ def _sympysage_false(self): from sage.symbolic.ring import SR return SR(False) + def _sympysage_true(self): """ EXAMPLES:: @@ -1015,8 +1071,8 @@ def _sympysage_true(self): return SR(True) -#------------------------------------------------------------------ -from sage.misc.misc import run_once +# ------------------------------------------------------------------ + @run_once def sympy_init(): @@ -1043,12 +1099,13 @@ def sympy_init(): from sympy import Mul, Pow, Symbol, Subs from sympy.core.function import (Function, AppliedUndef, Derivative) from sympy.core.numbers import (Float, Integer, Rational, Infinity, - NegativeInfinity, ComplexInfinity, Exp1, Pi, GoldenRatio, - EulerGamma, Catalan, ImaginaryUnit) + NegativeInfinity, ComplexInfinity, + Exp1, Pi, GoldenRatio, + EulerGamma, Catalan, ImaginaryUnit) from sympy.core.numbers import NaN as sympy_nan from sympy.core.relational import Relational from sympy.functions.combinatorial.factorials import (RisingFactorial, - FallingFactorial) + FallingFactorial) from sympy.functions.elementary.complexes import (re, im, Abs) from sympy.functions.elementary.exponential import LambertW from sympy.functions.elementary.integers import ceiling @@ -1136,6 +1193,7 @@ def sympy_init(): BooleanTrue._sage_ = _sympysage_true ceiling._sage_ = _sympysage_ceiling + def check_expression(expr, var_symbols, only_from_sympy=False): """ Does ``eval(expr)`` both in Sage and SymPy and does other checks. @@ -1177,6 +1235,7 @@ def check_expression(expr, var_symbols, only_from_sympy=False): assert S(e_sage) == e_sympy assert e_sage == SR(e_sympy) + def test_all(): """ Call some tests that were originally in SymPy. @@ -1241,14 +1300,14 @@ def test_issue_4023(): from sage.symbolic.ring import SR from sage.functions.all import log from sympy import integrate, simplify - a,x = SR.var("a x") - i = integrate(log(x)/a, (x, a, a + 1)) + a, x = SR.var("a x") + i = integrate(log(x) / a, (x, a, a + 1)) i2 = simplify(i) s = SR(i2) - assert s == (a*log(1 + a) - a*log(a) + log(1 + a) - 1)/a + assert s == (a * log(1 + a) - a * log(a) + log(1 + a) - 1) / a def test_integral(): - #test Sympy-->Sage + # test Sympy-->Sage check_expression("Integral(x, (x,))", "x", only_from_sympy=True) check_expression("Integral(x, (x, 0, 1))", "x", only_from_sympy=True) check_expression("Integral(x*y, (x,), (y, ))", "x,y", only_from_sympy=True) @@ -1269,13 +1328,13 @@ def test_undefined_function(): from sympy import Symbol, Function f = function('f') sf = Function('f') - x,y = SR.var('x y') + x, y = SR.var('x y') sx = Symbol('x') sy = Symbol('y') assert f(x)._sympy_() == sf(sx) assert f(x) == sf(sx)._sage_() - assert f(x,y)._sympy_() == sf(sx, sy) - assert f(x,y) == sf(sx, sy)._sage_() + assert f(x, y)._sympy_() == sf(sx, sy) + assert f(x, y) == sf(sx, sy)._sage_() assert f._sympy_() == sf assert f == sf._sage_() @@ -1287,7 +1346,7 @@ def test_undefined_function(): test_functions() test_issue_4023() test_integral() - #test_integral_failing() + # test_integral_failing() test_undefined_function() @@ -1307,7 +1366,7 @@ def sympy_set_to_list(set, vars): if isinstance(set, (And, Or, Relational)): if isinstance(set, And): return [[item for rel in set._args[0] - for item in sympy_set_to_list(rel, vars) ]] + for item in sympy_set_to_list(rel, vars)]] elif isinstance(set, Or): return [sympy_set_to_list(iv, vars) for iv in set._args[0]] elif isinstance(set, Relational):