Message Boards Message Boards

0
|
515 Views
|
11 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How can I skip the "middle" optional arguments in a function call?

Posted 1 month ago

Say I have a function with one mandatory and two optional arguments,

f[x_, y_ : 1, z_ : 2] := x^2 + y*z

Then how can I make a function call which gives values to both x and z, but not to y, such that y takes its default value. In Python I could call f[2, z=1] for example. What is the equivalent in Mathematica?

POSTED BY: Gernot H
11 Replies

With all due respect the opts___Rule method is the wrong way to do options. There is a reason we have OptionsPattern and OptionValue.

f[x_, OptionsPattern[{"y" -> 1, "z" -> 2}]] :=  x^2 + OptionValue["y"]*OptionValue["z"]

In[128]:= f[2, z -> 1]

Out[128]= 5
POSTED BY: Jason Biggs

I learned the wrong way of doing options on Maeder's book, decades ago.

POSTED BY: Gianluca Gorni
Posted 1 month ago

Jason, what is wrong with the opt___Rule method? Isn't this just a matter of coding style?

POSTED BY: Hans Milton
Posted 1 month ago

Using OptionsPattern will provide checks automatically.

POSTED BY: Eric Rimbey
Posted 1 month ago

You could wrap f with another function:

g[x_, z_ : 2, y_ : 1] := f[x, y, z]
POSTED BY: Eric Rimbey
Posted 1 month ago

Yes, but that would not be practical for the cases I have in mind. It happens sometimes that I define functions with longer lists of optional arguments, and I find it cumbersome to always have to have to add all arguments up until the last one which I give a non-default value at every single function call. With the method you propose I would have a separate wrapper function for each optional argument, which with many arguments quickly gets cumbersome.

POSTED BY: Gernot H
Posted 1 month ago

Well, if you're the one defining the function, why not order the arguments in a more convenient way? Or, you could just define your function to take a list of rules (or maybe a full-blown association).

POSTED BY: Eric Rimbey

One way could be with the options mechanism:

f[x_, opts___Rule] := x^2 + y*z /. {opts} /. {y -> 1, z -> 2}
f[2, z -> 1]
POSTED BY: Gianluca Gorni
Posted 1 month ago

If I evaluate this, I get as output:

4 + 2 (z -> 1)

So it does not appear to work as intended. Is it a feature introduced recently to Mathematica? I did not download the newest version I think (am on version 12.1.1.0).

POSTED BY: Gernot H

Try with reverse order:

Clear[f];
f[x_, opts__Rule] := x^2 + y*z /. {opts} /. {y -> 1, z -> 2}
f[x_, y_ : 1, z_ : 2] := x^2 + y*z
f[2]
f[2, z -> 1]
f[2, y -> 2]
POSTED BY: Gianluca Gorni
Posted 1 month ago

Is it a feature introduced recently to Mathematica?

I have access to version 12.1.1. Tried Gianlucas first suggestion, and it works. This is what I get:

$Version
"12.1.1 for Microsoft Windows (64-bit) (June 19, 2020)"

Clear@f
f[x_, opts___Rule] := x^2 + y*z /. {opts} /. {y -> 1, z -> 2}
f[2,z->1]
5
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract