3
$\begingroup$

I have a long logic expression

(x==2||x==3||x==4)&&(y==2||y==3||y==4)&&(z==2||z==3||z==4)&&(m==2||m==3||m==4)

How to get it by a terse express? This is current method

Or@@@(Thread[#=={2,3,4}]&/@And[x,y,z,m])

(x==2||x==3||x==4)&&(y==2||y==3||y==4)&&(z==2||z==3||z==4)&&(m==2||m==3||m==4)

But I'm very dissatisfy that /@ and that ().I think there must are terse method can do this.Anybody can give a try?

$\endgroup$
6
  • $\begingroup$ Your equations vary in time ... $\endgroup$
    – yarchik
    Commented Apr 9, 2016 at 18:57
  • $\begingroup$ @yarchik I'm very sorry for the serious typo.But everyone have that time.:) $\endgroup$
    – yode
    Commented Apr 9, 2016 at 19:03
  • $\begingroup$ Am I missing something? Is the construct that you have some set of variables, and you want to test if all of them are contained in some set of values? If so, then ConstainsAll[values,vars] will suffice, and for really big examples (I'm assuming since the example is not really long, it's just an example) it will be vastly faster (as in thousands of times for big cases) than solutions so far... $\endgroup$
    – ciao
    Commented Apr 10, 2016 at 4:38
  • $\begingroup$ @ciao Thanks for your concern this post.The question drived from this post.My solution is var={x,y,z,m}; Solve[x+2y+z+m==24&&(x==2||x==7||x==4)&&(y==2||y==7||y==4)&&(z==2||z==7||z==4)&&(m==2||m==7||m==4),var].So I want to get (x==2||x==7||x==4)&&(y==2||y==7||y==4)&&(z==2||z==7||z==4)&&(m==2||m==7||m==4) by a concise code. $\endgroup$
    – yode
    Commented Apr 10, 2016 at 6:13
  • $\begingroup$ @yode : ah, well for the referenced post (the goal of this question I take it?), seems to me Solve[x + 2 y + z + m == 14 && And @@ Thread[2 <= {x, y, z, m} <= 4], Integers] is pretty concise... $\endgroup$
    – ciao
    Commented Apr 10, 2016 at 6:37

3 Answers 3

11
$\begingroup$

My offering:

And @@ Or @@@ Outer[Equal, {x, y, z, m}, {2, 3, 4}]
$\endgroup$
3
  • $\begingroup$ Thanks, I've swapped the logical functions around to your new requirements. $\endgroup$ Commented Apr 9, 2016 at 18:55
  • $\begingroup$ I think Distribute[Equal[And[x,y,z,m],Or[2,3,4]],And] should have a same output.But actually not. $\endgroup$
    – yode
    Commented Apr 10, 2016 at 4:03
  • $\begingroup$ I see what you mean, it doesn't quite work does it! If it did And @@ Thread[{x, y, z} == Or[2, 3, 4]] would be a fraction terser $\endgroup$ Commented Apr 10, 2016 at 9:19
1
$\begingroup$

A set solution.

cond = {{x}, {y}, {z}, {m}} ∈ Interval[{2, 4}] && {x, y, z, m} ∈ Integers

Set x, y, z, and m to be Integers on the Interval 2 to 4. We can use cond to be certain it is defined as expected.

Solve[x + y + z + m == 8 && cond, {x, y, z, m}]
(* {{x -> 2, y -> 2, z -> 2, m -> 2}} *)

This gives the expected result.

Hope this helps.

$\endgroup$
0
$\begingroup$
And @@ Thread@Table[Or @@ Thread@Table[j == i, {i, {2, 3, 4}}], {j, {x, y, z, m}}]
$\endgroup$
2
  • $\begingroup$ With just one Table: And @@ Or @@@ Table[j == i, {j, {x, y, z, m}}, {i, {2, 3, 4}}] $\endgroup$ Commented Apr 9, 2016 at 18:53
  • $\begingroup$ yes that's much better. I now realize that the my Table is essentially doing what your Outer does. But your solution is so much prettier! $\endgroup$
    – Hubble07
    Commented Apr 9, 2016 at 19:00

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.