2

I am writing my own Mathematica code for simplifying tensor index manipulations with a metric. Here is what I wrote:

SetAttributes[metric, Orderless]
SetAttributes[metricInv, Orderless]
Format[Tensor[{a___}, {b___}]] := Subscript[Superscript["T", {a}], {b}]
metric/:Tensor[{x___, μ_, y___}, {a___}]· metric[μ_, ν_] := 
Tensor[{x, ν, y}, {a}]
metricInv/:Tensor[{a___}, {x___, μ_, y___}]· metricInv[μ_, ν_] := 
Tensor[{a}, {x, ν, y}]

Is there any way to say that other functions that I am defining, under other names, should follow the same rules for tensors as I have described above? In other words, if I have other functions that are NOT named Tensor, how can I say that they should behave as tensors, while keeping their different names intact?

LCarvalho
  • 9,233
  • 4
  • 40
  • 96
Gowri
  • 127
  • 8

1 Answers1

1

You can define a function that will add all desired properties to given symbol.

declareTensor[sym_Symbol, formatted_] := (
    Format[sym[{a___}, {b___}]] := Subscript[Superscript[formatted, {a}], {b}];
    metric /: sym[{x___, μ_, y___}, {a___}]·metric[μ_, ν_] :=
        sym[{x, ν, y}, {a}];
    metricInv /: sym[{a___}, {x___, μ_, y___}]·metricInv[μ_, ν_] :=
        sym[{a}, {x, ν, y}];
)

Now declare myTensor symbol as a "tensor":

declareTensor[myTensor, "myT"]

myTensor[{a, b}, {c, d}]·metric[b, e]
(* Subscript[Superscript["myT", {a, e}], {c, d}] *)
% // InputForm
(* myTensor[{a, e}, {c, d}] *)
LCarvalho
  • 9,233
  • 4
  • 40
  • 96
jkuczm
  • 15,078
  • 2
  • 53
  • 84