Message Boards Message Boards

1
|
7270 Views
|
6 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How do I use Options, OptionsPattern, and OptionValue in this function?

Hello community.

I'm having trouble learning how to use these commands (Options, OptionsPattern and OptionValue) by reading and trying through the documentation.

The following function (just a simple example) I can elaborate with optional arguments in this way below:

f1[a_] := f1[a, "ini2", 2, 10];
f1[a_, b_] := f1[a, b, 2, 10];
f1[a_, b_, c_] := f1[a, b, c, 10];
f1[a_, b_, c_, d_] := 
 Module[{esca, num, tik, 
   pattern = <|"pat1" -> {0, 1, 1, 1, 1}, "pat2" -> {0, 2, 2, 2, 2}, 
     "pat3" -> {0, 3, 3, 3, 3}|>}, num = pattern@a; 
  esca = Flatten@
    Map[Map[(b /. {"ini1" -> -10, "ini2" -> -5, "ini3" -> 0, 
             "ini4" -> 5}) + # &, Accumulate@num] + # &, {0, 10, 
       20}[[;; c]]]; tik = esca + d]

For example:

f1["pat1", "ini3", 3]
f1["pat1"]

ex1

This way above, the function works, but is built using optional arguments. I need it to be in another way, with Options command...

I have to do in a way that the arguments are function Options and not as optional arguments. To do this, the arguments ( "b ", "c " and "d _" ) of the function should be called "option1", “option2" and "option3". The default values for the function should be: "option1"->"ini2", "option2"->2 and "option3"->10.

Examples of Input that I need the function to work:

f1["pat1"]
f1["pat1", "option1" -> "ini3", "option3" -> 15]
f1["pat1", "option2" -> 1]
f1["pat1", "option3" -> 5]
f1["pat1", "option2" -> 3, "option3" -> 20]
f1["pat1", "option1" -> "ini1", "option2" -> 1, "option3" -> 15]

Giving the following results from the above example:

ex2

Anyone in the community who knows how to use Options, OptionsPattern, and OptionValue can help me, please??

I tried several times but it has been very difficult for me to understand...for now.

Thank you very much in advance.

POSTED BY: Claudio Chaib
6 Replies
Posted 5 years ago

A simple example of Options, without the need of separately defining Options[f]. Dont know if its of any help, but anyway:

f[a_, b_, OptionsPattern[op -> "Add"]] :=
    Switch[OptionValue[op],
     "Add", a + b,
     "Multiply", a b,
     "Power", a^b,
     _, "Unknown option value"
    ]

In[2]:= f[2, 3]
Out[2]= 5

In[3]:= f[2, 3, op -> "Add"]
Out[3]= 5

In[4]:= f[2, 3, op -> "Multiply"]
Out[4]= 6

In[5]:= f[2, 3, op -> "Power"]
Out[5]= 8

In[6]:= f[2, 3, op -> q]
Out[6]= "Unknown option value"
POSTED BY: Hans Milton

That's an interesting way, Hans! Thank you very much! It seems to be very useful... I didn't know this way of doing the function with Switch. I'm going to run some tests to learn how to use this! Yes, it was a lot of help. Thank you!

POSTED BY: Claudio Chaib

This is an impressive and innovative use of switch! I thought I knew what I was doing today, but it turns out I will be modifying all of my programs to function with this method!!! I had been told previously about the ability to use Switch for this purpose, but this example is perfectly suited to explain how this works for this purpose. Many many thanks to you, Hans!

POSTED BY: CA Trevillian

I think I figured out how to make use of these commands... I don't know why I wasn't getting it before, now that I've managed to solve it seems simpler than I previously believed.

f2[a_, OptionsPattern[{"option1" -> "ini2", "option2" -> 2, 
    "option3" -> 10}]] := 
 Module[{esca, num, tik, 
   pattern = <|"pat1" -> {0, 1, 1, 1, 1}, "pat2" -> {0, 2, 2, 2, 2}, 
     "pat3" -> {0, 3, 3, 3, 3}|>}, num = pattern@a; 
  esca = Flatten@
    Map[Map[(OptionValue["option1"] /. {"ini1" -> -10, "ini2" -> -5, 
             "ini3" -> 0, "ini4" -> 5}) + # &, 
        Accumulate@num] + # &, {0, 10, 20}[[;; 
       OptionValue["option2"]]]]; tik = esca + OptionValue["option3"]]

ex3

I don't know if this is the right way to use these commands, but apparently for the time being it worked... If I'm doing something wrong or if anyone knows a better way to use them, please can you give me a feedback..?!

Thanks again.

POSTED BY: Claudio Chaib

One thing to note is that you should also have something like

Options[f2] := {"option1" -> "somedefault", "option2" -> blah,  "option3" -> xxx};

This is useful for enabling those options and setting default values.

I confess I am not terribly proficient in the ways of options. I have used it only a few times for System` and WFR functions. Usually I just copy what I find in existing code.

POSTED BY: Daniel Lichtblau

From what I understood from the documentation I think it can both be done: with OptionsPattern[] and Options[f2]={default}, or without explicitly setting up Options by putting in the OptionsPattern directly the defaults for the function: OptionsPattern[{default}]... I don't know if it's a "normal" alternative, I think it works both ways! But thank you very much for the answer Daniel, I will do more tests!

With Options + empty OptionsPattern[]:

f2[a_, OptionsPattern[]] := 
 Module[{esca, num, tik, 
   pattern = <|"pat1" -> {0, 1, 1, 1, 1}, "pat2" -> {0, 2, 2, 2, 2}, 
     "pat3" -> {0, 3, 3, 3, 3}|>}, num = pattern@a; 
  Options[f2] = {"option1" -> "ini2", "option2" -> 2, 
    "option3" -> 10}; 
  esca = Flatten@
    Map[Map[(OptionValue["option1"] /. {"ini1" -> -10, "ini2" -> -5, 
             "ini3" -> 0, "ini4" -> 5}) + # &, 
        Accumulate@num] + # &, {0, 10, 20}[[;; 
       OptionValue["option2"]]]]; tik = esca + OptionValue["option3"]]

Without Options + full OptionsPattern:

f2[a_, OptionsPattern[{"option1" -> "ini2", "option2" -> 2, 
    "option3" -> 10}]] := 
 Module[{esca, num, tik, 
   pattern = <|"pat1" -> {0, 1, 1, 1, 1}, "pat2" -> {0, 2, 2, 2, 2}, 
     "pat3" -> {0, 3, 3, 3, 3}|>}, num = pattern@a; 
  esca = Flatten@
    Map[Map[(OptionValue["option1"] /. {"ini1" -> -10, "ini2" -> -5, 
             "ini3" -> 0, "ini4" -> 5}) + # &, 
        Accumulate@num] + # &, {0, 10, 20}[[;; 
       OptionValue["option2"]]]]; tik = esca + OptionValue["option3"]]

Thanks.

POSTED BY: Claudio Chaib
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