Skip to main content
added 189 characters in body
Source Link

I've read lots of examples here on how to set one matrix Equal to another, but how do you nest And and Or around several different equalities for values a matrix of variables can take on? Using what I've found so far, I've been able to cobble something together that works, but surely someone knows a better way?

The idea: Variables V can take on the values in either A or B or C.

Minimum Working Example:

Apply[
  And, 
  Apply[
    Or, 
    MapThread[
       Equal,
       {Transpose[
          Table[v[i], {c, 3}, {i, 10}]], 
          Table[{-1, 0, 1}, {i, 10}]},
       2], 
    {1}]]

Good output:

(v[1] == -1 || v[1] == 0 || v[1] == 1) && (v[2] == -1 || v[2] == 0 || v[2] == 1) &&
(v[3] == -1 || v[3] == 0 || v[3] == 1) && (v[4] == -1 || v[4] == 0 || v[4] == 1) &&
(v[5] == -1 || v[5] == 0 || v[5] == 1) && (v[6] == -1 || v[6] == 0 || v[6] == 1) &&
(v[7] == -1 || v[7] == 0 || v[7] == 1) && (v[8] == -1 || v[8] == 0 || v[8] == 1) &&
(v[9] == -1 || v[9] == 0 || v[9] == 1) && (v[10] == -1 || v[10] == 0 || v[10] == 1)

Ideally the answer would be something of the form

And[Or[V == {A, B, C}]]

instead of the heavy use of Table like I have, so we can see what's happening and adapt it for other problems, and often the possible values won't have such a regular pattern as these do (A, B, & C may have irregular patterns and even repetitions in order to fill out the matrix to the right shape).

Conceivably there's a way to do something like this, but I couldn't get this to work either (for use in Reduce or FindInstance):

Eqns && V ∈ {-1, 0, 1}

I've read lots of examples here on how to set one matrix Equal to another, but how do you nest And and Or around several different equalities for values a matrix of variables can take on? Using what I've found so far, I've been able to cobble something together that works, but surely someone knows a better way?

The idea: Variables V can take on the values in either A or B or C.

Minimum Working Example:

Apply[
  And, 
  Apply[
    Or, 
    MapThread[
       Equal,
       {Transpose[
          Table[v[i], {c, 3}, {i, 10}]], 
          Table[{-1, 0, 1}, {i, 10}]},
       2], 
    {1}]]

Good output:

(v[1] == -1 || v[1] == 0 || v[1] == 1) && (v[2] == -1 || v[2] == 0 || v[2] == 1) &&
(v[3] == -1 || v[3] == 0 || v[3] == 1) && (v[4] == -1 || v[4] == 0 || v[4] == 1) &&
(v[5] == -1 || v[5] == 0 || v[5] == 1) && (v[6] == -1 || v[6] == 0 || v[6] == 1) &&
(v[7] == -1 || v[7] == 0 || v[7] == 1) && (v[8] == -1 || v[8] == 0 || v[8] == 1) &&
(v[9] == -1 || v[9] == 0 || v[9] == 1) && (v[10] == -1 || v[10] == 0 || v[10] == 1)

Ideally the answer would be something of the form

And[Or[V == {A, B, C}]]

instead of the heavy use of Table like I have, so we can see what's happening and adapt it for other problems.

Conceivably there's a way to do something like this, but I couldn't get this to work either (for use in Reduce or FindInstance):

Eqns && V ∈ {-1, 0, 1}

I've read lots of examples here on how to set one matrix Equal to another, but how do you nest And and Or around several different equalities for values a matrix of variables can take on? Using what I've found so far, I've been able to cobble something together that works, but surely someone knows a better way?

The idea: Variables V can take on the values in either A or B or C.

Minimum Working Example:

Apply[
  And, 
  Apply[
    Or, 
    MapThread[
       Equal,
       {Transpose[
          Table[v[i], {c, 3}, {i, 10}]], 
          Table[{-1, 0, 1}, {i, 10}]},
       2], 
    {1}]]

Good output:

(v[1] == -1 || v[1] == 0 || v[1] == 1) && (v[2] == -1 || v[2] == 0 || v[2] == 1) &&
(v[3] == -1 || v[3] == 0 || v[3] == 1) && (v[4] == -1 || v[4] == 0 || v[4] == 1) &&
(v[5] == -1 || v[5] == 0 || v[5] == 1) && (v[6] == -1 || v[6] == 0 || v[6] == 1) &&
(v[7] == -1 || v[7] == 0 || v[7] == 1) && (v[8] == -1 || v[8] == 0 || v[8] == 1) &&
(v[9] == -1 || v[9] == 0 || v[9] == 1) && (v[10] == -1 || v[10] == 0 || v[10] == 1)

Ideally the answer would be something of the form

And[Or[V == {A, B, C}]]

instead of the heavy use of Table like I have, so we can see what's happening and adapt it for other problems, and often the possible values won't have such a regular pattern as these do (A, B, & C may have irregular patterns and even repetitions in order to fill out the matrix to the right shape).

Conceivably there's a way to do something like this, but I couldn't get this to work either (for use in Reduce or FindInstance):

Eqns && V ∈ {-1, 0, 1}
edited title
Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

What is an efficient way to Thread / Map / MapThread, And and Or around lists Creating Boolean expressions over a set of possible values forindexed variables

deleted 2 characters in body; edited tags
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

I've read lots of examples here on how to set one matrix Equal to another, but how do you nest And and Or around several different equalities for values a matrix of variables can take on? Using what I've found so far, I've been able to cobble something together that works, but surely someone knows a better way?

The idea: Variables V can take on the values in either A or B or C.



Minimum Minimum Working Example:

Apply[And, Apply[Or, MapThread[Equal,Apply[
                 {Table[v[i], {c, 3}, {i, 10}]\[Transpose]And, 
     Apply[
    Or,  
    MapThread[
       Equal,
       {Transpose[
          Table[v[i], {c, 3}, {i, 10}]],  
          Table[{-1, 0, 1}, {i, 10}]},
       2], 
    {1}]]

Good output:

(v[1] == -1 || v[1] == 0 || v[1] == 1) && (v[2] == -1 || v[2] == 0 || v[2] == 1) &&
(v[3] == -1 || v[3] == 0 || v[3] == 1) && (v[4] == -1 || v[4] == 0 || v[4] == 1) &&
(v[5] == -1 || v[5] == 0 || v[5] == 1) && (v[6] == -1 || v[6] == 0 || v[6] == 1) &&
(v[7] == -1 || v[7] == 0 || v[7] == 1) && (v[8] == -1 || v[8] == 0 || v[8] == 1) &&
(v[9] == -1 || v[9] == 0 || v[9] == 1) && (v[10] == -1 || v[10] == 0 || v[10] == 1)



Ideally Ideally the answer would be something of the form

And[Or[V==And[Or[V == {A, B, C}]]

instead of the heavy use of Table like I have, so we can see what's happening and adapt it for other problems.



Conceivably Conceivably there's a way to do something like this, but I couldn't get this to work either (for use in Reduce or FindInstance):

Eqns && V \[Element] {-1, 0, 1}

I've read lots of examples here on how to set one matrix Equal to another, but how do you nest And and Or around several different equalities for values a matrix of variables can take on? Using what I've found so far, I've been able to cobble something together that works, but surely someone knows a better way?

The idea: Variables V can take on the values in either A or B or C.



Minimum Working Example:

Apply[And, Apply[Or, MapThread[Equal,
                 {Table[v[i], {c, 3}, {i, 10}]\[Transpose], 
                                                     Table[{-1, 0, 1}, {i, 10}]}, 2], {1}]]

Good output:

(v[1] == -1 || v[1] == 0 || v[1] == 1) && (v[2] == -1 || v[2] == 0 || v[2] == 1) &&
(v[3] == -1 || v[3] == 0 || v[3] == 1) && (v[4] == -1 || v[4] == 0 || v[4] == 1) &&
(v[5] == -1 || v[5] == 0 || v[5] == 1) && (v[6] == -1 || v[6] == 0 || v[6] == 1) &&
(v[7] == -1 || v[7] == 0 || v[7] == 1) && (v[8] == -1 || v[8] == 0 || v[8] == 1) &&
(v[9] == -1 || v[9] == 0 || v[9] == 1) && (v[10] == -1 || v[10] == 0 || v[10] == 1)



Ideally the answer would be something of the form

And[Or[V=={A,B,C}]]

instead of the heavy use of Table like I have, so we can see what's happening and adapt it for other problems.



Conceivably there's a way to do something like this, but I couldn't get this to work either (for use in Reduce or FindInstance):

Eqns && V \[Element] {-1, 0, 1}

I've read lots of examples here on how to set one matrix Equal to another, but how do you nest And and Or around several different equalities for values a matrix of variables can take on? Using what I've found so far, I've been able to cobble something together that works, but surely someone knows a better way?

The idea: Variables V can take on the values in either A or B or C.

Minimum Working Example:

Apply[
  And, 
  Apply[
    Or,  
    MapThread[
       Equal,
       {Transpose[
          Table[v[i], {c, 3}, {i, 10}]],  
          Table[{-1, 0, 1}, {i, 10}]},
       2], 
    {1}]]

Good output:

(v[1] == -1 || v[1] == 0 || v[1] == 1) && (v[2] == -1 || v[2] == 0 || v[2] == 1) &&
(v[3] == -1 || v[3] == 0 || v[3] == 1) && (v[4] == -1 || v[4] == 0 || v[4] == 1) &&
(v[5] == -1 || v[5] == 0 || v[5] == 1) && (v[6] == -1 || v[6] == 0 || v[6] == 1) &&
(v[7] == -1 || v[7] == 0 || v[7] == 1) && (v[8] == -1 || v[8] == 0 || v[8] == 1) &&
(v[9] == -1 || v[9] == 0 || v[9] == 1) && (v[10] == -1 || v[10] == 0 || v[10] == 1)

Ideally the answer would be something of the form

And[Or[V == {A, B, C}]]

instead of the heavy use of Table like I have, so we can see what's happening and adapt it for other problems.

Conceivably there's a way to do something like this, but I couldn't get this to work either (for use in Reduce or FindInstance):

Eqns && V  {-1, 0, 1}
added 185 characters in body
Source Link
Loading
Source Link
Loading