0
$\begingroup$

I want to create a matrix of functions. As explained in this question, I can create a matrix of variables:

labels = {"A", "B", "C"};
M = Table[Symbol["f" <> i <> j], {i, labels}, {j, labels}];
M // MatrixForm

enter image description here

But I want to create a matrix with functions fAA[t] etc. The motivation is that I am generating a matrix of differential equations that I will solve with DSolve. I tried the following simple modification but it does not work

M = Table[Symbol["f" <> i <> j <> "[t]"], {i, labels}, {j, labels}];
$\endgroup$
2
  • 2
    $\begingroup$ Table[Symbol["f" <> i <> j][t], {i, labels}, {j, labels}] ? $\endgroup$
    – andre314
    Commented Aug 13, 2019 at 13:33
  • $\begingroup$ Oh yeah. I think that does the job. Will post if there are any issues. Thanks! $\endgroup$
    – Tom
    Commented Aug 13, 2019 at 13:35

1 Answer 1

3
$\begingroup$

Recommend that you consider using an indexed variable.

labels = {"A", "B", "C"};

If f[x, y] is distinct from f[y, x]

ClearAll[f]

Format[f[x_, y_]] := Subscript[f, ToString[x] <> ToString[y]]

(m = Outer[f[##][t] &, labels, labels]) // MatrixForm

enter image description here

If f[x, y] is equal to f[y, x]

ClearAll[f]

Format[f[x_, y_]] := Subscript[f, ToString[x] <> ToString[y]]

(m = Outer[(f @@ Sort[{##}])[t] &, labels, labels]) // MatrixForm

enter image description here

$\endgroup$
2
  • $\begingroup$ Why do you recommend indexed versus not indexed? For some reason I thought we recommended against indexes with subscripts? Regardless, lovely code!! $\endgroup$ Commented Aug 21, 2019 at 3:59
  • $\begingroup$ @CATrevillian - subscripts/superscripts can case problems with some functions/commands. Rather than try to remember which ones, I always use indexed variables and format as desired. $\endgroup$
    – Bob Hanlon
    Commented Aug 21, 2019 at 19:08

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.