Duplicate Tuples in two different waysStandardizing a coset table via matrix manipulationSpeeding up a functionDistributing function arguments with function compositions. How to compute $(f + g^2)(x) = f(x) + g(x)^2$?How to get name of “named row” in Dataset for function applied to each rowOptional argument as a correction of a main argumentDuplicate TuplesSelecting from a list returned by TuplesDelete duplicates in tuplesWhat is the identity for Tuples?
Are fuzzy sets appreciated by OR community?
Why does my browser attempt to download pages from http://clhs.lisp.se instead of viewing them normally?
Flowers sent by the birds
Why isn't there armor to protect from spells in the Potterverse?
What secular civic space would pioneers build for small frontier towns?
How can I tell the difference between fishing for rolls and being involved?
New road bike: alloy dual pivot brakes work poorly
Top off gas with old oil, is that bad?
Subverting the emotional woman and stoic man trope
Designing a time thief proof safe
Windows 10 deletes lots of tiny files super slowly. Anything that can be done to speed it up?
Character Transformation
Why, even after his imprisonment, people keep calling Hannibal Lecter "Doctor"?
What does it mean by "my days-of-the-week underwear only go to Thursday" in this context?
Can my former employer sue me if I don't give them the photos I took (taking pictures was not part of my job description)?
Can I enter the UK without my husband if we said we'd travel together in our visa application?
Can someone give the intuition behind Mean Absolute Error and the Median?
A food item only made possible by time-freezing storage?
Clear text passwords in Unix
Neural Network vs regression
Is a Middle Name a Given Name?
Practicality of 30 year fixed mortgage at 55 years of age
Why are there two fundamental laws of logic?
If a spaceship ran out of fuel somewhere in space between Earth and Mars, does it slowly drift off to Sun?
Duplicate Tuples in two different ways
Standardizing a coset table via matrix manipulationSpeeding up a functionDistributing function arguments with function compositions. How to compute $(f + g^2)(x) = f(x) + g(x)^2$?How to get name of “named row” in Dataset for function applied to each rowOptional argument as a correction of a main argumentDuplicate TuplesSelecting from a list returned by TuplesDelete duplicates in tuplesWhat is the identity for Tuples?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Writing:
n = 3;
Tuples["min", "MAX", n]
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
One way to duplicate this function is to fill in this table by columns. In fact, writing:
tuples = ConstantArray[0, 2^n, n];
flag = 1;
For[j = 1, j <= n, j++,
For[i = 1, i <= 2^n, i++,
If[flag == 1,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
];
If[Mod[i, 2^(n - j)] == 0, flag = flag + 1];
If[flag > 2, flag = 1]
]
];
tuples
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
Now I would like to duplicate this function by filling in this table by rows. My first approach is the following:
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[2^(j - 1) <= i <= 2^n/2^j,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
which offers only the first correct column:
"min", "MAX", "MAX", "min", "min", "MAX", "min", "MAX", "MAX", "min", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX","MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX"
Any idea to correct this code? Thanks!
function-construction tuples
$endgroup$
add a comment
|
$begingroup$
Writing:
n = 3;
Tuples["min", "MAX", n]
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
One way to duplicate this function is to fill in this table by columns. In fact, writing:
tuples = ConstantArray[0, 2^n, n];
flag = 1;
For[j = 1, j <= n, j++,
For[i = 1, i <= 2^n, i++,
If[flag == 1,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
];
If[Mod[i, 2^(n - j)] == 0, flag = flag + 1];
If[flag > 2, flag = 1]
]
];
tuples
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
Now I would like to duplicate this function by filling in this table by rows. My first approach is the following:
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[2^(j - 1) <= i <= 2^n/2^j,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
which offers only the first correct column:
"min", "MAX", "MAX", "min", "min", "MAX", "min", "MAX", "MAX", "min", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX","MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX"
Any idea to correct this code? Thanks!
function-construction tuples
$endgroup$
1
$begingroup$
Why does the order in which you fill the matrix matter?
$endgroup$
– m_goldberg
6 hours ago
$begingroup$
@m_goldberg: if we limit ourselves to filling a matrix I agree that one way is as good as another. However, if this algorithm is applied in other contexts, it may be much more efficient to proceed by rows. Unfortunately, I have not found this approach in any book.
$endgroup$
– TeM
6 hours ago
1
$begingroup$
Mathematica stores matrices in row-order, so proceeding by rows will always be more efficient than proceeding be columns. Trying to force a computation into column-order will only slow the computation down.
$endgroup$
– m_goldberg
6 hours ago
add a comment
|
$begingroup$
Writing:
n = 3;
Tuples["min", "MAX", n]
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
One way to duplicate this function is to fill in this table by columns. In fact, writing:
tuples = ConstantArray[0, 2^n, n];
flag = 1;
For[j = 1, j <= n, j++,
For[i = 1, i <= 2^n, i++,
If[flag == 1,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
];
If[Mod[i, 2^(n - j)] == 0, flag = flag + 1];
If[flag > 2, flag = 1]
]
];
tuples
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
Now I would like to duplicate this function by filling in this table by rows. My first approach is the following:
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[2^(j - 1) <= i <= 2^n/2^j,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
which offers only the first correct column:
"min", "MAX", "MAX", "min", "min", "MAX", "min", "MAX", "MAX", "min", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX","MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX"
Any idea to correct this code? Thanks!
function-construction tuples
$endgroup$
Writing:
n = 3;
Tuples["min", "MAX", n]
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
One way to duplicate this function is to fill in this table by columns. In fact, writing:
tuples = ConstantArray[0, 2^n, n];
flag = 1;
For[j = 1, j <= n, j++,
For[i = 1, i <= 2^n, i++,
If[flag == 1,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
];
If[Mod[i, 2^(n - j)] == 0, flag = flag + 1];
If[flag > 2, flag = 1]
]
];
tuples
I get:
"min", "min", "min", "min", "min", "MAX", "min", "MAX", "min", "min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min","MAX", "MAX", "MAX", "min", "MAX", "MAX", "MAX"
Now I would like to duplicate this function by filling in this table by rows. My first approach is the following:
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[2^(j - 1) <= i <= 2^n/2^j,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
which offers only the first correct column:
"min", "MAX", "MAX", "min", "min", "MAX", "min", "MAX", "MAX", "min", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX","MAX", "MAX", "MAX", "MAX", "MAX", "MAX", "MAX"
Any idea to correct this code? Thanks!
function-construction tuples
function-construction tuples
edited 5 hours ago
m_goldberg
91.7k8 gold badges75 silver badges209 bronze badges
91.7k8 gold badges75 silver badges209 bronze badges
asked 8 hours ago
TeMTeM
2,3657 silver badges21 bronze badges
2,3657 silver badges21 bronze badges
1
$begingroup$
Why does the order in which you fill the matrix matter?
$endgroup$
– m_goldberg
6 hours ago
$begingroup$
@m_goldberg: if we limit ourselves to filling a matrix I agree that one way is as good as another. However, if this algorithm is applied in other contexts, it may be much more efficient to proceed by rows. Unfortunately, I have not found this approach in any book.
$endgroup$
– TeM
6 hours ago
1
$begingroup$
Mathematica stores matrices in row-order, so proceeding by rows will always be more efficient than proceeding be columns. Trying to force a computation into column-order will only slow the computation down.
$endgroup$
– m_goldberg
6 hours ago
add a comment
|
1
$begingroup$
Why does the order in which you fill the matrix matter?
$endgroup$
– m_goldberg
6 hours ago
$begingroup$
@m_goldberg: if we limit ourselves to filling a matrix I agree that one way is as good as another. However, if this algorithm is applied in other contexts, it may be much more efficient to proceed by rows. Unfortunately, I have not found this approach in any book.
$endgroup$
– TeM
6 hours ago
1
$begingroup$
Mathematica stores matrices in row-order, so proceeding by rows will always be more efficient than proceeding be columns. Trying to force a computation into column-order will only slow the computation down.
$endgroup$
– m_goldberg
6 hours ago
1
1
$begingroup$
Why does the order in which you fill the matrix matter?
$endgroup$
– m_goldberg
6 hours ago
$begingroup$
Why does the order in which you fill the matrix matter?
$endgroup$
– m_goldberg
6 hours ago
$begingroup$
@m_goldberg: if we limit ourselves to filling a matrix I agree that one way is as good as another. However, if this algorithm is applied in other contexts, it may be much more efficient to proceed by rows. Unfortunately, I have not found this approach in any book.
$endgroup$
– TeM
6 hours ago
$begingroup$
@m_goldberg: if we limit ourselves to filling a matrix I agree that one way is as good as another. However, if this algorithm is applied in other contexts, it may be much more efficient to proceed by rows. Unfortunately, I have not found this approach in any book.
$endgroup$
– TeM
6 hours ago
1
1
$begingroup$
Mathematica stores matrices in row-order, so proceeding by rows will always be more efficient than proceeding be columns. Trying to force a computation into column-order will only slow the computation down.
$endgroup$
– m_goldberg
6 hours ago
$begingroup$
Mathematica stores matrices in row-order, so proceeding by rows will always be more efficient than proceeding be columns. Trying to force a computation into column-order will only slow the computation down.
$endgroup$
– m_goldberg
6 hours ago
add a comment
|
2 Answers
2
active
oldest
votes
$begingroup$
Noticing that going row-by-row, we're essentially counting in binary (with "min" as 0 and "MAX" as 1), we see that we can use BitAnd[i - 1, 2^(n - j)] == 0 as condition:
n = 3;
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[BitAnd[i - 1, 2^(n - j)] == 0,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
(* "min", "min", "min", "min", "min", "MAX", "min", "MAX", "min",
"min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min", "MAX",
"MAX", "MAX", "min", "MAX", "MAX", "MAX" *)
Or, more concise:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
i, 2^n,
j, n
];
tuples
(* same output *)
The condition works by checking the $(n-j)$-th bit of $i-1$ using BitAnd:
$n-j$ instead of e.g. $j$ because the zeroth bit is the $n$-th column, and the $n-1$-th bit is the first column- The $-1$ in $i-1$ is needed since row indices start at $1$, but the counting starts at $0$
Note: Since the condition is completely stateless (i.e. there is no flag variable or similar), we can simply swap the iteration indices to change the filling order:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
j, n,
i, 2^n
];
tuples
(* same output *)
$endgroup$
$begingroup$
I sometimes remain speechless, I would never have arrived! Now I will have to study the BitAnd function, because in some environments it is not pre-packaged. In any case, magnificent, thank you!
$endgroup$
– TeM
6 hours ago
1
$begingroup$
BitAndis essentially the bit wise AND operator from most other languages (e.g.&in C and related languages). Also, what exactly do you mean by "not pre-packaged in some environments"?
$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
In Mathematica, Excel, ... it is sufficient to make use of BitAnd, because it is already present in their library; in other programs, this function must be constructed using For, While and If commands.
$endgroup$
– TeM
5 hours ago
$begingroup$
Out of curiosity: can you give an example of such a language withoutBitAndequivalent?
$endgroup$
– Lukas Lang
5 hours ago
1
$begingroup$
I think you don't need to worry about this particular operator - I think I have yet to come across a language without bit wise operators
$endgroup$
– Lukas Lang
5 hours ago
|
show 1 more comment
$begingroup$
Lukes answer is excellent but it can be improved. This version is faster as well as more concise.
n = 3;
tuples = ConstantArray[0, 2^n, n];
Do[
tuples[[i, j]] = If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"],
j, n, i, 2^n];
tuples // MatrixForm

Your For-loop solution can also be improved by applying the same ideas.
flag = 0;
Do[
If[flag == 0, tuples[[i, j]] = "min", tuples[[i, j]] = "MAX"];
If[Mod[i, 2^(n - j)] == 0, flag = Mod[flag + 1, 2]],
j, n, i, 2^n];
$endgroup$
$begingroup$
You are really kind and at the same time brilliant, thanks for sharing your immense knowledge!
$endgroup$
– TeM
5 hours ago
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206648%2fduplicate-tuples-in-two-different-ways%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Noticing that going row-by-row, we're essentially counting in binary (with "min" as 0 and "MAX" as 1), we see that we can use BitAnd[i - 1, 2^(n - j)] == 0 as condition:
n = 3;
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[BitAnd[i - 1, 2^(n - j)] == 0,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
(* "min", "min", "min", "min", "min", "MAX", "min", "MAX", "min",
"min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min", "MAX",
"MAX", "MAX", "min", "MAX", "MAX", "MAX" *)
Or, more concise:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
i, 2^n,
j, n
];
tuples
(* same output *)
The condition works by checking the $(n-j)$-th bit of $i-1$ using BitAnd:
$n-j$ instead of e.g. $j$ because the zeroth bit is the $n$-th column, and the $n-1$-th bit is the first column- The $-1$ in $i-1$ is needed since row indices start at $1$, but the counting starts at $0$
Note: Since the condition is completely stateless (i.e. there is no flag variable or similar), we can simply swap the iteration indices to change the filling order:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
j, n,
i, 2^n
];
tuples
(* same output *)
$endgroup$
$begingroup$
I sometimes remain speechless, I would never have arrived! Now I will have to study the BitAnd function, because in some environments it is not pre-packaged. In any case, magnificent, thank you!
$endgroup$
– TeM
6 hours ago
1
$begingroup$
BitAndis essentially the bit wise AND operator from most other languages (e.g.&in C and related languages). Also, what exactly do you mean by "not pre-packaged in some environments"?
$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
In Mathematica, Excel, ... it is sufficient to make use of BitAnd, because it is already present in their library; in other programs, this function must be constructed using For, While and If commands.
$endgroup$
– TeM
5 hours ago
$begingroup$
Out of curiosity: can you give an example of such a language withoutBitAndequivalent?
$endgroup$
– Lukas Lang
5 hours ago
1
$begingroup$
I think you don't need to worry about this particular operator - I think I have yet to come across a language without bit wise operators
$endgroup$
– Lukas Lang
5 hours ago
|
show 1 more comment
$begingroup$
Noticing that going row-by-row, we're essentially counting in binary (with "min" as 0 and "MAX" as 1), we see that we can use BitAnd[i - 1, 2^(n - j)] == 0 as condition:
n = 3;
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[BitAnd[i - 1, 2^(n - j)] == 0,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
(* "min", "min", "min", "min", "min", "MAX", "min", "MAX", "min",
"min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min", "MAX",
"MAX", "MAX", "min", "MAX", "MAX", "MAX" *)
Or, more concise:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
i, 2^n,
j, n
];
tuples
(* same output *)
The condition works by checking the $(n-j)$-th bit of $i-1$ using BitAnd:
$n-j$ instead of e.g. $j$ because the zeroth bit is the $n$-th column, and the $n-1$-th bit is the first column- The $-1$ in $i-1$ is needed since row indices start at $1$, but the counting starts at $0$
Note: Since the condition is completely stateless (i.e. there is no flag variable or similar), we can simply swap the iteration indices to change the filling order:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
j, n,
i, 2^n
];
tuples
(* same output *)
$endgroup$
$begingroup$
I sometimes remain speechless, I would never have arrived! Now I will have to study the BitAnd function, because in some environments it is not pre-packaged. In any case, magnificent, thank you!
$endgroup$
– TeM
6 hours ago
1
$begingroup$
BitAndis essentially the bit wise AND operator from most other languages (e.g.&in C and related languages). Also, what exactly do you mean by "not pre-packaged in some environments"?
$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
In Mathematica, Excel, ... it is sufficient to make use of BitAnd, because it is already present in their library; in other programs, this function must be constructed using For, While and If commands.
$endgroup$
– TeM
5 hours ago
$begingroup$
Out of curiosity: can you give an example of such a language withoutBitAndequivalent?
$endgroup$
– Lukas Lang
5 hours ago
1
$begingroup$
I think you don't need to worry about this particular operator - I think I have yet to come across a language without bit wise operators
$endgroup$
– Lukas Lang
5 hours ago
|
show 1 more comment
$begingroup$
Noticing that going row-by-row, we're essentially counting in binary (with "min" as 0 and "MAX" as 1), we see that we can use BitAnd[i - 1, 2^(n - j)] == 0 as condition:
n = 3;
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[BitAnd[i - 1, 2^(n - j)] == 0,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
(* "min", "min", "min", "min", "min", "MAX", "min", "MAX", "min",
"min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min", "MAX",
"MAX", "MAX", "min", "MAX", "MAX", "MAX" *)
Or, more concise:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
i, 2^n,
j, n
];
tuples
(* same output *)
The condition works by checking the $(n-j)$-th bit of $i-1$ using BitAnd:
$n-j$ instead of e.g. $j$ because the zeroth bit is the $n$-th column, and the $n-1$-th bit is the first column- The $-1$ in $i-1$ is needed since row indices start at $1$, but the counting starts at $0$
Note: Since the condition is completely stateless (i.e. there is no flag variable or similar), we can simply swap the iteration indices to change the filling order:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
j, n,
i, 2^n
];
tuples
(* same output *)
$endgroup$
Noticing that going row-by-row, we're essentially counting in binary (with "min" as 0 and "MAX" as 1), we see that we can use BitAnd[i - 1, 2^(n - j)] == 0 as condition:
n = 3;
tuples = ConstantArray[0, 2^n, n];
For[i = 1, i <= 2^n, i++,
For[j = 1, j <= n, j++,
If[BitAnd[i - 1, 2^(n - j)] == 0,
tuples = ReplacePart[tuples, i, j -> "min"],
tuples = ReplacePart[tuples, i, j -> "MAX"]
]
]
];
tuples
(* "min", "min", "min", "min", "min", "MAX", "min", "MAX", "min",
"min", "MAX", "MAX", "MAX", "min", "min", "MAX", "min", "MAX",
"MAX", "MAX", "min", "MAX", "MAX", "MAX" *)
Or, more concise:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
i, 2^n,
j, n
];
tuples
(* same output *)
The condition works by checking the $(n-j)$-th bit of $i-1$ using BitAnd:
$n-j$ instead of e.g. $j$ because the zeroth bit is the $n$-th column, and the $n-1$-th bit is the first column- The $-1$ in $i-1$ is needed since row indices start at $1$, but the counting starts at $0$
Note: Since the condition is completely stateless (i.e. there is no flag variable or similar), we can simply swap the iteration indices to change the filling order:
tuples = ConstantArray[0, 2^n, n];
Do[
tuples = ReplacePart[
tuples,
i, j -> If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"]
],
j, n,
i, 2^n
];
tuples
(* same output *)
edited 6 hours ago
answered 6 hours ago
Lukas LangLukas Lang
9,9091 gold badge13 silver badges37 bronze badges
9,9091 gold badge13 silver badges37 bronze badges
$begingroup$
I sometimes remain speechless, I would never have arrived! Now I will have to study the BitAnd function, because in some environments it is not pre-packaged. In any case, magnificent, thank you!
$endgroup$
– TeM
6 hours ago
1
$begingroup$
BitAndis essentially the bit wise AND operator from most other languages (e.g.&in C and related languages). Also, what exactly do you mean by "not pre-packaged in some environments"?
$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
In Mathematica, Excel, ... it is sufficient to make use of BitAnd, because it is already present in their library; in other programs, this function must be constructed using For, While and If commands.
$endgroup$
– TeM
5 hours ago
$begingroup$
Out of curiosity: can you give an example of such a language withoutBitAndequivalent?
$endgroup$
– Lukas Lang
5 hours ago
1
$begingroup$
I think you don't need to worry about this particular operator - I think I have yet to come across a language without bit wise operators
$endgroup$
– Lukas Lang
5 hours ago
|
show 1 more comment
$begingroup$
I sometimes remain speechless, I would never have arrived! Now I will have to study the BitAnd function, because in some environments it is not pre-packaged. In any case, magnificent, thank you!
$endgroup$
– TeM
6 hours ago
1
$begingroup$
BitAndis essentially the bit wise AND operator from most other languages (e.g.&in C and related languages). Also, what exactly do you mean by "not pre-packaged in some environments"?
$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
In Mathematica, Excel, ... it is sufficient to make use of BitAnd, because it is already present in their library; in other programs, this function must be constructed using For, While and If commands.
$endgroup$
– TeM
5 hours ago
$begingroup$
Out of curiosity: can you give an example of such a language withoutBitAndequivalent?
$endgroup$
– Lukas Lang
5 hours ago
1
$begingroup$
I think you don't need to worry about this particular operator - I think I have yet to come across a language without bit wise operators
$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
I sometimes remain speechless, I would never have arrived! Now I will have to study the BitAnd function, because in some environments it is not pre-packaged. In any case, magnificent, thank you!
$endgroup$
– TeM
6 hours ago
$begingroup$
I sometimes remain speechless, I would never have arrived! Now I will have to study the BitAnd function, because in some environments it is not pre-packaged. In any case, magnificent, thank you!
$endgroup$
– TeM
6 hours ago
1
1
$begingroup$
BitAnd is essentially the bit wise AND operator from most other languages (e.g. & in C and related languages). Also, what exactly do you mean by "not pre-packaged in some environments"?$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
BitAnd is essentially the bit wise AND operator from most other languages (e.g. & in C and related languages). Also, what exactly do you mean by "not pre-packaged in some environments"?$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
In Mathematica, Excel, ... it is sufficient to make use of BitAnd, because it is already present in their library; in other programs, this function must be constructed using For, While and If commands.
$endgroup$
– TeM
5 hours ago
$begingroup$
In Mathematica, Excel, ... it is sufficient to make use of BitAnd, because it is already present in their library; in other programs, this function must be constructed using For, While and If commands.
$endgroup$
– TeM
5 hours ago
$begingroup$
Out of curiosity: can you give an example of such a language without
BitAnd equivalent?$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
Out of curiosity: can you give an example of such a language without
BitAnd equivalent?$endgroup$
– Lukas Lang
5 hours ago
1
1
$begingroup$
I think you don't need to worry about this particular operator - I think I have yet to come across a language without bit wise operators
$endgroup$
– Lukas Lang
5 hours ago
$begingroup$
I think you don't need to worry about this particular operator - I think I have yet to come across a language without bit wise operators
$endgroup$
– Lukas Lang
5 hours ago
|
show 1 more comment
$begingroup$
Lukes answer is excellent but it can be improved. This version is faster as well as more concise.
n = 3;
tuples = ConstantArray[0, 2^n, n];
Do[
tuples[[i, j]] = If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"],
j, n, i, 2^n];
tuples // MatrixForm

Your For-loop solution can also be improved by applying the same ideas.
flag = 0;
Do[
If[flag == 0, tuples[[i, j]] = "min", tuples[[i, j]] = "MAX"];
If[Mod[i, 2^(n - j)] == 0, flag = Mod[flag + 1, 2]],
j, n, i, 2^n];
$endgroup$
$begingroup$
You are really kind and at the same time brilliant, thanks for sharing your immense knowledge!
$endgroup$
– TeM
5 hours ago
add a comment
|
$begingroup$
Lukes answer is excellent but it can be improved. This version is faster as well as more concise.
n = 3;
tuples = ConstantArray[0, 2^n, n];
Do[
tuples[[i, j]] = If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"],
j, n, i, 2^n];
tuples // MatrixForm

Your For-loop solution can also be improved by applying the same ideas.
flag = 0;
Do[
If[flag == 0, tuples[[i, j]] = "min", tuples[[i, j]] = "MAX"];
If[Mod[i, 2^(n - j)] == 0, flag = Mod[flag + 1, 2]],
j, n, i, 2^n];
$endgroup$
$begingroup$
You are really kind and at the same time brilliant, thanks for sharing your immense knowledge!
$endgroup$
– TeM
5 hours ago
add a comment
|
$begingroup$
Lukes answer is excellent but it can be improved. This version is faster as well as more concise.
n = 3;
tuples = ConstantArray[0, 2^n, n];
Do[
tuples[[i, j]] = If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"],
j, n, i, 2^n];
tuples // MatrixForm

Your For-loop solution can also be improved by applying the same ideas.
flag = 0;
Do[
If[flag == 0, tuples[[i, j]] = "min", tuples[[i, j]] = "MAX"];
If[Mod[i, 2^(n - j)] == 0, flag = Mod[flag + 1, 2]],
j, n, i, 2^n];
$endgroup$
Lukes answer is excellent but it can be improved. This version is faster as well as more concise.
n = 3;
tuples = ConstantArray[0, 2^n, n];
Do[
tuples[[i, j]] = If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"],
j, n, i, 2^n];
tuples // MatrixForm

Your For-loop solution can also be improved by applying the same ideas.
flag = 0;
Do[
If[flag == 0, tuples[[i, j]] = "min", tuples[[i, j]] = "MAX"];
If[Mod[i, 2^(n - j)] == 0, flag = Mod[flag + 1, 2]],
j, n, i, 2^n];
edited 5 hours ago
answered 6 hours ago
m_goldbergm_goldberg
91.7k8 gold badges75 silver badges209 bronze badges
91.7k8 gold badges75 silver badges209 bronze badges
$begingroup$
You are really kind and at the same time brilliant, thanks for sharing your immense knowledge!
$endgroup$
– TeM
5 hours ago
add a comment
|
$begingroup$
You are really kind and at the same time brilliant, thanks for sharing your immense knowledge!
$endgroup$
– TeM
5 hours ago
$begingroup$
You are really kind and at the same time brilliant, thanks for sharing your immense knowledge!
$endgroup$
– TeM
5 hours ago
$begingroup$
You are really kind and at the same time brilliant, thanks for sharing your immense knowledge!
$endgroup$
– TeM
5 hours ago
add a comment
|
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206648%2fduplicate-tuples-in-two-different-ways%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Why does the order in which you fill the matrix matter?
$endgroup$
– m_goldberg
6 hours ago
$begingroup$
@m_goldberg: if we limit ourselves to filling a matrix I agree that one way is as good as another. However, if this algorithm is applied in other contexts, it may be much more efficient to proceed by rows. Unfortunately, I have not found this approach in any book.
$endgroup$
– TeM
6 hours ago
1
$begingroup$
Mathematica stores matrices in row-order, so proceeding by rows will always be more efficient than proceeding be columns. Trying to force a computation into column-order will only slow the computation down.
$endgroup$
– m_goldberg
6 hours ago