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}
And @@ Map[Element[#, {-1, 0, 1}] &, Array[v, 10]]
work? $\endgroup$And @@ Map[Or @@ Thread[# == {-1, 0, 1}] &, Array[v, 10]]
$\endgroup$Element
down the list. (Because Mathematica itself gives things like{a,b} \elem Integers
. It will take me a good long time to sort through what all your suggestions are doing. I pick up all kinds of programming languages in a flash, but I find Mathematica incomprehensible, and the documentation paltry. I'll have to look up and rememberArray
, that's much shorter. $\endgroup$Basic Examples
,Scope
, andProperties & Relations
. I think the documentation very much has a learn-by-example style. Anyway,Outer
is particularly useful in your case, where you have two lists, and every element in one list gets "matched" to every element in the other list somehow.Thread
andMapThread
or complementary, but I still sometimes have to think a moment to figure out which one to use.Map
ping is my go-to for basically everything, soMap
pingElement
came naturally. $\endgroup$