3

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
Michael
  • 123
  • 5
  • 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]] – kglr Jun 12 '18 at 09:01
  • See also this question: https://mathematica.stackexchange.com/questions/160700/efficient-way-to-get-elements-in-list – Sjoerd Smit Jun 12 '18 at 09:04
  • 1
    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). – Sjoerd Smit Jun 12 '18 at 09:45
  • Regrettably this does not work either because I am stuck with a hold that remains – Michael Jun 12 '18 at 09:48
  • That's why you have to use Unevaluated when extracting the elements from the held list. – Sjoerd Smit Jun 12 '18 at 10:20
  • There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please do your part. – rhermans Jun 19 '18 at 08:19

3 Answers3

3

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:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 1
    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. – Mr.Wizard Jun 12 '18 at 13:06
  • @Mr.Wizard Indeed, I was planning to, the first day since posting is for updates :) Thanks for edits. – Kuba Jun 12 '18 at 13:15
3

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]]
Sjoerd Smit
  • 23,370
  • 46
  • 75
0

Is this what you are looking for?

SetAttributes[ExtractUnevaluated, HoldFirst]
ExtractUnevaluated[l_List, n_] :=  ReleaseHold[Map[Hold, Hold@l, {2}]][[n]]
TimRias
  • 3,160
  • 13
  • 17