1
$\begingroup$

How can I create such lists of variables:

list1={sus1[t],sus2[t],sus3[t],...,sus99[t]};
list2={inf1[t],inf2[t],inf3[t],...,inf99[t]};

Consecutive Integers should be placed in between sus and [t] and between inf and [t] as in the above examples.

Earlier, @kglr did the following:

list = {das[t], das[t], das[t]};
Replace[list, {h_[a__] :> Symbol[SymbolName[h] <> "0"][a], 
 s_ :> Symbol[SymbolName[s] <> "0"]}, {1}]

Which replaces 0 only.

What I have in mind is a format like:

fn[sus_, n_, t_]:= something

and

fn[inf, 3, t] 

should generate

{inf1[t], inf2[t], inf3[t]}
$\endgroup$
10
  • 3
    $\begingroup$ Why are you doing this with symbols? Your life will be significantly better by just using 2D arrays $\endgroup$ – b3m2a1 Dec 6 '20 at 18:07
  • 1
    $\begingroup$ BTW, "tailor-made" generally means hand-altered, that is, type them out by hand; what you really want is "machine-made" or "factory-made." The answer to your problem must already exist on site: Table[ToExpression["das"<>ToString[k]][t], {k, 99}] -- You can use Symbol instead of ToExpression. $\endgroup$ – Michael E2 Dec 6 '20 at 18:12
  • 2
    $\begingroup$ @TugrulTemel just store your data in a list-of-list format or use indexed variables $\endgroup$ – b3m2a1 Dec 6 '20 at 18:27
  • 1
    $\begingroup$ I'd like to second @b3m2a1 's suggestion here. Don't bother with constructing variable names with strings. Instead use numerical indices. It's much more amenable to machine-manipulation down the line. $\endgroup$ – MarcoB Dec 6 '20 at 20:51
  • 1
    $\begingroup$ @Tugrul Yes I can see how that would be conceptually easier (indeed, I've done it myself in the past, and lived to regret it!). I think my suggestion is well exemplified by Bob Hanlon's answer below. Even more to the point may be to just keep working with lists of numbers if you only need numerical evaluation. Alternatively, I have been warming up to the use of Association or lists or Rules to store data together with their metadata (see e.g. mathematica.stackexchange.com/a/235579/27951). $\endgroup$ – MarcoB Dec 6 '20 at 21:33
3
$\begingroup$
Clear["Global`*"]

n = 3; (* Use any desired value *)

Use indexed variables

susV = Array[sus[#][t] &, n]

(* {sus[1][t], sus[2][t], sus[3][t]} *)

infV = Array[inf[#][t] &, n]

(* {inf[1][t], inf[2][t], inf[3][t]} *)

If you are going to assign values to the variables then the indexed variables will never show up in the output, just their values. Consequently, formatting would not be important. If however you want symbolic results (unlikely with vectors of length 99):

Format[sus[n_][t_]] := Subscript[sus, n][t];
Format[inf[n_][t_]] := Subscript[inf, n][t];

As much as possible, define functions with vector operations.

fn[t_] = susV.infV + infV // Simplify

enter image description here

$\endgroup$
2
$\begingroup$

Here's a way:

list = Table[ToExpression["das"<>ToString[k]][t], {k, 99}];

One can use Symbol instead of ToExpression.

The closest related Q&A I've found is Some challenge for a Table mixing between output number Variables and ToString symbols

One finds these solutions already presented in many questions:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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