|
1 |
| -from visma.calculus.differentiation import differentiate |
| 1 | +from visma.calculus.differentiation import differentiate, differentiationProductRule |
2 | 2 | from visma.calculus.integration import integrate
|
3 | 3 | from tests.tester import quickTest
|
4 | 4 |
|
|
9 | 9 |
|
10 | 10 | def test_differentiate():
|
11 | 11 |
|
12 |
| - assert quickTest("x^2 + x", differentiate, 'x') == "2.0x+1" |
| 12 | + assert quickTest("x^2 + x", differentiate, 'x') == "2.0x+1.0" |
13 | 13 |
|
14 |
| - assert quickTest("x + 2y + 3z + 4", differentiate, 'x') == "1" |
15 |
| - # FIXME(tokensToString error): assert quickTest("x + 2y + 3z + 4", differentiate, 'y') == "2" |
16 |
| - # FIXME(tokensToString error): assert quickTest("x + 2y + 3z + 4", differentiate, 'z') == "3" |
| 14 | + assert quickTest("x + 2y + 3z + 4", differentiate, 'x') == "1.0" |
| 15 | + assert quickTest("x + 2y + 3z + 4", differentiate, 'y') == "2.0" |
| 16 | + assert quickTest("x + 2y + 3z + 4", differentiate, 'z') == "3.0" |
17 | 17 |
|
18 | 18 | assert quickTest("xy + xy^2 + xyz", differentiate, 'x') == "y+y^(2.0)+yz"
|
19 | 19 | assert quickTest("xy + xy^2 + xyz", differentiate, 'y') == "x+2.0xy+xz"
|
20 |
| - assert quickTest("xy + xy^2 + xyz", differentiate, 'z') == "+xy" # FIXME: Remove unnecessary sign '+' |
| 20 | + assert quickTest("xy + xy^2 + xyz", differentiate, 'z') == "xy" |
| 21 | + |
| 22 | + assert quickTest("xy + z", differentiate, 'z') == "1.0" |
| 23 | + assert quickTest("z + xy", differentiate, 'z') == "1.0" |
| 24 | + assert quickTest("z - xy", differentiate, 'z') == "1.0" |
| 25 | + assert quickTest("xy - z", differentiate, 'z') == "-1.0" |
| 26 | + |
| 27 | + assert quickTest("sin(x)", differentiate, 'x') == "cos(x)*1.0" |
| 28 | + assert quickTest("sin(x)", differentiate, 'y') == "0.0" |
| 29 | + assert quickTest("sin(xxx)", differentiate, 'x') == "cos(x^(3.0))*3.0x^(2.0)" |
| 30 | + assert quickTest("sin(log(xx))", differentiate, 'x') == "cos(log(x^(2.0)))*x^(-1.0)*2.0x" |
| 31 | + |
| 32 | + assert quickTest("cos(x)", differentiate, 'x') == "-1.0*sin(x)*1.0" |
| 33 | + assert quickTest("cos(x)", differentiate, 'y') == "0.0" |
| 34 | + assert quickTest("cos(xxx)", differentiate, 'x') == "-1.0*sin(x^(3.0))*3.0x^(2.0)" |
| 35 | + assert quickTest("cos(log(xx))", differentiate, 'x') == "-1.0*sin(log(x^(2.0)))*x^(-1.0)*2.0x" |
| 36 | + |
| 37 | + assert quickTest("tan(x)", differentiate, 'x') == "sec(x)*1.0" |
| 38 | + # FIXME: Simplify module simplifies sec^2(x) as sec(x) and cosec^2(x) as cosec(x), however differentiation modules give correct output |
| 39 | + assert quickTest("tan(x)", differentiate, 'y') == "0.0" |
| 40 | + |
| 41 | + assert quickTest("cot(x)", differentiate, 'x') == "-1.0*csc(x)*1.0" |
| 42 | + # FIXME: Simplify module simplifies sec^2(x) as sec(x) and cosec^2(x) as cosec(x), however differentiation modules give correct output |
| 43 | + assert quickTest("cot(x)", differentiate, 'y') == "0.0" |
| 44 | + |
| 45 | + assert quickTest("csc(x)", differentiate, 'x') == "-1.0*csc(x)*cot(x)*1.0" |
| 46 | + assert quickTest("csc(x)", differentiate, 'y') == "0.0" |
| 47 | + |
| 48 | + assert quickTest("sec(x)", differentiate, 'x') == "sec(x)*tan(x)*1.0" |
| 49 | + assert quickTest("sec(x)", differentiate, 'y') == "0.0" |
| 50 | + |
| 51 | + assert quickTest("log(x)", differentiate, 'x') == "x^(-1.0)" |
| 52 | + assert quickTest("log(xx)", differentiate, 'x') == "2.0" |
| 53 | + |
| 54 | + # Tests for Product Rule of Differentiation. |
| 55 | + assert quickTest("sin(x)*cos(x)", differentiationProductRule, 'x') == "(cos(x)*1.0)*cos(x)+sin(x)*(-1.0*sin(x)*1.0)" |
| 56 | + assert quickTest("sin(x)*x", differentiationProductRule, 'x') == "(cos(x)*1.0)*x+sin(x)*(1.0)" |
| 57 | + assert quickTest("sin(x)*y", differentiationProductRule, 'x') == "(cos(x)*1.0)*y+sin(x)*(0.0)" |
| 58 | + assert quickTest("sin(x)*cos(x)*sec(x)", differentiationProductRule, 'x') == "(cos(x)*1.0)*cos(x)*sec(x)+sin(x)*(-1.0*sin(x)*1.0)*sec(x)+sin(x)*cos(x)*(sec(x)*tan(x)*1.0)" |
| 59 | + |
21 | 60 |
|
22 |
| - assert quickTest("xy + z", differentiate, 'z') == "1" |
23 |
| - assert quickTest("z + xy", differentiate, 'z') == "1" |
24 |
| - assert quickTest("z - xy", differentiate, 'z') == "1" |
25 |
| - assert quickTest("xy - z", differentiate, 'z') == "-1" |
26 | 61 | ########################
|
27 | 62 | # calculus.integration #
|
28 | 63 | ########################
|
29 | 64 |
|
30 | 65 |
|
31 | 66 | def test_integrate():
|
32 | 67 |
|
33 |
| - assert quickTest("x + 1", integrate, 'x') == "0.5x^(2)+x" |
| 68 | + assert quickTest("x + 1", integrate, 'x') == "0.5x^(2.0)+x" |
| 69 | + |
| 70 | + assert quickTest("xyz + xy/z + x + 1 + 1/x", integrate, 'x') == "0.5x^(2.0)yz+0.5x^(2.0)yz^(-1.0)+0.5x^(2.0)+x+1.0*log(x)" # FIXME(integration.py): Ignore coeff if 1 |
| 71 | + assert quickTest("xyz + xy/z + x + 1 + 1/x", integrate, 'y') == "0.5xy^(2.0)z+0.5xy^(2.0)z^(-1.0)+xy+y+x^(-1.0)y" |
| 72 | + assert quickTest("xyz + xy/z + x + 1 + 1/x", integrate, 'z') == "0.5xyz^(2.0)+xy*log(z)+xz+z+x^(-1.0)z" |
34 | 73 |
|
35 |
| - assert quickTest("xyz + xy/z + x + 1 + 1/x", integrate, 'x') == "0.5x^(2)yz+0.5x^(2)yz^(-1)+0.5x^(2)+x+1.0*log(x)" # FIXME(integration.py): Ignore coeff if 1 |
36 |
| - assert quickTest("xyz + xy/z + x + 1 + 1/x", integrate, 'y') == "0.5xy^(2)z+0.5xy^(2)z^(-1)+xy+y+x^(-1)y" |
37 |
| - assert quickTest("xyz + xy/z + x + 1 + 1/x", integrate, 'z') == "0.5xyz^(2)+xy*log(z)+xz+z+x^(-1)z" |
| 74 | + assert quickTest("sin(x)", integrate, 'x') == "-1.0*cos(x)" |
| 75 | + assert quickTest("cos(x)", integrate, 'x') == "sin(x)" |
| 76 | + assert quickTest("tan(x)", integrate, 'x') == "-1.0*ln(cos(x))" |
| 77 | + assert quickTest("csc(x)", integrate, 'x') == "-1.0*ln((csc(x)+cot(x)))" |
| 78 | + assert quickTest("sec(x)", integrate, 'x') == "ln((sec(x)+tan(x)))" |
| 79 | + assert quickTest("cot(x)", integrate, 'x') == "ln(sin(x))" |
0 commit comments