# Detecting errors and providing alternative results [closed]

I have the following piece of code:

enr [xx_] = 4*(1/xx^12 - 1/xx^6);
list=RandomReal[{0,1},{5,2}]
distanc = DistanceMatrix[list, DistanceFunction->(#1-#2 &)] // MatrixForm
Apply[enr @ {##}&, distanc, {2}]// MatrixForm


But here is my problem now, that I obtain 1/0 errors.

How can I write an If statement or something similar to say him that he should not divide by 0? I want a conditional statement to act on each vector element of the matrix.

• Possible duplicate of Why does MatrixForm affect calculations? Commented Jun 21, 2019 at 15:50
• Nonit is not don't worry :) it's not about the matrix form it's about how to write an if statement that it acts on the element of a vector in a matrix. Commented Jun 21, 2019 at 16:17
• Armani42, because you are doing distanc = ... // MatrixForm, distanc is the MatrixForm output and not the DistanceMatrix itself (that is, the postfix // operator goes first, then the Set = operator). You should remove it from your sample code because it's a mistake many people make and it might confuse people. Commented Jun 27, 2019 at 14:51

You have a fundamental error in your use of MatrixForm: as things stand, the value of distanc is not a (nested) list to which you can Apply another function; rather, distanc is just a special output form.

 (distanc = DistanceMatrix[list, DistanceFunction -> (#1 - #2 &)]) // MatrixForm


or:

 distanc = DistanceMatrix[list, DistanceFunction -> (#1 - #2 &)];
distanc//MatrixForm


Now to your actual question: What do you want to happen when the potential division-by-0 occurs?

• Okay, thank you! So if the potential division by 0 occurs, it should not divide by zero. Instead it should just write "0" for that vector element. Commented Jun 21, 2019 at 14:15
• Change the definition of "enr" to do what you want? For example, If[xx==0,0,(* what you want *)] Commented Jun 21, 2019 at 18:50

Check, sometimes abetted by Quiet, is the built-in function provided to handle your kind of problem. Like so:

SeedRandom[42]; list = RandomReal[{0, 1}, {5, 2}];
dist = DistanceMatrix[list, DistanceFunction -> (#1 - #2 &)]

{{{0., 0.}, {0.0788359, 0.0627175}, {0.130058, 0.101854}, {0.129057,0.184616}, {0.100736, 0.582302}},
{{0.0788359, 0.0627175}, {0., 0.}, {0.208894, 0.164571}, {0.0502213, 0.247333}, {0.0218996, 0.519584}},
{{0.130058, 0.101854}, {0.208894, 0.164571}, {0., 0.}, {0.259115, 0.0827616}, {0.230794, 0.684155}},
{{0.129057, 0.184616}, {0.0502213, 0.247333}, {0.259115, 0.0827616}, {0., 0.}, {0.0283216, 0.766917}},
{{0.100736, 0.582302}, {0.0218996, 0.519584}, {0.230794, 0.684155}, {0.0283216, 0.766917}, {0., 0.}}}

enr[xx_] := Quiet @ Check[4 (1/xx^12 - 1/xx^6), {0, 0}, Infinity::indet]
Apply[enr @ {##} &, dist, {2}]

{{{0, 0}, {6.94016*10^13, 1.07993*10^15}, {1.7077*10^11, 3.20869*10^12}, {1.87357*10^11, 2.55167*10^9}, {3.66324*10^12, 2529.39}},
{{6.94016*10^13, 1.07993*10^15}, {0, 0}, {5.79312*10^8, 1.01344*10^10},{1.55384*10^16, 7.63091*10^7}, {3.28711*10^20, 10128.8}},
{{1.7077*10^11, 3.20869*10^12}, {5.79312*10^8, 1.01344*10^10}, {0, 0}, {4.36528*10^7, 3.87358*10^13}, {1.75109*10^8, 341.361}},
{{1.87357*10^11, 2.55167*10^9}, {1.55384*10^16, 7.63091*10^7}, {4.36528*10^7, 3.87358*10^13}, {0, 0}, {1.50189*10^19, 76.9634}},
{{3.66324*10^12, 2529.39}, {3.28711*10^20, 10128.8}, {1.75109*10^8, 341.361}, {1.50189*10^19, 76.9634}, {0, 0}}}


So if the potential division by 0 occurs, it should not divide by zero. Instead it should just write "0" for that vector element.

It seems you want something like the following:

enr[xx_]:= If[Total@xx == 0, {0, 0}, 4*(1/xx^12 - 1/xx^6)];
list = RandomReal[{0, 1}, {5, 2}];
distanc = DistanceMatrix[list, DistanceFunction -> (#1 - #2 &)];
Apply[enr@{##} &, distanc, {2}]


That is, If the Total of xx is 0, return {0, 0}, otherwise return the output of your original calculation.

(But I am answering your question to the letter - you asked for an If. However, m_goldberg's answer is more correct and idiomatic)