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?
How do I respond to requests for a "guarantee" not to leave after a few months?
How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?
Why doesn't a marching band have strings?
Should I prioritize my 401(k) over my student loans?
Is it possible writing coservation of relativistic energy in this naive way?
Can we put equal sign after aggregate functions in sql?
Should developer taking test phones home or put in office?
Wifi dongle speed is slower than advertised
Proving a certain type of topology is discrete without the axiom of choice
Where do I get advice and guidance from in my PhD if my supervisor is not an expert in the field I am working on?
What does "play with your toy’s toys" mean?
Is a single radon-daughter atom in air a solid?
How to get cool night-vision without lame drawbacks?
Vanishing of certain coefficients coming from Coxeter groups
How do I turn off a repeating trade?
Can White Castle?
expiry or manufactured date?
Why do textbooks often include the solutions to odd or even numbered problems but not both?
Can humans ever directly see a few photons at a time? Can a human see a single photon?
Can the negators "jamais, rien, personne, plus, ni, aucun" be used in a single sentence?
C-152 carb heat on before landing in hot weather?
Iterate MapThread with matrices
Fill NAs in R with zero if the next valid data point is more than 2 intervals away
If I wouldn't want to read the story, is writing it still a good idea?
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
7 hours ago
$begingroup$
@chuy Thanks! butOuter
may not be a good fit whena,b ...
are matrices.
$endgroup$
– egwene sedai
7 hours ago
$begingroup$
Just realized thatp = a, b, c; Distribute[f[p, p], List]
works
$endgroup$
– egwene sedai
7 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 7 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
7 hours ago
$begingroup$
@chuy Thanks! butOuter
may not be a good fit whena,b ...
are matrices.
$endgroup$
– egwene sedai
7 hours ago
$begingroup$
Just realized thatp = a, b, c; Distribute[f[p, p], List]
works
$endgroup$
– egwene sedai
7 hours ago
add a comment |
$begingroup$
What aboutOuter
?
$endgroup$
– chuy
7 hours ago
$begingroup$
@chuy Thanks! butOuter
may not be a good fit whena,b ...
are matrices.
$endgroup$
– egwene sedai
7 hours ago
$begingroup$
Just realized thatp = a, b, c; Distribute[f[p, p], List]
works
$endgroup$
– egwene sedai
7 hours ago
$begingroup$
What about
Outer
?$endgroup$
– chuy
7 hours ago
$begingroup$
What about
Outer
?$endgroup$
– chuy
7 hours ago
$begingroup$
@chuy Thanks! but
Outer
may not be a good fit when a,b ...
are matrices.$endgroup$
– egwene sedai
7 hours ago
$begingroup$
@chuy Thanks! but
Outer
may not be a good fit when a,b ...
are matrices.$endgroup$
– egwene sedai
7 hours ago
$begingroup$
Just realized that
p = a, b, c; Distribute[f[p, p], List]
works$endgroup$
– egwene sedai
7 hours ago
$begingroup$
Just realized that
p = a, b, c; Distribute[f[p, p], List]
works$endgroup$
– egwene sedai
7 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
7 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
7 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
7 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
7 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
7 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
7 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
7 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
7 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
7 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
7 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
7 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
7 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
7 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
7 hours ago
2
$begingroup$
you can also useTuples[kp[p, p]] /. kp -> KroneckerProduct
(to preventKroneckerProduct
evaluating before Tuples does its job)
$endgroup$
– kglr
7 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
7 hours ago
$begingroup$
@egwenesedai, does
KroneckerProduct@@@Tuples[p, 2]
work?$endgroup$
– kglr
7 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
7 hours ago
$begingroup$
Works, thanks a lot!
$endgroup$
– egwene sedai
7 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
7 hours ago
$begingroup$
you can also use
Tuples[kp[p, p]] /. kp -> KroneckerProduct
(to prevent KroneckerProduct
evaluating before Tuples does its job)$endgroup$
– kglr
7 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 6 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
7 hours ago
$begingroup$
@chuy Thanks! but
Outer
may not be a good fit whena,b ...
are matrices.$endgroup$
– egwene sedai
7 hours ago
$begingroup$
Just realized that
p = a, b, c; Distribute[f[p, p], List]
works$endgroup$
– egwene sedai
7 hours ago