Skip to main content

Questions tagged [optional-arguments]

Tag for questions about using optional arguments in Mathematica functions.

Filter by
Sorted by
Tagged with
3 votes
1 answer
60 views

Function with complicated argument constraint with default value

I need to define a function, say f with an argument which is integer, positive and with a default value say 2. I tried: ...
Giovanni Russo's user avatar
3 votes
2 answers
64 views

Passing options (both default and input) to sub-function

I have a function with Options, and I want to pass the Options to a subfunctions, including both Options with default values and Options with input values. For example, I want to pass the Options of <...
Li Xiang's user avatar
4 votes
2 answers
91 views

How can I specify a pattern for OptionsPattern

I cannot find the method how to apply some pattern specification to the values of options in a function. For example, it is necessary to define the power (exponentiation) with both positive base and ...
Konstantin's user avatar
0 votes
0 answers
29 views

Strange behaviour when adding Options to Protected symbols

I want to use the TensorProduct function to work for some of my custom expressions with head FusionRing and I want it to be able ...
Gert's user avatar
  • 1,620
1 vote
0 answers
34 views

Separate definition of custom options and passed options in function definition

Why do these codes do not work? ...
azerbajdzan's user avatar
0 votes
0 answers
44 views

How to pass on optional arguments, but with different default values?

Main question I want to define a function g that calls a function f. g has optional ...
Learn2Burn's user avatar
4 votes
1 answer
81 views

How to specify the type/Head of the optional named arguments to a function?

I am trying to improve my own dsolver API. Currently it takes optional arguments. Is it possible to specify that an optional argument should say be String only? Or List Only? Or Integer only? and so ...
Nasser's user avatar
  • 147k
4 votes
1 answer
98 views

OptionValue not inserting correct option

I am using OptionsPattern and OptionValue to define some switches for my custom function. Here is a minimal example: ...
shanedrum's user avatar
  • 587
1 vote
1 answer
89 views

Ensure that Options for built-in symbols are the original defaults

I often create symbols (no args) or functions (with args) in which common options (and styles) are set in body and unique opts within args for managing multiple calls to built-in functions. these spit ...
Jules Manson's user avatar
  • 2,467
3 votes
1 answer
93 views

Propper way of getting rid of FilterRules statements when passing options?

When passing options from a function f to a function g in the body of f one can use the <...
Gert's user avatar
  • 1,620
1 vote
1 answer
133 views

Condition (/;) not working correctly on a function with optional arguments and options

I have read this post: How to avoid collision between optional arguments and options, and use Mr.Wizard's method to solve this problem. But when I add conditions to optional arguments, it seems that ...
李子涵's user avatar
  • 368
2 votes
1 answer
78 views

Force only selected optional arguments

Is there a way to force only selected values for function argument? For example for this function: g[x_, y_, a_: ("b" || "c")] := Print[x, y, a] ...
Tigran's user avatar
  • 51
1 vote
0 answers
82 views

Skip middle optional arguments

Is there a way to skip some middle optional arguments? For example for this function: ...
Tigran's user avatar
  • 51
0 votes
0 answers
91 views

How to have Mathematica showing a dropdown list after entering an option and the -> in a function argument [duplicate]

When I enter Plot[Sin[x], {x, 0, 2 Pi}] Mathematica returns, as expected, the expected Sinus wave of one period. Plot has many, many options, when I enter: ...
nilo de roock's user avatar
8 votes
0 answers
74 views

Why does Longest not work on OptionsPattern?

When having a function with optional arguments and options, I frequently find myself writing something like this: ...
Lukas Lang's user avatar
  • 34.2k
2 votes
0 answers
71 views

How to produce an error message for each argument?

I want to print a helpful message for every wrong input. How do you compactly implement a function with custom pattern constraints, default values and error messages for each argument separately? For ...
Mauricio Fernández's user avatar
0 votes
1 answer
86 views

Why won't Sequence work when wrapping a list of arguments for deletion with Remove?

I wrote a quick and dirty little function freshStart which does a variety of things meant to give me a nearly fresh Kernel without having to ...
Jules Manson's user avatar
  • 2,467
5 votes
1 answer
101 views

Type safety and scoping in OptionsPattern arguments

I am new to OptionsPattern. Consider ...
lineage's user avatar
  • 1,164
1 vote
1 answer
79 views

How to manually create options that can be entered as a sequence or list?

I am trying to implement custom options in my code manually, rather than using the built-in Options tools. So I have functions that look like ...
WillG's user avatar
  • 980
2 votes
1 answer
112 views

Simplest way to check if optional parameter is omitted?

Suppose I have a function f[x_,y___]:= (code) and I want my code to check whether the parameter y is omitted. What's the easiest ...
WillG's user avatar
  • 980
1 vote
1 answer
44 views

How to create custom NIntegrate with special option behavior?

I'm trying to create a custom version of NIntegrate, called MyNIntegrate, with custom option behavior. In particular, I'd like ...
WillG's user avatar
  • 980
2 votes
0 answers
131 views

How to change behavior of Echo or turn it off without removing it from code?

I wish to use Echo for output of intermediate results if an OptionValue I call ...
Jules Manson's user avatar
  • 2,467
2 votes
2 answers
145 views

Help understanding options on ContourPlot3D

I am creating some figures showing the intersection between two surfaces. I managed to get most of what I want with the following code: ...
Charlie H.'s user avatar
0 votes
1 answer
83 views

passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
lineage's user avatar
  • 1,164
0 votes
1 answer
79 views

Keep irrelevant variables outside function arguments

I want to define several functions of variables x,y say ...
Weather Report's user avatar
1 vote
1 answer
139 views

Default Options not working in user-defined function for SymbolName

Abstract I have a function objectName[symbol] that returns the SymbolName as both a standalone function or when nested inside ...
Jules Manson's user avatar
  • 2,467
3 votes
2 answers
91 views

Setting nested optional argument with a default when unpacking from a given Head

I have a head that holds a single argument, and I want to unpack the value inside it while also assigning a symbol to the value itself, for example: ...
Joe Bentley's user avatar
1 vote
0 answers
68 views

String arguments break optional argument default values? [duplicate]

Following a tip in this post, I use string parameters to label my functions. For example, instead of f[x]:=x^2, I use ...
WillG's user avatar
  • 980
9 votes
1 answer
141 views

Why Optional in Set is not held by the HoldFirst attribute?

When evaluatingf[x_:x0] := x, x0 will be evaluated, regardless of the HoldAll attribute of <...
bcegkmqs23's user avatar
2 votes
1 answer
99 views

Issue using optional argument before options [duplicate]

I have a problem using optional arguments before an OptionsPattern: ...
M.R.'s user avatar
  • 31.6k
10 votes
2 answers
764 views

Define function that behaves almost identically to Mathematica function

Often I like to define my own functions that are almost exactly the same as Mathematica defined functions, apart from a few tweaks. See this question for example. I want to define them properly so ...
Tom's user avatar
  • 3,416
5 votes
1 answer
89 views

Why is { } interpreted as an option in my function? [closed]

I don't understand the output from: ...
MeMyselfI's user avatar
  • 1,116
0 votes
0 answers
60 views

Optional argument in Set Function [duplicate]

Optional arguments can be defined in SetDelayed (:=) Functions (see f1 below and here and here). However, how can one accomplish this in Set (=) Functions (see f2 and f3 below)? ...
LBogaardt's user avatar
  • 1,605
2 votes
1 answer
98 views

OptionValue and OptionPattern not Changing Values

I am trying to produce a function with a few sets of arguments, one of which can accept options. However it doesn't seem to be responding to the changes that I accept. ...
user avatar
0 votes
0 answers
56 views

Method option of function MatrixRank

By running ??MatrixRank, I see that it has an optional argument named Method. However, this argument is not in the ...
Liam Baker's user avatar
7 votes
0 answers
266 views

OptionsPattern[] Vs. opt___ [closed]

Since version 6 I've been dutifully using OptionsPattern[] instead of the original opts___ for passing options to functions. I ...
M. Decker's user avatar
  • 111
8 votes
2 answers
432 views

Are Mathematica commands always supposed to check that Method -> name is valid?

When calling a Mathematica command which accepts a Method -> name option, I always thought that if the name given is not a ...
Nasser's user avatar
  • 147k
1 vote
1 answer
2k views

Set absolute size of Axis or frame for a plot

I have to publish some work and it requires to meet specific criteria for the image size and plot size. 1) the Area of the figure must be fix 2) the width of the x-axis also must be fix for any plot ...
Jared Lo's user avatar
  • 121
2 votes
0 answers
54 views

Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
Sascha's user avatar
  • 8,499
4 votes
2 answers
115 views

How to prevent an Optional parameter from taking an OptionsPattern parameter without multiple function definitions? [duplicate]

I have function the needs to enable some Optional parameters and an OptionsPattern. The issue I am facing is that the options ...
Edmund's user avatar
  • 42.4k
3 votes
1 answer
58 views

Using other functions' replacement rules

I have a module that uses some default options for NDSolve like so ...
bjorne's user avatar
  • 569
1 vote
1 answer
48 views

Why doesn't OptionValue cooperate with Map? [duplicate]

Consider the following definition: Options[f] = {a -> 1, b -> 2} f[x_,OptionsPattern[]] := {x,OptionValue/@{a,b}} I thought that doing something like ...
user42582's user avatar
  • 4,225
11 votes
1 answer
154 views

Except @ OptionsPattern[] affects OptionValue

The background This example works as expected, small function with a special case rule: ...
Kuba's user avatar
  • 137k
-1 votes
3 answers
127 views

OptionsPattern Confusion

I want to use the whole options pattern as the argument for HTTPRequest's "Query" parameter, but I'm not sure how to use them. When I run the following code: ...
Joe Blow's user avatar
  • 417
5 votes
1 answer
165 views

What is the difference between the second argument of ToString and its option FormatType?

Is there any real difference between ToString[expr, form] and ToString[expr, FormatType→form]? In all my examples, they seem to ...
QuantumDot's user avatar
  • 19.7k
1 vote
0 answers
70 views

What is the best way to pass on options? [duplicate]

Say I have two functions which take the same set of options, e.g. Options[f1] = {a -> 1}; Options[f2] = {a -> 1}; Now, ...
JEM_Mosig's user avatar
  • 3,003
4 votes
2 answers
163 views

How to define an option or an argument whose values are confined

I want to define a function whose arguments or options can only be selected in a range. For example: ...
zchenkui's user avatar
5 votes
0 answers
59 views

Optional in MatchQ [duplicate]

Why isn't the first one True in: ...
Kuba's user avatar
  • 137k
1 vote
0 answers
107 views

Automatic is not always the default option

Contrary to what one might expect, specifying an option to be Automatic sometimes changes the output. For example, by default ...
tparker's user avatar
  • 1,856
10 votes
1 answer
2k views

define a function with optional arguments

I have defined a function as follows: ...
Dimitris's user avatar
  • 4,844