# Function of an array with argument

Suppose c[i,t] is an array such as c[1,2]={1,2,3}

I want to define a function TOT[c[i,t]] which, for instance, computes Total[Array]. This does not work:

TOT[c[i_,t_]]:=Total[c[i,t]]

Any help?

• I don't understand the question. Is c[i_, t_] a function that returns a one-dimensional list? If so, then c[1, 2] // Total should give you what you want. – Shredderroy Jul 25 '19 at 21:48
• Writing the definition as TOT[c[i_, t_]] := Total[c[i, t]] is problematic because your expression likely does not retain the Head c when passed to TOT. Try something like TOT = c /* Total. Then TOT[1, 2] = Total[c[1, 2]], which seems to be what you want. – Shredderroy Jul 25 '19 at 21:52

## 2 Answers

You can use the HoldFirst attribute on your TOT function:

c[1, 2] = {1, 2, 3};

SetAttributes[TOT, HoldFirst];
TOT[c[i_, t_]] := Total[c[i, t]]

TOT[c[1, 2]]
(*    6    *)

• This is is exactly what I wanted. Thank you so much. – Ferhat Jul 26 '19 at 7:12
• It is generally a good practice to set Attributes before any definitions because attributes also affect how patterns on the lhs are treated. – Anton.Sakovich Jul 26 '19 at 12:45
• Thanks @Anton.Sakovich for the hint, I've updated the submission. – Roman Jul 26 '19 at 13:06

Why not simply:

c[1, 2] = {1, 2, 3};

TOT[lst_] := Total[lst]

TOT[c[1, 2]]
(* 6 *)
$$$$

• or even TOT=Total` – evanb Jul 26 '19 at 6:36