Skip to content

Commit f4508f6

Browse files
committed
Add option to Steps/Stats/Steps to *return* information instead of printing
1 parent 066d93c commit f4508f6

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

Rubi/PacletInfo.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Paclet[
77
Name -> "Rubi",
8-
Version -> "4.15.2.1",
8+
Version -> "4.16.0.2",
99
MathematicaVersion -> "11+",
1010
Description -> "Package for Rule-based Integration",
1111
Creator -> "Albert D. Rich",

Rubi/Rubi.m

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
RubiStats::usage = "RubiStats is a symbolic wrapper that contains statistical information about an integration." <>
2424
"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," <>
2525
"(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)).";
26-
26+
RubiPrintInformation::usage = "RubiPrintInformation is an option to Steps and Stats that prints information if set to True and returns as a list otherwise.";
2727
Unintegrable::usage = "Unintegrable[expn,var] indicates <expn> is not integrable with respect to <var> in closed-form.";
2828
CannotIntegrate::usage = "CannotIntegrate[expn,var] indicates Rubi is unable to integrate <expn> with respect to <var>.";
2929

@@ -287,9 +287,12 @@
287287
];
288288

289289
Int::argFlag = "The `` routine can only be used with the form Int[expr, x] where x is a symbol.";
290-
Int::noShowSteps = "To use this function, you need to define $LoadShowSteps=True before loading the Rubi package"
290+
Int::noShowSteps = "To use this function, you need to define $LoadShowSteps=True before loading the Rubi package";
291291
SetAttributes[Steps, {HoldAllComplete}];
292-
Steps[Int[expr_, x_], n_Integer : Infinity] := Module[{result, steps},
292+
Options[Steps] = {
293+
RubiPrintInformation -> True
294+
};
295+
Steps[Int[expr_, x_], n_Integer : Infinity, OptionsPattern[]] /; n > 0 := Module[{result, steps},
293296
{result, steps} = Reap@Block[{$ShowSteps = True},
294297
FixedPoint[
295298
Function[int,
@@ -298,31 +301,48 @@
298301
ReleaseHold[held]
299302
]
300303
], Int[expr, x],
301-
n
304+
n - 1
302305
]
303306
];
304-
PrintRubiSteps[steps];
305-
result
307+
If[OptionValue[RubiPrintInformation] === True,
308+
PrintRubiSteps[steps];
309+
result,
310+
{steps, result}
311+
]
306312
] /; TrueQ[$LoadShowSteps] && Head[x] === Symbol && n > 0;
307313
Steps[int : Int[__]] := (Message[Int::noShowSteps]; int);
308314
Steps[___] := Null /; Message[Int::argFlag, "Steps"];
309315

310316
SetAttributes[Step, {HoldAllComplete}];
311-
Step[Int[expr_, x_]] := Module[{result, step},
317+
Options[Step] = {
318+
RubiPrintInformation -> True
319+
};
320+
Step[Int[expr_, x_], OptionsPattern[]] := Module[{result, step},
312321
{result, step} = Reap@Block[{$ShowSteps = True}, Int[expr, x]];
313-
PrintRubiSteps[step];
314-
result
322+
If[OptionValue[RubiPrintInformation] === True,
323+
PrintRubiSteps[step];
324+
result,
325+
{step, result}
326+
]
315327
] /; TrueQ[$LoadShowSteps && Head[x] === Symbol];
316328
Step[int : Int[__]] := (Message[Int::noShowSteps]; int);
317329
Step[___] := Null /; Message[Int::argFlag, "Step"];
318330

319331
SetAttributes[Stats, {HoldAllComplete}];
320-
Stats[Int[expr_, x_]] := Block[{$ShowSteps = False, $StepCounter = 0, $RuleList = {}},
332+
Options[Stats] = {
333+
RubiPrintInformation -> True
334+
};
335+
Stats[Int[expr_, x_], OptionsPattern[]] := Block[{$ShowSteps = False, $StepCounter = 0, $RuleList = {}},
321336
With[{result = Int[expr, x]},
322-
{
323-
RubiStats@{$StepCounter, Length[$RuleList], LeafCount[expr], LeafCount[result], N[Length[$RuleList] / LeafCount[expr], 4], $RuleList},
324-
result
325-
}]] /; TrueQ[$LoadShowSteps] && Head[x] === Symbol;
337+
If[OptionValue[RubiPrintInformation] === True,
338+
Print@RubiStats@{$StepCounter, Length[$RuleList], LeafCount[expr], LeafCount[result], N[Length[$RuleList] / LeafCount[expr], 4], $RuleList};
339+
result,
340+
{
341+
RubiStats@{$StepCounter, Length[$RuleList], LeafCount[expr], LeafCount[result], N[Length[$RuleList] / LeafCount[expr], 4], $RuleList},
342+
result
343+
}
344+
]
345+
]] /; TrueQ[$LoadShowSteps] && Head[x] === Symbol;
326346
Stats[int : Int[_, _Symbol]] := (Message[Int::noShowSteps]; int);
327347
Stats[___] := Null /; Message[Int::argFlag, "Stats"];
328348

@@ -332,25 +352,17 @@
332352
Int[_, _, flag : (Stats | Step | Steps)] := Null /; Message[Int::oldFlag, flag];
333353

334354

335-
Int[u_, {x_Symbol, a_, b_}] :=
336-
With[{result = Int[u, x]},
337-
Limit[result, x -> b] - Limit[result, x -> a]]
338-
339-
340-
Int[{u__}, x_Symbol] :=
341-
Map[Function[Int[#, x]], {u}]
342-
355+
Int[u_, {x_Symbol, a_, b_}] := With[{result = Int[u, x]}, Limit[result, x -> b] - Limit[result, x -> a]];
356+
Int[{u__}, x_Symbol] := Map[Function[Int[#, x]], {u}];
343357

344358
Protect[Int];
345359

346-
347360
$Unintegrable = False;
348-
349-
350361
Unintegrable[u_, x_] :=
351362
If[$Unintegrable === True,
352363
Defer[Unintegrable][u, x],
353-
Defer[Int][u, x]];
364+
Defer[Int][u, x]
365+
];
354366

355367
CannotIntegrate[u_, x_] := Defer[Int][u, x];
356368
LoadRules[$ruleFormatting];

Rubi/ShowStepRoutines.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ rhs is the expression on the right side of the rule (i.e. the consequent of the
233233
If[Head[$RuleList] === List && Not[MemberQ[$RuleList, num]],
234234
$RuleList = Append[$RuleList, num]];
235235
If[$ShowSteps === True,
236-
Sow[RubiRule[condStrg, MakeExpression[lhsStrg], MakeExpression[rhsStrg], num + 1 ]];
236+
Sow[RubiRule[condStrg, MakeExpression[lhsStrg], MakeExpression[rhsStrg], num ]];
237237
Block[{SimplifyFlag = False},
238238
ReplaceAll[ReleaseHold[rhs], {Unintegrable -> Defer[Int], CannotIntegrate -> Defer[Int]}]],
239239
ReleaseHold[rhs]] )

0 commit comments

Comments
 (0)