Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica Reduce/Solve: Best way to ask for non-repeating values

A few times I found I had a system and I need to specify that all variables get different values (ie non-repeating).

I usually do things like this:

k = {a, b, c, d, e, f, g}; 
Reduce[
   a != 2 && f == a + b && g == c - d && f == (d + e)/2 && f == e + g &&
   First[And @@@ {0 < # < 8 & /@ k}] && 
   Times@(Sequence @@ (Subsets[k, {2}] /. {x_, y_} -> (x - y))) != 0, 
k, Integers]

Where the last part of the Reduce equation

Times@(Sequence @@ (Subsets[k, {2}] /. {x_, y_} -> (x - y))) != 0

asks for different values.

Are there better ways to do this? (I mean, not the product equal zero, but to specify I need all variables different)

like image 538
Dr. belisarius Avatar asked May 28 '11 00:05

Dr. belisarius


People also ask

What does reduce do in Mathematica?

reduces the statement expr by solving equations or inequalities for vars and eliminating quantifiers.

Can Mathematica solve inequalities?

Wolfram MathematicaVersion 12 provides functionality for solving several new classes of equation and inequality systems. This example shows several such classes.


1 Answers

From the speed viewpoint, you are inducing a large overhead with that product condition. If your solutions are always numbers, you can produce all solutions with Reduce and then filter them - it may be faster in some cases. For example, in the case at hand:

k = {a, b, c, d, e, f, g};
sl = Reduce[ a != 2 && f == a + b && g == c - d && f == (d + e)/2 && f == e + g &&
      First[And @@@ {0 < # < 8 & /@ k}], k, Integers]

You can do the post-processing, for example like this (perhaps not the best way):

In[21]:= Select[{#, First[k /. Solve[#, k]]} & /@ List @@ sl, 
            MatchQ[Tally[#[[2]], Equal][[All, 2]], {1 ..}] &][[All, 1]]

Out[21]= {a == 3 && b == 2 && c == 7 && d == 6 && e == 4 && f == 5 && g == 1}

At least for this particular case, it was much faster.

like image 154
Leonid Shifrin Avatar answered Nov 17 '22 12:11

Leonid Shifrin