Let's assume that a, b are complex variables, and A[t],B[u], C[t,u] are matrices.
In[1] TensorExpand[(a A[t].B[u] + b C[t, u]).C[t, u]]
Out[1] (b C[t, u]).C[t, u] + (a A[t].B[u]).C[t, u]
Now this is not quite what I want. Since a and b are complex, the parentheses are unnecessary. But wait, I can let Mathematica know, that they are not vectors or matrices:
In[1] $Assumptions = Element[{a, b}, Complexes]
TensorExpand[(a A[t].B[u] + b C[t, u]).C[t, u]]
Out[1] b C[t, u].C[t, u] + a A[t].B[u].C[t, u]
But I want to take this one step further, let's write a function that takes care of the job:
In[1] myExpand[expr_] := Module[{},
$Assumptions = Element[_Symbol, Complexes];
Return[TensorExpand[expr]];
]
Out[1] (b C[t, u]).C[t, u] + (a A[t].B[u]).C[t, u]
Well, that didn't work as I wanted. But I can still read all the symbolic variables:
In[1] symbolicQ[x_] := MatchQ[Head[x], Symbol];
myExpand[expr_] := Module[{},
$Assumptions = Element[DeleteDuplicates[Select[Level[expr, Infinity], symbolicQ]], Complexes];
Return[TensorExpand[expr]];
]
However, this feels clumsy. Is there a better way? What is the better way? If in doubt, choose the more performant way.
Answer
This works:
myExpand[expr_] := Assuming[
Cases[expr, _Symbol, All] ∈ Reals,
TensorExpand@expr
]
myExpand[(a A[t].B[u] + b C[t, u]).C[t, u]]
(* b C[t, u].C[t, u] + a A[t].B[u].C[t, u] *)
A few notes:
- Your
Module
is unnecessary - it doesn't do anything like this Return
is also unnecessary (even withModule
). See here- If you want to temporarily set
$Assumptions
, useAssuming
. In general (i.e. when there's no function that does it for you, useBlock
:Block[{$var=…},…]
- Use
Cases[expr,pat,level]
instead ofSelect[Level[expr,level],check]
- Use
MatchQ[expr,_head]
instead ofMatchQ[Head[expr],head]
. This also makes usingCases
more natural in this case
Comments
Post a Comment