0

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Armani42
  • 13
  • 3

3 Answers3

1

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?

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

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}}}
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
1

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)

Carl Lange
  • 13,065
  • 1
  • 36
  • 70