1
$\begingroup$

Consider the following three integrals:

Integral1[x_, y_] := 
 NIntegrate[Exp[-x^2. z + y*t], {z, 0, 9}, {t, -3, 4}]
Integral2[x_, y_] := 
 NIntegrate[Exp[-x^2. z + y*t]*Sin[x*z^4]*t, {z, 0, 9}, {t, -3, 4}]
Integral3[x_, y_] := 
 NIntegrate[Exp[-x^2. z + y*t]/Sqrt[
  x^2*z^2 + y^2*t^4], {z, 0, 9}, {t, -3, 4}]

I want to associate it with the labels "a", "b", and "c" correspondingly:

IntegralValues[x_, y_, label_] := 
 Association[{{x, y, "a"} -> Integral1[x, y], {x, y, "b"} -> 
     Integral2[x, y], {x, y, "c"} -> Integral3[x, y]}][{x, y, label}]

This dumb way leads to slowing down of the evaluation since the association calls three integrals simultaneously:

Integral1[1, 2] // AbsoluteTiming
IntegralValues[1, 2, "a"] // AbsoluteTiming

{0.0171722, 1490.29}

{0.421919, 1490.29}

I.e. the evaluation of the first integral (the fastest one) is accompanied by evaluating of the second and the third integrals which are much slower.

Could you please tell me what the analog of Association in this case is?

$\endgroup$

1 Answer 1

2
$\begingroup$

Add HoldForm to the integrals:

Integral1[x_, y_] := 
HoldForm[NIntegrate][Exp[-x^2. z + y*t], {z, 0, 9}, {t, -3, 4}]
Integral2[x_, y_] := 
HoldForm[NIntegrate][
Exp[-x^2. z + y*t]*Sin[x*z^4]*t, {z, 0, 9}, {t, -3, 4}]
Integral3[x_, y_] := 
HoldForm[NIntegrate][
Exp[-x^2. z + y*t]/Sqrt[x^2*z^2 + y^2*t^4], {z, 0, 9}, {t, -3, 4}]

We associate as follows:

IntegralValues[x_, y_, label_] := 
ReleaseHold@Lookup[Association[{"a" -> Integral1[x, y], "b" -> Integral2[x, y], 
"c" -> Integral3[x, y]}], label]

Test:

IntegralValues[1, 2, "a"] // AbsoluteTiming
$\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.