0
$\begingroup$

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.

$\endgroup$

closed as off-topic by Henrik Schumacher, Daniel Lichtblau, Alex Trounev, Edmund, MarcoB Jul 1 at 16:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, Alex Trounev, Edmund
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Possible duplicate of Why does MatrixForm affect calculations? $\endgroup$ – Henrik Schumacher Jun 21 at 15:50
  • $\begingroup$ 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. $\endgroup$ – Armani42 Jun 21 at 16:17
  • $\begingroup$ 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. $\endgroup$ – Carl Lange Jun 27 at 14:51
1
$\begingroup$

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.

You want, instead,

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Armani42 Jun 21 at 14:15
  • $\begingroup$ Change the definition of "enr" to do what you want? For example, If[xx==0,0,(* what you want *)] $\endgroup$ – Mark R Jun 21 at 18:50
1
$\begingroup$

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}}}
$\endgroup$
1
$\begingroup$

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)

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.