Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-24fa
minimathematica
Commits
ac2867e6
Commit
ac2867e6
authored
6 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Update test_estimation.py
parent
373c84a8
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/3.A/test_estimation.py
+41
-15
tests/3.A/test_estimation.py
with
41 additions
and
15 deletions
+41
-15
tests/3.A/test_estimation.py
View file @
ac2867e6
from
decimal
import
Decimal
from
sympy
import
series
,
atan
,
nsimplify
,
diff
from
sympy.calculus.util
import
Interval
,
maximum
import
math
from
math
import
floor
,
factorial
from
src.taylor
import
arctan
import
pytest
...
...
@@ -12,20 +11,47 @@ def term(N, xval, a):
ivl
=
Interval
(
xval
-
0.001
,
a
+
0.001
)
+
Interval
(
a
-
0.001
,
xval
+
0.001
)
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
))))
return
nsimplify
(
abs
(((
M
*
((
x
-
a
)
**
(
N
+
1
))
/
factorial
(
N
+
1
)).
subs
(
x
,
xval
))))
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
ref_taylor
(
atan
(
x
),
a
,
N
,
xval
),
term
(
N
,
xval
,
a
)
@
pytest
.
mark
.
parametrize
(
'q'
,
range
(
20
))
def
test_arctan
(
q
):
xval
=
q
/
10
_
,
error
=
approx_atan
(
10
,
xval
,
0
)
v
,
e
=
arctan
(
Decimal
(
xval
))
if
xval
<
0.9
:
assert
0
<=
float
(
e
)
-
float
(
error
)
<
0.003
assert
abs
(
math
.
atan
(
xval
)
-
float
(
v
))
<=
float
(
e
)
EPSILON
=
0.0001
def
approx_atan
(
N
,
xval
):
if
floor
(
xval
)
==
0
:
return
ref_taylor
(
atan
(
x
),
0
,
N
,
xval
),
term
(
N
,
xval
,
0
)
else
:
_
,
center_error
=
approx_atan
(
N
,
floor
(
xval
)
-
EPSILON
)
return
ref_taylor
(
atan
(
x
),
floor
(
xval
),
N
,
xval
),
center_error
+
term
(
N
,
xval
,
floor
(
xval
)
-
EPSILON
)
@
pytest
.
mark
.
parametrize
(
'xval'
,
[
x
/
10
for
x
in
range
(
20
)])
def
test_arctan
(
xval
):
print
(
'testing'
,
xval
)
ref_val1
,
ref_error
=
approx_atan
(
10
,
xval
)
ref_val1
=
float
(
ref_val1
)
ref_error
=
float
(
ref_error
)
ref_val2
=
atan
(
xval
)
our_val
,
our_error
=
arctan
(
Decimal
(
xval
))
our_val
=
float
(
our_val
)
our_error
=
float
(
our_error
)
errors_diff
=
our_error
-
ref_error
error_with_ref1
=
abs
(
ref_val1
-
our_val
)
error_with_ref2
=
abs
(
ref_val2
-
our_val
)
epsilon
=
0.15
if
xval
<
0.5
:
epsilon
=
0.001
elif
xval
<
1.0
:
epsilon
=
0.05
assert
0
<=
errors_diff
<
epsilon
assert
error_with_ref1
<=
our_error
assert
error_with_ref2
<=
our_error
assert
abs
(
error_with_ref2
-
our_error
)
<
epsilon
if
__name__
==
"__main__"
:
test_arctan
(
0.0
)
test_arctan
(
0.5
)
test_arctan
(
0.9
)
test_arctan
(
1.0
)
test_arctan
(
1.5
)
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment