def equation(self, xaxis_name='x', yaxis_name='y'): """ Returns the equation for this line. Optional parameters xaxis_name and yaxis_name can be used to specify the names of the symbols used for the equation. """ x = C.Symbol(xaxis_name, real=True) y = C.Symbol(yaxis_name, real=True) a, b, c = self.coefficients return simplify(a * x + b * y + c)
def __contains__(self, o): if isinstance(o, Point): x = C.Symbol('x', real=True, dummy=True) y = C.Symbol('y', real=True, dummy=True) res = self.equation(x, y).subs({x: o[0], y: o[1]}) res = trigsimp(simplify(res)) return res == 0 elif isinstance(o, Ellipse): return (self == o) return False
def equation(self, x='x', y='y'): """ Returns the equation of the circle. Optional parameters x and y can be used to specify symbols, or the names of the symbols used in the equation. """ if isinstance(x, basestring): x = C.Symbol(x, real=True) if isinstance(y, basestring): y = C.Symbol(y, real=True) t1 = (x - self.center[0])**2 t2 = (y - self.center[1])**2 return t1 + t2 - self.hradius**2
def __contains__(self, o): """Return True if o is on this Line, or False otherwise.""" if isinstance(o, Line): return self.__eq__(o) elif isinstance(o, Point): x = C.Symbol('x', real=True) y = C.Symbol('y', real=True) r = self.equation().subs({x: o[0], y: o[1]}) x = simplify(r) return simplify(x) == 0 else: return False
def fdiff(self, argindex=1): if argindex == 1: return 1/self.args[0] s = C.Symbol('x', dummy=True) return Lambda(s**(-1), s) else: raise ArgumentIndexError(self, argindex)
def _eval_expand_trig(self, **hints): arg = self.args[0] x = None if arg.is_Add: from sympy import symmetric_poly n = len(arg.args) TX = [] for x in arg.args: tx = tan(x, evaluate=False)._eval_expand_trig() TX.append(tx) Yg = numbered_symbols('Y') Y = [Yg.next() for i in xrange(n)] p = [0, 0] for i in xrange(n + 1): p[1 - i % 2] += symmetric_poly(i, Y) * (-1)**((i % 4) // 2) return (p[0] / p[1]).subs(zip(Y, TX)) else: coeff, terms = arg.as_coeff_Mul(rational=True) if coeff.is_Integer and coeff > 1: I = S.ImaginaryUnit z = C.Symbol('dummy', real=True) P = ((1 + I * z)**coeff).expand() return (C.im(P) / C.re(P)).subs([(z, tan(terms))]) return tan(arg)
def random_point(self): """Returns a random point on the ellipse.""" from random import random t = C.Symbol('t', real=True) p = self.arbitrary_point('t') # get a random value in [-pi, pi) subs_val = float(S.Pi) * (2 * random() - 1) return Point(p[0].subs(t, subs_val), p[1].subs(t, subs_val))
def parse_expr(s, local_dict=None, transformations=standard_transformations): """ Converts the string ``s`` to a SymPy expression, in ``local_dict`` Examples ======== >>> from sympy.parsing.sympy_parser import parse_expr >>> parse_expr("1/2") 1/2 >>> type(_) <class 'sympy.core.numbers.Half'> """ if local_dict is None: local_dict = {} global_dict = {} exec 'from sympy import *' in global_dict # keep autosimplification from joining Integer or # minus sign into a Mul; this modification doesn't # prevent the 2-arg Mul from becoming an Add, however. hit = False if '(' in s: kern = '_kern' while kern in s: kern += "_" s = re.sub(r'(\d *\*|-) *\(', r'\1%s*(' % kern, s) hit = kern in s tokens = [] input_code = StringIO(s.strip()) for toknum, tokval, _, _, _ in generate_tokens(input_code.readline): tokens.append((toknum, tokval)) for transform in transformations: tokens = transform(tokens, local_dict, global_dict) code = untokenize(tokens) expr = eval( code, global_dict, local_dict) # take local objects in preference if not hit: return expr rep = {C.Symbol(kern): 1} def _clear(expr): if hasattr(expr, 'xreplace'): return expr.xreplace(rep) elif isinstance(expr, (list, tuple, set)): return type(expr)([_clear(e) for e in expr]) if hasattr(expr, 'subs'): return expr.subs(rep) return expr return _clear(expr)
def parse_expr(s, local_dict=None, rationalize=False, convert_xor=False): """ Converts the string ``s`` to a SymPy expression, in ``local_dict`` Examples ======== >>> from sympy.parsing.sympy_parser import parse_expr >>> parse_expr("1/2") 1/2 >>> type(_) <class 'sympy.core.numbers.Half'> """ if local_dict is None: local_dict = {} global_dict = {} exec 'from sympy import *' in global_dict # keep autosimplification from joining Integer or # minus sign into a Mul; this modification doesn't # prevent the 2-arg Mul from becoming and Add. hit = False if '(' in s: kern = '_kern' while kern in s: kern += "_" s = re.sub(r'(\d *\*|-) *\(', r'\1%s*(' % kern, s) hit = kern in s code = _transform(s.strip(), local_dict, global_dict, rationalize, convert_xor) expr = eval(code, global_dict, local_dict) # take local objects in preference if not hit: return expr try: return expr.xreplace({C.Symbol(kern): 1}) except (TypeError, AttributeError): return expr
def plot_interval(self, parameter_name='t'): """Returns a typical plot interval used by the plotting module.""" t = C.Symbol(parameter_name, real=True) return [t, -S.Pi, S.Pi]
def arbitrary_point(self, parameter_name='t'): """Returns a symbolic point that is on the ellipse.""" t = C.Symbol(parameter_name, real=True) return Point(self.center[0] + self.hradius * C.cos(t), self.center[1] + self.vradius * C.sin(t))
def plot_interval(self, parameter_name='t'): t = C.Symbol(parameter_name, real=True) return [t, 0, 1]
def arbitrary_point(self, parameter_name='t'): """Returns a symbolic point that is on this line segment.""" t = C.Symbol(parameter_name, real=True) x = simplify(self.p1[0] + t * (self.p2[0] - self.p1[0])) y = simplify(self.p1[1] + t * (self.p2[1] - self.p1[1])) return Point(x, y)
def plot_interval(self, parameter_name='t'): """Returns the plot interval for the default geometric plot of line""" t = C.Symbol(parameter_name, real=True) return [t, -5, 5]
def __new__(cls, expr, *symbols, **assumptions): expr = sympify(expr).expand() if expr is S.NaN: return S.NaN if symbols: symbols = map(sympify, symbols) else: symbols = list(expr.atoms(C.Symbol)) symbols.sort(Basic.compare) if expr.is_Order: new_symbols = list(expr.symbols) for s in symbols: if s not in new_symbols: new_symbols.append(s) if len(new_symbols) == len(expr.symbols): return expr symbols = new_symbols elif symbols: symbol_map = {} new_symbols = [] for s in symbols: if isinstance(s, C.Symbol): new_symbols.append(s) continue z = C.Symbol('z', dummy=True) x1, s1 = s.solve4linearsymbol(z) expr = expr.subs(x1, s1) symbol_map[z] = s new_symbols.append(z) if symbol_map: r = Order(expr, *new_symbols, **assumptions) expr = r.expr.subs(symbol_map) symbols = [] for s in r.symbols: if symbol_map.has_key(s): symbols.append(symbol_map[s]) else: symbols.append(s) else: if expr.is_Add: lst = expr.extract_leading_order(*symbols) expr = C.Add(*[f.expr for (e, f) in lst]) else: expr = expr.as_leading_term(*symbols) coeff, terms = expr.as_coeff_terms() if coeff is S.Zero: return coeff expr = C.Mul(*[t for t in terms if t.has(*symbols)]) elif expr is not S.Zero: expr = S.One if expr is S.Zero: return expr # create Order instance: obj = Basic.__new__(cls, expr, *symbols, **assumptions) return obj
""" This module mainly implements special orthogonal polynomials. See also functions.combinatorial.numbers which contains some combinatorial polynomials. """ from sympy.core.basic import Basic, S, C from sympy.core import Rational, Symbol from sympy.core.function import Function from sympy.utilities.memoization import recurrence_memo, assoc_recurrence_memo _x = C.Symbol('x', dummy=True) class PolynomialSequence(Function): """Polynomial sequence with 1 index n >= 0 """ nargs = 2 @classmethod def canonize(cls, n, x): if n.is_integer and n >= 0: return cls.calc(int(n)).subs(_x, x) if n.is_negative: raise ValueError("%s index must be nonnegative integer (got %r)" % (cls, n))