Mapping a function f[xi_,xj_] over a list {x1, …, xn} with the i < j restriction
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
.everyonelovesstackoverflow{position:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;}
$begingroup$
I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:
$$
{...f[x_i,x_j]...}text{ with } i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal, n]
{0.547984, 0.601967, 0.000917483, 0.738287}
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]
{f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}
(in real situation f
is defined f[xi_,xj_]:= xi-xj
for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
$endgroup$
add a comment
|
$begingroup$
I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:
$$
{...f[x_i,x_j]...}text{ with } i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal, n]
{0.547984, 0.601967, 0.000917483, 0.738287}
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]
{f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}
(in real situation f
is defined f[xi_,xj_]:= xi-xj
for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
$endgroup$
add a comment
|
$begingroup$
I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:
$$
{...f[x_i,x_j]...}text{ with } i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal, n]
{0.547984, 0.601967, 0.000917483, 0.738287}
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]
{f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}
(in real situation f
is defined f[xi_,xj_]:= xi-xj
for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
$endgroup$
I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:
$$
{...f[x_i,x_j]...}text{ with } i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal, n]
{0.547984, 0.601967, 0.000917483, 0.738287}
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]
{f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}
(in real situation f
is defined f[xi_,xj_]:= xi-xj
for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
list-manipulation programming
edited May 28 at 11:40
Picaud Vincent
asked May 28 at 11:14
Picaud VincentPicaud Vincent
1,5827 silver badges18 bronze badges
1,5827 silver badges18 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
$begingroup$
Subsets[f @@ x, {2}]
{f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}
You can also use
f @@@ Subsets[x, {2}]
Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]
$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/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%2f199243%2fmapping-a-function-fxi-xj-over-a-list-x1-xn-with-the-i-j-restrict%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Subsets[f @@ x, {2}]
{f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}
You can also use
f @@@ Subsets[x, {2}]
Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]
$endgroup$
add a comment
|
$begingroup$
Subsets[f @@ x, {2}]
{f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}
You can also use
f @@@ Subsets[x, {2}]
Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]
$endgroup$
add a comment
|
$begingroup$
Subsets[f @@ x, {2}]
{f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}
You can also use
f @@@ Subsets[x, {2}]
Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]
$endgroup$
Subsets[f @@ x, {2}]
{f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}
You can also use
f @@@ Subsets[x, {2}]
Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]
edited May 28 at 11:37
answered May 28 at 11:32
kglrkglr
221k10 gold badges251 silver badges508 bronze badges
221k10 gold badges251 silver badges508 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%2f199243%2fmapping-a-function-fxi-xj-over-a-list-x1-xn-with-the-i-j-restrict%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