It is important to understand that functions will attempt to operate on pattern objects (like ___
) as they will any other, and sometimes this confounds the intent you had for them (the pattern objects).
Consider for example:
Plus[_, _, _]
3 _
This evaluates to 3 _
(FullForm
Times[3, Blank[]]
), which is not a pattern expression that will match x + y + z
, because _
is not treated specially in evaluation, so it is just like Plus[x, x, x]
evaluating to 3 x
.
Now consider:
Abs[x]
Dot[x]
Plus[x]
Power[x]
Times[x]
Abs[x]
x
x
x
x
What to do about this will depend on why you are preparing these patterns.
If you want to use all patterns at once I would suggest Alternatives
:
pat = (Abs | Dot | Plus | Power | Times)[___]
(* unchanged by evaluation *)
Now e.g.
MatchQ[a^b, pat]
True
If you really need individual pattern expressions you will need to prevent evaluation from making undesired changes. The canonical method for that is HoldPattern
as proposed by MB1965 in a comment:
HoldPattern[#[___]] & /@ {Abs, Dot, Plus, Power, Times}
{HoldPattern[Abs[___]],
HoldPattern[Dot[___]],
HoldPattern[+___],
HoldPattern[Power[___]],
HoldPattern[Times[___]]}
Note: +___
is due to an output formatting rule and not evaluation itself, and the pattern will still match a + b
etc. See Returning an unevaluated expression with values substituted in for more on this.
Recommended reading:
Possible duplicate:
Through[{Abs, Defer[Dot], Defer[Plus], Defer[Power], Defer[Times]}[___]]
? $\endgroup$Abs
holds its argument if it's non-numeric. That's your issue here. TheDefer
kglr proposes will work. An alternative is to use(HoldPattern[#[__]]&)/@<funcs>
because you probably want that for pattern matching anyway. $\endgroup$Defer
is only for formatting within notebooks. Unless the output is copied and pasted back, the headDefer
will stay in the expression. $\endgroup$BlankNullSequence
.Plus[x]
will evaluate tox
for anyx
. It can't be kept asPlus[x]
unless it wrapped withHold
or similar. What are you actually trying to do? Perhaps you wantHoldPattern
. $\endgroup$ComplexExpand[Conjugate[ff[x]], {ff[x]}]
and I have to specify that the functions inside ff are complex; for example:ComplexExpand[Conjugate[Abs[x]+Dot[x, y]], {x, y,Dot[___],Abs[___],Plus[___]}]
. My idea is to search the functions (Symbol
) that are inside ff, append to them the [___] and put the list inComplexExpand
$\endgroup$