Iterate MapThread with matricesEquating matrices (or higher order tensors) element-wiseCan I return lists with different dimensions from a compiled function?MapThread multiple functions onto multiple listsFinding elements from a sparse matrixModifying a list of matrices with conditional statementEasy way to perform multiplication of two 2x2 matrices, that contain list elements?Cannot use Part in Pure FunctionFaster position-based duplicate removal in a ragged array?Importing and saving .csv files as variables with the original filebasenamesWhat is the fastest way of combining elemts of lists in rules?
C-152 carb heat on before landing in hot weather?
Why do some professors with PhDs leave their professorships to teach high school?
Apply brace expansion in "reverse order"
Accidentals and ties
Is it illegal to withhold someone's passport and green card in California?
How does a blind passenger not die, if driver becomes unconscious
Is a single radon-daughter atom in air a solid?
How risky is real estate?
Require advice on power conservation for backpacking trip
Can the negators "jamais, rien, personne, plus, ni, aucun" be used in a single sentence?
Line mode command to delete from column X to end of line?
Why is the high-pass filter result in a discrete wavelet transform (DWT) downsampled?
Unusual mail headers, evidence of an attempted attack. Have I been pwned?
Is adding a new player (or players) a DM decision, or a group decision?
Set multicolumn to a exact width
If I wouldn't want to read the story, is writing it still a good idea?
What's currently blocking the construction of the wall between Mexico and the US?
How do I respond to requests for a "guarantee" not to leave after a few months?
Why is C++ initial allocation so much larger than C's?
What reason would an alien civilization have for building a Dyson Sphere (or Swarm) if cheap Nuclear fusion is available?
Links to webpages in books
Hot coffee brewing solutions for deep woods camping
How dangerous are set-size assumptions?
Employer wants to use my work email account after I quit
Iterate MapThread with matrices
Equating matrices (or higher order tensors) element-wiseCan I return lists with different dimensions from a compiled function?MapThread multiple functions onto multiple listsFinding elements from a sparse matrixModifying a list of matrices with conditional statementEasy way to perform multiplication of two 2x2 matrices, that contain list elements?Cannot use Part in Pure FunctionFaster position-based duplicate removal in a ragged array?Importing and saving .csv files as variables with the original filebasenamesWhat is the fastest way of combining elemts of lists in rules?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Consider a function f
and a list p=a,b,c
. I want to get a list of
f[a,a]
f[a,b]
f[a,c]
f[b,a]
f[b,b]
f[b,c]
f[c,a]
f[c,b]
f[c,c]
In real use, p
can have higher dimensions, and f may take more arguments, e.g. I may need to generate a list of f[a,a,a,a]
through f[z,z,z,z]
. Is there a cleaner way to do this other than making lists that approximately repeat the elements of p
then use MapThread
? e.g.
p=a,b
p1 = a, a, b, b
p2 = a, b, a, b
MapThread[f, p1, p2]
Here, a
, b
can be matrices
list-manipulation map
$endgroup$
add a comment |
$begingroup$
Consider a function f
and a list p=a,b,c
. I want to get a list of
f[a,a]
f[a,b]
f[a,c]
f[b,a]
f[b,b]
f[b,c]
f[c,a]
f[c,b]
f[c,c]
In real use, p
can have higher dimensions, and f may take more arguments, e.g. I may need to generate a list of f[a,a,a,a]
through f[z,z,z,z]
. Is there a cleaner way to do this other than making lists that approximately repeat the elements of p
then use MapThread
? e.g.
p=a,b
p1 = a, a, b, b
p2 = a, b, a, b
MapThread[f, p1, p2]
Here, a
, b
can be matrices
list-manipulation map
$endgroup$
$begingroup$
What aboutOuter
?
$endgroup$
– chuy
8 hours ago
$begingroup$
@chuy Thanks! butOuter
may not be a good fit whena,b ...
are matrices.
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Just realized thatp = a, b, c; Distribute[f[p, p], List]
works
$endgroup$
– egwene sedai
8 hours ago
add a comment |
$begingroup$
Consider a function f
and a list p=a,b,c
. I want to get a list of
f[a,a]
f[a,b]
f[a,c]
f[b,a]
f[b,b]
f[b,c]
f[c,a]
f[c,b]
f[c,c]
In real use, p
can have higher dimensions, and f may take more arguments, e.g. I may need to generate a list of f[a,a,a,a]
through f[z,z,z,z]
. Is there a cleaner way to do this other than making lists that approximately repeat the elements of p
then use MapThread
? e.g.
p=a,b
p1 = a, a, b, b
p2 = a, b, a, b
MapThread[f, p1, p2]
Here, a
, b
can be matrices
list-manipulation map
$endgroup$
Consider a function f
and a list p=a,b,c
. I want to get a list of
f[a,a]
f[a,b]
f[a,c]
f[b,a]
f[b,b]
f[b,c]
f[c,a]
f[c,b]
f[c,c]
In real use, p
can have higher dimensions, and f may take more arguments, e.g. I may need to generate a list of f[a,a,a,a]
through f[z,z,z,z]
. Is there a cleaner way to do this other than making lists that approximately repeat the elements of p
then use MapThread
? e.g.
p=a,b
p1 = a, a, b, b
p2 = a, b, a, b
MapThread[f, p1, p2]
Here, a
, b
can be matrices
list-manipulation map
list-manipulation map
edited 8 hours ago
egwene sedai
asked 8 hours ago
egwene sedaiegwene sedai
1,86310 silver badges21 bronze badges
1,86310 silver badges21 bronze badges
$begingroup$
What aboutOuter
?
$endgroup$
– chuy
8 hours ago
$begingroup$
@chuy Thanks! butOuter
may not be a good fit whena,b ...
are matrices.
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Just realized thatp = a, b, c; Distribute[f[p, p], List]
works
$endgroup$
– egwene sedai
8 hours ago
add a comment |
$begingroup$
What aboutOuter
?
$endgroup$
– chuy
8 hours ago
$begingroup$
@chuy Thanks! butOuter
may not be a good fit whena,b ...
are matrices.
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Just realized thatp = a, b, c; Distribute[f[p, p], List]
works
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
What about
Outer
?$endgroup$
– chuy
8 hours ago
$begingroup$
What about
Outer
?$endgroup$
– chuy
8 hours ago
$begingroup$
@chuy Thanks! but
Outer
may not be a good fit when a,b ...
are matrices.$endgroup$
– egwene sedai
8 hours ago
$begingroup$
@chuy Thanks! but
Outer
may not be a good fit when a,b ...
are matrices.$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Just realized that
p = a, b, c; Distribute[f[p, p], List]
works$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Just realized that
p = a, b, c; Distribute[f[p, p], List]
works$endgroup$
– egwene sedai
8 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
You can use Tuples
:
Tuples[f[a, b, c, a, b, c]]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a],
f[c, b], f[c, c]
f @@@ Tuples[a, b, c, 2]
same result
Tuples[f[a, b, r, s, t, x, y]]
f[a, r, x], f[a, r, y], f[a, s, x], f[a, s, y], f[a, t, x],
f[a, t, y], f[b, r, x], f[b, r, y], f[b, s, x], f[b, s, y],
f[b, t, x], f[b, t, y]
f @@@ Tuples[a, b, r, s, t, x, y]
same result
p = a, b;
p1 = a, a, b, b;
Tuples[f[p, p1]]
f[a, a], f[a, a], f[a, b], f[a, b], f[b, a], f[b, a], f[b, b],
f[b, b]
$endgroup$
$begingroup$
Oops, might need to be changed whena,b,c
are matrices?
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Thanks! But when I use e.g. 2x2 matrices fora, b, c
,p=a,b,c
, thenTuples[KroneckerProduct[p, p]] // Dimensions
is262144, 9, 4
, and does not give 9 4-by-4 matrices...
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
@egwenesedai, doesKroneckerProduct@@@Tuples[p, 2]
work?
$endgroup$
– kglr
8 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
8 hours ago
add a comment |
$begingroup$
You could also use Tuples
as follows:
Tuples[f[a,b,c], 2]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a], f[c, b],
f[c, c]
If f
evaluates, and you want to do this for matrices, you could do:
p = RandomInteger[1, 3, 2, 2];
Block[KroneckerProduct, Tuples[KroneckerProduct @@ p, 2]] //Dimensions
9, 4, 4
$endgroup$
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/3.0/"u003ecc by-sa 3.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%2f200730%2fiterate-mapthread-with-matrices%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$
You can use Tuples
:
Tuples[f[a, b, c, a, b, c]]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a],
f[c, b], f[c, c]
f @@@ Tuples[a, b, c, 2]
same result
Tuples[f[a, b, r, s, t, x, y]]
f[a, r, x], f[a, r, y], f[a, s, x], f[a, s, y], f[a, t, x],
f[a, t, y], f[b, r, x], f[b, r, y], f[b, s, x], f[b, s, y],
f[b, t, x], f[b, t, y]
f @@@ Tuples[a, b, r, s, t, x, y]
same result
p = a, b;
p1 = a, a, b, b;
Tuples[f[p, p1]]
f[a, a], f[a, a], f[a, b], f[a, b], f[b, a], f[b, a], f[b, b],
f[b, b]
$endgroup$
$begingroup$
Oops, might need to be changed whena,b,c
are matrices?
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Thanks! But when I use e.g. 2x2 matrices fora, b, c
,p=a,b,c
, thenTuples[KroneckerProduct[p, p]] // Dimensions
is262144, 9, 4
, and does not give 9 4-by-4 matrices...
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
@egwenesedai, doesKroneckerProduct@@@Tuples[p, 2]
work?
$endgroup$
– kglr
8 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
8 hours ago
add a comment |
$begingroup$
You can use Tuples
:
Tuples[f[a, b, c, a, b, c]]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a],
f[c, b], f[c, c]
f @@@ Tuples[a, b, c, 2]
same result
Tuples[f[a, b, r, s, t, x, y]]
f[a, r, x], f[a, r, y], f[a, s, x], f[a, s, y], f[a, t, x],
f[a, t, y], f[b, r, x], f[b, r, y], f[b, s, x], f[b, s, y],
f[b, t, x], f[b, t, y]
f @@@ Tuples[a, b, r, s, t, x, y]
same result
p = a, b;
p1 = a, a, b, b;
Tuples[f[p, p1]]
f[a, a], f[a, a], f[a, b], f[a, b], f[b, a], f[b, a], f[b, b],
f[b, b]
$endgroup$
$begingroup$
Oops, might need to be changed whena,b,c
are matrices?
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Thanks! But when I use e.g. 2x2 matrices fora, b, c
,p=a,b,c
, thenTuples[KroneckerProduct[p, p]] // Dimensions
is262144, 9, 4
, and does not give 9 4-by-4 matrices...
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
@egwenesedai, doesKroneckerProduct@@@Tuples[p, 2]
work?
$endgroup$
– kglr
8 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
8 hours ago
add a comment |
$begingroup$
You can use Tuples
:
Tuples[f[a, b, c, a, b, c]]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a],
f[c, b], f[c, c]
f @@@ Tuples[a, b, c, 2]
same result
Tuples[f[a, b, r, s, t, x, y]]
f[a, r, x], f[a, r, y], f[a, s, x], f[a, s, y], f[a, t, x],
f[a, t, y], f[b, r, x], f[b, r, y], f[b, s, x], f[b, s, y],
f[b, t, x], f[b, t, y]
f @@@ Tuples[a, b, r, s, t, x, y]
same result
p = a, b;
p1 = a, a, b, b;
Tuples[f[p, p1]]
f[a, a], f[a, a], f[a, b], f[a, b], f[b, a], f[b, a], f[b, b],
f[b, b]
$endgroup$
You can use Tuples
:
Tuples[f[a, b, c, a, b, c]]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a],
f[c, b], f[c, c]
f @@@ Tuples[a, b, c, 2]
same result
Tuples[f[a, b, r, s, t, x, y]]
f[a, r, x], f[a, r, y], f[a, s, x], f[a, s, y], f[a, t, x],
f[a, t, y], f[b, r, x], f[b, r, y], f[b, s, x], f[b, s, y],
f[b, t, x], f[b, t, y]
f @@@ Tuples[a, b, r, s, t, x, y]
same result
p = a, b;
p1 = a, a, b, b;
Tuples[f[p, p1]]
f[a, a], f[a, a], f[a, b], f[a, b], f[b, a], f[b, a], f[b, b],
f[b, b]
edited 8 hours ago
answered 8 hours ago
kglrkglr
200k10 gold badges228 silver badges454 bronze badges
200k10 gold badges228 silver badges454 bronze badges
$begingroup$
Oops, might need to be changed whena,b,c
are matrices?
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Thanks! But when I use e.g. 2x2 matrices fora, b, c
,p=a,b,c
, thenTuples[KroneckerProduct[p, p]] // Dimensions
is262144, 9, 4
, and does not give 9 4-by-4 matrices...
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
@egwenesedai, doesKroneckerProduct@@@Tuples[p, 2]
work?
$endgroup$
– kglr
8 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
8 hours ago
add a comment |
$begingroup$
Oops, might need to be changed whena,b,c
are matrices?
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Thanks! But when I use e.g. 2x2 matrices fora, b, c
,p=a,b,c
, thenTuples[KroneckerProduct[p, p]] // Dimensions
is262144, 9, 4
, and does not give 9 4-by-4 matrices...
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
@egwenesedai, doesKroneckerProduct@@@Tuples[p, 2]
work?
$endgroup$
– kglr
8 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
8 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
8 hours ago
$begingroup$
Oops, might need to be changed when
a,b,c
are matrices?$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Oops, might need to be changed when
a,b,c
are matrices?$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Thanks! But when I use e.g. 2x2 matrices for
a, b, c
, p=a,b,c
, then Tuples[KroneckerProduct[p, p]] // Dimensions
is 262144, 9, 4
, and does not give 9 4-by-4 matrices...$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Thanks! But when I use e.g. 2x2 matrices for
a, b, c
, p=a,b,c
, then Tuples[KroneckerProduct[p, p]] // Dimensions
is 262144, 9, 4
, and does not give 9 4-by-4 matrices...$endgroup$
– egwene sedai
8 hours ago
2
2
$begingroup$
@egwenesedai, does
KroneckerProduct@@@Tuples[p, 2]
work?$endgroup$
– kglr
8 hours ago
$begingroup$
@egwenesedai, does
KroneckerProduct@@@Tuples[p, 2]
work?$endgroup$
– kglr
8 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
8 hours ago
2
2
$begingroup$
you can also use
Tuples[kp[p, p]] /. kp -> KroneckerProduct
(to prevent KroneckerProduct
evaluating before Tuples does its job)$endgroup$
– kglr
8 hours ago
$begingroup$
you can also use
Tuples[kp[p, p]] /. kp -> KroneckerProduct
(to prevent KroneckerProduct
evaluating before Tuples does its job)$endgroup$
– kglr
8 hours ago
add a comment |
$begingroup$
You could also use Tuples
as follows:
Tuples[f[a,b,c], 2]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a], f[c, b],
f[c, c]
If f
evaluates, and you want to do this for matrices, you could do:
p = RandomInteger[1, 3, 2, 2];
Block[KroneckerProduct, Tuples[KroneckerProduct @@ p, 2]] //Dimensions
9, 4, 4
$endgroup$
add a comment |
$begingroup$
You could also use Tuples
as follows:
Tuples[f[a,b,c], 2]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a], f[c, b],
f[c, c]
If f
evaluates, and you want to do this for matrices, you could do:
p = RandomInteger[1, 3, 2, 2];
Block[KroneckerProduct, Tuples[KroneckerProduct @@ p, 2]] //Dimensions
9, 4, 4
$endgroup$
add a comment |
$begingroup$
You could also use Tuples
as follows:
Tuples[f[a,b,c], 2]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a], f[c, b],
f[c, c]
If f
evaluates, and you want to do this for matrices, you could do:
p = RandomInteger[1, 3, 2, 2];
Block[KroneckerProduct, Tuples[KroneckerProduct @@ p, 2]] //Dimensions
9, 4, 4
$endgroup$
You could also use Tuples
as follows:
Tuples[f[a,b,c], 2]
f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a], f[c, b],
f[c, c]
If f
evaluates, and you want to do this for matrices, you could do:
p = RandomInteger[1, 3, 2, 2];
Block[KroneckerProduct, Tuples[KroneckerProduct @@ p, 2]] //Dimensions
9, 4, 4
answered 7 hours ago
Carl WollCarl Woll
84.9k3 gold badges109 silver badges220 bronze badges
84.9k3 gold badges109 silver badges220 bronze badges
add a comment |
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%2f200730%2fiterate-mapthread-with-matrices%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
$begingroup$
What about
Outer
?$endgroup$
– chuy
8 hours ago
$begingroup$
@chuy Thanks! but
Outer
may not be a good fit whena,b ...
are matrices.$endgroup$
– egwene sedai
8 hours ago
$begingroup$
Just realized that
p = a, b, c; Distribute[f[p, p], List]
works$endgroup$
– egwene sedai
8 hours ago