def gt21_02(x): ''' ground truth for 2nd taylor of fexpr2_01. ''' fexpr2_01 = make_prod(make_pwr('x', 1.0), make_e_expr(make_pwr('x', 1.0))) f0 = tof(fexpr2_01) f1 = tof(deriv(fexpr2_01)) f2 = tof(deriv(deriv(fexpr2_01))) return f0(2.0) + f1(2.0) * (x - 2.0) + (f2(2.0) / 2) * (x - 2.0)**2
def test_03(self): #(x+11)/(x-3) ^3 print('\n***** Test 03 ***********') q = make_quot(make_plus(make_pwr('x', 1.0), make_const(11.0)), make_plus(make_pwr('x', 1.0), make_const(-3.0))) pex = make_pwr_expr(q, 3.0) print('-- function expression is:\n') print(pex) pexdrv = deriv(pex) assert not pexdrv is None print('\n - - derivative is:\n') print(pexdrv) pexdrvf = tof(pexdrv) assert not pexdrvf is None gt = lambda x: -42.0 * (((x + 11.0)**2) / ((x - 3.0)**4)) err = 0.00001 print('\n - -comparison with ground truth:\n') for i in range(10): if i != 3.0: print(pexdrvf(i), gt(i)) #print("gt: ",gt(0)) assert abs(pexdrvf(i) - gt(i)) <= err #print("pexdrvf(0): ",pexdrvf(0),"gt(i): ", gt(0)) print('Test 03:pass')
def test_prob_02_ut_01(self): print('\n***** Problem 02: UT 01 ************') fex = make_prod(make_pwr('x', 1.0), make_prod(make_plus(make_pwr('x', 1.0), make_const(1.0)), make_plus(make_pwr('x', 1.0), make_const(2.0)))) drv = logdiff(fex) assert not drv is None print(drv) drvf = tof(drv) assert not drvf is None def gt_drvf(x): z = x*(x + 1.0)*(x + 2.0) z2 = (1.0/x + 1.0/(x + 1.0) + 1.0/(x + 2.0)) return z * z2 err = 0.0001 for i in range(1, 10): #print(drvf(i), gt_drvf(i)) assert abs(gt_drvf(i) - drvf(i)) <= err for i in range(-10, -1): if i == -1 or i == -2: continue #print(drvf(i), gt_drvf(i)) assert abs(gt_drvf(i) - drvf(i)) <= err print('Problem 02: UT 01: pass')
def test_05(): fexpr2_01 = make_prod(make_pwr('x', 1.0), make_e_expr(make_pwr('x', 1.0))) print(gt21_02(2.001)) fex = taylor_poly(fexpr2_01, make_const(2.001), make_const(2)) print(fex) fex_tof = tof(fex) print(fex_tof(2.001))
def test_06(): #y = x; y = -x +6 ln1 = make_line_eq(make_var('y'), make_pwr('x', 1.0)) ln2 = make_line_eq( make_var('y'), make_plus(make_prod(make_const(-1.0), make_pwr('x', 1.0)), make_const(6.0))) print(line_intersection(ln1, ln2))
def test_11(self): ''' (x+1)^4 *(4x-1)^2 => (x+1)^4 *(4x-1)^2 * ( (4/(x+1) + 8/(4x-1)) ln(x+1)^4 ''' print('*******Test 10********') fex1 = make_pwr_expr(make_plus(make_pwr('x', 1.0), make_const(1.0)), 4.0) fex2 = make_pwr_expr(make_plus(make_prod(make_const(4.0), make_pwr('x', 1.0)), make_const(-1.0)), 2.0) fex = make_prod(fex1, fex2) print(fex) drv = logdiff(fex) assert not drv is None print(drv) drvf = tof(drv) assert not drvf is None def gt_drvf(x): z1 = ((x + 1.0) **4.0) * ((4*x - 1.0)** 2.0) z2 = (4.0/(x + 1.0)) + (8.0/ (4*x - 1.0)) return z1 * z2 err = 0.0001 for i in range(1, 10): #print(drvf(i), gt_drvf(i)) assert abs(gt_drvf(i) - drvf(i)) <= err print('Test 11: pass')
def nra_ut_00(): ''' Approximating x^3 - x - 2. ''' fexpr = make_pwr('x', 3.0) fexpr = make_plus(fexpr, make_prod(make_const(-1.0), make_pwr('x', 1.0))) fexpr = make_plus(fexpr, make_const(-2.0)) # print('fexpr',str(fexpr)) print(nra(fexpr, make_const(1.0), make_const(10)))
def test_assign_03_prob_01_ut_05(self): print('\n***** Assign 03: Problem 01: Unit Test 05 *****') e1 = make_plus(make_pwr('x', 1.0), make_const(1.0)) e2 = make_pwr('x', 3.0) e3 = make_prod(make_const(0.0), make_pwr('x', 2.0)) e4 = make_plus(e2, e3) e5 = make_prod(make_const(5.0), make_pwr('x', 1.0)) e6 = make_plus(e4, e5) e7 = make_plus(e6, make_const(2.0)) e8 = make_prod(e1, e7) # 1) print the expression we just constructed print('-- function expression is:\n') print(e8) # 2) differentiate and make sure that it not None drv = deriv(e8) assert not drv is None print('\n-- derivative is:\n') print(e8) # 3) convert drv into a function e8f = tof(drv) assert not e8f is None # steps 2) and 3) can be combined into tof(deriv(e6)). # 4) construct the ground truth function gt = lambda x: 4.0 * (x**3) + 3 * (x**2) + 10.0 * x + 7.0 # 5) compare the ground gruth with what we got in # step 3) on an appropriate number range. print('\n--comparison with ground truth:\n') err = 0.00001 for i in range(15): #print(e8f(i), gt(i)) assert abs(e8f(i) - gt(i)) <= err print('Assign 03: Problem 01: Unit Test 05: pass')
def test_assign_03_prob_01_ut_04(self): print('\n***** Assign 03: Problem 01: Unit Test 04 *****') e1 = make_pwr('x', 2.0) e2 = make_plus(e1, make_prod(make_const(0.0), make_pwr('x', 1.0))) e3 = make_plus(e2, make_const(-1.0)) e4 = make_pwr_expr(e3, 4.0) e5 = make_pwr('x', 2.0) e6 = make_plus(e5, make_prod(make_const(0.0), make_pwr('x', 1.0))) e7 = make_plus(e6, make_const(1.0)) e8 = make_pwr_expr(e7, 5.0) e9 = make_prod(e4, e8) print(e9) e9f = tof(deriv(e9)) assert not e9f is None err = 0.000001 def drvf(x): return 2 * x * ((x**2 - 1.0)**3) * ( (x**2 + 1.0)**4) * (9 * x**2 - 1.0) for i in range(10): #print(e9f(i), drvf(i)) assert abs(e9f(i) - drvf(i)) <= err print('Assign 03: Problem 01: Unit Test 04: pass')
def test_06(self): #x^-3+7e^(5x)+4*x^-1 => (1/-2)*(x^-2)+0 +(7*(1/5)*e(5x)+ (4*ln|x|) print("****Unit Test 06********") fex1 = make_pwr('x', -3.0) fex2 = make_prod( make_const(7.0), make_e_expr(make_prod(make_const(5.0), make_pwr('x', 1.0)))) fex3 = make_prod(make_const(4.0), make_pwr('x', -1.0)) fex4 = make_plus(fex1, fex2) fex = make_plus(fex4, fex3) print(fex) afex = antideriv(fex) assert not afex is None def gt(x): v1 = -0.5 * (x**(-2.0)) v2 = (7.0 / 5.0) * (math.e**(5.0 * x)) v3 = 4.0 * (math.log(abs(x), math.e)) return v1 + v2 + v3 afexf = tof(afex) assert not afexf is None err = 0.001 for i in range(1, 10): print(afexf(i), gt(i)) assert abs(afexf(i) - gt(i)) <= err * gt(i) print("Unit Test 06 pass")
def nra_ut_10(): ''' Approximating e^(5-x) = 10 - x. ''' fexpr = make_e_expr( make_plus(make_const(5.0), make_prod(make_const(-1.0), make_pwr('x', 1.0)))) fexpr = make_plus(fexpr, make_pwr('x', 1.0)) fexpr = make_plus(fexpr, make_const(-10.0)) print(nra(fexpr, make_const(1.0), make_const(10000)))
def problem_2_deriv(): fexpr = make_prod(make_pwr('t', 2.0), make_ln(make_pwr('t', 1.0))) print(fexpr) dv = deriv(fexpr) print(dv) second_dv = deriv(dv) tof_second = tof(second_dv) print(tof_second(1.0))
def test_07(): ln1 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(-1.0/5.0), make_pwr('x', 1.0)), make_const(10.0))) ln2 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(1.0/5.0), make_pwr('x', 1.0)), make_const(5.0))) print(line_intersection(ln1, ln2))
def nra_ut_13(): #approximate a zero for x^3-3x^2+14x-2 f1 = make_plus(make_pwr('x', 3.0), make_prod(const(-3.0), make_pwr('x', 2.0))) f2 = make_plus(make_prod(const(14.0), make_pwr('x', 1.0)), const(-2.0)) fexpr = make_plus(f1, f2) approx = nra(fexpr, const(1.0), const(1000000)) print(approx)
def nra_ut_14(): #approximate a zero for x^2 - 17x + 14 f1 = make_plus(make_pwr('x', 2.0), make_prod(const(-17.0), make_pwr('x', 1.0))) fexpr = make_plus(f1, const(14.0)) approx = nra(fexpr, const(1.0), const(1000000)) gt = (17 - math.sqrt(233)) / 2 assert (approx - gt) <= .00001 print('Good approximation')
def test_assgn_01_ut_02(self): print('\n***** Assign 01: Unit Test 02 ************') fex1 = make_pwr('x', 4.0) fex2 = make_pwr('x', 3.0) fex3 = make_pwr('x', 1.0) fex4 = plus(elt1=fex1, elt2=fex2) fex5 = plus(elt1=fex4, elt2=fex3) graph_drv(fex5, [-2.5, 2.5], [-10.0, 10.0]) print('Assign 01: Unit Test 02: pass')
def test_11(): ln1 = make_line_eq(make_var('x'), make_const(1.0)) ln2 = make_line_eq(make_var('y'), make_prod(make_const(0.5), make_pwr('x', 1.0))) print(line_intersection(ln1, ln2)) ln3 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(-3.0/4), make_pwr('x', 1.0)), make_const(3.0))) print(line_intersection(ln1, ln3)) print(line_intersection(ln2, ln3))
def test_03(): f1 = make_prod(make_const(1.0 / 3.0), make_pwr('x', 3.0)) f2 = make_prod(make_const(-2.0), make_pwr('x', 2.0)) f3 = make_prod(make_const(3.0), make_pwr('x', 1.0)) f4 = make_plus(f1, f2) f5 = make_plus(f4, f3) poly = make_plus(f5, make_const(1.0)) print 'f(x) = ', poly xtrma = loc_xtrm_1st_drv_test(poly) for i, j in xtrma: print i, str(j)
def test_05(): #y = x; y = 2x; y = 3x-10 ln1 = make_line_eq(make_var('y'), make_pwr('x', 1.0)) ln2 = make_line_eq(make_var('y'), make_prod(make_const(2.0), make_pwr('x', 1.0))) ln3 = make_line_eq( make_var('y'), make_plus(make_prod(make_const(3.0), make_pwr('x', 1.0)), make_const(-10.0))) print(get_line_coeffs(ln1)) print(get_line_coeffs(ln2)) print(get_line_coeffs(ln3))
def max_rev_test(): print("***Max Revenue Test 01 *****") e1 = make_prod( make_const(1.0/12.0), make_pwr('x', 2.0)) e2 = make_prod(make_const(-10.0), make_pwr('x', 1.0)) sum1 = make_plus(e1, e2) demand_expr = make_plus(sum1, make_const(300.0)) num_units, rev, price = maximize_revenue(demand_expr, constraint=lambda x: 0 <= x <= 60) print('x = ', num_units.get_val()) print("rev= ", rev.get_val()) print('price = ', price.get_val()) print("Max Revenue Test: pass")
def test_02(): f0 = make_prod(make_const(0.5), make_pwr('x', 2.0)) f1 = make_prod(make_const(6.0), make_pwr('x', 1.0)) f2 = make_plus(f0, f1) poly = make_plus(f2, make_const(0.0)) print '+++', poly zeros = find_poly_2_zeros(poly) for c in zeros: print c pf = tof(poly) for c in zeros: assert abs(pf(c.get_val()) - 0.0) <= 0.0001 print 'True zeros!'
def test_05(): ln1 = make_line_eq(make_var('y'), make_pwr('x', 1.0)) ln2 = make_line_eq(make_var('y'), make_prod(make_const(2.0), make_pwr('x', 1.0))) ln3 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(3.0), make_pwr('x', 1.0)), make_const(-10.0))) print(get_line_coeffs(ln1)) print(get_line_coeffs(ln2)) print(get_line_coeffs(ln3)) print(line_intersection(ln1, ln2)) print(line_intersection(ln2, ln3)) print(line_intersection(ln1, ln3))
def problem_1_deriv(): # still has problems # f(x) =(x+1)(2x+1)(3x+1) /(4x+1)^.5 fex1 = make_plus(make_pwr('x', 1.0), const(1.0)) fex2 = make_plus(make_prod(const(2.0), make_pwr('x', 1.0)), const(1.0)) fex3 = make_plus(make_prod(const(3.0), make_pwr('x', 1.0)), const(1.0)) fex4 = make_prod(fex1, fex2) fex5 = make_prod(fex4, fex3) fex6 = make_pwr_expr( make_plus(make_prod(const(4.0), make_pwr('x', 1.0)), const(1.0)), 0.5) fex = make_quot(fex5, fex6) print(fex) drv = deriv(fex) print('drv: ', drv) drvf = tof(drv) def gt_drvf(x): n = (60.0 * x**3) + (84 * x**2) + 34 * x + 4 d = (4 * x + 1)**(3 / 2) return n / d for i in range(1, 10): print(drvf(i), gt_drvf(i)) assert abs(gt_drvf(i) - drvf(i)) <= 0.001 assert drv is not None print(drv) # zeros = find_poly_2_zeros(drv) # print(zeros) f1 = tof(fex) f2 = tof(deriv(fex)) xvals = np.linspace(1, 5, 10000) yvals1 = np.array([f1(x) for x in xvals]) yvals2 = np.array([f2(x) for x in xvals]) fig1 = plt.figure(1) fig1.suptitle('Graph') plt.xlabel('x') plt.ylabel('y') plt.ylim() plt.xlim() plt.grid() plt.plot(xvals, yvals1, label=fex.__str__(), c='r') plt.plot(xvals, yvals2, label=deriv(fex).__str__(), c='g') plt.legend(loc='best') plt.show()
def oil_disk_test(): yt = make_prod(make_const(0.02 * math.pi), make_pwr('r', 2.0)) print(yt) dydt = dydt_given_x_dxdt(yt, make_const(150.0), make_const(20.0)) assert not dydt is None assert isinstance(dydt, const) print(dydt)
def test_assgn_01_prob_02_ut_04(self): print('\n***** Assign 01: Unit Test 04 ************') fex1 = prod(mult1=make_const(2), mult2=make_pwr('x', 2.0)) fex2 = plus(elt1=fex1, elt2=make_const(2.0)) graph_drv(fex2, [-10, 10], [-50.0, 50.0]) print('Assign 01: Unit Test 04: pass')
def test_assign_03_prob_01_ut_01(self): print('\n***** Assign 03: Problem 01: Unit Test 01 *****') e1 = make_pwr('x', 2.0) e2 = make_pwr('x', 3.0) e3 = make_prod(e1, e2) print(e3) drv = deriv(e3) assert not drv is None print(drv) e3f = tof(drv) assert not e3f is None f = lambda x: 5.0 * (x**4) err = 0.0001 for i in range(10): assert abs(e3f(i) - f(i)) <= err print('Assign 03: Problem 01: Unit Test 01: pass')
def problem_3_tangent(): fex = make_e_expr(make_pwr('x', 1.0)) print("f(x)= ", fex) drv = deriv(fex) print("f'(x)= ", drv) drvf = tof(drv) print("f'(-1)= ", drvf(-1))
def spread_of_news_model(p, k): assert isinstance(p, const) and isinstance(k, const) #t0 = 4 #t1 = ? #p = .5 * P => P(1 - e^(-k*4)) = .5*P (where t=4) => k = -0.25*ln(0.5) ~= 1.73 => f(t) = P(1 - e^(-1.73*t)) #p1 = .9 * P (p0 and p1 given) #we can now compute t1 #0.9*P = P(1 - e^(-1.73*t)) => t ~= 13.3 #generally #f(t) = P(1-e^(-kt)) we know that f(0) = 0 #where p0 is initial knowing pop at time t0 #and p1 is pop at time t1 #return f(t) #where f(t) = p*(1 - e^(-k*t)) #where k = (1/t0)*ln(p0) #so return p*(1 - e^(( -( (1/t0)*ln(p0) ) )*t)) #if given p0 and t0 #we are given k so: #return p*(1 - e^(-k*t)) return prod( p, plus( 1, prod(const(-1.0), make_e_expr(prod(prod(const(-1), k), make_pwr('t', 1))))))
def spread_of_disease_model(p, t0, p0, t1, p1): assert isinstance(p, const) and isinstance(t0, const) assert isinstance(p0, const) and isinstance(t1, const) #f(t) = P/(1+B*e^(-c*t)) #p = 500,000 #let t0 = 0 such that f(t0) = f(0) #this would make t1 = delta_t #where delta_t = t1 - t0 #f(0) = p0 = 200 = 500,000/(1+B*e^0) = 500,000/(1+B) => B = 2499 #generally: B = (p-p0)/p0 #f(1) = p1 = 500 = 500,000/(1+2499*e^(-c*1)) => c = .92 #generally: c = -ln((p-p1)/(p1*B))/t #so we get f(t) = p/(1+((p-p0)/p0)*e^(-(-ln((p-p1)/(p1*((p-p0)/p0))))*t)) #or f(t) = p/(1+((p-p0)/p0)*e^((ln((p-p1)/(p1*((p-p0)/p0)))/(t1-t0)*t)) #return f(t) return quot( p, plus( const(1.0), prod( quot(plus(p, prod(const(-1.0), p0)), p0), make_e_expr( ln( prod( quot( quot( plus(p, prod(const(-1.0), p1)), prod( p1, quot(plus(p, prod(const(-1.0), p0)), p0))), plus(t1, prod(const(-1.0), t0))), make_pwr('t', 1)))))))
def test_11(): f1 = make_prod(make_const(-1.0), make_pwr('x', 3.0)) f2 = make_prod(make_const(8.5), make_pwr('x', 2.0)) f3 = make_prod(make_const(0.0), make_pwr('x', 0.0)) f4 = make_plus(f1, f2) f5 = make_plus(f4, f3) f6 = make_plus(f5, make_const(100.0)) print(f6) ips = find_infl_pnts(f6) err = 0.0001 assert len(ips) == 1 ip = ips[0] # assert abs(ip.get_x().get_val() - 1.0) <= err # assert abs(ip.get_y().get_val() - 3.0) <= err print("inflection points: ", ip)