# Mapping multiple functions to one general rule/pattern

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?

• Take a look at built-in and third party tensor capabilities: Tensor analysis. Sep 19, 2016 at 13:54
• 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. Sep 20, 2016 at 4:20

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}] *)

• Thanks! This is exactly what I had in mind. Sep 20, 2016 at 4:20