Skip to content

Commit

Permalink
Add BoolEval package (#1289)
Browse files Browse the repository at this point in the history
Until we have a PacletManager solution, add this to Packages.
  • Loading branch information
rocky authored Jan 12, 2025
1 parent cab9ea8 commit 7b3ce70
Show file tree
Hide file tree
Showing 12 changed files with 3,426 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ Bugs
Mathics3 Packages
+++++++++++++++++

* Rubi added (preliminary)
* ``BoolEval``
* ``CleanSlate``
* ``Combinatorica`` moved to a separate repository and v.9 renamed to 0.9.1.
More code v0.9.1 works. v2.0 renamed v2.0.1 and some code now works.
* ``Rubi`` version 4.17 (work in progress; algebraic integrations work)


Python Package Updates
Expand Down
1 change: 1 addition & 0 deletions mathics/Packages/BoolEval.m
80 changes: 80 additions & 0 deletions mathics/Packages/BoolEval/BoolEval.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(* Mathematica Package *)
(* :Context: BoolEval` *)
(* :Author: szhorvat *)
(* :Date: 2014-05-30 *)

BeginPackage["BoolEval`"]
(* Exported symbols added here with SymbolName::usage *)

BoolEval::usage =
"BoolEval[array > value] replaces elements of the numerical array 'array' which are greater than 'value' with 1, and the rest with 0.\n" <>
"BoolEval[condition] takes a condition expressed in terms of >, >=, <, <=, ==, != and logical operators, and evaluates it for each element of the arrays appearing in the condition. The result is returned as a Boolean array of 0s and 1s.";

BoolPick::usage =
"BoolPick[array, condition] will return the elements of a numerical array for which condition is True.";

BoolCount::usage =
"BoolCount[condition] counts the number of elements satisfying the array condition.";

Begin["`Private`"] (* Begin Private Context *)

greaterEq[a_, b_] := UnitStep@Subtract[a, b]
lessEq[a_, b_] := UnitStep@Subtract[b, a]
greater[a_, b_] := Subtract[1, lessEq[a, b]]
less[a_, b_] := Subtract[1, greaterEq[a, b]]
unequal[a_, b_] := Unitize@Subtract[a, b]
equal[a_, b_] := Subtract[1, unequal[a, b]]

equal[a_, b_, c__] := equal[a, b] equal[b, c]
less[a_, b_, c__] := less[a, b] less[b, c]
greater[a_, b_, c__] := greater[a, b] greater[b, c]
lessEq[a_, b_, c__] := lessEq[a, b] lessEq[b, c]
greaterEq[a_, b_, c__] := greaterEq[a, b] greaterEq[b, c]

unequal[a__] := Times @@ (unequal @@@ Subsets[{a}, {2}])

rules = Dispatch[{
(* Do not descend into the innards of Image/Audio, so they are safe to use. *)
(* Note that options within these may contain True/False,
which must not be converted to 1/0. *)
im_Image :> im, au_System`Audio :> au,

(* Relational operators *)
Less -> less, LessEqual -> lessEq,
Greater -> greater, GreaterEqual -> greaterEq,
Equal -> equal, Unequal -> unequal,

(* Boolean operators *)
Or -> (Unitize@Plus[##]&), And -> Times, Not -> (Subtract[1, #] &),
Nor -> (Subtract[1, Unitize@Plus[##]]&), Nand -> (Subtract[1, Times[##]]&),
Xor -> (Mod[Plus[##], 2]&), Xnor -> (Subtract[1, Mod[Plus[##], 2]]&),

(* Boolean values *)
True -> 1, False -> 0
}];

(* Convert Inequality expressions to canonical form, e.g. a < b > c -> a < b && b > c *)
ineq = Dispatch[{
HoldPattern@Inequality[a_, op_, b_] :> op[a, b],
HoldPattern@Inequality[a_, op_, b_, rest__] :> op[a, b] && Inequality[b, rest]
}];

SyntaxInformation[BoolEval] = {"ArgumentsPattern" -> {_}};
SetAttributes[BoolEval, HoldAll]
BoolEval[condition_] := First[Hold[condition] //. ineq /. rules]

SyntaxInformation[BoolPick] = {"ArgumentsPattern" -> {_, _}};
SetAttributes[BoolPick, HoldRest]
BoolPick[array_, condition_] :=
Pick[array,
BoolEval[condition],
1
]

SyntaxInformation[BoolCount] = {"ArgumentsPattern" -> {_}};
SetAttributes[BoolCount, HoldAll]
BoolCount[condition_] := Total[BoolEval[condition], Infinity]

End[] (* End Private Context *)

EndPackage[]
189 changes: 189 additions & 0 deletions mathics/Packages/BoolEval/Documentation/English/Guides/BoolEval.nb
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
Notebook[{
Cell[TextData[{
"New in: ",
Cell["1.0", "HistoryData",
CellTags->"New"],
" | Modified in: ",
Cell[" ", "HistoryData",
CellTags->"Modified"],
" | Obsolete in: ",
Cell[" ", "HistoryData",
CellTags->"Obsolete"],
" | Excised in: ",
Cell[" ", "HistoryData",
CellTags->"Excised"]
}], "History",
CellID->1247902091],

Cell[CellGroupData[{

Cell["Categorization", "CategorizationSection",
CellID->1122911449],

Cell["Guide", "Categorization",
CellLabel->"Entity Type",
CellID->686433507],

Cell["BoolEval", "Categorization",
CellLabel->"Paclet Name",
CellID->605800465],

Cell["BoolEval`", "Categorization",
CellLabel->"Context",
CellID->468444828],

Cell["BoolEval/guide/BoolEval", "Categorization",
CellLabel->"URI"]
}, Closed]],

Cell[CellGroupData[{

Cell["Keywords", "KeywordsSection",
CellID->1427428552],

Cell["BoolEval", "Keywords",
CellID->1251852827],

Cell["vectorize", "Keywords",
CellID->431852709],

Cell["vectorise", "Keywords",
CellID->1581027636],

Cell["array comparison", "Keywords",
CellID->1783044154],

Cell["select", "Keywords",
CellID->1354248362]
}, Closed]],

Cell[CellGroupData[{

Cell["Details", "DetailsSection",
CellID->307771771],

Cell["XXXX", "Details",
CellLabel->"Lead",
CellID->795394057],

Cell["XXXX", "Details",
CellLabel->"Developers",
CellID->350963985],

Cell["XXXX", "Details",
CellLabel->"Authors",
CellID->802101],

Cell["XXXX", "Details",
CellLabel->"Feature Name",
CellID->509823116],

Cell["XXXX", "Details",
CellLabel->"QA",
CellID->199884516],

Cell["XXXX", "Details",
CellLabel->"DA",
CellID->463951815],

Cell["XXXX", "Details",
CellLabel->"Docs",
CellID->8237523],

Cell["XXXX", "Details",
CellLabel->"Features Page Notes",
CellID->813936640],

Cell["XXXX", "Details",
CellLabel->"Comments",
CellID->240026365]
}, Closed]],

Cell[CellGroupData[{

Cell["BoolEval", "GuideTitle",
CellID->942062912],

Cell[TextData[{
"The BoolEval package provides an easy way to vectorize comparison \
operations, and thus filter or count array elements in a highly efficient \
way. BoolEval was inspired by the question ",
ButtonBox["\"Does Mathematica have advanced indexing?\"",
BaseStyle->"Hyperlink",
ButtonData->{
URL["https://mathematica.stackexchange.com/q/2821/12"], None}],
" on the ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" StackExchange."
}], "GuideAbstract",
CellID->2001916300]
}, Open ]],

Cell[CellGroupData[{

Cell["", "GuideFunctionsSection",
CellID->1866139230],

Cell[TextData[{
Cell[BoxData[
ButtonBox["BoolEval",
BaseStyle->"Link",
ButtonData->"paclet:BoolEval/ref/BoolEval"]], "InlineGuideFunction"],
" \[LongDash] mark elements of an array that satisfy a condition"
}], "GuideText",
CellID->1550317592],

Cell[TextData[{
Cell[BoxData[
ButtonBox["BoolPick",
BaseStyle->"Link",
ButtonData->"paclet:BoolEval/ref/BoolPick"]], "InlineGuideFunction"],
" \[LongDash] filter an array based on a condition"
}], "GuideText",
CellID->203374175],

Cell[TextData[{
Cell[BoxData[
ButtonBox["BoolCount",
BaseStyle->"Link",
ButtonData->"paclet:BoolEval/ref/BoolCount"]], "InlineGuideFunction"],
" \[LongDash] count elements of an array that satisfy a condition"
}], "GuideText",
CellID->1463276848]
}, Open ]],

Cell[CellGroupData[{

Cell["Tutorials", "GuideTutorialsSection",
CellID->415694126],

Cell[TextData[ButtonBox["Introduction to BoolEval",
BaseStyle->"Link",
ButtonData->
"paclet:BoolEval/tutorial/IntroductionToBoolEval"]], "GuideTutorial",
CellID->806871991]
}, Open ]],

Cell[CellGroupData[{

Cell["More About", "GuideMoreAboutSection",
CellID->23220180],

Cell[TextData[ButtonBox["Elements of Lists",
BaseStyle->"Link",
ButtonData->"paclet:guide/ElementsOfLists"]], "GuideMoreAbout",
CellID->1567025153]
}, Open ]],

Cell["Related Links", "GuideRelatedLinksSection",
CellID->415694148]
},
WindowSize->{700, 770},
WindowMargins->{{4, Automatic}, {Automatic, 0}},
TrackCellChangeTimes->False,
FrontEndVersion->"10.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (December 4, \
2014)",
StyleDefinitions->FrontEnd`FileName[{"Wolfram"}, "GuidePageStyles.nb",
CharacterEncoding -> "UTF-8"]
]
Loading

0 comments on commit 7b3ce70

Please sign in to comment.