def sin(X): try: y = ADnum(np.sin(X.val), der=np.cos(X.val) * X.der, ops=(1 - X.counted) * X.ops + 2, rops=1, tfops=X.tfops + 2 + X.ins, trops=X.trops + 2 + 2 * X.ins) X.counted = 1 y.graph = X.graph if X not in y.graph: y.graph[X] = [] y.graph[X].append((y, 'sin', np.cos(X.val))) return y except AttributeError: return np.sin(X)
def tan(X): try: y = ADnum(np.tan(X.val), der=(1 / np.cos(X.val)**2) * X.der, ops=(1 - X.counted) * X.ops + 4, rops=3, tfops=X.tfops + 2 + X.ins, trops=X.trops + 2 + 2 * X.ins) X.counted = 1 y.graph = X.graph if X not in y.graph: y.graph[X] = [] y.graph[X].append((y, 'tan', (1 / np.cos(X.val))**2)) return y except AttributeError: return np.tan(X)
def sqrt(X): try: y = ADnum(np.sqrt(X.val), der=X.der / (2 * np.sqrt(X.val)), ops=(1 - X.counted) * X.ops + 3, rops=3, tfops=X.tfops + 2 + X.ins, trops=X.trops + 2 + 2 * X.ins) X.counted = 1 y.graph = X.graph if X not in y.graph: y.graph[X] = [] y.graph[X].append((y, 'sqrt', 1 / (2 * np.sqrt(X.val)))) return y except AttributeError: return np.sqrt(X)
def arccos(X): try: y = ADnum(np.arccos(X.val), der=-1 / np.sqrt(1 - X.val**2) * X.der, ops=(1 - X.counted) * X.ops + 5, rops=4, tfops=X.tfops + 2 + X.ins, trops=X.trops + 2 + 2 * X.ins) X.counted = 1 y.graph = X.graph if X not in y.graph: y.graph[X] = [] y.graph[X].append((y, 'arccos', -1 / np.sqrt(1 - X.val**2))) return y except AttributeError: return np.arccos(X)
def csc(X): try: y = ADnum(1 / np.sin(X.val), der=(-1 / np.tan(X.val)) * (1 / np.sin(X.val)) * X.der, ops=(1 - X.counted) * X.ops + 5, rops=4, tfops=X.tfops + 2 + X.ins, trops=X.trops + 2 + 2 * X.ins) X.counted = 1 y.graph = X.graph if X not in y.graph: y.graph[X] = [] y.graph[X].append( (y, 'csc', (-1 / np.tan(X.val)) * (1 / np.sin(X.val)))) return y except: return 1 / np.sin(X)
def get_x(): xval = input( 'At what value of x would you like to evaluate the function and its derivative?\n' ) try: x = ADnum(float(xval), ins=2, ind=0) except: print('x value must be a float.') x = get_x() return x
def get_y(): yval = input( 'At what value of y would you like to evaluate the function and its derivative?\n' ) try: y = ADnum(float(yval), ins=2, ind=1) except: print('y value must be a float.') y = get_y() return y
def plot_ADnum(f, ins=1, 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 ''' if ins == 1: vals = np.linspace(xmin, xmax, 100) evals = [f(ADnum(value, der=1)).val for value in vals] ders = [f(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 if ins == 2: fig = plt.figure() ax = fig.gca(projection='3d') vals = np.linspace(xmin, xmax, 100) z = f(vals, vals) ax.plot_trisurf(vals, vals, z, antialiased=True) return fig if ins > 2: fig = plt.figure() return fig