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?

  • $\begingroup$ Take a look at built-in and third party tensor capabilities: Tensor analysis. $\endgroup$
    – jkuczm
    Sep 19, 2016 at 13:54
  • $\begingroup$ I know they exist! But I am taking a class and we were encouraged to make our own. I am not supposed to use downloaded packages. $\endgroup$
    – Gowri
    Sep 20, 2016 at 4:20

1 Answer 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}] *)
  • $\begingroup$ Thanks! This is exactly what I had in mind. $\endgroup$
    – Gowri
    Sep 20, 2016 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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