# Tag Info

2

Your syntax is not quite right, which is why your attempt did not work. Pattern objects should not be on the right-hand-side of the definition. Correcting that alone solves the problem: f1[x_, y_] = {{x, y}, {x y, x + y}}; f2[x_, y_] = {{x, y, 0}, {0, x + y, 0}, {1, x - y, x y}}; g[1, x_, y_] = f1[x, y]; g[2, x_, y_] = f2[x, y]; Test: g[2, a, b] ...

1

g[1, x_, y_] := f1[x, y] g[2, x_, y_] := f2[x, y] It works.

2

Another approach is to define your f's in a manner more conducive to easy manipulations down the road. For instance, f[1, x_, y_] := {{x, y}, {x y, x + y}}; f[2, x_, y_] := {{x, y, 0}, {0, x + y, 0}, {1, x - y, x y}}; g[i_, x_, y_] := f[i, x, y]; allows a simple definition of g. Of course, in this case, you don't even need the g at all.

9

I don't know why no one mentioned this, but all you have to do is to use a special form of OptionsPattern: pfunc[x0_, plotopts : OptionsPattern[{Plot, pfunc}]] := your-code where inside OptionsPattern go all sub-functions you need, in a list. Now everything is fine and dandy. There might be a downside of this, in case when several sub-functions can take ...

9

It gives those errors because you explicitly specified that pfunc only has "test" as an option. OptionValue is finicky and will complain if it sees options that it doesn't recognize. There are a couple of alternatives that I can think of: 1: Use FilterRules everywhere instead of OptionValue ClearAll@pfunc2 pfunc2[x0_, plotopts : OptionsPattern[]] := ...

5

I believe you need to add all of Plot's options to pfunc as well, like this: Options[pfunc] = Join[ Options[Plot], {"test" -> True, ...} ] I'd like to note that this is what builtins do as well. For example, Plot also carries all possible Graphics options. The downside is that any changes to the default options of Plot won't affect pfunc. The upside ...

0

I have found a workaround, but I don't think it's the most elegant solution available. Basically, I separate the Plot options from the pfunc options via curly brackets, like this: Options[pfunc] = {"test" -> True}; pfunc[{x0_, plotopts : OptionsPattern[]}, OptionsPattern[]] := Module[{}, Plot[x^2, {x, -x0, x0}, PlotStyle -> ...

5

OptionValue and OptionsPattern[] are magical constructs, which work by certain macro-like trick at run-time. So, I am not surprised that OptionValue did not work here. I would suggest to use it's long form: OptionValue[f, {opts}, optionName], and declare options as opts:OptionsPattern[], rather than just OptionsPattern[] - it will work then, and ...

Top 50 recent answers are included