Skip to content

Commit

Permalink
Add some new functions for Rubi.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Sep 25, 2017
1 parent d945503 commit fa7204f
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 11 deletions.
2 changes: 2 additions & 0 deletions expreduce/builtin_list.go
Expand Up @@ -578,5 +578,7 @@ func GetListDefinitions() (defs []Definition) {
defs = append(defs, Definition{Name: "Last"})
defs = append(defs, Definition{Name: "First"})
defs = append(defs, Definition{Name: "Rest"})
defs = append(defs, Definition{Name: "Count"})
defs = append(defs, Definition{Name: "Tally"})
return
}
7 changes: 3 additions & 4 deletions expreduce/builtin_numbertheory.go
Expand Up @@ -146,9 +146,8 @@ func GetNumberTheoryDefinitions() (defs []Definition) {
})*/
defs = append(defs, Definition{Name: "EvenQ"})
defs = append(defs, Definition{Name: "OddQ"})
defs = append(defs, Definition{
Name: "FactorInteger",
OmitDocumentation: true,
})
defs = append(defs, Definition{Name: "FactorInteger"})
defs = append(defs, Definition{Name: "FractionalPart"})
defs = append(defs, Definition{Name: "IntegerPart"})
return
}
1 change: 1 addition & 0 deletions expreduce/evalstate.go
Expand Up @@ -143,6 +143,7 @@ func (es *EvalState) Init(loadAllDefs bool) {

es.MarkSeen("System`Exp")
es.MarkSeen("System`AppellF1")
es.MarkSeen("System`Hypergeometric2F1")

es.MarkSeen("System`Cosh")
es.MarkSeen("System`Sinh")
Expand Down
8 changes: 4 additions & 4 deletions expreduce/resources.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions expreduce/resources/list.m
Expand Up @@ -378,3 +378,18 @@
ESameTest[foo[a,b,c], Join[foo[a],foo[b,c]]]
]
};

Count::usage = "`Count[l, pattern]` returns the number of expressions in `l` matching `pattern`.";
Count[l_List, pat_] := Length[Cases[l, pat]];
Attributes[Count] = {Protected};

Tally::usage = "`Tally[list]` creates tallies of the elements in `list`.";
Tally[l_List] := {#, Count[l, #]} & /@ DeleteDuplicates[l];
Attributes[Tally] = {Protected};
Tests`Tally = {
ESimpleExamples[
ESameTest[{{a, 2}, {b, 2}}, Tally[{a, a, b, b}]],
ESameTest[{{b, 2}, {a, 2}}, Tally[{b, b, a, a}]],
ESameTest[{{b, 2}, {a, 1}}, Tally[{b, b, a}]],
]
};
93 changes: 90 additions & 3 deletions expreduce/resources/numbertheory.m
Expand Up @@ -93,6 +93,7 @@
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];
Mod[Rational[a_,b_],c_Integer] := Mod[a,c*b] / b;
Attributes[Mod] = {Listable, NumericFunction, ReadProtected, Protected};
Tests`Mod = {
ESimpleExamples[
Expand All @@ -105,7 +106,16 @@
ESameTest[Mod[a,3], Mod[a,3]],
ESameTest[Indeterminate, Mod[0,0]],
ESameTest[Mod[2,a], Mod[2,a]],
ESameTest[Mod[0,a], Mod[0,a]]
ESameTest[Mod[0,a], Mod[0,a]],
ESameTest[1,Mod[-1,2]],
ESameTest[5/4,Mod[-(3/4),2]],
ESameTest[3/2,Mod[-(1/2),2]],
ESameTest[7/4,Mod[-(1/4),2]],
ESameTest[0,Mod[0,2]],
ESameTest[1/4,Mod[1/4,2]],
ESameTest[1/2,Mod[1/2,2]],
ESameTest[3/4,Mod[3/4,2]],
ESameTest[1,Mod[1,2]],
], EKnownFailures[
ESameTest[1.5, Mod[1.5,3]],
ESameTest[0., Mod[2,0.5]]
Expand Down Expand Up @@ -138,7 +148,7 @@
]
};

FactorInteger[n_] := Switch[n,
(*FactorInteger[n_] := Switch[n,
-20, {{-1,1},{2,2},{5,1}},
-7, {{-1,1},{7,1}},
-3, {{-1,1},{3,1}},
Expand All @@ -155,4 +165,81 @@
Rational[-15,2], {{-1,1},{2,-1},{3,1},{5,1}},
Rational[-1,2], {{-1, 1}, {2, -1}},
Rational[1,2], {{2, -1}},
_, Print["Invalid call to FactorInteger!", n]];
_, Print["Invalid call to FactorInteger!", n]];*)

FactorInteger::usage = "`FactorInteger[n]` factors the integer `n`.";
FactorInteger[Rational[n_, d_]] :=
DeleteCases[Join[FactorInteger[n], ({#[[1]], -#[[2]]} &) /@ FactorInteger[d]] // Sort, {1,1}];
FactorInteger[int_Integer?Negative] := DeleteCases[Prepend[FactorInteger[-int], {-1, 1}], {1,1}];
FactorInteger[0] := {{0, 1}};
(* TODO: use Pollard's rho algorithm. *)

FactorInteger[int_Integer?Positive] := Module[{n = int, i = 2, factors},
If[n === 1, Return[{{1, 1}}]];
factors = {};
While[n =!= 1,
While[Mod[n, i] =!= 0, i = i + 1];
AppendTo[factors, i];
n = n/i;
i = 2
];
Tally[factors] // Sort
];
Attributes[FactorInteger] = {Listable, Protected};
Tests`FactorInteger = {
ESimpleExamples[
ESameTest[{{2, 3}}, FactorInteger[8]],
ESameTest[{{-1,1},{2,-1},{3,1},{5,1}}, FactorInteger[Rational[-15,2]]],
], ETests[
ESameTest[{{-1,1},{2,2},{5,1}}, FactorInteger[-20]],
ESameTest[{{-1,1},{7,1}}, FactorInteger[-7]],
ESameTest[{{-1,1},{3,1}}, FactorInteger[-3]],
ESameTest[{{-1,1},{2,1}}, FactorInteger[-2]],
ESameTest[{{-1,1}}, FactorInteger[-1]],
ESameTest[{{0, 1}}, FactorInteger[0]],
ESameTest[{{1, 1}}, FactorInteger[1]],
ESameTest[{{2, 1}}, FactorInteger[2]],
ESameTest[{{3, 1}}, FactorInteger[3]],
ESameTest[{{2, 2}}, FactorInteger[4]],
ESameTest[{{7, 1}}, FactorInteger[7]],
ESameTest[{{2, -2}, {3, 1}, {7, 1}}, FactorInteger[Rational[21,4]]],
ESameTest[{{-1, 1}, {2, -1}}, FactorInteger[Rational[-1,2]]],
ESameTest[{{2, -1}}, FactorInteger[Rational[1,2]]],
]
};

FractionalPart::usage = "`FractionalPart[n]` gives the fractional part of `n`";
FractionalPart[n_Rational] := If[n >= 0, Mod[n, 1], -Mod[-n, 1]];
FractionalPart[n_Integer] := 0;
Attributes[FractionalPart] = {Listable, NumericFunction, Protected, ReadProtected};
Tests`FractionalPart = {
ESimpleExamples[
ESameTest[0,FractionalPart[-1]],
ESameTest[-(3/4),FractionalPart[-(3/4)]],
ESameTest[-(1/2),FractionalPart[-(1/2)]],
ESameTest[-(1/4),FractionalPart[-(1/4)]],
ESameTest[0,FractionalPart[0]],
ESameTest[1/4,FractionalPart[1/4]],
ESameTest[1/2,FractionalPart[1/2]],
ESameTest[3/4,FractionalPart[3/4]],
ESameTest[0,FractionalPart[1]],
]
};

IntegerPart::usage = "`IntegerPart[n]` gives the integer part of `n`";
IntegerPart[n_Rational] := n - FractionalPart[n];
IntegerPart[n_Integer] := n;
Attributes[IntegerPart] = {Listable, NumericFunction, Protected, ReadProtected};
Tests`IntegerPart = {
ESimpleExamples[
ESameTest[-1,IntegerPart[-1]],
ESameTest[0,IntegerPart[-(3/4)]],
ESameTest[0,IntegerPart[-(1/2)]],
ESameTest[0,IntegerPart[-(1/4)]],
ESameTest[0,IntegerPart[0]],
ESameTest[0,IntegerPart[1/4]],
ESameTest[0,IntegerPart[1/2]],
ESameTest[0,IntegerPart[3/4]],
ESameTest[1,IntegerPart[1]],
]
};

0 comments on commit fa7204f

Please sign in to comment.