1

As the titles describes, I want to create a list of functions like the following, where the indices are not in the argument slots:

f1[x_]=a1*x;
f2[x_]=a2*x;
f3[x_]=a3*x;
... 
fn[x_]=an*x;

This is my unsuccessful attempt:

ToExpression["f"<>ToString[k]][x_]=ToExpression["a"<>ToString[k]]*x    

So how should the code be written?

User18
  • 882
  • 4
  • 12
  • 3
    But why do you want to do this? In my experience, it's always better to use either SubValues or just straight-up Downvalues. Anyway, something like f[k][x_] = a[k]*x. If you really feel the need, replace ToExpression with Symbol, but in general this type of thing is unnecessary and perhaps even detrimental. Just do my first suggestion. – march Feb 29 '16 at 04:32
  • 2
    As march says, this really isn't a good way to do things. Nevertheless: With[{idx = IntegerString[1]}, Set @@ Hold[Symbol["f" <> idx][x_], Symbol["a" <> idx] x]] (I'm not posting this as an answer since I don't want to.) – J. M.'s missing motivation Feb 29 '16 at 04:36
  • @march Thank you for the advice! Yes I think f[k][x_] = a[k]*x is better, at least far simpler. By the way, what are SubValues? I searched for the term in the Help documents but didn't find it. – User18 Feb 29 '16 at 05:09
  • @User18. See here. – march Feb 29 '16 at 05:12
  • @J. M. Thanks for the comment. I tried the code, but when I entered f6[x], for instance, the output is still f6[x], instead of a6*x. What's wrong? – User18 Feb 29 '16 at 05:20
  • @march That's a good resource. – User18 Feb 29 '16 at 05:22
  • Maybe you have uncleared definitions. Run ClearAll["Global`*"] and try my snippet again. – J. M.'s missing motivation Feb 29 '16 at 05:24
  • @J. This I what I tried, still without the desired result: ClearAll["Global`*"]; With[{idx = IntegerString[1]}, Set @@ Hold[Symbol["f" <> idx][x_], Symbol["a" <> idx] x]]; f6[x] – User18 Feb 29 '16 at 05:47
  • Sigh... replace IntegerString[1] with IntegerString[6]. I thought it was sufficiently apparent, but I guess not. – J. M.'s missing motivation Feb 29 '16 at 06:04
  • @J. M. OK, thanks. : ) – User18 Feb 29 '16 at 10:20
  • @LLlAMnYP Thank you! That page has exactly the things I'd like to know, including, in particular, the Notation Package. – User18 Feb 29 '16 at 10:38
  • That question linked to uses a title that contains too little information about what it is really about, to be readily found by searching. – User18 Feb 29 '16 at 10:43
  • I agree, that's why the system of marking as duplicate exists, so someone will find your question and follow the link to the duplicate. – LLlAMnYP Feb 29 '16 at 10:52
  • Marking a question as duplicate is not meant to patronize a user, but to link existing threads, precisely to mitigate bad titling, etc, so as to facilitate easier searching. – LLlAMnYP Feb 29 '16 at 10:56
  • I agree. My question is much easier to find. : ) – User18 Feb 29 '16 at 11:09

0 Answers0