4
$\begingroup$

I would like to have a function to select terms containing q[i] terms in expressions, for example, for an expression

a q[i]+b +c q[j]+d

I would like the function to return

a q[i]+c q[j]

as result. Also at the same time, for a bare

q[i]

the same function would return

q[i]

How to realize such function? Thanks

$\endgroup$
3
$\begingroup$
expr = a q[i] + b + c q[j] + d;
f = DeleteCases[#, Except[_.  _q], If[Head[#] === q, 0, 1]] &
f @ expr

a q[i] + c q[j]

f @ q[i]

q[i]

Alternatively,

f2 = Select[#, Function[x, If[Head@# === q, True, Not @ FreeQ[x, _q]]]] &;
f2 @ expr

a q[i] + c q[j]

f2 @ q[i]

q[i]

$\endgroup$
  • $\begingroup$ This realize the first requirement, but for the bare $q[i]$, it returns $q[]$ $\endgroup$ – XiaoaiX Jan 11 at 7:24
  • $\begingroup$ @XiaoaiX, I will update if i find a fix. $\endgroup$ – kglr Jan 11 at 7:39
  • $\begingroup$ thanks, from your answer, I also got confused about how the Select and Cases handle the List argument, see the question I just post here mathematica.stackexchange.com/questions/189270/… $\endgroup$ – XiaoaiX Jan 11 at 7:46
3
$\begingroup$

A pattern-based approach.

fn[x_. y_q + z_.] := x y + fn[z]
_fn = 0;

a q[i] + b + c q[j] + d // fn

q[i] // fn
a q[i] + c q[j]

q[i]
$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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