0
$\begingroup$

Consider the following code:

ilist = {0.1, 0.5, 3, 5}
{intTest[x, ilist[[1]]], intTest[x, ilist[[2]]], 
  intTest[x, ilist[[3]]], intTest[x, ilist[[4]]]} = 
 Interpolation[Table[{x, Exp[-x^#]/(x^# + 1)}, {x, 0., 1, 0.1}], 
     InterpolationOrder -> 1][x] & /@ ilist

It associates an interpolation intTest to the value of the particular element from ilist. In general, ilist may not be a list of numbers but just some string.

I would like to make this association rule independent of the number of elements of ilist, just to avoid something like the tedious construction

{intTest[x, ilist[[1]]], ..., intTest[x, ilist[[999]]]} = 

I tried to make the following:

Clear[tabdefs]
tabdefs = intTest[x_, #] & /@ ilist
tabdefs = 
 Interpolation[Table[{x, Exp[-x^#]/(x^# + 1)}, {x, 0., 1, 0.1}], 
     InterpolationOrder -> 1][x] & /@ ilist

However, the last string does not work: instead of defining what is each element of tabdefs (namely, intTest[x,ilist[[j]]]) it defines tabdefs as a whole quantity.

Another attempt is

Do[intTest[x_, ilist[[i]]] = 
  Interpolation[
    Table[{x, Exp[-x^ilist[[i]]]/(x^ilist[[i]] + 1)}, {x, 0., 1, 
      0.1}], InterpolationOrder -> 1][x], {i, 1, Length[ilist], 1}]

However, it looks a bit bulky, especially if the same routine association has to be made many times.

What is the correct syntax in this case?

$\endgroup$
3
  • $\begingroup$ First, the title (and the question) is a bit misleading because you refer to association but you are in fact doing down-values. Second, why don't you use simple Do with i iterating over ilist like Do[intTest[x, i] = Interpolation[Table[{x, Exp[-x^i]/(x^i + 1)}, {x, 0., 1, 0.1}], InterpolationOrder -> 1][x], {i, ilist}]? $\endgroup$
    – Domen
    Commented May 4, 2023 at 8:54
  • $\begingroup$ @Domen : Thanks, I will edit the question title. As for Do, earlier, I have edited the question by adding a similar solution. It is a bit bulky in my opinion. $\endgroup$ Commented May 4, 2023 at 8:58
  • 2
    $\begingroup$ Yes, yours is slightly bulky, mine not so much because I used only i iterated over ilist and then there is no need for Length or ilist[[i]]. I really don't see any true bulkiness anymore ... :D $\endgroup$
    – Domen
    Commented May 4, 2023 at 8:59

2 Answers 2

1
$\begingroup$

I think "MapThread" is your friend. It will evaluate a function of n arguments over n lists. In your case, the function will be an assignment and the first list is "ilist" and the second is a list of interpolating functions, call it "funs".:

ilist = {0.1, 0.5, 3, 5}
funs = Interpolation[Table[{x, Exp[-x^#]/(x^# + 1)}, {x, 0., 1, 0.1}],
       InterpolationOrder -> 1][x] & /@ ilist;

Then MapThread will do all the assignments:

MapThread[(#1 = #2) &, {Table[intTest[x, ilist[[i]]], {i, 4}], funs}];

We can test if this worked e.g. by:

intTest[x, 0.5]

enter image description here

$\endgroup$
1
$\begingroup$

For convenience lets define short-hand for the function generating the interpolations (my understand is that its form is besides the point anyway).

intfunc = Interpolation[Table[{x, Exp[-x^#]/(x^# + 1)}, {x, 0., 1, 0.1}], 
InterpolationOrder -> 1][x] &

We can then provide several compact ways of making the desired assignment that are agnostic to the length of ilist.

First:

Clear[intTest];
Evaluate[Table[intTest[x, i], {i, ilist}]] = intfunc /@ ilist

Second:

Clear[intTest];
Evaluate[intTest[x, #] & /@ ilist] = intfunc /@ ilist

Third:

(intTest[x, #] = intfunc[#]) & /@ ilist
$\endgroup$

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.