Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8371 > unrolled thread

Getting stuck with finding an elegant solution without global variables

Started byFredob <fredrik.doberl@gmail.com>
First post2012-02-07 09:08 +0000
Last post2012-02-08 10:36 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Getting stuck with finding an elegant solution without global variables Fredob <fredrik.doberl@gmail.com> - 2012-02-07 09:08 +0000
    Re: Getting stuck with finding an elegant solution without global variables Ray Koopman <koopman@sfu.ca> - 2012-02-08 10:30 +0000
    Re: Getting stuck with finding an elegant solution without global variables David Reiss <dbreiss@gmail.com> - 2012-02-08 10:36 +0000

#8371 — Getting stuck with finding an elegant solution without global variables

FromFredob <fredrik.doberl@gmail.com>
Date2012-02-07 09:08 +0000
SubjectGetting stuck with finding an elegant solution without global variables
Message-ID<jgqpmc$n0p$1@smc.vnet.net>
Hi,

I am trying to find an elegant solution, i.e. without a global
variable to the following problem:

Given a list, e.g. {1,2,3,4}, create a new list where each element is
a function of the "i th" element and the rest of the list, e.g.

Branch[Elem_, List_] := Elem + Cases[List, Except[Elem]]

The the result would be {{3, 4, 5}, {3, 5, 6}, {4, 5, 7}, {5, 6, 7}}

I have used Map[f, List] but then I have to use a global variable in f
to access the list itself, e.g. BranchG[Elem_] := Elem + Cases[List,
Except[Elem]]

[toc] | [next] | [standalone]


#8391

FromRay Koopman <koopman@sfu.ca>
Date2012-02-08 10:30 +0000
Message-ID<jgtiru$6ap$1@smc.vnet.net>
In reply to#8371
On Feb 7, 1:08 am, Fredob <fredrik.dob...@gmail.com> wrote:
> Hi,
>
> I am trying to find an elegant solution, i.e. without a global
> variable to the following problem:
>
> Given a list, e.g. {1,2,3,4}, create a new list where each element is
> a function of the "i th" element and the rest of the list, e.g.
>
> Branch[Elem_, List_] := Elem + Cases[List, Except[Elem]]
>
> The the result would be {{3, 4, 5}, {3, 5, 6}, {4, 5, 7}, {5, 6, 7}}
>
> I have used Map[f, List] but then I have to use a global variable in f
> to access the list itself, e.g. BranchG[Elem_] := Elem + Cases[List,
> Except[Elem]]

In[1]:= f[a_List] := a + Array[Delete[a,#]&,Length@a]

In[2]:= f@{1,2,3,4}

Out[2]= {{3,4,5},{3,5,6},{4,5,7},{5,6,7}}

[toc] | [prev] | [next] | [standalone]


#8403

FromDavid Reiss <dbreiss@gmail.com>
Date2012-02-08 10:36 +0000
Message-ID<jgtj7e$6fh$1@smc.vnet.net>
In reply to#8371
Perhaps something like this using MapIndexed?

In[1]:= Function[x,
  MapIndexed[ff[#1, ReplacePart[x, #2[[1]] -> Sequence[]]] &, x]][{a,
  b, c, d}]

Out[1]= {ff[a, {b, c, d}], ff[b, {a, c, d}], ff[c, {a, b, d}],
 ff[d, {a, b, c}]}

--David


On Feb 7, 4:08 am, Fredob <fredrik.dob...@gmail.com> wrote:
> Hi,
>
> I am trying to find an elegant solution, i.e. without a global
> variable to the following problem:
>
> Given a list, e.g. {1,2,3,4}, create a new list where each element is
> a function of the "i th" element and the rest of the list, e.g.
>
> Branch[Elem_, List_] := Elem + Cases[List, Except[Elem]]
>
> The the result would be {{3, 4, 5}, {3, 5, 6}, {4, 5, 7}, {5, 6, 7}}
>
> I have used Map[f, List] but then I have to use a global variable in f
> to access the list itself, e.g. BranchG[Elem_] := Elem + Cases[List,
> Except[Elem]]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web