3
$\begingroup$

Consider I have a list l

l = {
  b + Integrate[Sin[t*q], {t, 0, 1.1}]^2 == b + Integrate[Cos[t*q], {t, 0, 1.1}]
, a + Integrate[Sin[t*p], {t, 0, 1.1}]^2 ==  b + Integrate[Cos[t*q], {t, 0, 1.1}]
}

Now I want another function to take just take one part of the expression without evaluating it. How do I do it? Basically I want something along the lines of

ExtractUnevaluated[l, 1]

With result

b + Integrate[Sin[t*q], {t, 0, 1.1}]^2 == b + Integrate[Cos[t*q], {t, 0, 1.1}]

How do I do this? Specificically how do I define ExtractUnevalued[l_List,n_]:= ????

To be a bit clearer I want to pass the part of the list to a function defined by:

SetAttributes[DiscretizeIntegralOnSet,HoldFirst]

DiscretizeIntegralOnSet[ Integrate[A_,{t_,tmin_,tmax_}], discretpointlist_
]:=some stuff(not relevant)

DiscretizeIntegralOnSet[ A_+B_, discretpointlist_
]:=some other stuff(not relevant)

That then allows me to symbolically write a discretized integral as sum over a set.

$\endgroup$
6
  • $\begingroup$ If l is not already evaluated you can use Inactivate as follows: l2 =Inactivate[{b + Integrate[Sin[t*q], {t, 0, 1.1}]^2 == b + Integrate[Cos[t*q], {t, 0, 1.1}], a + Integrate[Sin[t*p], {t, 0, 1.1}]^2 == b + Integrate[Cos[t*q], {t, 0, 1.1}]}, Integrate]; l2[[2]] $\endgroup$
    – kglr
    Jun 12, 2018 at 9:01
  • $\begingroup$ See also this question: mathematica.stackexchange.com/questions/160700/… $\endgroup$ Jun 12, 2018 at 9:04
  • 1
    $\begingroup$ If you're going to pass it into another function, Extract[list, pos, Unevaluated] should do the trick. However, you need to make sure the list doesn't evaluate when you define it (by using Hold instead of List, for example). $\endgroup$ Jun 12, 2018 at 9:45
  • $\begingroup$ Regrettably this does not work either because I am stuck with a hold that remains $\endgroup$
    – Michael
    Jun 12, 2018 at 9:48
  • $\begingroup$ That's why you have to use Unevaluated when extracting the elements from the held list. $\endgroup$ Jun 12, 2018 at 10:20

3 Answers 3

3
$\begingroup$

Here's a way to do it: you store the equations in Hold to prevent evaluation and then use Extract to get the elements out unevaluated:

l = Hold[
  b + Integrate[Sin[t*q], {t, 0, 1.1}]^2 == b + Integrate[Cos[t*q], {t, 0, 1.1}], 
  a + Integrate[Sin[t*p], {t, 0, 1.1}]^2 == b + Integrate[Cos[t*q], {t, 0, 1.1}]
]


ClearAll[DiscretizeIntegralOnSet]
DiscretizeIntegralOnSet[i : Integrate[A_, {t_, tmin_, tmax_}], discretpointlist_] := 
  Hold[i, A, t, tmin, tmax];


With[{int = Extract[l, {1, 1, 2, 1}, Unevaluated]}, 
 DiscretizeIntegralOnSet[int, points]]
$\endgroup$
3
$\begingroup$

Assuming DiscretizeIntegralOnSet is fixed and you don't want /it is not possible to add any syntactic sugar, you can do this:

l = Hold @ {..., ...};

l[[ {1}, n ]] /. Hold[x_]:> DiscretizeIntegralOnSet[x, whatever]

or

Function[
  x, DiscretizeIntegralOnSet[x, whatever], HoldFirst
] @@ l[[{1}, n]]

Regarding the first method you may reference:

$\endgroup$
2
  • 1
    $\begingroup$ I hope you don't mind my edit; I think it's needed to understand what's going on there if you area not already quite familiar with Mathematica. $\endgroup$
    – Mr.Wizard
    Jun 12, 2018 at 13:06
  • $\begingroup$ @Mr.Wizard Indeed, I was planning to, the first day since posting is for updates :) Thanks for edits. $\endgroup$
    – Kuba
    Jun 12, 2018 at 13:15
0
$\begingroup$

Is this what you are looking for?

SetAttributes[ExtractUnevaluated, HoldFirst]
ExtractUnevaluated[l_List, n_] :=  ReleaseHold[Map[Hold, Hold@l, {2}]][[n]]
$\endgroup$
1
  • $\begingroup$ The hold that remains makes it regrettably not what I wanted $\endgroup$
    – Michael
    Jun 12, 2018 at 8:58

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.