1

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.

infinitezero
  • 1,419
  • 8
  • 18

1 Answers1

2

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 with Module). See here
  • If you want to temporarily set $Assumptions, use Assuming. In general (i.e. when there's no function that does it for you, use Block: Block[{$var=…},…]
  • Use Cases[expr,pat,level] instead of Select[Level[expr,level],check]
  • Use MatchQ[expr,_head] instead of MatchQ[Head[expr],head]. This also makes using Cases more natural in this case
Lukas Lang
  • 33,963
  • 1
  • 51
  • 97
  • Thank you quite a lot, also for your additional remarks. They are really helpful in my journey of learning mathematica :) – infinitezero Jun 20 '18 at 14:09