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
Machine learning and operations research projects
diff shows a file that does not exist
Bronze Age Underwater Civilization
'rm' (delete) thousands of files selectively
What is this welding tool I found in my attic?
Robbers: The Hidden OEIS Substring
Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?
Are neural networks prone to catastrophic forgetting?
The monorail explodes before I can get on it
Is it rude to tell recruiters I would only change jobs for a better salary?
How can I deal with a player trying to insert real-world mythology into my homebrew setting?
Credit union holding car note, refuses to provide details of how payments have been applied
How to achieve this rough borders and stippled illustration look?
Why did my rum cake turn black?
When did the Roman Empire fall according to contemporaries?
What's the minimum number of sensors for a hobby GPS waypoint-following UAV?
Extract an attribute value from XML
Optimising Table wrapping over a Select
Correct use of ergeben?
What would be the ideal melee weapon made of "Phase Metal"?
<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?
Where is the USB2 OTG port on the RPi 4 Model B located?
Are there any intersection of Theory A and Theory B?
Can I use "candidate" as a verb?
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
7 hours ago
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 8 hours ago
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
7 hours ago
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
7 hours ago
$begingroup$
You want
AdjacencyMatrix to return the argument itself only when it is a matrix, or whenever it is not a graph?$endgroup$
– AccidentalFourierTransform
7 hours ago
$begingroup$
You want
AdjacencyMatrix to return the argument itself only when it is a matrix, or whenever it is not a graph?$endgroup$
– AccidentalFourierTransform
7 hours ago
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$
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
8 hours ago
$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
7 hours ago
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$
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$
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 7 hours ago
Carl WollCarl Woll
86.8k3 gold badges114 silver badges221 bronze badges
86.8k3 gold badges114 silver badges221 bronze badges
add a comment |
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
8 hours ago
$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
7 hours ago
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
8 hours ago
$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
7 hours ago
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 8 hours ago
AccidentalFourierTransformAccidentalFourierTransform
6,2971 gold badge12 silver badges45 bronze badges
6,2971 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
8 hours ago
$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
7 hours ago
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
8 hours ago
$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
7 hours ago
$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
8 hours ago
$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
8 hours ago
$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
7 hours ago
$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
7 hours ago
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
7 hours ago