예제 #1
0
a - b

a + b

Note que a classe `Rational` funciona com expressões racionais *exatas*. Isto contrasta com o tipo de dado `float`, padrão do Python, que usa a representação em ponto flutuante para 
*aproximar* números racionais.

Podemos converter o tipo `sympy.Rational` em uma variável de ponto flutuante no Python usando `float` ou o método `evalf` do objeto `Rational`. O método `evalf` pode levar um argumento que especifica quantos dígitos devem ser calculados para a aproximação de ponto flutuante (nem todos podem ser usados pelo tipo de ponto flutuante do Python, evidentemente).

c = Rational(2, 3)
c

float(c)

c.evalf()

c.evalf(50)

### Diferenciação e Integração

O SymPy é capaz de executar a diferenciação e integração de muitas funções:

from sympy import Symbol, exp, sin, sqrt, diff
x = Symbol('x')
y = Symbol('y')
diff(sin(x), x)

diff(sin(x), y)

diff(10 + 3*x + 4*y + 10*x**2 + x**9, x)
예제 #2
0
a = 2 * x - y + x - y
print(a, '\n')  # 3*x - 2*y
a_result = a.subs(x, 10)
print(a_result, '\n')

x, y, z = symbols('x, y, z')
b = 3 * x - 2 * y + 4 * z + 4 * z
print(b, '\n')
b_result = b.subs({x: 10, y: 15, z: 11})
print(b_result)

### Numeric types
splitter('Numeric types')
a_r = Rational(33, 22)
b_r = Rational(1, 7)
print(a_r)
print(a_r * b_r)
print(a_r.evalf())

##
from sympy import *

x, y, h = symbols('x y h')
gfg_exp = x / (2 * sqrt(h * x))

print("Before Integration : {}".format(gfg_exp))

# Use sympy.integrate() method
intr = integrate(gfg_exp, (x, 0, h))

print("After Integration : {}".format(intr))
예제 #3
0
''')

pi_ker = ker.get_function("estimate_pi")

threads_per_block = 32
blocks_per_grid = 512

total_threads = threads_per_block * blocks_per_grid

hits_d = gpuarray.zeros((total_threads, ), dtype=np.uint64)

iters = 2**24

pi_ker(np.uint64(iters),
       hits_d,
       grid=(blocks_per_grid, 1, 1),
       block=(threads_per_block, 1, 1))

total_hits = np.sum(hits_d.get())
total = np.uint64(total_threads) * np.uint64(iters)

est_pi_symbolic = Rational(4) * Rational(int(total_hits), int(total))

est_pi = np.float(est_pi_symbolic.evalf())

print("Our Monte Carlo estimate of Pi is : %s" % est_pi)
print("NumPy's Pi constant is: %s " % np.pi)

print("Our estimate passes NumPy's 'allclose' : %s" %
      np.allclose(est_pi, np.pi))
예제 #4
0
def test_J():
    "Second problem in Guillemin"
    s, k = symbols('s k')
    w = symbols('w', real=True)

    pprint("test_I")
    Z = (s**2 + s + 8) / (s**2 + 2 * s + 2)
    pprint(f"Z: {Z}")
    Y = 1 / Z

    #plot_real_part( sympy.lambdify(s, Y, "numpy"))

    real_part = cancel(sympy.re(Y.subs({s: sympy.I * w})))
    print(f"real_part: {real_part}")

    roots = sympy.solveset(real_part, w)
    print(f"roots for w: {roots}")
    #plot( sympy.lambdify(w, real_part, "numpy"))

    w0 = 2

    target0 = radsimp(Y.subs({s: sympy.I * w0}) / (sympy.I * w0))
    print(f"target: {target0.evalf()}")
    target0 = Rational(1, 2)

    target1 = radsimp(Y.subs({s: sympy.I * w0}) * (sympy.I * w0))
    print(f"target: {target1.evalf()}")
    target1 = Rational(2, 1)

    assert target0 > 0
    eq = sympy.Eq(Y.subs({s: k}) / k, target0)
    #assert target1 > 0
    #eq = sympy.Eq( Z.subs({s:k})*k, target1)

    roots = sympy.solveset(eq, k)
    print(f"roots for k: {roots}")

    k0 = Rational(1, 1)
    Y_k0 = Y.subs({s: k0})
    print(k0, Y_k0)
    print(k0.evalf(), Y_k0.evalf())

    den = cancel((k0 * Y_k0 - s * Y))
    print(f"den factored: {sympy.factor(den)}")

    num = cancel((k0 * Y - s * Y_k0))
    print(f"num factored: {sympy.factor(num)}")

    eta = cancel(num / den)
    print(k0, Y_k0, eta)

    print("normal")
    Y0 = eta * Y_k0
    print(f"Y0: {Y0}")

    Z1 = ratsimp(1 / Y0 - 4)
    print(f"Z1: {Z1}")
    C = Cascade.Series(4)

    Y2 = ratsimp(1 / Z1)
    print(f"Y2: {Y2}")

    C = C.hit(Cascade.Shunt(s / 10))
    C = C.hit(Cascade.Shunt(2 / (5 * s)))

    eta_Y_k0 = cancel(C.terminate_with_admittance(0))
    print(f"eta_Y_k0: {eta_Y_k0}")
    assert sympy.Eq(cancel(eta_Y_k0 - Y0), 0)

    print("recip")
    Y0 = ratsimp(Y_k0 / eta)
    print(f"Y0: {Y0}")

    Y1 = ratsimp(Y0 - 1)
    print(f"Y1: {Y1}")
    C = Cascade.Shunt(1)

    Z2 = ratsimp(1 / Y1 - 2 * s / 5 - 8 / (5 * s))
    print(f"Z2: {Z2}")

    C = C.hit(Cascade.Series(2 * s / 5))
    C = C.hit(Cascade.Series(8 / (5 * s)))
    eta_over_Y_k0 = cancel(1 / C.terminate(0))

    print(f"eta_over_Y_k0: {eta_over_Y_k0}")
    assert sympy.Eq(cancel(eta_over_Y_k0 - Y0), 0)

    def p(a, b):
        return a * b / (a + b)

    constructed_Y = cancel(
        p(eta_Y_k0, (k0 * Y_k0) / s) + p(eta_over_Y_k0, (Y_k0 * s) / k0))
    print(f"constructed_Y: {constructed_Y}")

    assert sympy.Eq(cancel(constructed_Y - Y), 0)
예제 #5
0
def test_I():
    "Hazony problem 5.3.a"
    s, k = symbols('s k')
    w = symbols('w', real=True)

    pprint("test_I")
    Z = (s**3 + 3 * s**2 + s + 1) / (s**3 + s**2 + 3 * s + 1)
    pprint(f"Z: {Z}")

    #plot_real_part( sympy.lambdify(s, Z, "numpy"))

    real_part = cancel(sympy.re(Z.subs({s: sympy.I * w})))
    print(f"real_part: {real_part}")

    roots = sympy.solveset(real_part, w)
    print(f"roots for w: {roots}")
    #plot( sympy.lambdify(w, real_part, "numpy"))

    w0 = 1

    target0 = radsimp(Z.subs({s: sympy.I * w0}) / (sympy.I * w0))
    print(f"target: {target0}")

    target1 = radsimp(Z.subs({s: sympy.I * w0}) * (sympy.I * w0))
    print(f"target: {target1}")

    assert target0 > 0
    eq = sympy.Eq(Z.subs({s: k}) / k, target0)
    #assert target1 > 0
    #eq = sympy.Eq( Z.subs({s:k})*k, target1)

    roots = sympy.solveset(eq, k)
    print(f"roots for k: {roots}")

    k0 = Rational(1, 1)
    Z_k0 = Z.subs({s: k0})
    print(k0, Z_k0)
    print(k0.evalf(), Z_k0.evalf())

    den = cancel((k0 * Z_k0 - s * Z))
    print(f"den factored: {sympy.factor(den)}")

    num = cancel((k0 * Z - s * Z_k0))
    print(f"num factored: {sympy.factor(num)}")

    eta = cancel(num / den)
    print(k0, Z_k0, eta)

    print("normal")
    Z0 = eta * Z_k0
    print(f"Z0: {Z0}")

    Y1 = ratsimp(1 / Z0 - 1)
    print(f"Y1: {Y1}")
    C = Cascade.Shunt(1)

    Z2 = ratsimp(1 / Y1 - s / 2 - 1 / (2 * s))
    print(f"Z2: {Z2}")

    C = C.hit(Cascade.Series(s / 2))
    C = C.hit(Cascade.Series(1 / (2 * s)))

    eta_Z_k0 = cancel(C.terminate(0))
    print(f"eta_Z_k0: {eta_Z_k0}")
    assert sympy.Eq(cancel(eta_Z_k0 - Z0), 0)

    print("recip")
    Z0 = cancel(Z_k0 / eta)
    print(f"Z0: {Z0}")

    Z1 = ratsimp(Z0 - 1)
    print(f"Z1: {Z1}")
    C = Cascade.Series(1)

    Y2 = ratsimp(1 / Z1 - s / 2 - 1 / (2 * s))
    print(f"Y2: {Y2}")

    C = C.hit(Cascade.Shunt(s / 2))
    C = C.hit(Cascade.Shunt(1 / (2 * s)))
    eta_over_Z_k0 = cancel(1 / C.terminate_with_admittance(0))
    print(f"eta_over_Z_k0: {eta_over_Z_k0}")
    assert sympy.Eq(cancel(eta_over_Z_k0 - Z0), 0)

    def p(a, b):
        return a * b / (a + b)

    constructed_Z = cancel(
        p(eta_Z_k0, (k0 * Z_k0) / s) + p(eta_over_Z_k0, (Z_k0 * s) / k0))
    print(f"constructed_Z: {constructed_Z}")

    assert sympy.Eq(cancel(constructed_Z - Z), 0)
예제 #6
0
def test_H():
    "Hazony problem 5.3.a"
    s, k = symbols('s k')
    w = symbols('w', real=True)

    pprint("test_H")
    Z = (s**3 + 4 * s**2 + 5 * s + 8) / (2 * s**3 + 2 * s**2 + 20 * s + 9)
    pprint(f"Z: {Z}")

    #plot_real_part( sympy.lambdify(s, Z, "numpy"))

    real_part = cancel(sympy.re(Z.subs({s: sympy.I * w})))
    print(f"real_part: {real_part}")

    roots = sympy.solveset(real_part, w)
    print(f"roots for w: {roots}")
    #plot( sympy.lambdify(w, real_part, "numpy"))

    w0 = sympy.sqrt(6)

    target0 = radsimp(Z.subs({s: sympy.I * w0}) / (sympy.I * w0))
    print(f"target: {target0}")

    target1 = radsimp(Z.subs({s: sympy.I * w0}) * (sympy.I * w0))
    print(f"target: {target1}")

    assert target0 > 0
    eq = sympy.Eq(Z.subs({s: k}) / k, target0)
    #eq = sympy.Eq( Z.subs({s:k})*k, target1)

    print(f"eq: {eq}")

    roots = sympy.solveset(eq, k)
    print(f"roots for k: {roots}")

    k0 = Rational(1, 4) + sympy.sqrt(33) / 4
    Z_k0 = Z.subs({s: k0})
    print(k0, Z_k0)
    print(k0.evalf(), Z_k0.evalf())

    return

    f = s**2 + 6

    den = cancel((k0 * Z_k0 - s * Z) / f)
    print(f"den factored: {sympy.factor(den)}")

    num = cancel((k0 * Z - s * Z_k0) / f)
    print(f"num factored: {sympy.factor(num).evalf()}")

    print(sympy.factor(cancel(den / num)))

    return

    eta = cancel(((k0 * Z - s * Z_k0) / (k0 * Z_k0 - s * Z)).evalf())
    print(k0, Z_k0, eta)

    print(k0, Z_k0, eta.evalf())

    print("normal")
    Z0 = eta * Z_k0
    print(f"Z0: {Z0}")

    Y1 = cancel(1 / Z0 - 4)
    print(f"Y1: {Y1}")
    C = Cascade.Shunt(4)

    Z2 = cancel(1 / Y1 - s / 6 - 1 / (3 * s))
    print(f"Z2: {Z2}")
    C = C.hit(Cascade.Series(s / 6))
    C = C.hit(Cascade.Series(1 / (3 * s)))

    eta_Z_k0 = cancel(C.terminate(0))
    print(f"eta_Z_k0: {eta_Z_k0}")
    assert sympy.Eq(cancel(eta_Z_k0 - Z0), 0)

    print("recip")
    Z0 = cancel(Z_k0 / eta)
    print(f"Z0: {Z0}")

    Z1 = cancel(Z0 - 1)
    print(f"Z1: {Z1}")
    C = Cascade.Series(1)

    Y2 = cancel(1 / Z1 - 2 * s / 3 - 4 / (3 * s))
    print(f"Y2: {Y2}")

    C = C.hit(Cascade.Shunt(2 * s / 3))
    C = C.hit(Cascade.Shunt(4 / (3 * s)))
    eta_over_Z_k0 = cancel(1 / C.terminate_with_admittance(0))
    print(f"eta_over_Z_k0: {eta_over_Z_k0}")
    assert sympy.Eq(cancel(eta_over_Z_k0 - Z0), 0)

    def p(a, b):
        return 1 / (1 / a + 1 / b)

    constructed_Z = cancel(
        p(eta_Z_k0, (k0 * Z_k0) / s) + p(eta_over_Z_k0, (Z_k0 * s) / k0))
    print(f"constructed_Z: {constructed_Z}")

    assert sympy.Eq(cancel(constructed_Z - Z), 0)