Skip to content

Commit ba0f9eb

Browse files
authored
Add additional steps format and auto-highlights
This modification is a result from [issue#34](RuleBasedIntegration#34 (comment)); the additional `IntWithStepsOfTeXForm` by [@wuyudi](https://github.com/wuyudi) and `IntTraditional` by [@asdasd1dsadsa](https://github.com/asdasd1dsadsa) are integrated (with some change) as accepted values (resp., `TeXForm` and `TraditionalForm`) for the `RubiPrintInformation` option to the `Steps` function. The corresponding `::usage` message is modified as well. `MakeBox` formatting for `Subst` is added as suggested by [@asdasd1dsadsa](https://github.com/asdasd1dsadsa), with minor modification. `SyntaxInformation` is added for `Int` and `Steps`; this covers the [pull-request#43](RuleBasedIntegration#43 (comment)).
1 parent a57f10f commit ba0f9eb

1 file changed

Lines changed: 39 additions & 9 deletions

File tree

Rubi/Rubi.m

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
Subst::usage = "Subst[expn1,var,expn2] substitutes <expn2> for <var> in <expn1>.";
2424
Step::usage = "Step[Int[expn, var]] displays the first step in the integration of <expn> with respect to <var> and returns the intermediate result.";
2525
Steps::usage = "Steps[Int[expn, var]] displays all the steps in the integration of <expn> with respect to <var> and returns the antiderivative.";
26-
Stats::usage = "Stats[Int[expn, var]] prints statistical information of the integration before returning the antiderivative <expn> with respect to <var>." <>
27-
"It consists of (a) the number of steps used to integrate, (b) the number of distinct rules used, (c) is the leaf count size of the input," <>
26+
Stats::usage = "Stats[Int[expn, var]] prints statistical information of the integration before returning the antiderivative <expn> with respect to <var>. " <>
27+
"It consists of (a) the number of steps used to integrate, (b) the number of distinct rules used, (c) is the leaf count size of the input, " <>
2828
"(d) the leaf count size of the antiderivative, and (e) the rule-to-size ratio of the integration (i.e. the quotient of (b) and (c)).";
2929

3030
$RubiVersion::usage = "$RubiVersion shows the currently loaded version of Rubi.";
3131
RubiRule::usage = "RubiRule is a symbolic wrapper that is used when displaying integration steps.";
3232
RubiIntermediateResult::usage = "RubiIntermediateResult is a symbolic wrapper that is used when displaying integration steps.";
33-
RubiStats::usage = "RubiStats is a symbolic wrapper that contains statistical information about an integration." <>
34-
"It consists of (a) the number of steps used to integrate, (b) the number of distinct rules used, (c) is the leaf count size of the input," <>
33+
RubiStats::usage = "RubiStats is a symbolic wrapper that contains statistical information about an integration. " <>
34+
"It consists of (a) the number of steps used to integrate, (b) the number of distinct rules used, (c) is the leaf count size of the input, " <>
3535
"(d) the leaf count size of the antiderivative, and (e) the rule-to-size ratio of the integration (i.e. the quotient of (b) and (c)).";
36-
RubiPrintInformation::usage = "RubiPrintInformation is an option to Steps and Stats that prints information if set to True and returns as a list otherwise.";
36+
RubiPrintInformation::usage = "RubiPrintInformation is an option to Steps and Stats that prints information if set to True and returns as a list otherwise. " <>
37+
"For Steps only: RubiPrintInformation can also be set to TeXForm or TraditionalForm to produce a stylized step-by-step solution; " <>
38+
"issues a message when set to anything other than True, False, TeXForm or TraditionalForm, but returns the steps as a list nevertheless.";
3739
RubiClearMemoryImages::usage = "RubiClearMemoryImages[] deletes the memory files that are created for each system to speed-up the loading time of the package. " <>
3840
"The memory files are recreated during the next loading of the Rubi package.";
3941

@@ -95,6 +97,11 @@
9597

9698
Unprotect[Int]; Clear[Int]; Clear[Unintegrable]; Clear[CannotIntegrate];
9799

100+
(*auto-highlighting for Int[]*)
101+
SyntaxInformation[Int]={"LocalVariables"->{"Integrate",{2,2}}};
102+
(*Formatted display of Subst*)
103+
Subst /: MakeBoxes[HoldPattern@Subst[expr_,src_,tar_], TraditionalForm] := RowBox@{UnderscriptBox[StyleBox["subst.", FontSize -> Medium], RowBox[{MakeBoxes[src, TraditionalForm], ":\[Rule]", MakeBoxes[tar, TraditionalForm]}]], MakeBoxes[expr, TraditionalForm]}
104+
98105
(* The order of loading the rule-files below is crucial to ensure a functional Rubi integrator! *)
99106
LoadRules[$utilityPackage];
100107
LoadRules[{"9 Miscellaneous", "9.1 Integrand simplification rules"}];
@@ -338,7 +345,10 @@
338345
Options[Steps] = {
339346
RubiPrintInformation -> True
340347
};
348+
SyntaxInformation[Steps] = {"ArgumentsPattern" -> {_, OptionsPattern[]}, "OptionNames" -> ToString /@ First /@ Options[Steps]}; (* front end examination for Options*)
341349
Int::wrngUsage = "Wrong usage of the `1` function. Please use `1`[Int[expr, x]].";
350+
Steps::wrngOpt = "Value of option `1` is not recognized. Acceptable specifications are `2`.";
351+
Steps[fpar___, Null, epar___] := Steps[fpar, Default[Steps, Length[{epar}]+1], epar] (*allow writing Steps[Int[expr],,opts]; cf. https://mathematica.stackexchange.com/questions/95407/skipping-middle-optional-arguments*)
342352
Steps[Int[expr_, x_], opts : OptionsPattern[]] := Steps[Int[expr, x], $IterationLimit, opts];
343353
Steps[Int[expr_, x_], n_Integer, OptionsPattern[]] := Module[{result, steps},
344354
If[$LoadShowSteps =!= True,
@@ -356,10 +366,30 @@
356366
n - 1
357367
]
358368
];
359-
If[OptionValue[RubiPrintInformation] === True,
360-
PrintRubiSteps[steps];
361-
result,
362-
{steps, result}
369+
Switch[OptionValue[RubiPrintInformation],
370+
True,
371+
PrintRubiSteps[steps]; result,
372+
False,
373+
{steps, result},
374+
TeXForm, (*not actually used, but is good as an option name*)
375+
With[{TeX2Str = Convert`TeX`ExpressionToTeX(*ToString@*TeXForm is also OK*)},
376+
{steps, result}
377+
// Flatten // Most
378+
// Cases[RubiIntermediateResult[x_]:>"=&"<>(TeX2Str[HoldForm @@ x])<>"\\\\"]
379+
// {"\\begin{aligned}", TeX2Str@HoldForm@Int[expr, var], Sequence @@ #, "\\end{aligned}"} &
380+
// StringReplace[{"\\, d" <> ToString[var] -> "\\, \\mathrm{d}" <> ToString[var], "\\int" -> "\\displaystyle \\int"}]
381+
// StringRiffle],
382+
TraditionalForm,
383+
Grid[{"\[LongEqual]", TraditionalForm@#}& /@
384+
Cases[Flatten@First@{steps, result},
385+
RubiIntermediateResult[x_] :> HoldForm@@x
386+
] // Prepend[{"",TraditionalForm@HoldForm@Int[expr,var]}],
387+
Alignment -> Left],
388+
_,
389+
Message[Steps::wrngOpt,
390+
RubiPrintInformation -> OptionValue[RubiPrintInformation],
391+
First /@ Options[Steps]
392+
]; {steps, result}
363393
]
364394
] /; Head[x] === Symbol && If[TrueQ[n > 0], True, Message[Steps::negSteps]; False];
365395
Steps[___] := (Message[Int::wrngUsage, Steps]; $Failed);

0 commit comments

Comments
 (0)