Skip to content

Commit

Permalink
Even better automatic simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 24, 2017
1 parent 7ac0c5a commit 8ea323c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions expreduce/builtin_arithmetic.go
Expand Up @@ -340,5 +340,6 @@ func getArithmeticDefinitions() (defs []Definition) {
},
})
defs = append(defs, Definition{Name: "Abs"})
defs = append(defs, Definition{Name: "Divide"})
return
}
14 changes: 12 additions & 2 deletions expreduce/builtin_system.go
Expand Up @@ -27,10 +27,20 @@ func exprToN(es *EvalState, e Ex) Ex {
toReturn, _ := RationalToFlt(asRat)
return toReturn
}
_, isSym := e.(*Symbol)
if isSym {
toReturn, defined, _ := es.GetDef(
"System`N",
NewExpression([]Ex{&Symbol{"System`N"}, e}),
)
if defined {
return toReturn
}
}
asExpr, isExpr := e.(*Expression)
if isExpr {
toReturn, defined, _ := es.GetDef(
"N",
"System`N",
NewExpression([]Ex{&Symbol{"System`N"}, e}),
)
if defined {
Expand All @@ -39,7 +49,7 @@ func exprToN(es *EvalState, e Ex) Ex {
exToReturn := NewEmptyExpression()
for _, part := range asExpr.Parts {
toAdd, defined, _ := es.GetDef(
"N",
"System`N",
NewExpression([]Ex{&Symbol{"System`N"}, part}),
)
if !defined {
Expand Down
14 changes: 13 additions & 1 deletion expreduce/resources/arithmetic.m
Expand Up @@ -116,7 +116,8 @@
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;
Sin[x_]*Cos[x_]^(-1)*rest___ := Tan[x]*rest;
Cos[x_]*Sin[x_]^(-1)*rest___ := Cot[x]*rest;
Attributes[Times] = {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected};
Tests`Times = {
ESimpleExamples[
Expand Down Expand Up @@ -197,6 +198,8 @@

Abs::usage = "`Abs[expr]` calculates the absolute value of `expr`.";
Abs[a_?NumberQ] := If[a<0,-a,a];
Abs[Infinity] := Infinity;
Abs[ComplexInfinity] := Infinity;
Abs[-a_] := Abs[a];
Attributes[Abs] = {Listable, NumericFunction, Protected, ReadProtected};
Tests`Abs = {
Expand All @@ -213,3 +216,12 @@
ESameTest[Abs[x^(a + b)], Abs[-x^(a + b)]]
]
};

Divide::usage = "`Divide[a, b]` computes `a/b`.";
Divide[a_,b_] := a/b;
Attributes[Divide] = {Listable, NumericFunction, Protected};
Tests`Divide = {
ESimpleExamples[
ESameTest[2, Divide[10, 5]]
]
};
5 changes: 4 additions & 1 deletion expreduce/resources/numbertheory.m
Expand Up @@ -91,12 +91,15 @@
};

Mod::usage = "`Mod[x, y]` finds the remainder when `x` is divided by `y`.";
(* Factor out numeric constants like Pi: *)
Mod[a_Integer*c_?NumericQ,b_Integer*c_?NumericQ] := c * Mod[a,b];
Attributes[Mod] = {Listable, NumericFunction, ReadProtected, Protected};
Tests`Mod = {
ESimpleExamples[
ESameTest[2, Mod[5,3]],
ESameTest[0, Mod[0,3]],
ESameTest[Indeterminate, Mod[2,0]]
ESameTest[Indeterminate, Mod[2,0]],
ESameTest[Pi, Mod[-2 Pi,3 Pi]]
], ETests[
ESameTest[1, Mod[-5,3]],
ESameTest[Mod[a,3], Mod[a,3]],
Expand Down
5 changes: 5 additions & 0 deletions expreduce/resources/power.m
Expand Up @@ -162,10 +162,15 @@

Log::usage = "`Log[e]` finds the natural logarithm of `e`.";
Log[-1] := I*Pi;
Log[ComplexInfinity] := Infinity;
Log[Infinity] := Infinity;
Log[-ComplexInfinity] := Infinity;
Log[-Infinity] := Infinity;
Log[0] := -Infinity;
Log[1] := 0;
Log[E] := 1;
Log[E^p_?NumberQ] := p;
Log[Rational[1,b_]] := -Log[a];
Attributes[Log] = {Listable,NumericFunction,Protected};
Tests`Log = {
ESimpleExamples[
Expand Down
6 changes: 2 additions & 4 deletions expreduce/resources/specialsyms.m
@@ -1,8 +1,6 @@
Infinity::usage = "`Infinity` represents the mathematical concept of infinity.";
Plus[Infinity, _Integer, rest___] := Infinity + rest;
Plus[Infinity, _Real, rest___] := Infinity + rest;
Plus[-Infinity, _Integer, rest___] := -Infinity + rest;
Plus[-Infinity, _Real, rest___] := -Infinity + rest;
Plus[Infinity, _, rest___] := Infinity + rest;
Plus[-Infinity, _, rest___] := -Infinity + rest;
Plus[Infinity, -Infinity, rest___] := Indeterminate + rest;
Attributes[Infinity] = {ReadProtected, Protected};
Tests`Infinity = {
Expand Down
3 changes: 3 additions & 0 deletions expreduce/resources/trig.m
@@ -1,10 +1,12 @@
Sin::usage = "`Sin[x]` is the sine of `x`.";
Sin[0] := 0;
Sin[-x_] := -Sin[x];
Sin[p_Plus] := -Sin[-p] /; (MatchQ[p[[1]], -_] || p[[1]] < 0);
Sin[x_Integer?Negative] := -Sin[-x];
Sin[Pi] := 0;
Sin[n_Integer*Pi] := 0;
Sin[I*a_] := I*Sinh[a];
Sin[Indeterminate] := Indeterminate;
Attributes[Sin] = {Listable, NumericFunction, Protected};

Cos::usage = "`Cos[x]` is the cosine of `x`.";
Expand All @@ -15,6 +17,7 @@
Cos[I*a_] := Cosh[a];
Cos[-x_] := Cos[x];
Cos[x_Integer?Negative] := Cos[-x];
Cos[Indeterminate] := Indeterminate;
Attributes[Cos] = {Listable, NumericFunction, Protected};

Tan::usage = "`Tan[x]` is the tangent of `x`.";
Expand Down

0 comments on commit 8ea323c

Please sign in to comment.