Mercurial > hg > tvii
annotate tests/test_derivative.py @ 92:f1d1f2388fd6
test linear regression
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 14:26:15 -0800 |
parents | 1b61ce99ee82 |
children |
rev | line source |
---|---|
83
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 import os |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 import shutil |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 import tempfile |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 import unittest |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 from tvii import derivative |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 class TestDerivatives(unittest.TestCase): |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 def test_midpoint(self): |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 def cube(x): |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 return x**3 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 exact = 3 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 expected = 3.0001 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 got = derivative.midpoint(cube, 1, 0.01) |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 assert abs(expected - got) < 1e-6, "Expected {}; got {}".format(expected, got) |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 if __name__ == '__main__': |
1b61ce99ee82
derivative calculation: midpoint rule
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 unittest.main() |