2
$\begingroup$

Consider the following simple example:

tlist1[a_: "a"] = Switch[a, "a", {1, 2, 3}, "b", {4, 5, 6}];
tfct1[a_: "a", x_] := #[[1]] + #[[2]]*x^#[[3]] &[tlist1[a]];
tfct2[a_: "a", x_] = D[tfct1[a, x], x];

What I want to do is to save a set of parameters as a list (tlist1) in order to generate multiple functions from the same pattern (in real, this will be Sellmeier coefficients for the Sellmeier formula). So tfct1 should give me the desired function when I specify a. E.g. tfct1["a",x]=1+2*x^3 or tfct1["b",x]=4+5*x^6.

Now, tfct2 uses derivatives (and second-order derivatives, but I skipped that for simplification), and I would like a result like tfct2["a",x]=6*x^2and tfct2["b",x]=30*x^5. But it should be usable as tfct2["a",2]=24 or tfct2["b",2]=960.

Because of the Switch statement I was using delayed evaluation := in the definition of tfct1 but this somehow screws up the derivation process.

So, how do I use Switch right like in this example when I would like to use derivatives? Or is there a better way to deal with such things? (still, I would like to use symbols like "a", "b", "c" for my coefficient sets)

$\endgroup$

1 Answer 1

2
$\begingroup$

I would implement this as follows:

tlist1 is essentially a lookup table, so let us use an association:

tlist1 = <|"a" -> {1, 2, 3}, "b" -> {4, 5, 6}|>;

(If you use Mathematica 9 or earlier, which does not have associations, then simply define tlist1["a"] = ...; tlist1["b"] = ..., etc.)

Let us use a slightly different notation for tfct1:  tfct1[a][x] instead of tfct1[a,x]. This way tfct1[a] is a function in its own right that can be operated on e.g. by Derivative.

tfct1[a_] := Function[x, #1 + #2 x^#3] & @@ tlist1[a]

Example usage:

tfct1["a"]
(* Function[x, 1 + 2 x^3] *)

tfct1["a"][z]
(* 1 + 2 z^3 *)

We can also take the derivative:

tfct1["a"]'
(* Function[x, 6 x^2] *)

tfct1["a"]'[x]
(* 6 x^2 *)

tfct1["a"]'[2]
(* 24 *)

If you still want to define tfct2, you can do so as

tfct2[a_] := tfct1[a]'
$\endgroup$
8
  • $\begingroup$ nice, thanks! As it is more comfortable for me, I'll define a tfct3[a_,x_]:=tfct2[a][x]; to be able to use it as intended. It is somehow bothersome that I have to define one function more (tfct3) and I don't completely get why this is necessary, but I have a rough idea. However, your solution works as well with my definition of tlist1 using Switch. I guess, Association is faster? $\endgroup$ Commented Feb 23, 2017 at 10:47
  • $\begingroup$ @riddleculous In this specific case, Association is probably not objectively better (speed is pretty much irrelevant here). I simply like it better, and find it more "Mathematica-like". In other contexts, it has clear advantages: it is a separate, modifiable, copyable data structure that is not tied to one symbol name. For example, what if you want to make this lookup table a parameter of your function? $\endgroup$
    – Szabolcs
    Commented Feb 23, 2017 at 10:55
  • $\begingroup$ makes sense! Insofar thanks for pointing me to that, might need it in the future."Mathematica-likeliness" is a funny parameter, though ;-) $\endgroup$ Commented Feb 23, 2017 at 10:59
  • $\begingroup$ One could also do: tfct1 = Function[x, #[[1]] + #[[2]] x^#[[3]]] & /@ tlist1 and tfct2 = #' & /@ tfct1; - and use it the same way as your functions. Any comment on which to prefer? I will normally work with only one or two of these association Keys but the functions get very complicated and costly. $\endgroup$ Commented Feb 23, 2017 at 14:08
  • $\begingroup$ @riddleculous No, that will not work. The resulting Functions must only contain formulas, not programming constructs such as Part. Otherwise Derivative will not work. $\endgroup$
    – Szabolcs
    Commented Feb 23, 2017 at 14:13

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.