I have a simple calculation which I am trying to use if condition to evaluate summation of some values, here's a code;

ClearAll[lst, n, k, i, t, L, s]; ClearAll[f, G, OS, SI, p, delta];
<< Combinatorica`;
Block[{$ContextPath}, Needs["Combinatorica`"]];
L = 10;
lst = Range@L;
f[i_, j_] := -1/(L*Sin[Pi* (0.5 - (i - j))/L]);
G = SetPrecision[Array[f, {L, L}], 100];
OS = SetPrecision[(G + IdentityMatrix[L]).Inverse[
     G - IdentityMatrix[L]], 100];
s = SetPrecision[Det[(IdentityMatrix[L] - G)/2], 100];
  config = {NthSubset[i, lst]};
  Prob1 = SetPrecision[s*Det[-OS[[#, #]]] & /@ config, 100]//Chop;
  , {i, 1, 2^L - 1}];

The Prob1 values are too small and sometimes Mathematica gives even negative values which I suppose are numerical issues. I tried to use Chop to covert the small values to be zero. The problem is that, the if condition return only zero value for Sh1; I am thinking that the condition term might not set correctly but tried different things such as considering delta=N[10^-100,100] and changing the condition to if Prob1>delta, but still it did not fix the problem. I am trying to find a way to not include terms that are very small(almost zero) in Sh1, as the Log function will not be valid.

  • $\begingroup$ I can not understand the question. It is better to explain the problem more clearly. $\endgroup$
    – Kattern
    Jul 6, 2015 at 5:01
  • 1
    $\begingroup$ It seems to me that Chop is actually not doing anything in your code: in fact, it is applied to the value returned by SetPrecision, but then the chopped value returned by Chop is not assigned to anything and discarded. More generally, it is hard to suggest alternatives, since we don't know what $A$ looks like. Show us an example of a matrix A that is giving you this trouble. $\endgroup$
    – MarcoB
    Jul 6, 2015 at 5:35
  • $\begingroup$ A better approach would probably be to increase the precision of $A$ before calculating its determinant: SetPrecision[A, 100]; Det[A]. $\endgroup$
    – MarcoB
    Jul 6, 2015 at 5:40
  • $\begingroup$ Hi all, thanks for the comments, I have included the entire code, hope that clarifies what's the problem... $\endgroup$
    – setareh
    Jul 6, 2015 at 15:51
  • $\begingroup$ @MarcoB: well actually Chop makes the small values to be zero, so if you print the Prob1 values, it return zero, but if you run the code for L=2, one should get 3 different value for Prob1, which two of them are small and using Chop becomes zero, while the last one is nonzero, but the if statement is not picking it up correctly to evaluate Sh1.... $\endgroup$
    – setareh
    Jul 6, 2015 at 15:56

1 Answer 1


I see at least two problems here:

  1. As written Prob1 is a List; you need to add // First (or similar) to extract a scalar value.

  2. You failed to define a starting value for Sh1 therefore Sh1 = 0 + Sh1 leads to an infinite recursion when the first scalar value is Equal to zero.

Additionally some more improvements to be made:

  1. You should avoid starting user Symbol names with capital letters as these can conflict with built-in Symbols, now or in the future.

  2. If you use the Block[{$ContextPath}, Needs["Combinatorica`"]]; method to load a package, which avoids name collisions with built-ins, you must use the fully qualified Symbol name i.e. Combinatorica`NthSubset

  3. You do not need to Clear (or ClearAll) Symbols that are automatically scoped, such as the iterators in Table or Do, or the named patterns in SetDelayed definition.

Please try this corrected code:

ClearAll[lst, L, s, f, G, OS, Sh1];
Block[{$ContextPath}, Needs["Combinatorica`"]];
L = 10;
lst = Range@L;
Sh1 = 0;
f[i_, j_] := -1/(L*Sin[Pi*(0.5 - (i - j))/L]);
G = SetPrecision[Array[f, {L, L}], 100];
OS = SetPrecision[(G + IdentityMatrix[L]).Inverse[G - IdentityMatrix[L]], 100];
s = SetPrecision[Det[(IdentityMatrix[L] - G)/2], 100];
Sh1 = 0;
Table[config = {Combinatorica`NthSubset[i, lst]};
 Prob1 = SetPrecision[s*Det[-OS[[#, #]]] & /@ config, 100] // Chop // First;
 If[Prob1 > 0, Sh1 = -Prob1*Log[Prob1] + Sh1, Sh1 = 0 + Sh1], {i, 1, 15}]
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
879820719374, \
221643260420, \
092279558416, \
827660495069, \

(I used a Table range of one through 15 simply to limit the size of output for this post.)

  • $\begingroup$ Thanks for helping, I found your answer very helpful and also learned new things as I am not expert in Mathematica. $\endgroup$
    – setareh
    Jul 6, 2015 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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