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
Wrapper in return method for test class
How do I determine whether a permit is required for a new gas line?
Was the Ford Model T black because of the speed black paint dries?
Correct use of ergeben?
If a specific mass of air is polluted, will the pollution stick with it?
Was adding milk to tea started to reduce employee tea break time?
Where is the USB2 OTG port on the RPi 4 Model B located?
Do you make saves against the Web spell as you move through it?
<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?
Are there any double stars that I can actually see orbit each other?
What can the U.S. government do to prevent powerful people to get extremely favorable plea bargain deals like Jeff Epstein?
How were hypergolic propellants delivered to the Shuttle launch pad?
Cubic programming and beyond?
What's the fastest way to get Hard To Borrow (HTB) stocks?
Why did my rum cake turn black?
Optimising Table wrapping over a Select
Who Can Help Retag This?
I have a ruthless DM and I'm considering leaving the party. What are my options to minimize the negative impact to the rest of the group?
Filtering fine silt/mud from water (not necessarily bacteria etc.)
Why isn't there research to build a standard lunar, or Martian mobility platform?
What is this welding tool I found in my attic?
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?
Does Google Maps take into account hills/inclines for route times?
Credit union holding car note, refuses to provide details of how payments have been applied
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 wantAdjacencyMatrix
to return the argument itself only when it is a matrix, or whenever it is not a graph?
$endgroup$
– AccidentalFourierTransform
8 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 9 hours ago
amator2357amator2357
81210 bronze badges
81210 bronze badges
$begingroup$
You wantAdjacencyMatrix
to return the argument itself only when it is a matrix, or whenever it is not a graph?
$endgroup$
– AccidentalFourierTransform
8 hours ago
add a comment |
$begingroup$
You wantAdjacencyMatrix
to return the argument itself only when it is a matrix, or whenever it is not a graph?
$endgroup$
– AccidentalFourierTransform
8 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
8 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
8 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$
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph]
.
$endgroup$
– AccidentalFourierTransform
33 mins 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 headList
on 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 leaveList
in).
$endgroup$
– AccidentalFourierTransform
8 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$
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph]
.
$endgroup$
– AccidentalFourierTransform
33 mins ago
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$
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph]
.
$endgroup$
– AccidentalFourierTransform
33 mins ago
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 8 hours ago
Carl WollCarl Woll
86.8k3 gold badges114 silver badges221 bronze badges
86.8k3 gold badges114 silver badges221 bronze badges
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph]
.
$endgroup$
– AccidentalFourierTransform
33 mins ago
add a comment |
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:Quiet[Check[...], AdjacencyMatrix::graph]
.
$endgroup$
– AccidentalFourierTransform
33 mins ago
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:
Quiet[Check[...], AdjacencyMatrix::graph]
.$endgroup$
– AccidentalFourierTransform
33 mins ago
$begingroup$
This is much better than my approach. Note: OP may want to suppress the error message here:
Quiet[Check[...], AdjacencyMatrix::graph]
.$endgroup$
– AccidentalFourierTransform
33 mins 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 headList
on 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 leaveList
in).
$endgroup$
– AccidentalFourierTransform
8 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 headList
on 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 leaveList
in).
$endgroup$
– AccidentalFourierTransform
8 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 9 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 headList
on 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 leaveList
in).
$endgroup$
– AccidentalFourierTransform
8 hours ago
add a comment |
$begingroup$
For the good answer, maybe remove the pattern headList
on 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 leaveList
in).
$endgroup$
– AccidentalFourierTransform
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$
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
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
8 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
AdjacencyMatrix
to return the argument itself only when it is a matrix, or whenever it is not a graph?$endgroup$
– AccidentalFourierTransform
8 hours ago