def test_expand_mul(): # part of issue 20597 e = Mul(2, 3, evaluate=False) assert e.expand() == 6 e = Mul(2, 3, 1 / x, evaluate=False) assert e.expand() == 6 / x e = Mul(2, R(1, 3), evaluate=False) assert e.expand() == R(2, 3)
def test_expand_radicals(): a = (x + y)**R(1, 2) assert (a**1).expand() == a assert (a**3).expand() == x*a + y*a assert (a**5).expand() == x**2*a + 2*x*y*a + y**2*a assert (1/a**1).expand() == 1/a assert (1/a**3).expand() == 1/(x*a + y*a) assert (1/a**5).expand() == 1/(x**2*a + 2*x*y*a + y**2*a) a = (x + y)**R(1, 3) assert (a**1).expand() == a assert (a**2).expand() == a**2 assert (a**4).expand() == x*a + y*a assert (a**5).expand() == x*a**2 + y*a**2 assert (a**7).expand() == x**2*a + 2*x*y*a + y**2*a
def test_issues_5919_6830(): # issue 5919 n = -1 + 1 / x z = n / x / (-n)**2 - 1 / n / x assert expand( z) == 1 / (x**2 - 2 * x + 1) - 1 / (x - 2 + 1 / x) - 1 / (-x + 1) # issue 6830 p = (1 + x)**2 assert expand_multinomial( (1 + x * p)**2) == (x**2 * (x**4 + 4 * x**3 + 6 * x**2 + 4 * x + 1) + 2 * x * (x**2 + 2 * x + 1) + 1) assert expand_multinomial( (1 + (y + x) * p)**2) == (2 * ((x + y) * (x**2 + 2 * x + 1)) + (x**2 + 2 * x * y + y**2) * (x**4 + 4 * x**3 + 6 * x**2 + 4 * x + 1) + 1) A = Symbol('A', commutative=False) p = (1 + A)**2 assert expand_multinomial( (1 + x * p)**2) == (x**2 * (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) + 2 * x * (1 + 2 * A + A**2) + 1) assert expand_multinomial( (1 + (y + x) * p)**2) == ((x + y) * (1 + 2 * A + A**2) * 2 + (x**2 + 2 * x * y + y**2) * (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) + 1) assert expand_multinomial((1 + (y + x) * p)**3) == ( (x + y) * (1 + 2 * A + A**2) * 3 + (x**2 + 2 * x * y + y**2) * (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) * 3 + (x**3 + 3 * x**2 * y + 3 * x * y**2 + y**3) * (1 + 6 * A + 15 * A**2 + 20 * A**3 + 15 * A**4 + 6 * A**5 + A**6) + 1) # unevaluate powers eq = (Pow((x + 1) * ((A + 1)**2), 2, evaluate=False)) # - in this case the base is not an Add so no further # expansion is done assert expand_multinomial(eq) == \ (x**2 + 2*x + 1)*(1 + 4*A + 6*A**2 + 4*A**3 + A**4) # - but here, the expanded base *is* an Add so it gets expanded eq = (Pow(((A + 1)**2), 2, evaluate=False)) assert expand_multinomial(eq) == 1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4 # coverage def ok(a, b, n): e = (a + I * b)**n return verify_numerically(e, expand_multinomial(e)) for a in [2, S.Half]: for b in [3, R(1, 3)]: for n in range(2, 6): assert ok(a, b, n) assert expand_multinomial((x + 1 + O(z))**2) == \ 1 + 2*x + x**2 + O(z) assert expand_multinomial((x + 1 + O(z))**3) == \ 1 + 3*x + 3*x**2 + x**3 + O(z) assert expand_multinomial(3**(x + y + 3)) == 27 * 3**(x + y)
def test_expand_arit(): a = Symbol("a") b = Symbol("b", positive=True) c = Symbol("c") p = R(5) e = (a + b)*c assert e == c*(a + b) assert (e.expand() - a*c - b*c) == R(0) e = (a + b)*(a + b) assert e == (a + b)**2 assert e.expand() == 2*a*b + a**2 + b**2 e = (a + b)*(a + b)**R(2) assert e == (a + b)**3 assert e.expand() == 3*b*a**2 + 3*a*b**2 + a**3 + b**3 assert e.expand() == 3*b*a**2 + 3*a*b**2 + a**3 + b**3 e = (a + b)*(a + c)*(b + c) assert e == (a + c)*(a + b)*(b + c) assert e.expand() == 2*a*b*c + b*a**2 + c*a**2 + b*c**2 + a*c**2 + c*b**2 + a*b**2 e = (a + R(1))**p assert e == (1 + a)**5 assert e.expand() == 1 + 5*a + 10*a**2 + 10*a**3 + 5*a**4 + a**5 e = (a + b + c)*(a + c + p) assert e == (5 + a + c)*(a + b + c) assert e.expand() == 5*a + 5*b + 5*c + 2*a*c + b*c + a*b + a**2 + c**2 x = Symbol("x") s = exp(x*x) - 1 e = s.nseries(x, 0, 3)/x**2 assert e.expand() == 1 + x**2/2 + O(x**4) e = (x*(y + z))**(x*(y + z))*(x + y) assert e.expand(power_exp=False, power_base=False) == x*(x*y + x* z)**(x*y + x*z) + y*(x*y + x*z)**(x*y + x*z) assert e.expand(power_exp=False, power_base=False, deep=False) == x* \ (x*(y + z))**(x*(y + z)) + y*(x*(y + z))**(x*(y + z)) e = x * (x + (y + 1)**2) assert e.expand(deep=False) == x**2 + x*(y + 1)**2 e = (x*(y + z))**z assert e.expand(power_base=True, mul=True, deep=True) in [x**z*(y + z)**z, (x*y + x*z)**z] assert ((2*y)**z).expand() == 2**z*y**z p = Symbol('p', positive=True) assert sqrt(-x).expand().is_Pow assert sqrt(-x).expand(force=True) == I*sqrt(x) assert ((2*y*p)**z).expand() == 2**z*p**z*y**z assert ((2*y*p*x)**z).expand() == 2**z*p**z*(x*y)**z assert ((2*y*p*x)**z).expand(force=True) == 2**z*p**z*x**z*y**z assert ((2*y*p*-pi)**z).expand() == 2**z*pi**z*p**z*(-y)**z assert ((2*y*p*-pi*x)**z).expand() == 2**z*pi**z*p**z*(-x*y)**z n = Symbol('n', negative=True) m = Symbol('m', negative=True) assert ((-2*x*y*n)**z).expand() == 2**z*(-n)**z*(x*y)**z assert ((-2*x*y*n*m)**z).expand() == 2**z*(-m)**z*(-n)**z*(-x*y)**z # issue 5482 assert sqrt(-2*x*n) == sqrt(2)*sqrt(-n)*sqrt(x) # issue 5605 (2) assert (cos(x + y)**2).expand(trig=True) in [ (-sin(x)*sin(y) + cos(x)*cos(y))**2, sin(x)**2*sin(y)**2 - 2*sin(x)*sin(y)*cos(x)*cos(y) + cos(x)**2*cos(y)**2 ] # Check that this isn't too slow x = Symbol('x') W = 1 for i in range(1, 21): W = W * (x - i) W = W.expand() assert W.has(-1672280820*x**15)
__email__ = "*****@*****.**" def getWeights(stencil): Q = len(stencil) def weightForDirection(direction): absSum = sum([abs(d) for d in direction]) return getWeights.weights[Q][absSum] return [weightForDirection(d) for d in stencil] getWeights.weights = { 9: { 0: R(4, 9), 1: R(1, 9), 2: R(1, 36), }, 15: { 0: R(2, 9), 1: R(1, 9), 3: R(1, 72), }, 19: { 0: R(1, 3), 1: R(1, 18), 2: R(1, 36), }, 27: { 0: R(8, 27),
def test_issue_6121(): eq = -I*exp(-3*I*pi/4)/(4*pi**(S(3)/2)*sqrt(x)) assert eq.expand(complex=True) # does not give oo recursion eq = -I*exp(-3*I*pi/4)/(4*pi**(R(3, 2))*sqrt(x)) assert eq.expand(complex=True) # does not give oo recursion
def _calcMetrics(self): """Calculates the co and contravariant metrics""" #{{{Covariant g # Initialize covariant g self.g_ = {} # Initialize list which will be the input in the matrix matrix = [] for i in cylCoord: # Initialize second coord self.g_[i] = {} # Initialize a row in the matrix row = [] for j in cylCoord: self.g_[i][j] = simplify(\ self.__getattribute__(str(i) + 'CovBasis')*\ self.__getattribute__(str(j) + 'CovBasis')\ ) # Append the element to the row row.append(self.g_[i][j]) # Append the row to the matrix matrix.append(row) # Define the matrix (makes it easy to display) self.covMetrics = Matrix(matrix) #}}} #{{{Contravariant g # Initialize contravariant g self.g = {} # Initialize list which will be the input in the matrix matrix = [] for i in cylCoord: # Initialize second coord self.g[i] = {} # Initialize a row in the matrix row = [] for j in cylCoord: self.g[i][j] = simplify(\ self.__getattribute__(str(i) + 'ConBasis')*\ self.__getattribute__(str(j) + 'ConBasis')\ ) # Append the element to the row row.append(self.g[i][j]) # Append the row to the matrix matrix.append(row) # Define the matrix (makes it easy to display) self.conMetrics = Matrix(matrix) #}}} #{{{ Calculate J # Define the jacobian self.J = simplify(sqrt(self.covMetrics.det())) #}}} #{{{Calculate Christoffel #The Christoffel symbols reads #$$ #\Gamma^i_{kl}=\frac{1}{2}g^{im} #\left( # \frac{\partial g_{mk}}{\partial x^l} + \frac{\partial # g_{ml}}{\partial x^k} - \frac{\partial g_{kl}}{\partial x^m} # \right) #$$ self.C = dict.fromkeys(cylCoord) for i in cylCoord: self.C[i] = dict.fromkeys(cylCoord) # Initialize list which will be the input in the matrix matrix = [] for j in cylCoord: # Initialize a row in the matrix row = [] self.C[i][j] = dict.fromkeys(cylCoord) for k in cylCoord: self.C[i][j][k] = 0 for l in cylCoord: self.C[i][j][k] +=\ R(1/2)*self.g[i][l]\ *(\ self.g_[l][j].diff(k)\ + self.g_[l][k].diff(j)\ - self.g_[j][k].diff(l)\ ) self.C[i][j][k] = simplify(self.C[i][j][k]) # Append the element to the row row.append(self.C[i][j][k]) # Append the row to the matrix matrix.append(row) # Define the matrix (makes it easy to display) self.__setattr__('C' + str(i), Matrix(matrix))
def time_expnad_radicals(self): (((x + y)**R(1, 2))**3).expand() (1 / ((x + y)**R(1, 2))**3).expand()
def rat(self): return R(self.sign * self.significand * (2**self.exponent))
# coding=utf-8 from util import latex from fractions import Fraction as f import sympy as sy import numpy as np from sympy import Rational as R from gurobipy import * from math import * from operator import itemgetter A = np.array([ [R('-20'), R('-35'), R('-95'), R('1'), R('0'), R('-319')], [R('-5'), R('-9'), R('-23'), R('0'), R('1'), R('0')], ]) A.astype('object') class Simplex: def __init__(self, A): self.A = A self.height = len(A) self.width = len(A[0]) self.c = A[self.height - 1] def primalPivot(self, i=0, doPrint=True): if self.c[i] <= 0: print "Error, cannot iterate on negative or zero c at idx: " + str( i)
#! /usr/bin/env python3 # -*- coding: utf-8 -*- # 2018-04-10T17:48+08:00 from sympy import Rational as R from sympy.abc import x, y from sympy.plotting import plot_implicit # x^2 + y^2 = (2*x^2 + 2*y^2 - x)^2 plot_implicit(x**2 + y**2 - (2 * x**2 + 2 * y**2 - x)**2, (x, -0.5, 1.5), (y, -0.75, 0.75), title='cardioid') # x^(2/3) + y^(2/3) = 4 plot_implicit(x**R(2, 3) + y**R(2, 3) - 4, (x, -10, 10), (y, -10, 10), title='astroid') # 2*(x^2 + y^2)^2 = 25*(x^2 - y^2) plot_implicit(2 * (x**2 + y**2)**2 - 25 * (x**2 - y**2), title='lemniscate') # y^2*(y^2 - 4) = x^2*(x^2 - 5) plot_implicit(y**2 * (y**2 - 4) - x**2 * (x**2 - 5), title='devil\'s curve') # References: # Calculus, 7ed, James.Stewart, P157