All Questions

Filter by
Sorted by
Tagged with
9
votes
4answers
114 views

How to add Optional Sequence

Note: This question could be a duplicate but I couldn't find any so far. It is known that Optional (:) can be use to represent expression if omitted. ...
31
votes
2answers
2k views

How to Combine Pattern Constraints and Default Values for Function Arguments?

EDIT: As several respondents have noted in the answers and comments below, the original example had a default value that would never be used because of the way patterns and default values are ...
2
votes
0answers
30 views

Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
64
votes
6answers
5k views

How to avoid collision between optional arguments and options

Is it possible to create a function with optional arguments that also takes options? Here is a simple example. I have a function f with option "g". It also has ...
4
votes
2answers
84 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 ...
1
vote
1answer
39 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 ...
9
votes
1answer
101 views

Except @ OptionsPattern[] affects OptionValue

The background This example works as expected, small function with a special case rule: ...
1
vote
0answers
47 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, ...
32
votes
3answers
2k views

Is there any way to define pure functions with optional arguments?

For example consider the following function: Function[{u,v},u^2+v^4] Is there anyway to define default values for the variables ...
7
votes
2answers
95 views

How to write a `HeldOptional` variant of `Optional` that does not evaluate its second argument?

How to specify optional arguments that take functional values made me wonder: Can we come up with a variant of Optional that allows to do the following: ...
3
votes
4answers
186 views

How to specify optional arguments that take functional values

I am trying to make a function with an optional argument that may change but mathematica caches the value when the function is defined. Here is an example where the optional time variable is set when ...
3
votes
2answers
62 views

How to define a function with an optional value related to the arguments?

It's known that we can easily define a function with optional value which is not related with other arguments, but my question is how can I define a function with an optional value which is really ...
12
votes
4answers
600 views

Optional argument that can be completely omitted?

I am writing a simple function return similar words. Clear[similarWords] similarWords[string_]:=Nearest[WordList[],string] I want to add another argument ...
2
votes
0answers
106 views

How to create a function with option that shows dropdownlist like ( for example ) Manipulate with ControlPlacement? [duplicate]

Consider the Mathematica function Manipulate where you assign the placement of the controls with the option parameter ...
12
votes
3answers
281 views

Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
0
votes
0answers
37 views

How to use OptionValue inside a pure function [duplicate]

I want to define a function with OptionsPattern as argument which returns a pure function using OptionValue: ...
1
vote
1answer
91 views
2
votes
2answers
130 views

Plot does not take range with OptionValue and PlotRange option

I am making a function that is to use Show to overlay a few plots of a function. I want the first Plot in ...
18
votes
4answers
552 views

Skipping “middle” Optional Arguments

Is there a way to call a function where the "middle" arguments should use their default value while specifying values for the "right-most" arguments? For example, given this function: ...
7
votes
2answers
393 views

Optional arguments with lists as function parameter

I'm developing a function f which should support optional arguments {x_, y_} in list form. A simple example looks like this: <...
2
votes
1answer
121 views

Is there a shorthand for Optional with Condition?

I have a recursive function that does some work to an object for a repeatedamount of times. ...
39
votes
5answers
2k views

Can I make a default for an optional argument the value of another argument?

I'd like to define a function with several optional arguments, some of which default to the value supplied for other arguments. For example, I'd like to be able to write something like ...
3
votes
0answers
92 views

Function of arguments given in non-fixed order [duplicate]

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 ...
7
votes
1answer
154 views

A question about two ways to use Default

Usually we define a function with default values by the syntax x_:default, but there is another technique to do this by registering a global default value with <...
1
vote
3answers
2k views

How to combine multiple functions into a single function, indexed bya new argument

I have a system of expressions (matrices) which correspond to increasing orders of precision in a theory. These expressions are quite complex (require about 40+ lines of code to produce) and are not ...
3
votes
1answer
171 views

How can I define a function taking two distinguishable sets of options?

It commonly happens that one wants to define some function foo which in turn calls some other function (e.g., Plot) that can ...
7
votes
2answers
183 views

Change some option of a function when calling it [duplicate]

Say I have defined some function like f[p_] := Plot[Sin[p*x], {x, 0, 2 Pi}] Now in most cases, I just need to specify the value of ...
2
votes
0answers
100 views

How can this function's options be used in different places? [duplicate]

I wrote this function to make a ListPlot where the points are colored by a third argument. ...