Returning the argument of a function if the argument is not of the right typeFunction argument to default under certain conditionDefine a function on a parameter argument set to zeroHow to make a function with its own options as well as passing options to other functionsCan I make a function which depends on several arguments which are not independent?Supply a function as an argument to another, and find its minimumTake the derivative of a function of time by another function of timeRemembering Previously Evaluated Function Values with Optional ArgumentImposing conditions on argument for a function definitionHow to make functional rules not depend on an argument?Saving remembered function when closing down notebook
Function pointer parameter without asterisk
Do we have to introduce the character's name before using their names in a dialogue tag?
Host telling me to cancel my booking in exchange for a discount?
What kind of curve (or model) should I fit to my percentage data?
What would be the effects of (relatively) widespread precognition on the stock market?
Killing a star safely
Why can't a country print its own money to spend it only abroad?
Has Iron Man made any suit for underwater combat?
Is there a smart way to pick up Page Design other than Template to Design mapping?
Question about differential signals as input of an operational amplifier
is FIND WORDS in P?
Are there foods that astronauts are explicitly never allowed to eat?
Group recursively adjacent tuples from a list in Python
How to pass array of values in lualatex?
Cargo capacity of a kayak
How did pilots avoid thunderstorms and related weather before “reliable” airborne weather radar was introduced on airliners?
What does a Nintendo Game Boy do when turned on without a game cartridge inserted?
How does mathematics work?
Strange LED behavior
What is the best word describing the nature of expiring in a short amount of time, connoting "losing public attention"?
Is it better to deliver many low-value stories or few high-value stories?
Book in which the "mountain" in the distance was a hole in the flat world
What else can you do in the turn you ready an action?
Calculating Fibonacci sequence in several different ways
Returning the argument of a function if the argument is not of the right type
Function argument to default under certain conditionDefine a function on a parameter argument set to zeroHow to make a function with its own options as well as passing options to other functionsCan I make a function which depends on several arguments which are not independent?Supply a function as an argument to another, and find its minimumTake the derivative of a function of time by another function of timeRemembering Previously Evaluated Function Values with Optional ArgumentImposing conditions on argument for a function definitionHow to make functional rules not depend on an argument?Saving remembered function when closing down notebook
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Best to give an example. Let us consider the function AdjacencyMatrix. When we pass, say a matrix to it, we get:
test = 1,1,1,1,2,2,3,3,3;
AdjacencyMatrix[%]
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,1,1,1,2,2,3,3,3].
AdjacencyMatrix[1, 1, 1, 1, 2, 2, 3, 3, 3]
Is there a way of modifying AdjacencyMatrix so that if the argument isn't a graph object it would simply return the argument itself?
functions attributes
$endgroup$
add a comment |
$begingroup$
Best to give an example. Let us consider the function AdjacencyMatrix. When we pass, say a matrix to it, we get:
test = 1,1,1,1,2,2,3,3,3;
AdjacencyMatrix[%]
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,1,1,1,2,2,3,3,3].
AdjacencyMatrix[1, 1, 1, 1, 2, 2, 3, 3, 3]
Is there a way of modifying AdjacencyMatrix so that if the argument isn't a graph object it would simply return the argument itself?
functions attributes
$endgroup$
$begingroup$
You wantAdjacencyMatrixto return the argument itself only when it is a matrix, or whenever it is not a graph?
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:50
add a comment |
$begingroup$
Best to give an example. Let us consider the function AdjacencyMatrix. When we pass, say a matrix to it, we get:
test = 1,1,1,1,2,2,3,3,3;
AdjacencyMatrix[%]
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,1,1,1,2,2,3,3,3].
AdjacencyMatrix[1, 1, 1, 1, 2, 2, 3, 3, 3]
Is there a way of modifying AdjacencyMatrix so that if the argument isn't a graph object it would simply return the argument itself?
functions attributes
$endgroup$
Best to give an example. Let us consider the function AdjacencyMatrix. When we pass, say a matrix to it, we get:
test = 1,1,1,1,2,2,3,3,3;
AdjacencyMatrix[%]
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,1,1,1,2,2,3,3,3].
AdjacencyMatrix[1, 1, 1, 1, 2, 2, 3, 3, 3]
Is there a way of modifying AdjacencyMatrix so that if the argument isn't a graph object it would simply return the argument itself?
functions attributes
functions attributes
asked Jul 12 at 16:12
amator2357amator2357
81210 bronze badges
81210 bronze badges
$begingroup$
You wantAdjacencyMatrixto return the argument itself only when it is a matrix, or whenever it is not a graph?
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:50
add a comment |
$begingroup$
You wantAdjacencyMatrixto return the argument itself only when it is a matrix, or whenever it is not a graph?
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:50
$begingroup$
You want
AdjacencyMatrix to return the argument itself only when it is a matrix, or whenever it is not a graph?$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:50
$begingroup$
You want
AdjacencyMatrix to return the argument itself only when it is a matrix, or whenever it is not a graph?$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:50
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
You can use Check in a user defined function:
adjacencyMatrix[g_] := Check[AdjacencyMatrix[g], g]
Then:
adjacencyMatrix[Graph[1->2,2->3,3->4]] //Normal
adjacencyMatrix[1->2, 2->3, 3->4] //Normal
adjacencyMatrix[1,2,3,4]
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,2,3,4].
1, 2, 3, 4
$endgroup$
1
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph].
$endgroup$
– AccidentalFourierTransform
Jul 13 at 1:09
add a comment |
$begingroup$
Bad answer:
Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];
Good answer:
adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat
$endgroup$
$begingroup$
For the good answer, maybe remove the pattern headListon the second line, as this is the behavior requested for anything that's not aGraph.
$endgroup$
– Roman
Jul 12 at 16:46
$begingroup$
@Roman good point. Let me ask OP to be sure. (If they only want the identity for matrices, then it is safer to leaveListin).
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:49
add a comment |
Your Answer
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%2f201998%2freturning-the-argument-of-a-function-if-the-argument-is-not-of-the-right-type%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$
You can use Check in a user defined function:
adjacencyMatrix[g_] := Check[AdjacencyMatrix[g], g]
Then:
adjacencyMatrix[Graph[1->2,2->3,3->4]] //Normal
adjacencyMatrix[1->2, 2->3, 3->4] //Normal
adjacencyMatrix[1,2,3,4]
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,2,3,4].
1, 2, 3, 4
$endgroup$
1
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph].
$endgroup$
– AccidentalFourierTransform
Jul 13 at 1:09
add a comment |
$begingroup$
You can use Check in a user defined function:
adjacencyMatrix[g_] := Check[AdjacencyMatrix[g], g]
Then:
adjacencyMatrix[Graph[1->2,2->3,3->4]] //Normal
adjacencyMatrix[1->2, 2->3, 3->4] //Normal
adjacencyMatrix[1,2,3,4]
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,2,3,4].
1, 2, 3, 4
$endgroup$
1
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph].
$endgroup$
– AccidentalFourierTransform
Jul 13 at 1:09
add a comment |
$begingroup$
You can use Check in a user defined function:
adjacencyMatrix[g_] := Check[AdjacencyMatrix[g], g]
Then:
adjacencyMatrix[Graph[1->2,2->3,3->4]] //Normal
adjacencyMatrix[1->2, 2->3, 3->4] //Normal
adjacencyMatrix[1,2,3,4]
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,2,3,4].
1, 2, 3, 4
$endgroup$
You can use Check in a user defined function:
adjacencyMatrix[g_] := Check[AdjacencyMatrix[g], g]
Then:
adjacencyMatrix[Graph[1->2,2->3,3->4]] //Normal
adjacencyMatrix[1->2, 2->3, 3->4] //Normal
adjacencyMatrix[1,2,3,4]
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0
AdjacencyMatrix::graph: A graph object is expected at position 1 in AdjacencyMatrix[1,2,3,4].
1, 2, 3, 4
answered Jul 12 at 17:02
Carl WollCarl Woll
87.8k3 gold badges117 silver badges226 bronze badges
87.8k3 gold badges117 silver badges226 bronze badges
1
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph].
$endgroup$
– AccidentalFourierTransform
Jul 13 at 1:09
add a comment |
1
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph].
$endgroup$
– AccidentalFourierTransform
Jul 13 at 1:09
1
1
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:
Quiet[Check[...], AdjacencyMatrix::graph].$endgroup$
– AccidentalFourierTransform
Jul 13 at 1:09
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:
Quiet[Check[...], AdjacencyMatrix::graph].$endgroup$
– AccidentalFourierTransform
Jul 13 at 1:09
add a comment |
$begingroup$
Bad answer:
Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];
Good answer:
adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat
$endgroup$
$begingroup$
For the good answer, maybe remove the pattern headListon the second line, as this is the behavior requested for anything that's not aGraph.
$endgroup$
– Roman
Jul 12 at 16:46
$begingroup$
@Roman good point. Let me ask OP to be sure. (If they only want the identity for matrices, then it is safer to leaveListin).
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:49
add a comment |
$begingroup$
Bad answer:
Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];
Good answer:
adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat
$endgroup$
$begingroup$
For the good answer, maybe remove the pattern headListon the second line, as this is the behavior requested for anything that's not aGraph.
$endgroup$
– Roman
Jul 12 at 16:46
$begingroup$
@Roman good point. Let me ask OP to be sure. (If they only want the identity for matrices, then it is safer to leaveListin).
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:49
add a comment |
$begingroup$
Bad answer:
Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];
Good answer:
adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat
$endgroup$
Bad answer:
Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];
Good answer:
adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat
answered Jul 12 at 16:41
AccidentalFourierTransformAccidentalFourierTransform
6,4421 gold badge12 silver badges45 bronze badges
6,4421 gold badge12 silver badges45 bronze badges
$begingroup$
For the good answer, maybe remove the pattern headListon the second line, as this is the behavior requested for anything that's not aGraph.
$endgroup$
– Roman
Jul 12 at 16:46
$begingroup$
@Roman good point. Let me ask OP to be sure. (If they only want the identity for matrices, then it is safer to leaveListin).
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:49
add a comment |
$begingroup$
For the good answer, maybe remove the pattern headListon the second line, as this is the behavior requested for anything that's not aGraph.
$endgroup$
– Roman
Jul 12 at 16:46
$begingroup$
@Roman good point. Let me ask OP to be sure. (If they only want the identity for matrices, then it is safer to leaveListin).
$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:49
$begingroup$
For the good answer, maybe remove the pattern head
List on the second line, as this is the behavior requested for anything that's not a Graph.$endgroup$
– Roman
Jul 12 at 16:46
$begingroup$
For the good answer, maybe remove the pattern head
List on the second line, as this is the behavior requested for anything that's not a Graph.$endgroup$
– Roman
Jul 12 at 16:46
$begingroup$
@Roman good point. Let me ask OP to be sure. (If they only want the identity for matrices, then it is safer to leave
List in).$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:49
$begingroup$
@Roman good point. Let me ask OP to be sure. (If they only want the identity for matrices, then it is safer to leave
List in).$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:49
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%2f201998%2freturning-the-argument-of-a-function-if-the-argument-is-not-of-the-right-type%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$
You want
AdjacencyMatrixto return the argument itself only when it is a matrix, or whenever it is not a graph?$endgroup$
– AccidentalFourierTransform
Jul 12 at 16:50