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
)
reduces the statement expr by solving equations or inequalities for vars and eliminating quantifiers.
Wolfram MathematicaVersion 12 provides functionality for solving several new classes of equation and inequality systems. This example shows several such classes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With