3

normally we define functions as

 f[x_, y_, z_] := ArcSin[x^2*y/z]

however when one has many arguments (as I do) I find difficult to remember what is the meaning of the argument "number 2", that is y in the above example. Of course a ?f would do the job of refreshing my memory ... but I want to find another solution, possibly more direct.

I would like to achieve the definition of a function that evaluates independently of the order in which I give the arguments, still keeping their meaning. For this I thought to exploit the nice feature of Options patters that are non-positional.

  Options[disorderedf] = {x -> 0, y -> 0, z -> 0}
  disorderedf[opts : OptionsPattern[]] := 
    ArcSin[OptionValue[x]^2*OptionValue[y]/OptionValue[z]]

which beautifully evaluates the same independent of the order I give the values of the three coordinates

disorderedf[x -> 1, y -> 3, z -> Pi]
ArcSin[3/π]
disorderedf[y -> 3, z -> Pi, x -> 1]
ArcSin[3/π]

First of all I would like to ask if this is reinventing the wheel because Mathematica has already some type of functionality to support this way of giving arguments.

Secondly I wanted to ask if there are suggestions to speed up or simplify this or get the same behavior with simpler and more easily extendable code. I am imagining that I might in the future add more arguments and the way I did it might not be the most economical one.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Rho Phi
  • 1,420
  • 11
  • 21
  • 4
    For what it's worth, I think what you show is a perfectly reasonable way to obtain the desired behavior. – Daniel Lichtblau Oct 11 '14 at 22:32
  • I believe this question can be considered a duplicate of (55833). Your method is the same as the one I proposed there, so clearly I agree with Daniel that it is a reasonable way to go. I do show some variations that I think may interest you so please take a look at my answer. Then let me know if you feel that this question should not be closed. – Mr.Wizard Oct 12 '14 at 00:53
  • Hi Mr.Wizard, yes I think both our solutions and our questions are very similar. Thanks for pointing out your post. I am still looking into it to figure out the details and see how I can improve my current function. – Rho Phi Oct 12 '14 at 23:22

0 Answers0