2
$\begingroup$

I want to obtain the argument of a TensorProduct as a list of elements by applying another function.

For example, I wish to write a function h which, when applied to a tensor product, produces the arguments as follows

h[TensorProduct[a__]]:={a}

However, this doesn't work. The output is simply

TensorProduct[a]

Strangely it works for heads different from TensorProduct. For example

h[g[a__]]:={a}

works.

I can get the right output by doing for example

h[TensorProduct[a__]]:=List@@TensorProduct[a]

but why can I not just extract the argument in the previous way?

$\endgroup$
1
$\begingroup$

Give h the attribute HoldFirst to prevent TensorProduct from evaluating:

ClearAll[h]
SetAttributes[h, HoldFirst]
h[TensorProduct[a__]] := {a}
h[TensorProduct[a, b]]

{a, b}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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