Skip to content

Commit

Permalink
Improve calculus module.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 24, 2017
1 parent b1014ce commit b712655
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
7 changes: 7 additions & 0 deletions expreduce/evalstate.go
Expand Up @@ -131,6 +131,13 @@ func (es *EvalState) Init(loadAllDefs bool) {
es.MarkSeen("System`Temporary")
es.MarkSeen("System`Stub")

es.MarkSeen("System`Sec")
es.MarkSeen("System`Csc")
es.MarkSeen("System`Cot")
es.MarkSeen("System`ArcSin")
es.MarkSeen("System`ArcCos")
es.MarkSeen("System`ArcTan")

for _, defSet := range GetAllDefinitions() {
for _, def := range defSet.Defs {
es.MarkSeen(es.GetStringDef("System`$Context", "") + def.Name)
Expand Down
1 change: 1 addition & 0 deletions expreduce/resources/arithmetic.m
Expand Up @@ -116,6 +116,7 @@
Times[den_Integer^-1, num_Integer, rest___] := Rational[num,den] * rest;
(1/Infinity) := 0;
Times[ComplexInfinity, rest___] := ComplexInfinity;
Cos[x_Symbol]*Sin[x_Symbol]^(-1)*rest___ := Cot[x]*rest;
Attributes[Times] = {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected};
Tests`Times = {
ESimpleExamples[
Expand Down
55 changes: 41 additions & 14 deletions expreduce/resources/calculus.m
@@ -1,8 +1,11 @@
D::usage = "`D[f, x]` finds the partial derivative of `f` with respect to `x`.";
D[x_,x_] := 1;
D[a_,x_] := 0;
D[a_,x_] := 0 /; FreeQ[a,x];
D[a_+b_,x_] := D[a,x]+D[b,x];
D[a_ b_,x_] := D[a,x]*b + a*D[b,x];
(*Chain rule*)
(*Prime notation would be nicer here*)
D[f_[g_[x_Symbol]], x_Symbol] := (Evaluate[D[f[#], #]] &)[g[x]]*D[g[x],x];
(*The times operator is needed here. Whitespace precedence is messed up*)
D[a_^(b_), x_] := a^b*(D[b,x] Log[a]+D[a,x]/a*b);
D[Log[a_], x_] := D[a, x]/a;
Expand All @@ -29,26 +32,28 @@
ESameTest[-2 Sin[2 x], D[Cos[2 x], x]],
ESameTest[Cos[x]/x - Sin[x]*Power[x,-2], D[(Sin[x]*x^-1), x]],
ESameTest[-((2 Cos[x])*Power[x,-2]) + (2 Sin[x])*Power[x,-3] - Sin[x]/x, D[D[(Sin[x]*x^-1), x], x]],
ESameTest[-((2 Cos[x])*Power[x,-2]) + (2 Sin[x])*Power[x,-3] - Sin[x]/x, D[D[(Sin[x]*x^-1+Sin[y]), x], x]]
ESameTest[-((2 Cos[x])*Power[x,-2]) + (2 Sin[x])*Power[x,-3] - Sin[x]/x, D[D[(Sin[x]*x^-1+Sin[y]), x], x]],
ESameTest[-Cos[Cos[x]] Sin[x], D[Sin[Cos[x]],x]],
ESameTest[Cos[Log[x]]/x, D[Sin[Log[x]],x]],
ESameTest[-(Sin[Log[x]]/x), D[Cos[Log[x]],x]],
ESameTest[1-(1+Cot[x]) Sin[x+Log[Sin[x]]], D[Cos[Log[Sin[x]]+x]+x,x]]
]
};

Integrate::usage = "`Integrate[f, x]` finds the indefinite integral of `f` with respect to `x`.
!!! warning \"Under development\"
This function is under development, and as such will be incomplete and inaccurate.";
(*Might need to be implemented in code. Try running Integrate[-10x, {x, 1, 5}]*)
(*with this*)
(*"Integrate[a_,{x_Symbol,start_Integer,end_Integer}]": "ReplaceAll[Integrate[a, x],x->end] - ReplaceAll[Integrate[a, x],x->start]",*)
Integrate[a_,{x_Symbol,start_,end_}] :=
ReplaceAll[Integrate[a, x],x->end] - ReplaceAll[Integrate[a, x],x->start];
Integrate[a_Integer,x_Symbol] := a*x;
Integrate[a_Integer*b_,x_Symbol] := a*Integrate[b,x];
Integrate[a_+b__,x_Symbol] := Integrate[a,x]+Integrate[Plus[b],x];
Integrate[a_*b_,x_Symbol] := a*Integrate[b,x] /; FreeQ[a,x];
Integrate[a_+b_,x_Symbol] := Integrate[a,x]+Integrate[b,x];

(*Basic power integrals*)
Integrate[x_Symbol,x_Symbol] := x^2/2;
Integrate[a_Symbol,x_Symbol] := a*x;
Integrate[x_Symbol^n_Integer, x_Symbol] := x^(n+1)/(n+1);
Integrate[x_Symbol^n_Rational, x_Symbol] := x^(n+1)/(n+1);
Integrate[a_Symbol,x_Symbol] := If[a===x, x^2/2, a*x];
Integrate[x_Symbol^e_, x_Symbol] := x^(e+1)/(e+1) /; FreeQ[e, x];
Integrate[a_^(b_*x_Symbol),x_Symbol] := a^(b x)/(b Log[a]) /; (FreeQ[a, x] && FreeQ[b, x]);
Integrate[1/x_Symbol,x_Symbol] := Log[Abs[x]];
Integrate[Log[x_Symbol],x_Symbol] := -x + x Log[x];
Integrate[x_Symbol*Log[x_Symbol],x_Symbol] := -((x^2)/4) + (1/2)*(x^2)*Log[x];
Expand All @@ -57,14 +62,36 @@
Integrate[Sin[x_Symbol],x_Symbol] := -Cos[x];
Integrate[Cos[x_Symbol],x_Symbol] := Sin[x];
Integrate[Tan[x_Symbol],x_Symbol] := -Log[Cos[x]];
Integrate[Sec[x_Symbol]^2,x_Symbol] := Tan[x];
Integrate[Csc[x_Symbol]^2,x_Symbol] := -Cot[x];
Integrate[Sec[x_Symbol]Tan[x_Symbol],x_Symbol] := Sec[x];
Integrate[Csc[x_Symbol]Cot[x_Symbol],x_Symbol] := -Csc[x];
Integrate[Sqrt[1-x_Symbol^2]^(-1),x_Symbol] := ArcSin[x];
Integrate[(1+x_Symbol^2)^(-1),x_Symbol] := ArcTan[x];

(*This may not always reduce. Look into this*)
(*Integrate[u_Symbol*v_, u_Symbol] := u*Integrate[v, u] - Integrate[Integrate[v, u], u];*)

(*Integrate by parts*)
(*{"Integrate[u_*v_, x_Symbol]", "u*Integrate[v, x] - Integrate[D[u, x]*Integrate[v, x], x]"},*)
Attributes[Integrate] = {ReadProtected, Protected};
Tests`Integrate = {
ESimpleExamples[
ESameTest[2 x + (3 x^(5/3))/5 + (3 x^2)/2, Integrate[x^(2/3) + 3 x + 2, x]],
ESameTest[-((3 x^2)/4) + (1/2) (x^2) Log[x] - Sin[x], Integrate[Integrate[Sin[x] + Log[x], x], x]]
ESameTest[-((3 x^2)/4) + (1/2) (x^2) Log[x] - Sin[x], Integrate[Integrate[Sin[x] + Log[x], x], x]],
ESameTest[1/3, Integrate[x^2, {x, 0, 1}]],
ESameTest[True, (Integrate[x^2, {x, 0.5, 1.}] - 0.29166667) < .00001],
(*ESameTest[-(E^(3 x)/9)+1/3 E^(3 x) x, Integrate[E^(3*x)*x, x]//Expand],*)
ESameTest[E^(3*x)/3, Integrate[E^(3*x), x]],
ESameTest[x^(1 + a + b)/(1 + a + b), Integrate[x^(a + b), x]],
ESameTest[n^3/3, Integrate[x^2, {x, 0, n}]],
ESameTest[x^3/3, Integrate[x^2, {x, 0, x}]]
], ETests[
(*Test some trig definitions*)
ESameTest[Tan[x], Integrate[Sec[x]^2,x]],
ESameTest[-Cot[x], Integrate[Csc[x]^2,x]],
ESameTest[Sec[x], Integrate[Sec[x]Tan[x],x]],
ESameTest[-Csc[x], Integrate[Csc[x]Cot[x],x]],
ESameTest[ArcSin[x], Integrate[1/Sqrt[1-x^2],x]],
ESameTest[ArcTan[x], Integrate[1/(1+x^2),x]]
], EKnownFailures[
ESameTest[Log[x] - 1/2 Log[1 + 2 x^2], Integrate[1/(2 x^3 + x), x]]
]
Expand Down

0 comments on commit b712655

Please sign in to comment.