Commit 5f83d117 authored by Adam Blank's avatar Adam Blank
Browse files

Add B/A tests

parent 5b518fb4
No related merge requests found
Showing with 53 additions and 0 deletions
+53 -0
from sympy import series, atan, nsimplify
from src.taylor import estimate
import pytest
from sympy.abc import x
def ref_taylor(f, a, N, xval):
return nsimplify(series(f, x=x, x0=a, n=N).removeO().subs(x, xval).doit())
at_one_tenth = [(0, N, 0.1) for N in range(15)]
at_two_tenths = [(0, N, 0.2) for N in range(15)]
at_one = [(0, N, 1) for N in range(15)]
@pytest.mark.parametrize('a, N, xval', at_one_tenth + at_two_tenths + at_one)
def test_arctan_taylor(a, N, xval):
result = estimate('arctan', xval, a, atan(a), N)
ref = ref_taylor(atan(x), a, N, xval)
print(float(result), float(ref))
assert abs(float(result) - float(ref)) < 10**(-8)
\ No newline at end of file
from sympy import series, atan, oo, nsimplify, poly, diff
from decimal import Decimal
from sympy import series, atan, nsimplify, diff
from sympy.calculus.util import Interval, maximum
from sympy.abc import x
import math
from src.taylor import arctan
from decimal import Decimal
import pytest
from sympy.abc import x
def term(N, xval, a):
ivl = Interval(xval - 0.001, a + 0.001) + Interval(a - 0.001, xval + 0.001)
d = diff(f, x, N + 1)
d = diff(atan(x), x, N + 1)
M = maximum(d, x, ivl)
return nsimplify(abs(((M * ((x - a)**(N + 1))/math.factorial(N + 1)).subs(x, xval))))
f = atan(x)
def ref_taylor(f, a, N, xval):
return nsimplify(series(f, x=x, x0=a, n=N).removeO().subs(x, xval).doit())
def approx_atan(N, xval, a):
return nsimplify(series(f, x=x, x0=a, n=N+1).removeO().subs(x, xval).doit()), term(N, xval, a)
return ref_taylor(atan(x), a, N, xval), term(N, xval, a)
xval = 0.2
for q in range(0, 20):
@pytest.mark.parametrize('q', range(20))
def test_arctan(q):
xval = q / 10
result, error = approx_atan(11, xval, 0)
#print(xval, float(error), abs(math.atan(xval) - result) <= float(error))
_, error = approx_atan(10, xval, 0)
v, e = arctan(Decimal(xval))
#print(xval, float(error), abs(math.atan(xval) - float(v)) <= float(error))
print(xval, float(e), abs(math.atan(xval) - float(v)), abs(math.atan(xval) - float(v)) <= float(e))
if xval < 0.9:
assert 0 <= float(e) - float(error) < 0.003
assert abs(math.atan(xval) - float(v)) <= float(e)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment