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;








2












$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?










share|improve this question









$endgroup$











  • $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

















2












$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?










share|improve this question









$endgroup$











  • $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













2












2








2





$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?










share|improve this question









$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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 9 hours ago









amator2357amator2357

81210 bronze badges




81210 bronze badges











  • $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















$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










2 Answers
2






active

oldest

votes


















4












$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







share|improve this answer









$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


















4












$begingroup$

Bad answer:



Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];


Good answer:



adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat





share|improve this answer









$endgroup$












  • $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













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
);



);













draft saved

draft discarded


















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









4












$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







share|improve this answer









$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















4












$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







share|improve this answer









$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













4












4








4





$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







share|improve this answer









$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








share|improve this answer












share|improve this answer



share|improve this answer










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
















  • $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













4












$begingroup$

Bad answer:



Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];


Good answer:



adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat





share|improve this answer









$endgroup$












  • $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















4












$begingroup$

Bad answer:



Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];


Good answer:



adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat





share|improve this answer









$endgroup$












  • $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













4












4








4





$begingroup$

Bad answer:



Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];


Good answer:



adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat





share|improve this answer









$endgroup$



Bad answer:



Unprotect[AdjacencyMatrix];
AdjacencyMatrix[mat_List] := mat
Protect[AdjacencyMatrix];


Good answer:



adjacencyMatrix[graph_Graph] := AdjacencyMatrix[graph]
adjacencyMatrix[mat_List] := mat






share|improve this answer












share|improve this answer



share|improve this answer










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 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$
    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$
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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單