Esempio n. 1
0
def pprint_matrix(data, digits=3):
    """Print a numpy array as a latex matrix."""
    header = (r"\begin{{pmatrix}}" r"{d}\end{{pmatrix}}")
    d = data.__str__()[1:-1]
    d = d.replace(']', '')
    d = d.replace('\n', r'\\')
    d = re.sub(r' *\[ *', '', d)
    d = re.sub(r' +', ' & ', d)
    display.display_latex(display.Latex(header.format(d=d)))
Esempio n. 2
0
def pprint_matrix(data, digits=3):
    """Print a numpy array as a latex matrix."""
    header = r"\begin{{pmatrix}}" r"{d}\end{{pmatrix}}"
    d = data.__str__()[1:-1]
    d = d.replace("]", "")
    d = d.replace("\n", r"\\")
    d = re.sub(r" *\[ *", "", d)
    d = re.sub(r" +", " & ", d)
    display.display_latex(display.Latex(header.format(d=d)))
def ode_scipy():
    # -*- coding:utf-8 -*-

    from scipy.integrate import odeint
    import matplotlib.pyplot as plt
    import numpy as np

    from IPython import display

    # 冷却定律的微分方程
    def cooling_law_equ(w, t, a, H):
        return -1 * a * (w - H)

    # 冷却定律求解得到的温度temp关于时间t的函数
    def cooling_law_func(t, a, H, T0):
        return H + (T0 - H) * np.e**(-a * t)

    def ode_array(initial_temp):
        law_func = Cooling_law_module(0.5, 30)
        t = np.arange(0, 10, 0.01)
        temp2 = odeint_torch(law_func, initial_temp, torch.FloatTensor(t))
        return temp2[-1]

    law_func = Cooling_law_module(0.5, 30)
    t = np.arange(0, 10, 0.01)
    initial_temp = (90)  #初始温度
    temp = odeint(cooling_law_equ, initial_temp, t, args=(0.5, 30))  #冷却系数和环境温度
    temp1 = cooling_law_func(t, 0.5, 30, initial_temp)  #推导的函数与scipy计算的结果对比
    temp2 = odeint_torch(law_func, torch.Tensor([initial_temp]),
                         torch.FloatTensor(t))
    temp2 = temp2.squeeze(1)
    plt.subplot(3, 1, 1)
    plt.plot(t, temp)
    plt.ylabel("temperature")

    plt.subplot(3, 1, 2)
    plt.plot(t, temp1)
    plt.xlabel("time")
    plt.ylabel("temperature")

    plt.subplot(3, 1, 3)
    plt.plot(t, np.array(temp2.detach().numpy()))
    plt.xlabel("time")
    plt.ylabel("temperature")

    display.Latex("牛顿冷却定律 $T'(t)=-a(T(t)- H)$)(上)和 $T(t)=H+(T_0-H)e^{-at}$(下)")
    plt.show()
    input_data = torch.FloatTensor([initial_temp])
    input_data.requires_grad = True
    torch.autograd.gradcheck(ode_array, input_data)
Esempio n. 4
0
a, b, c = Array(np.array([a, b, c]) /
                gcd).applyfunc(lambda coeff: Rational(coeff))

factored_f = a * x**2 + b * x + c

latex_expr = latex(factored_f)

f = factored_f * gcd

problem_content = f'{gcd}({latex_expr})' if gcd != 1 else latex_expr

vertex_x = Rational(-b, 2 * a)
vertex_y = f.subs(x, vertex_x)

ipyd.Latex(
    f'problem: ${problem_content}$ solution: ${(vertex_x, vertex_y)}$, gcd:${gcd}$ {(a, b, c)}'
)

#%% [markdown]
# ### Determining possible number of real roots of quadratic function
#
# By analyzing discriminant
#
# 2, 1 (repeated), or 0
#
# **We need a method to generate a quadratic function that has an equal likelihood of having 0, 1, or 2 roots**

#%%
#

#%% [markdown]