Message Boards Message Boards

[Code-Sharing] Defining symbolic Reals, Tensors, Vectors and Scalars

0 Zero

Don't be mislead and read the following misleading introduction. All caution is advised. Jump straight for 2.

1 Misleading Introduction

People have been asking themselves for eons things like:

Is my variable Real? - Aristoteles 335 BC

And perhaps

I'm not sure if that is a Tensor or a Vector, I wish there was a way to know it with a simple Mathematica code! - Plato 370 BC

Plato tried the following simple Mathematica code but failed miserably:

isreal[a_] = "Dunno"

One may have noticed that Plato doesn't follow standard Mathematica convention and all his commands are lowercase and barely readable (perhaps he is a Matlab user?), and also his code does not accomplish anything! It's a very famous ruse, be aware!

If you were haunted by such problems, fear not my friend, new mathematical insight from the early 500BC's has come to the rescue, with novel slick mathematical functions! And color!

2 Real Code

Defining the following functions:

(* Script functions without protection and/or meaning *)
ScriptFunctions =
  Subscript[_, __] | Superscript[_, __] | Subsuperscript[_, _, _] |Overscript[_, _] | Underscript[_, _] | Underoverscript[_, _, _] |
   SubPlus[_] | SubMinus[_] | SubStar[_] | SuperPlus[_] |SuperMinus[_] | SuperStar[_] | SuperDagger[_] |
   OverBar[_] | OverVector[_] | OverTilde[_] | OverHat[_] |OverDot[_] | UnderBar[_];

DefineReal[vars : Repeated[ScriptFunctions | _Symbol]] := (Function[{tag, var},
       tag /: Element[var, Reals] = True;
       tag /: Re[v : var] := v;
       tag /: Im@var = 0;
       tag /: Conjugate[v : var] := v;
       tag /: Abs[v : var] := RealAbs@Re@v;
       ][If[Head@# === Symbol, #, Head@#], #] & /@ {vars};)

DefineImaginary[
  vars : Repeated[ScriptFunctions | _Symbol]] := (Function[{tag, var},
       tag /: Element[var, Reals] = False;
       tag /: Re@var = 0;
       tag /: Im[v : var] := v/I;
       tag /: Conjugate[v : var] := -v;
       tag /: Abs[v : var] := RealAbs@Im@v;
       ][If[Head@# === Symbol, #, Head@#], #] & /@ {vars};)

Where now we can define things as (patterns are allowed):

DefineReal[a, Subscript[b, 1], Subscript[c, _]]

Now you can use it as:

Conjugate[a + Subscript[b, 1] + Subscript[c, car]]
output = a + Subscript[b, 1] + Subscript[c, car]

4 Tensor Code

The same can be done for tensors and vectors.

DefineSymbolicTensor[v : ScriptFunctions | _Symbol, rank_Integer: 2,  dim_Integer: 3] := (Function[{tag, var},
     tag /: ArrayQ@var = rank != 0;
     tag /: TensorQ@var = rank != 0;
     tag /: MatrixQ@var = rank == 2;
     tag /: VectorQ@var = rank == 1;
     tag /: ListQ@var = rank != 0;
     tag /: ScalarQ@var = rank == 0;         
     tag /: TensorRank@var = rank;
     tag /: TensorDimensions@var = ConstantArray[dim, {rank}];         
     tag /: Element[var, Arrays@TensorDimensions@var] = True;
     tag /: Element[var, Matrices@{dim, dim}] = rank == 2;
     tag /: Element[var, Vectors@dim] = rank == 1;
     ][If[Head@v === Symbol, v, Head@v], v]);    
DefineSymbolicTensor[v : {Repeated[ScriptFunctions | _Symbol]}, rank_Integer: 2, dim_Integer: 3] := (DefineSymbolicTensor[#, rank, dim] & /@ v;)

DefineSymbolicVector[v : (ScriptFunctions | _Symbol) | {Repeated[ScriptFunctions | _Symbol]}, dim_Integer: 3] := DefineSymbolicTensor[v, 1, dim]
DefineSymbolicScalar[v : Repeated[(ScriptFunctions | _Symbol)]] := DefineSymbolicTensor[{v}, 0, 1]

Where some definitions are pretty much overkill and/or unnecessary...

5 Usage

We are going to calculate the Maxwell Stress Tensor (electric part) for a superposition of two waves (electric field).

MST

TensorExpand can't do its job since it does not know what is a tensor, vector or scalar, hence, lots of things are not expanded "correctly" (sorry for using an image, but I like to use DoubleStruck...). Therefore we Define them!

MST 2.0

Now we can get a proper result! And many more symbolic algebraic manipulation of tensors is now possible.

PS: I'm currently building some (extremely) simple tensorial derivatives for these symbolic tensors defined in here. Hence, to take the gradient or divergent of tensor products, cross products etc, will be possible in a symbolic way. BTW, sorry for the lame jokes and puns, and the distraction of the main topic or the lack of proper explanations... My steam ended with the jokes... And if anybody have a slick way of Undefine the upvalues associated with that particular definition, it is needed!

POSTED BY: Thales Fernandes
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract