2
$\begingroup$

In Mathematica, a trick to speed up the process of definite integrals is by adding assumptions. For example, the following codes run faster if we add such assumptions:

Integrate[1/(Sqrt[2 π ] sx) Exp[-(x - xi)^2/(2 sx^2)] 
1/(Sqrt[2 π ] sy)Exp[-(yi - k x - b)^2/(2 sy^2)], {x, -∞, ∞}] // AbsoluteTiming

(*{23.4375, 
 ConditionalExpression[E^(-((b + k xi - yi)^2/(2 (k^2 sx^2 + sy^2))))/
(Sqrt[2 π] Sqrt[sx] Sqrt[1/sx^2 + k^2/sy^2] Sqrt[sy]), Re[1/sx^2 + k^2/sy^2] > 0]}*)


Assuming[sx > 0 && sy > 0 && xi > 0 && yi > 0 && k > 0 && b > 0, 
  Integrate[1/(Sqrt[2 π] sx) Exp[-(x - xi)^2/(2 sx^2)] 
1/(Sqrt[2 π ] sy)Exp[-(yi - k x - b)^2/(2 sy^2)], {x, -∞, ∞}]] // AbsoluteTiming

(*{0.995044, E^(-((b + k xi - yi)^2/(2 (k^2 sx^2 + sy^2))))/
(Sqrt[2 π] Sqrt[k^2 sx^2 + sy^2])}*)

In most cases, the parameters in the integrals are positive or reals instead of complex, and it's effective to add assumptions for the parameters. So how to add assumptions for the parameters automatically? Just like a single code will automatically generate assumptions that all of the parameters are real or positive.

$\endgroup$
2
  • $\begingroup$ Use the GenerateConditions -> False option to this end Integrate[ 1/(Sqrt[2 \[Pi]] sx) Exp[-(x - xi)^2/(2 sx^2)] 1/(Sqrt[ 2 \[Pi]] sy) Exp[-(yi - k x - b)^2/(2 sy^2)], {x, -\[Infinity], \[Infinity]}, GenerateConditions -> False] // AbsoluteTiming and generate conditions by hand. $\endgroup$
    – user64494
    Jun 8, 2021 at 6:33
  • $\begingroup$ This would also be useful for Solve,Reduce, Simplify, etc. Two alternatives that will reduce your typing are to use Thread[{sx, sy, xi, yi, k, b} > 0] or {sx, sy, xi, yi, k, b} ∈ PositiveReals (the latter is available in MMA 12+ only) in place of sx > 0 && sy > 0 && xi > 0 && yi > 0 && k > 0 && b > 0, where you can get the symbol using ESC el ESC. Note that {sx, sy, xi, yi, k, b} > 0 will not work, since that's telling MMA that the list {sx, sy, xi, yi, k, b} is greater than zero (and I don't know what that means), rather than that each element is greater than zero. $\endgroup$
    – theorist
    Jun 9, 2021 at 1:13

1 Answer 1

2
$\begingroup$

Hmm… I doubt if this is a good idea, but anyway:

SetAttributes[addpositiveassumption, HoldAll];
addpositiveassumption[int : Integrate[expr_, domain : {_, _, _} .., OptionsPattern[]]]:=
  Assuming[Complement[Variables@Level[expr, {-1}], First /@ {domain}] > 0// Thread, int]

addpositiveassumption@
  Integrate[1/(Sqrt[2 π] sx) Exp[-(x - xi)^2/(2 sx^2)] 1/(Sqrt[
        2 π] sy) Exp[-(yi - k x - b)^2/(2 sy^2)], {x, -∞, ∞}] // AbsoluteTiming

Related:

Extracting variables from an expression

Is there an analogue of the Variables command for general expressions?

How to find all symbols in an expression and perform an operation on them?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.