コード例 #1
0
def plot_ADnum(x, xmin = -10, xmax = 10):
    '''Function to plot f and its derivative for single variable input

    INPUTS
    ======
    x : ADnum
    xmin : starting value of input
    xmax : ending value of input

    OUTPUTS
    =======
    A plot of x evaluated from xmin to xmax and its derivative
    '''
    vals = np.linspace(xmin, xmax, 100)
    evals = [x(ADnum(value, der=1)).val for value in vals]
    ders = [x(ADnum(value, der=1)).der for value in vals]
    fig = plt.figure()
    plt.plot(vals, evals, label = 'f', linewidth = 2)
    plt.plot(vals, ders, label = 'df/dx', linewidth = 2)
    plt.legend(fontsize = 20)
    plt.xlabel('x', fontsize = 20)
    plt.ylabel('f', fontsize = 20)
    plt.xticks(fontsize = 12)
    plt.yticks(fontsize = 12)
    return fig
コード例 #2
0
ファイル: AD20_GUI.py プロジェクト: CS207-AD20/AD20_GUI
    def confirm():
        global function_expression
        global function_output
        #     function_output = lambda x: eval(function_expression)
        #     graph_window(master)
        try:
            function_output = lambda x: eval(function_expression)

    #         graph_window(master)
        except:
            #         error_window(master)
            #         raise exception("expression error")
            messagebox.showinfo(
                "Error", "Syntax error in your expression, please try again.")
        try:
            textVal = function_output(ADnum(5, der=1)).val
            textDer = function_output(ADnum(5, der=1)).der
            messagebox.showinfo(
                "Continue",
                "Your input is a function. Please continue to draw graphs.")
            graph_window(master)
        except AttributeError:
            messagebox.showinfo("Constant result:",
                                "The value is {}".format(function_output(1)))
        except SyntaxError:
            messagebox.showerror(
                "Error",
                "Syntax error in your expression.  Please use \'Clear All\', and try again."
            )
コード例 #3
0
def arctan(X):
    try:
        y = ADnum(np.arctan(X.val), der=1 / (1 + X.val**2) * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'arctan'))
        return y
    except AttributeError:
        return np.arctan(X)
コード例 #4
0
def arccos(X):
    try:
        y = ADnum(np.arccos(X.val), der=-1 / np.sqrt(1 - X.val**2) * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'arccos'))
        return y
    except AttributeError:
        return np.arccos(X)
コード例 #5
0
def cot(X):
    try:
        y = ADnum(1 / np.tan(X.val), der=-1 / (np.sin(X.val)**2) * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'cot'))
        return y
    except AttributeError:
        return 1 / np.tan(X)
コード例 #6
0
def sin(X):
    try:
        y = ADnum(np.sin(X.val), der=np.cos(X.val) * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'sin'))
        return y
    except AttributeError:
        return np.sin(X)
コード例 #7
0
def sqrt(X):
    try:
        y = ADnum(np.sqrt(X.val), der=X.der / (2 * np.sqrt(X.val)))
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'sqrt'))
        return y
    except AttributeError:
        return np.sqrt(X)
コード例 #8
0
def sec(X):
    try:
        y = ADnum(1 / np.cos(X.val), der=np.tan(X.val) / np.cos(X.val) * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'sec'))
        return y
    except AttributeError:
        return 1 / np.cos(X)
コード例 #9
0
def log(X):
    try:
        y = ADnum(np.log(X.val), der=1 / X.val * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'log'))
        return y
    except AttributeError:
        return np.log(X)
コード例 #10
0
def tanh(X):
    try:
        y = ADnum(np.tanh(X.val), der=1 / (np.cosh(X.val)**2) * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'tanh'))
        return y
    except AttributeError:
        return np.tanh(X)
コード例 #11
0
def csc(X):
    try:
        y = ADnum(1 / np.sin(X.val),
                  der=(-1 / np.tan(X.val)) * (1 / np.sin(X.val)) * X.der)
        y.graph = X.graph
        if X not in y.graph:
            y.graph[X] = []
        y.graph[X].append((y, 'csc'))
        return y
    except:
        return 1 / np.sin(X)
コード例 #12
0
ファイル: AD20_GUI.py プロジェクト: CS207-AD20/AD20_GUI
 def display():
     if not (type(value.get()) == float):
         messagebox.showerror('Error',
                              'Please enter a numeric value for x.')
     show_value = tk.Label(graph_window,
                           text=str(
                               function_output(ADnum(value.get(),
                                                     der=1)).val),
                           height=3,
                           width=20).grid(row=4, column=1, columnspan=2)
     show_derivatice = tk.Label(
         graph_window,
         text=str(function_output(ADnum(value.get(), der=1)).der),
         height=3,
         width=20).grid(row=5, column=1, columnspan=2)
コード例 #13
0
def test_get_labels():
    X = ADnum(1, der=1)
    Y = ADmath.sin(X) + 3
    labs = ADgraph.get_labels(Y)
    assert labs[X] == 'X0'
    assert labs[Y] == 'X2'
    assert len(labs) == 4
コード例 #14
0
def test_plot_ADnum():
    X = ADnum(1, der=1)

    def Y(x):
        return ADmath.sin(x)

    fig = ADgraph.plot_ADnum(Y)
    assert type(fig) == matplotlib.figure.Figure
コード例 #15
0
def test_gen_graph():
    d = {'y': [('x', 'test')]}
    Y = ADnum(1, der=1, graph=d)
    G = ADgraph.gen_graph(Y)
    assert 'y' in G
    assert 'x' in G
    assert type(G) == nx.classes.digraph.DiGraph
    assert G.number_of_nodes() == 2
    assert G.number_of_edges() == 1
コード例 #16
0
def test_get_colorsandsizes():
    X = ADnum(1, der=1)
    Y = ADmath.sin(X) + 3
    labs = ADgraph.get_labels(Y)
    G = ADgraph.gen_graph(Y)
    cols = ADgraph.get_colors(G, Y)
    sizes = ADgraph.get_sizes(G, Y, labs)
    assert len(cols) == 4
    assert len(sizes) == 4
コード例 #17
0
ファイル: AD20_GUI.py プロジェクト: CS207-AD20/AD20_GUI
 def draw_graph():
     plot_graph = tk.Toplevel(graph_window)
     plot_graph.title("Computational Graph")
     plot_graph.geometry("600x600")
     fig = ADgraph.draw_graph(function_output(ADnum(value.get(),
                                                    der=1)))
     canvas = FigureCanvasTkAgg(fig,
                                master=plot_graph)  # A tk.DrawingArea.
     canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
     canvas.draw()
     toolbar = NavigationToolbar2Tk(canvas, plot_graph)
     toolbar.update()
     canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
コード例 #18
0
ファイル: AD20_GUI.py プロジェクト: CS207-AD20/AD20_GUI
        def draw_table():
            plot_graph2 = tk.Toplevel(graph_window)
            plot_graph2.title("Computational Table")
            plot_graph2.geometry("600x600")
            #         fig = ADgraph.gen_table(function_output)

            f = tk.Frame(plot_graph2)
            f.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
            df = ADgraph.gen_table(function_output(ADnum(value.get(), der=1)))
            table = pt = Table(f,
                               dataframe=df,
                               showtoolbar=True,
                               showstatusbar=True)
            pt.show()
コード例 #19
0
 def display():
     if not (type(value_x.get()) == float and type(
             value_y.get()) == float and type(value_z.get()) == float
             and type(value_m.get()) == float and type(value_n.get())
             == float and type(value_k.get()) == float):
         messagebox.showerror('Error',
                              'Please enter a numeric value for x.')
     x = ADnum(value_x.get(), ins=master_ins, ind=0)
     if master_ins > 1:
         y = ADnum(value_y.get(), ins=master_ins, ind=1)
         if master_ins > 2:
             z = ADnum(value_z.get(), ins=master_ins, ind=2)
             if master_ins > 3:
                 u = ADnum(value_m.get(), ins=master_ins, ind=3)
                 if master_ins > 4:
                     v = ADnum(value_n.get(), ins=master_ins, ind=4)
                     if master_ins > 5:
                         w = ADnum(value_k.get(), ins=master_ins, ind=5)
     global out_num
     if master_ins == 1:
         out_num = function_output(x)
     if master_ins == 2:
         out_num = function_output(x, y)
     if master_ins == 3:
         out_num = function_output(x, y, z)
     if master_ins == 4:
         out_num = function_output(x, y, z, u)
     if master_ins == 5:
         out_num = function_output(x, y, z, u, v)
     if master_ins == 6:
         out_num = function_output(x, y, z, u, v, w)
     show_value = tk.Label(graph_window,
                           text=str(round(out_num.val, 2)),
                           height=3,
                           width=20).grid(row=master_ins + 3,
                                          column=1,
                                          columnspan=2)
     show_derivatice = tk.Label(graph_window,
                                text=str(round(out_num.der, 2)),
                                height=3,
                                width=20).grid(row=master_ins + 4,
                                               column=1,
                                               columnspan=2)
コード例 #20
0
def test_vecinput_multi():
    x = ADnum([1, 2, 3], ins=2, ind=0)
    assert np.array_equal(x.der, np.array([[1., 1., 1.], [0., 0., 0.]]))
コード例 #21
0
def test_ADnum_nodernothing():
    with pytest.raises(Exception):
        z = ADnum(3)
コード例 #22
0
def test_ADnum_sub():
    x = ADnum(5.0, der=1)
    f = x - 2.0
    assert f.val == 3.0
    assert f.der == 1.0
    assert len(f.graph) == 2
コード例 #23
0
def test_reverse_graph():
    d = {'y': [('x', 'test')]}
    rd = {'x': [('y', 'test')]}
    Y = ADnum(1, der=1, graph=d)
    rg = ADgraph.reverse_graph(Y)
    assert rd == rg
コード例 #24
0
def test_ADnum_rmul():
    x = ADnum(3.0, der=1)
    f = 2.0 * x
    assert f.val == 6.0
    assert f.der == 2.0
    assert len(f.graph) == 2
コード例 #25
0
def test_ADnum_radd():
    x = ADnum(3.0, der=1)
    f = 2.0 + x
    assert f.val == 5.0
    assert f.der == 1.0
    assert len(f.graph) == 2
コード例 #26
0
def test_neg():
    x = ADnum(4, der=1)
    f = -x
    assert f.val == -4.0
    assert f.der == -1.0
    assert len(f.graph) == 1
コード例 #27
0
def test_graphiput():
    z = ADnum(1, der=1, graph={'a': 1})
    assert z.graph == {'a': 1}
コード例 #28
0
def test_draw_graph():
    X = ADnum(1, der=1)
    Y = ADmath.sin(X) + 3
    fig = ADgraph.draw_graph(Y)
    assert type(fig) == matplotlib.figure.Figure
コード例 #29
0
def test_ADnum_derconsistent():
    with pytest.raises(ValueError):
        z = ADnum(3, der=np.array([1, 3]), ins=5)
コード例 #30
0
def test_gen_table():
    X = ADnum(1, der=1)
    Y = ADmath.sin(X) + 3
    dat = ADgraph.gen_table(Y)
    assert type(dat) == pandas.core.frame.DataFrame