Exemplo n.º 1
0
def test_separatevars_advanced_factor():
    x,y,z = symbols('x,y,z')
    assert separatevars(1 + log(x)*log(y) + log(x) + log(y)) == (log(x) + 1)*(log(y) + 1)
    assert separatevars(1 + x - log(z) - x*log(z) - exp(y)*log(z) - x*exp(y)*log(z) + x*exp(y) + exp(y)) == \
        -((x + 1)*(log(z) - 1)*(exp(y) + 1))
    x, y = symbols('x,y', positive=True)
    assert separatevars(1 + log(x**log(y)) + log(x*y)) == (log(x) + 1)*(log(y) + 1)
Exemplo n.º 2
0
def test_separatevars_advanced_factor():
    x,y,z = symbols('x,y,z')
    assert separatevars(1 + log(x)*log(y) + log(x) + log(y)) == (log(x) + 1)*(log(y) + 1)
    assert separatevars(1 + x - log(z) - x*log(z) - exp(y)*log(z) - x*exp(y)*log(z) + x*exp(y) + exp(y)) == \
        -((x + 1)*(log(z) - 1)*(exp(y) + 1))
    x, y = symbols('x,y', positive=True)
    assert separatevars(1 + log(x**log(y)) + log(x*y)) == (log(x) + 1)*(log(y) + 1)
Exemplo n.º 3
0
def test_separatevars_advanced_factor():
    # If factor() is ever improved to factor non-symbolic expressions, this
    # should XPASS
    assert separatevars(1 + log(x)*log(y) + log(x) + log(y)) == (log(x) + 1)*(log(y) + 1)
    assert separatevars(1 + x - log(z) - x*log(z) - exp(y)*log(z) - \
        x*exp(y)*log(z) + x*exp(y) + exp(y)) == \
        (1 + x)*(1 - log(z))*(1 + exp(y))
    x, y = symbols('xy', positive=True)
    assert separatevars(1 + log(x**log(y)) + log(x*y)) == (log(x) + 1)*(log(y) + 1)
Exemplo n.º 4
0
def test_separatevars():
    x,y,z,n = symbols('xyzn')
    assert separatevars(2*n*x*z+2*x*y*z) == 2*x*z*(n+y)
    assert separatevars(x*z+x*y*z) == x*z*(1+y)
    assert separatevars(pi*x*z+pi*x*y*z) == pi*x*z*(1+y)
    assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == x*(sin(y) + y**2)*sin(x)
    assert separatevars(x*exp(x+y)+x*exp(x)) == x*(1 + exp(y))*exp(x)
    assert separatevars((x*(y+1))**z) == x**z*(1 + y)**z
    assert separatevars(1+x+y+x*y) == (x+1)*(y+1)
    assert separatevars(y / pi * exp(-(z - x) / cos(n))) == y * exp((x - z) / cos(n)) / pi
    # 1759
    p=Symbol('p',positive=True)
    assert separatevars(sqrt(p**2 + x*p**2)) == p*sqrt(1 + x)
    assert separatevars(sqrt(y*(p**2 + x*p**2))) == p*sqrt(y)*sqrt(1 + x)
Exemplo n.º 5
0
def test_separatevars():
    x,y,z,n = symbols('xyzn')
    assert separatevars(2*n*x*z+2*x*y*z) == 2*x*z*(n+y)
    assert separatevars(x*z+x*y*z) == x*z*(1+y)
    assert separatevars(pi*x*z+pi*x*y*z) == pi*x*z*(1+y)
    assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == x*(sin(y) + y**2)*sin(x)
    assert separatevars(x*exp(x+y)+x*exp(x)) == x*(1 + exp(y))*exp(x)
    assert separatevars((x*(y+1))**z) == x**z*(1 + y)**z
    assert separatevars(1+x+y+x*y) == (x+1)*(y+1)
    assert separatevars(y / pi * exp(-(z - x) / cos(n))) == y * exp((x - z) / cos(n)) / pi
    # 1759
    p=Symbol('p',positive=True)
    assert separatevars(sqrt(p**2 + x*p**2)) == p*sqrt(1 + x)
    assert separatevars(sqrt(y*(p**2 + x*p**2))) == p*sqrt(y)*sqrt(1 + x)
Exemplo n.º 6
0
def test_separatevars():
    x,y,z,n = symbols('xyzn')
    assert separatevars(2*n*x*z+2*x*y*z) == 2*x*z*(n+y)
    assert separatevars(x*z+x*y*z) == x*z*(1+y)
    assert separatevars(pi*x*z+pi*x*y*z) == pi*x*z*(1+y)
    assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == x*(sin(y) + y**2)*sin(x)
    assert separatevars(x*exp(x+y)+x*exp(x)) == x*(1 + exp(y))*exp(x)
    assert separatevars((x*(y+1))**z) == x**z*(1 + y)**z
    assert separatevars(1+x+y+x*y) == (x+1)*(y+1)
Exemplo n.º 7
0
 def _banish(b, x, w, m):
     if x not in m.free_symbols:
         return Mul(Expect(b, Lambda(x, w)), m)
     if m.is_Mul:
         (w1, m) = unweight(m)
         separation = separatevars(w1, [x], dict=True)
         return separation['coeff'] * _banish(b, x, w * separation[x], m)
     if m.is_Add:
         return Add(*(_banish(b, x, w, term) for term in m.args))
     if isinstance(m, Bind) and x not in m.before.free_symbols:
         return Bind(m.before, m.variable, _banish(b, x, w, m.after))
     raise BanishFailure(x, m)
Exemplo n.º 8
0
 def _split(ms):
     ms = sp.sympify(ms)
     if isinstance(ms, Number):
         d = {'coeff': ms}
     elif len(ms.free_symbols) == 0:
         d = {'coeff': ms.evalf()}
     else:
         d = sp.separatevars(ms.factor(), dict=True)
     if d is None:
         raise RuntimeError('Could not split', ms)
     d = defaultdict(lambda: 1, {str(k): v for k, v in d.items()})
     dc = d['coeff']
     d['coeff'] = int(dc) if isinstance(dc,
                                        (sp.Integer, int)) else float(dc)
     return d
Exemplo n.º 9
0
def alap(n):
    from sympy.integrals.transforms import inverse_laplace_transform
    from sympy import separatevars
    import sympy
    from sympy import simplify

    from sympy.parsing.sympy_parser import parse_expr

    x = sympy.Symbol("s", real=True)
    f = parse_expr(n)
    f = f.subs({parse_expr("s"): x})

    f = inverse_laplace_transform(f, x, 't')
    f = separatevars(f, force=True)
    #print(f)
    f = simplify(f, measure=my_measure1, ratio=10)

    return str(f)
Exemplo n.º 10
0
 def _straighten(m, k):
     if m.is_Add:
         if add:
             return Add(*(_straighten(term, k) for term in m.args))
         else:
             m = Add(*(_straighten(term, Ret) for term in m.args))
             # fall through
     if m.is_zero:
         return S.Zero
     if isinstance(m, Ret):
         return k(m.value)
     if m.is_Mul:
         (w, m) = unweight(m)
         return w * _straighten(m, k)
     if isinstance(m, Bind):
         (x, after) = m.enter([k])
         (w, afterk) = unweight(_straighten(after, k))
         separation = separatevars(w, [x], dict=True)
         return separation['coeff'] * _straighten(m.before, Lambda(x, separation[x] * afterk))
     if k == Ret:
         return m
     r = parameter(k, 'r')
     return Bind(m, r, k(r))
# module 3 Separation of Variables
# part 1 Method of Substitution
import sympy
from sympy.abc import t
print sympy.integrate(sympy.cos(t**2), t)

print sympy.integrate(sympy.cos(t**2) * 2 * t, t)

# ex1
from sympy.abc import x
print sympy.integrate((4 * x) / (1 + 4 * x**2), x)

# part 2 separable equations
from sympy.abc import y
f = -2 * t * y**2
print sympy.separatevars(f, symbols=(t, y))

print sympy.separatevars(y**3 + t**2, symbols=(t, y))

# ex2
print sympy.separatevars(t * y + t * y**2, symbols=(t, y))

print sympy.separatevars(t**2 * y + t * y**2, symbols=(t, y))

# ex3

# ex4
from sympy import Function, dsolve, Derivative
y = Function('y')
dsolve(Derivative(y(t), t) - 2 * t * (y(t))**2 - 3 * (y(t))**2, y(t))
Exemplo n.º 12
0
def test_separatevars():
    x, y, z, n = symbols('x,y,z,n')
    assert separatevars(2 * n * x * z + 2 * x * y * z) == 2 * x * z * (n + y)
    assert separatevars(x * z + x * y * z) == x * z * (1 + y)
    assert separatevars(pi * x * z + pi * x * y * z) == pi * x * z * (1 + y)
    assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == \
        x*(sin(y) + y**2)*sin(x)
    assert separatevars(x * exp(x + y) +
                        x * exp(x)) == x * (1 + exp(y)) * exp(x)
    assert separatevars((x * (y + 1))**z).is_Pow  # != x**z*(1 + y)**z
    assert separatevars(1 + x + y + x * y) == (x + 1) * (y + 1)
    assert separatevars(y/pi*exp(-(z - x)/cos(n))) == \
        y*exp(x/cos(n))*exp(-z/cos(n))/pi
    assert separatevars((x + y) * (x - y) + y**2 + 2 * x + 1) == (x + 1)**2
    # issue 4858
    p = Symbol('p', positive=True)
    assert separatevars(sqrt(p**2 + x * p**2)) == p * sqrt(1 + x)
    assert separatevars(sqrt(y * (p**2 + x * p**2))) == p * sqrt(y * (1 + x))
    assert separatevars(sqrt(y*(p**2 + x*p**2)), force=True) == \
        p*sqrt(y)*sqrt(1 + x)
    # issue 4865
    assert separatevars(sqrt(x * y)).is_Pow
    assert separatevars(sqrt(x * y), force=True) == sqrt(x) * sqrt(y)
    # issue 4957
    # any type sequence for symbols is fine
    assert separatevars(((2*x + 2)*y), dict=True, symbols=()) == \
        {'coeff': 1, x: 2*x + 2, y: y}
    # separable
    assert separatevars(((2*x + 2)*y), dict=True, symbols=[x]) == \
        {'coeff': y, x: 2*x + 2}
    assert separatevars(((2*x + 2)*y), dict=True, symbols=[]) == \
        {'coeff': 1, x: 2*x + 2, y: y}
    assert separatevars(((2*x + 2)*y), dict=True) == \
        {'coeff': 1, x: 2*x + 2, y: y}
    assert separatevars(((2*x + 2)*y), dict=True, symbols=None) == \
        {'coeff': y*(2*x + 2)}
    # not separable
    assert separatevars(3, dict=True) is None
    assert separatevars(2 * x + y, dict=True, symbols=()) is None
    assert separatevars(2 * x + y, dict=True) is None
    assert separatevars(2 * x + y, dict=True, symbols=None) == {
        'coeff': 2 * x + y
    }
    # issue 4808
    n, m = symbols('n,m', commutative=False)
    assert separatevars(m + n * m) == (1 + n) * m
    assert separatevars(x + x * n) == x * (1 + n)
    # issue 4910
    f = Function('f')
    assert separatevars(f(x) + x * f(x)) == f(x) + x * f(x)
    # a noncommutable object present
    eq = x * (1 + hyper((), (), y * z))
    assert separatevars(eq) == eq
Exemplo n.º 13
0
def test_separatevars():
    x, y, z, n = symbols('x,y,z,n')
    assert separatevars(2*n*x*z + 2*x*y*z) == 2*x*z*(n + y)
    assert separatevars(x*z + x*y*z) == x*z*(1 + y)
    assert separatevars(pi*x*z + pi*x*y*z) == pi*x*z*(1 + y)
    assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == \
        x*(sin(y) + y**2)*sin(x)
    assert separatevars(x*exp(x + y) + x*exp(x)) == x*(1 + exp(y))*exp(x)
    assert separatevars((x*(y + 1))**z).is_Pow  # != x**z*(1 + y)**z
    assert separatevars(1 + x + y + x*y) == (x + 1)*(y + 1)
    assert separatevars(y/pi*exp(-(z - x)/cos(n))) == \
        y*exp(x/cos(n))*exp(-z/cos(n))/pi
    assert separatevars((x + y)*(x - y) + y**2 + 2*x + 1) == (x + 1)**2
    # issue 4858
    p = Symbol('p', positive=True)
    assert separatevars(sqrt(p**2 + x*p**2)) == p*sqrt(1 + x)
    assert separatevars(sqrt(y*(p**2 + x*p**2))) == p*sqrt(y*(1 + x))
    assert separatevars(sqrt(y*(p**2 + x*p**2)), force=True) == \
        p*sqrt(y)*sqrt(1 + x)
    # issue 4865
    assert separatevars(sqrt(x*y)).is_Pow
    assert separatevars(sqrt(x*y), force=True) == sqrt(x)*sqrt(y)
    # issue 4957
    # any type sequence for symbols is fine
    assert separatevars(((2*x + 2)*y), dict=True, symbols=()) == \
        {'coeff': 1, x: 2*x + 2, y: y}
    # separable
    assert separatevars(((2*x + 2)*y), dict=True, symbols=[x]) == \
        {'coeff': y, x: 2*x + 2}
    assert separatevars(((2*x + 2)*y), dict=True, symbols=[]) == \
        {'coeff': 1, x: 2*x + 2, y: y}
    assert separatevars(((2*x + 2)*y), dict=True) == \
        {'coeff': 1, x: 2*x + 2, y: y}
    assert separatevars(((2*x + 2)*y), dict=True, symbols=None) == \
        {'coeff': y*(2*x + 2)}
    # not separable
    assert separatevars(3, dict=True) is None
    assert separatevars(2*x + y, dict=True, symbols=()) is None
    assert separatevars(2*x + y, dict=True) is None
    assert separatevars(2*x + y, dict=True, symbols=None) == {'coeff': 2*x + y}
    # issue 4808
    n, m = symbols('n,m', commutative=False)
    assert separatevars(m + n*m) == (1 + n)*m
    assert separatevars(x + x*n) == x*(1 + n)
    # issue 4910
    f = Function('f')
    assert separatevars(f(x) + x*f(x)) == f(x) + x*f(x)
    # a noncommutable object present
    eq = x*(1 + hyper((), (), y*z))
    assert separatevars(eq) == eq
Exemplo n.º 14
0
def test_separatevars():
    x, y, z, n = symbols('x,y,z,n')
    assert separatevars(2 * n * x * z + 2 * x * y * z) == 2 * x * z * (n + y)
    assert separatevars(x * z + x * y * z) == x * z * (1 + y)
    assert separatevars(pi * x * z + pi * x * y * z) == pi * x * z * (1 + y)
    assert separatevars(x * y**2 * sin(x) +
                        x * sin(x) * sin(y)) == x * (sin(y) + y**2) * sin(x)
    assert separatevars(x * exp(x + y) +
                        x * exp(x)) == x * (1 + exp(y)) * exp(x)
    assert separatevars((x * (y + 1))**z).is_Pow  # != x**z*(1 + y)**z
    assert separatevars(1 + x + y + x * y) == (x + 1) * (y + 1)
    assert separatevars(y / pi * exp(-(z - x) / cos(n))) == y * exp(
        (x - z) / cos(n)) / pi
    # 1759
    p = Symbol('p', positive=True)
    assert separatevars(sqrt(p**2 + x * p**2)) == p * sqrt(1 + x)
    assert separatevars(sqrt(y * (p**2 + x * p**2))) == p * sqrt(y * (1 + x))
    assert separatevars(sqrt(y * (p**2 + x * p**2)),
                        force=True) == p * sqrt(y) * sqrt(1 + x)
    # 1766
    assert separatevars(sqrt(x * y)).is_Pow
    assert separatevars(sqrt(x * y), force=True) == sqrt(x) * sqrt(y)
    # 1858
    # any type sequence for symbols is fine
    assert separatevars(((2 * x + 2) * y), dict=True, symbols=()) == {
        'coeff': 2,
        x: x + 1,
        y: y
    }
    # separable
    assert separatevars(((2 * x + 2) * y), dict=True, symbols=[]) == {
        'coeff': 2,
        x: x + 1,
        y: y
    }
    assert separatevars(((2 * x + 2) * y), dict=True) == {
        'coeff': 2,
        x: x + 1,
        y: y
    }
    assert separatevars(((2 * x + 2) * y), dict=True, symbols=None) == {
        'coeff': 2 * y * (x + 1)
    }
    # not separable
    assert separatevars(2 * x + y, dict=True, symbols=()) is None
    assert separatevars(2 * x + y, dict=True) is None
    assert separatevars(2 * x + y, dict=True, symbols=None) == {
        'coeff': 2 * x + y
    }
Exemplo n.º 15
0
display(p_s_mu)
display(syp.nsimplify(p_dmu_mu))

display(pF)

# %%
F = -log(pF[0]) - C
F = F.expand(force=True)
F = F.collect(sigma_s)
F = F.collect(Sigma_x)
F = syp.nsimplify(F)
display(F)

# %%
gd_mux = Eq(-diff("F", mux, evaluate=False),
            -syp.separatevars(diff(F, mux), force=True),
            evaluate=False)
display(gd_mux)
print(syp.latex(gd_mux))

# %%
d_dmux = Eq(-diff("F", dmux, evaluate=False),
            -diff(F, dmux).simplify(),
            evaluate=False)
display(d_dmux)
print(syp.latex(d_dmux))

# %%
a = symbols("a", real=True)
sa = syp.Function("s")(a)
F = F.subs(s, sa)
Exemplo n.º 16
0
def get_order(fn):
    seper = sp.separatevars(fn,(v_xsnorm,v_ysnorm),dict=True)
    res = (seper[v_xsnorm] * seper[v_ysnorm]).subs(v_xsnorm,2).subs(v_ysnorm,2.0001)
    return res
Exemplo n.º 17
0
print("Calculating Taylor expansion...")
f_approx = Taylor_polynomial_sympy(f_exact, (v_xsnorm,v_ysnorm), (0,0), order)



def get_order(fn):
    seper = sp.separatevars(fn,(v_xsnorm,v_ysnorm),dict=True)
    res = (seper[v_xsnorm] * seper[v_ysnorm]).subs(v_xsnorm,2).subs(v_ysnorm,2.0001)
    return res

print("Sorting...")
f_approx_sum = list(f_approx.args)
f_approx_sum.sort(key=get_order)

f_approx_all = [sp.separatevars(fn,(v_xsnorm,v_ysnorm),dict=True) for fn in f_approx_sum]

print("Simplifying...")
for i in range(len(f_approx_all)):
    f_approx_all[i][v_xsnorm] *= f_approx_all[i][v_ysnorm]
    f_approx_all[i][v_xsnorm] = sp.simplify(f_approx_all[i][v_xsnorm])
    f_approx_all[i]["coeff"] = sp.simplify(f_approx_all[i]["coeff"])

print("Reducing...")
for i in range(len(f_approx_all)):
    j = i
    while j<len(f_approx_all):
        if i!=j and i<len(f_approx_all) and j<len(f_approx_all):
            c = f_approx_all[j]["coeff"] / f_approx_all[i]["coeff"]
            if c.is_constant():
                c = sp.simplify(c)
Exemplo n.º 18
0
def test_separatevars():
    x,y,z,n = symbols('x,y,z,n')
    assert separatevars(2*n*x*z+2*x*y*z) == 2*x*z*(n+y)
    assert separatevars(x*z+x*y*z) == x*z*(1+y)
    assert separatevars(pi*x*z+pi*x*y*z) == pi*x*z*(1+y)
    assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == x*(sin(y) + y**2)*sin(x)
    assert separatevars(x*exp(x+y)+x*exp(x)) == x*(1 + exp(y))*exp(x)
    assert separatevars((x*(y+1))**z).is_Pow # != x**z*(1 + y)**z
    assert separatevars(1+x+y+x*y) == (x+1)*(y+1)
    assert separatevars(y / pi * exp(-(z - x) / cos(n))) == y * exp((x - z) / cos(n)) / pi
    # 1759
    p=Symbol('p',positive=True)
    assert separatevars(sqrt(p**2 + x*p**2)) == p*sqrt(1 + x)
    assert separatevars(sqrt(y*(p**2 + x*p**2))) == p*sqrt(y*(1 + x))
    assert separatevars(sqrt(y*(p**2 + x*p**2)), force=True) == p*sqrt(y)*sqrt(1 + x)
    # 1766
    assert separatevars(sqrt(x*y)).is_Pow
    assert separatevars(sqrt(x*y), force=True) == sqrt(x)*sqrt(y)
    # 1858
    # any type sequence for symbols is fine
    assert separatevars(((2*x+2)*y), dict=True, symbols=()) == {'coeff': 2, x: x + 1, y: y}
    # separable
    assert separatevars(((2*x+2)*y), dict=True, symbols=[]) == {'coeff': 2, x: x + 1, y: y}
    assert separatevars(((2*x+2)*y), dict=True) == {'coeff': 2, x: x + 1, y: y}
    assert separatevars(((2*x+2)*y), dict=True, symbols=None) == {'coeff': 2*y*(x + 1)}
    # not separable
    assert separatevars(2*x+y, dict=True, symbols=()) is None
    assert separatevars(2*x+y, dict=True) is None
    assert separatevars(2*x+y, dict=True, symbols=None) == {'coeff': 2*x + y}
Exemplo n.º 19
0
def test_separatevars():
    x, y, z, n = symbols("x,y,z,n")
    assert separatevars(2 * n * x * z + 2 * x * y * z) == 2 * x * z * (n + y)
    assert separatevars(x * z + x * y * z) == x * z * (1 + y)
    assert separatevars(pi * x * z + pi * x * y * z) == pi * x * z * (1 + y)
    assert separatevars(x * y**2 * sin(x) +
                        x * sin(x) * sin(y)) == x * (sin(y) + y**2) * sin(x)
    assert separatevars(x * exp(x + y) +
                        x * exp(x)) == x * (1 + exp(y)) * exp(x)
    assert separatevars((x * (y + 1))**z).is_Pow  # != x**z*(1 + y)**z
    assert separatevars(1 + x + y + x * y) == (x + 1) * (y + 1)
    assert (separatevars(y / pi * exp(-(z - x) / cos(n))) == y *
            exp(x / cos(n)) * exp(-z / cos(n)) / pi)
    assert separatevars((x + y) * (x - y) + y**2 + 2 * x + 1) == (x + 1)**2
    # issue 4858
    p = Symbol("p", positive=True)
    assert separatevars(sqrt(p**2 + x * p**2)) == p * sqrt(1 + x)
    assert separatevars(sqrt(y * (p**2 + x * p**2))) == p * sqrt(y * (1 + x))
    assert separatevars(sqrt(y * (p**2 + x * p**2)),
                        force=True) == p * sqrt(y) * sqrt(1 + x)
    # issue 4865
    assert separatevars(sqrt(x * y)).is_Pow
    assert separatevars(sqrt(x * y), force=True) == sqrt(x) * sqrt(y)
    # issue 4957
    # any type sequence for symbols is fine
    assert separatevars(((2 * x + 2) * y), dict=True, symbols=()) == {
        "coeff": 1,
        x: 2 * x + 2,
        y: y,
    }
    # separable
    assert separatevars(((2 * x + 2) * y), dict=True, symbols=[x]) == {
        "coeff": y,
        x: 2 * x + 2,
    }
    assert separatevars(((2 * x + 2) * y), dict=True, symbols=[]) == {
        "coeff": 1,
        x: 2 * x + 2,
        y: y,
    }
    assert separatevars(((2 * x + 2) * y), dict=True) == {
        "coeff": 1,
        x: 2 * x + 2,
        y: y,
    }
    assert separatevars(((2 * x + 2) * y), dict=True, symbols=None) == {
        "coeff": y * (2 * x + 2)
    }
    # not separable
    assert separatevars(3, dict=True) is None
    assert separatevars(2 * x + y, dict=True, symbols=()) is None
    assert separatevars(2 * x + y, dict=True) is None
    assert separatevars(2 * x + y, dict=True, symbols=None) == {
        "coeff": 2 * x + y
    }
    # issue 4808
    n, m = symbols("n,m", commutative=False)
    assert separatevars(m + n * m) == (1 + n) * m
    assert separatevars(x + x * n) == x * (1 + n)
    # issue 4910
    f = Function("f")
    assert separatevars(f(x) + x * f(x)) == f(x) + x * f(x)
    # a noncommutable object present
    eq = x * (1 + hyper((), (), y * z))
    assert separatevars(eq) == eq

    s = separatevars(abs(x * y))
    assert s == abs(x) * abs(y) and s.is_Mul
    z = cos(1)**2 + sin(1)**2 - 1
    a = abs(x * z)
    s = separatevars(a)
    assert not a.is_Mul and s.is_Mul and s == abs(x) * abs(z)
    s = separatevars(abs(x * y * z))
    assert s == abs(x) * abs(y) * abs(z)

    # abs(x+y)/abs(z) would be better but we test this here to
    # see that it doesn't raise
    assert separatevars(abs((x + y) / z)) == abs((x + y) / z)
Exemplo n.º 20
0
    def define_px_poly_eqn(
        self, eta_choice: Rational = None, do_ndim: bool = False
    ) -> None:
        r"""
        Define :math:`p_x` polynomial.

        TODO: remove ref to xiv_0

        Define polynomial form of function combining normal-slowness
        covector components :math:`(p_x,p_z)`
        (where the latter is given in terms of the vertical erosion rate
        :math:`\xi^{\downarrow} = -\dfrac{1}{p_z}`)
        and the erosion model flow component :math:`\varphi(\mathbf{r})`

        Args:
            eta_choice (:class:`~sympy.core.numbers.Rational`):
                value of :math:`\eta` to use instead value given at
                instantiation; otherwise the latter value is used

        Attributes:
            poly_px_xiv_varphi_eqn (:class:`~sympy.polys.polytools.Poly`):
                :math:`\operatorname{Poly}{\left(
                \left(\xi^{\downarrow}\right)^{4}
                \varphi^{4}{\left(\mathbf{r} \right)} p_{x}^{6}
                -  \left(\xi^{\downarrow}\right)^{4} p_{x}^{2}
                -  \left(\xi^{\downarrow}\right)^{2}, p_{x},
                domain=\mathbb{Z}\left[\varphi{\left(\mathbf{r} \right)},
                \xi^{\downarrow}\right] \right)}`

            poly_px_xiv_eqn (:class:`~sympy.core.relational.Equality`):
                :math:`\varphi_0^{4}
                \left(\xi^{\downarrow{0}}\right)^{4} p_{x}^{6}
                \left(\varepsilon
                + \left(\frac{x_{1} - {r}^x}{x_{1}}\right)^{2 \mu}\right)^{4}
                - \left(\xi^{\downarrow{0}}\right)^{4} p_{x}^{2}
                - \left(\xi^{\downarrow{0}}\right)^{2} = 0`
        """
        logging.info(f"gme.core.pxpoly.define_px_poly_eqn (ndim={do_ndim})")
        if do_ndim:
            # Non-dimensionalized version
            varphi0_solns = solve(
                self.sinCi_xih0_eqn.subs({eta: eta_choice}), varphi_0
            )
            varphi0_eqn = Eq(varphi_0, varphi0_solns[0])
            if eta_choice is not None and eta_choice <= 1:
                tmp_eqn = separatevars(
                    simplify(
                        self.px_xiv_varphi_eqn.subs({eta: eta_choice})
                        .subs({varphi_r(rvec): self.varphi_rxhat_eqn.rhs})
                        .subs(e2d(self.px_pxhat_eqn))
                        .subs(e2d(varphi0_eqn))
                    )
                )
                self.poly_pxhat_xiv_eqn = simplify(
                    Eq(
                        (
                            numer(tmp_eqn.lhs)
                            - denom(tmp_eqn.lhs) * (tmp_eqn.rhs)
                        )
                        / xiv ** 2,
                        0,
                    )
                )
                self.poly_pxhat_xiv0_eqn = self.poly_pxhat_xiv_eqn.subs(
                    {xiv: xiv_0}
                ).subs(e2d(self.xiv0_xih0_Ci_eqn))
            else:
                tmp_eqn = separatevars(
                    simplify(
                        self.px_xiv_varphi_eqn.subs({eta: eta_choice})
                        .subs({varphi_r(rvec): self.varphi_rxhat_eqn.rhs})
                        .subs(e2d(self.px_pxhat_eqn))
                        .subs(e2d(varphi0_eqn))
                    )
                )
                self.poly_pxhat_xiv_eqn = simplify(
                    Eq(
                        (
                            numer(tmp_eqn.lhs)
                            - denom(tmp_eqn.lhs) * (tmp_eqn.rhs)
                        )
                        / xiv ** 2,
                        0,
                    )
                )
                self.poly_pxhat_xiv0_eqn = simplify(
                    Eq(
                        self.poly_pxhat_xiv_eqn.lhs.subs({xiv: xiv_0}).subs(
                            e2d(self.xiv0_xih0_Ci_eqn)
                        )
                        / xih_0 ** 2,
                        0,
                    )
                )
        else:
            # Dimensioned version
            tmp_eqn = simplify(self.px_xiv_varphi_eqn.subs({eta: eta_choice}))
            if eta_choice is not None and eta_choice <= 1:
                self.poly_px_xiv_varphi_eqn = poly(tmp_eqn.lhs, px)
            else:
                self.poly_px_xiv_varphi_eqn = poly(
                    numer(tmp_eqn.lhs) - denom(tmp_eqn.lhs) * (tmp_eqn.rhs), px
                )
            self.poly_px_xiv_eqn = Eq(
                self.poly_px_xiv_varphi_eqn.subs(e2d(self.varphi_rx_eqn)), 0
            )