Define function that behaves almost identically to Mathematica functionErrorListPlot crops error barsHow to...
Where can I educate myself on D&D universe lore, specifically on vampires and supernatural monsters?
Manager has noticed coworker's excessive breaks. Should I warn him?
Identical projects by students at two different colleges: still plagiarism?
Are encryption algorithms with fixed-point free permutations inherently flawed?
Can "ee" appear in Latin?
Why is Shelob considered evil?
Why does finding small effects in large studies indicate publication bias?
Badly designed reimbursement form. What does that say about the company?
Can I legally make a website about boycotting a certain company?
What does “to the numbers” mean in landing clearance?
Rudeness by being polite
Will linear voltage regulator step up current?
Buying a "Used" Router
Does changing "sa" password require a SQL restart (in mixed mode)?
Short story where Earth is given a racist governor who likes species of a certain color
Is it ethical to apply for a job on someone's behalf?
Which was the first story to feature space elevators?
How can I differentiate duration vs starting time
Why would you use 2 alternate layout buttons instead of 1, when only one can be selected at once
Why is Bernie Sanders maximum accepted donation on actblue $5600?
Can you wish for more wishes from an Efreeti bound to service via an Efreeti Bottle?
Sing Baby Shark
Is Apex Sometimes Case Sensitive?
Integral check. Is partial fractions the only way?
Define function that behaves almost identically to Mathematica function
ErrorListPlot crops error barsHow to test if optional argument has been given?create function that behaves like continuedFractionHow can I define a function taking two distinguishable sets of options?How to define a Mathematica Function with varying number of input and output argumentsHow to combine multiple functions into a single function, indexed bya new argumentDefine function that counts recursive fibonacciShadowing of optional function arguments?Define a function with multiple lines mathematicaAn argument which returns the function that is applied to this argumentImposing conditions on argument for a function definition
$begingroup$
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 they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot that is almost identical to ListPlot except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal[], {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]

Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot. In other words, I would like to modify myListPlot so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
$endgroup$
add a comment |
$begingroup$
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 they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot that is almost identical to ListPlot except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal[], {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]

Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot. In other words, I would like to modify myListPlot so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
$endgroup$
$begingroup$
Try changingmyListPlot[data_, opts_]tomyListPlot[data_, opts___].
$endgroup$
– rafalc
21 hours ago
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago
add a comment |
$begingroup$
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 they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot that is almost identical to ListPlot except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal[], {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]

Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot. In other words, I would like to modify myListPlot so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
$endgroup$
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 they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot that is almost identical to ListPlot except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal[], {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]

Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot. In other words, I would like to modify myListPlot so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
functions optional-arguments arguments
asked 22 hours ago
TomTom
1,293919
1,293919
$begingroup$
Try changingmyListPlot[data_, opts_]tomyListPlot[data_, opts___].
$endgroup$
– rafalc
21 hours ago
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago
add a comment |
$begingroup$
Try changingmyListPlot[data_, opts_]tomyListPlot[data_, opts___].
$endgroup$
– rafalc
21 hours ago
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago
$begingroup$
Try changing
myListPlot[data_, opts_] to myListPlot[data_, opts___].$endgroup$
– rafalc
21 hours ago
$begingroup$
Try changing
myListPlot[data_, opts_] to myListPlot[data_, opts___].$endgroup$
– rafalc
21 hours ago
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.
myListPlot[data_, opts : OptionsPattern[]] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]

$endgroup$
$begingroup$
OptionsPattern[ListPlot]is more precise.
$endgroup$
– Edmund
13 hours ago
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_any single expression
x_any single expression, to be namedx
__any sequence of one or more expressions
x__sequence namedx
x__hsequence of expressions, all of whose heads areh
___any sequence of zero or more expressions
x___sequence of zero or more expressions namedx
x___hsequence of zero or more expressions, all of whose heads areh
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191884%2fdefine-function-that-behaves-almost-identically-to-mathematica-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.
myListPlot[data_, opts : OptionsPattern[]] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]

$endgroup$
$begingroup$
OptionsPattern[ListPlot]is more precise.
$endgroup$
– Edmund
13 hours ago
add a comment |
$begingroup$
If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.
myListPlot[data_, opts : OptionsPattern[]] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]

$endgroup$
$begingroup$
OptionsPattern[ListPlot]is more precise.
$endgroup$
– Edmund
13 hours ago
add a comment |
$begingroup$
If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.
myListPlot[data_, opts : OptionsPattern[]] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]

$endgroup$
If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.
myListPlot[data_, opts : OptionsPattern[]] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]

answered 21 hours ago
chuychuy
9,3131741
9,3131741
$begingroup$
OptionsPattern[ListPlot]is more precise.
$endgroup$
– Edmund
13 hours ago
add a comment |
$begingroup$
OptionsPattern[ListPlot]is more precise.
$endgroup$
– Edmund
13 hours ago
$begingroup$
OptionsPattern[ListPlot] is more precise.$endgroup$
– Edmund
13 hours ago
$begingroup$
OptionsPattern[ListPlot] is more precise.$endgroup$
– Edmund
13 hours ago
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_any single expression
x_any single expression, to be namedx
__any sequence of one or more expressions
x__sequence namedx
x__hsequence of expressions, all of whose heads areh
___any sequence of zero or more expressions
x___sequence of zero or more expressions namedx
x___hsequence of zero or more expressions, all of whose heads areh
$endgroup$
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_any single expression
x_any single expression, to be namedx
__any sequence of one or more expressions
x__sequence namedx
x__hsequence of expressions, all of whose heads areh
___any sequence of zero or more expressions
x___sequence of zero or more expressions namedx
x___hsequence of zero or more expressions, all of whose heads areh
$endgroup$
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_any single expression
x_any single expression, to be namedx
__any sequence of one or more expressions
x__sequence namedx
x__hsequence of expressions, all of whose heads areh
___any sequence of zero or more expressions
x___sequence of zero or more expressions namedx
x___hsequence of zero or more expressions, all of whose heads areh
$endgroup$
The usual way to define a Wolfram Language function that takes n arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_any single expression
x_any single expression, to be namedx
__any sequence of one or more expressions
x__sequence namedx
x__hsequence of expressions, all of whose heads areh
___any sequence of zero or more expressions
x___sequence of zero or more expressions namedx
x___hsequence of zero or more expressions, all of whose heads areh
answered 21 hours ago
rafalcrafalc
697212
697212
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191884%2fdefine-function-that-behaves-almost-identically-to-mathematica-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Try changing
myListPlot[data_, opts_]tomyListPlot[data_, opts___].$endgroup$
– rafalc
21 hours ago
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago