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


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

Replace

Started bySam Korn <sam@kornfamily.com>
First post2011-08-25 11:12 +0000
Last post2011-08-28 08:13 +0000
Articles 12 — 7 participants

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


Contents

  Replace Sam Korn <sam@kornfamily.com> - 2011-08-25 11:12 +0000
    Re: Replace Ray Koopman <koopman@sfu.ca> - 2011-08-26 09:27 +0000
    Re: Replace "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-08-26 09:30 +0000
    Re: Replace Peter Breitfeld <phbrf@t-online.de> - 2011-08-26 09:31 +0000
    Re: Replace Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-08-26 09:33 +0000
      Re: Replace Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-08-27 12:22 +0000
        Re: Replace Peter Pein <petsie@dordos.net> - 2011-08-28 08:09 +0000
      Re: Replace Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-08-29 07:49 +0000
    Re: Replace Peter Pein <petsie@dordos.net> - 2011-08-26 09:35 +0000
    Re: Replace Murray Eisenberg <murray@math.umass.edu> - 2011-08-27 12:25 +0000
      Re: Replace Ray Koopman <koopman@sfu.ca> - 2011-08-28 08:06 +0000
    Re: Replace Murray Eisenberg <murray@math.umass.edu> - 2011-08-28 08:13 +0000

#4566 — Replace

FromSam Korn <sam@kornfamily.com>
Date2011-08-25 11:12 +0000
SubjectReplace
Message-ID<j35am1$og8$1@smc.vnet.net>
Hi, another stupid question. I have a list, and each element in the list is
a sublist with a pair of integers. How do I replace each element along the
lines of "{5,6}" with "(5|6)"?

I know it's a stupid question, and there's probably something really obvious
I'm missing, but I'm really new to Mathematica.

Thanks!
-Sam

[toc] | [next] | [standalone]


#4574

FromRay Koopman <koopman@sfu.ca>
Date2011-08-26 09:27 +0000
Message-ID<j37oul$4ij$1@smc.vnet.net>
In reply to#4566
On Aug 25, 4:12 am, Sam Korn <s...@kornfamily.com> wrote:
> Hi, another stupid question. I have a list, and each element in the list is
> a sublist with a pair of integers. How do I replace each element along the
> lines of "{5,6}" with "(5|6)"?
>
> I know it's a stupid question, and there's probably something really obvious
> I'm missing, but I'm really new to Mathematica.
>
> Thanks!
> -Sam

Alternatives @@@ data

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


#4586

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-08-26 09:30 +0000
Message-ID<j37p4d$4jv$1@smc.vnet.net>
In reply to#4566
On Thu, 25 Aug 2011 12:12:01 +0100, Sam Korn <sam@kornfamily.com> wrote:

> Hi, another stupid question. I have a list, and each element in the list  
> is
> a sublist with a pair of integers. How do I replace each element along  
> the
> lines of "{5,6}" with "(5|6)"?
>
> I know it's a stupid question, and there's probably something really  
> obvious
> I'm missing, but I'm really new to Mathematica.
>
> Thanks!
> -Sam

In[1] :=
list = RandomInteger[{-5, 5}, {10, 2}]

Out[1] =
{{-4, -4}, {-1, -3}, {4, 4}, {0, -4}, {-5, 3}, {-5, 4}, {-1, -2}, {5, -2},  
{1, -1}, {-4, 1}}

In[2] :=
list /. {x_, y_} :> (x | y)

Out[2] =
{-4 | -4, -1 | -3, 4 | 4, 0 | -4, -5 | 3, -5 | 4, -1 | -2, 5 | -2, 1 | -1,  
-4 | 1}

("/." is a sigil used in infix notation to denote the function ReplaceAll.  
":>" is RuleDelayed in the same sense.)

It's unclear to me if the above is what you intended; (x | y) is  
understood by Mathematica as a pattern which matches either x or y. If you  
had wanted something that simply displays as such without having any  
particular meaning you may instead use

In[3] :=
list /. {x_, y_} :> StringForm["(`1`|`2`)", x, y]

Out[3] =
{(-4|-4),(-1|-3),(4|4),(0|-4),(-5|3),(-5|4),(-1|-2),(5|-2),(1|-1),(-4|1)}

or perhaps

In[4] :=
Format@customizedElement[x_, y_] := StringForm["(`1`|`2`)", x, y];
customizedElement @@@ list

Out[4] =
{(-4|-4),(-1|-3),(4|4),(0|-4),(-5|3),(-5|4),(-1|-2),(5|-2),(1|-1),(-4|1)}

which has the advantage of allowing other operations to be defined on  
customizedElement if desired.

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


#4587

FromPeter Breitfeld <phbrf@t-online.de>
Date2011-08-26 09:31 +0000
Message-ID<j37p5b$4k8$1@smc.vnet.net>
In reply to#4566
One possibility might be:

barF[{x_,y_}]:=Row[{"(", x, "|", y, ")"}]

{5,6}//barF    gives (5|6)

In:= barF/@{{5, 6}, {5, 8}, {-3, x}, {y, 1/2}}
Out= {(5|6),(5|8),(-3|x),(y|1/2)}

But you know, that you can use this for display only.

Sam Korn wrote:

> Hi, another stupid question. I have a list, and each element in the list is
> a sublist with a pair of integers. How do I replace each element along the
> lines of "{5,6}" with "(5|6)"?
>
> I know it's a stupid question, and there's probably something really obvious
> I'm missing, but I'm really new to Mathematica.
>
> Thanks!
> -Sam

-- 
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

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


#4591

FromArmand Tamzarian <mike.honeychurch@gmail.com>
Date2011-08-26 09:33 +0000
Message-ID<j37p96$4lk$1@smc.vnet.net>
In reply to#4566
On Aug 25, 9:12 pm, Sam Korn <s...@kornfamily.com> wrote:
> Hi, another stupid question. I have a list, and each element in the list is
> a sublist with a pair of integers. How do I replace each element along the
> lines of "{5,6}" with "(5|6)"?
>
> I know it's a stupid question, and there's probably something really obvious
> I'm missing, but I'm really new to Mathematica.
>
> Thanks!
> -Sam

Here are a couple of ways:

tmp = RandomInteger[{1, 20}, {20, 2}]

First using Replace

tmp /. {x_, y_} :> x/y

For many this is a more intuitive way of doing it but will be
(relatively) slow for very large lists. This is a faster way of doing
it:

tmp[[All, 1]]/tmp[[All, 2]]


Mike

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


#4611

FromArmand Tamzarian <mike.honeychurch@gmail.com>
Date2011-08-27 12:22 +0000
Message-ID<j3anhr$hg0$1@smc.vnet.net>
In reply to#4591
On Aug 26, 7:33 pm, Armand Tamzarian <mike.honeychu...@gmail.com>
wrote:
> On Aug 25, 9:12 pm, Sam Korn <s...@kornfamily.com> wrote:
>
> > Hi, another stupid question. I have a list, and each element in the list is
> > a sublist with a pair of integers. How do I replace each element along the
> > lines of "{5,6}" with "(5|6)"?
>
> > I know it's a stupid question, and there's probably something really obvious
> > I'm missing, but I'm really new to Mathematica.
>
> > Thanks!
> > -Sam
>
> Here are a couple of ways:
>
> tmp = RandomInteger[{1, 20}, {20, 2}]
>
> First using Replace
>
> tmp /. {x_, y_} :> x/y
>
> For many this is a more intuitive way of doing it but will be
> (relatively) slow for very large lists. This is a faster way of doing
> it:
>
> tmp[[All, 1]]/tmp[[All, 2]]
>
> Mike

Doh! you wanted "|" not "/"

It still looks like using Part will give the fastest solution (on my
system at least, 8.0.1 OS X 10.6.8)

tmp = RandomInteger[{1, 20}, {20000, 2}];

Timing[tmp /. {x_, y_} :> x | y;]

Timing[Alternatives @@@ tmp;]

Timing[Thread[tmp[[All, 1]] | tmp[[All, 2]]];]

Mike

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


#4628

FromPeter Pein <petsie@dordos.net>
Date2011-08-28 08:09 +0000
Message-ID<j3ct3f$5m7$1@smc.vnet.net>
In reply to#4611
Am 27.08.2011 14:22, schrieb Armand Tamzarian:
...
> Doh! you wanted "|" not "/"
> 
> It still looks like using Part will give the fastest solution (on my
> system at least, 8.0.1 OS X 10.6.8)
> 
> tmp = RandomInteger[{1, 20}, {20000, 2}];
> 
> Timing[tmp /. {x_, y_} :> x | y;]
> 
> Timing[Alternatives @@@ tmp;]
> 
> Timing[Thread[tmp[[All, 1]] | tmp[[All, 2]]];]
> 
> Mike
> 

Transpose seems to be even a bit faster than Part:

In[1]:= tmp = RandomInteger[{1, 20}, {2000000, 2}];
{t, r} = Transpose[{
           Timing[tmp /. {x_, y_} :> x | y],
           Timing[Apply[Alternatives, tmp, {1}]],
           Timing[Thread[tmp[[All,1]] | tmp[[All,2]]]],
           Timing[Thread[Alternatives @@ Transpose[tmp]]]}
        ];
t
SameQ @@ r

Out[3]= {1.56,0.764,0.624,0.531}
Out[4]= True

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


#4645

FromArmand Tamzarian <mike.honeychurch@gmail.com>
Date2011-08-29 07:49 +0000
Message-ID<j3fgb3$fbe$1@smc.vnet.net>
In reply to#4591
On Aug 28, 6:09 pm, Peter Pein <pet...@dordos.net> wrote:
> Am 27.08.2011 14:22, schrieb Armand Tamzarian:
> ...
>
> > Doh! you wanted "|" not "/"
>
> > It still looks like using Part will give the fastest solution (on my
> > system at least, 8.0.1 OS X 10.6.8)
>
> > tmp = RandomInteger[{1, 20}, {20000, 2}];
>
> > Timing[tmp /. {x_, y_} :> x | y;]
>
> > Timing[Alternatives @@@ tmp;]
>
> > Timing[Thread[tmp[[All, 1]] | tmp[[All, 2]]];]
>
> > Mike
>
> Transpose seems to be even a bit faster than Part:
>
> In[1]:= tmp = RandomInteger[{1, 20}, {2000000, 2}];
> {t, r} = Transpose[{
>            Timing[tmp /. {x_, y_} :> x | y],
>            Timing[Apply[Alternatives, tmp, {1}]],
>            Timing[Thread[tmp[[All,1]] | tmp[[All,2]]]],
>            Timing[Thread[Alternatives @@ Transpose[tmp]]]}
>         ];
> t
> SameQ @@ r
>
> Out[3]= {1.56,0.764,0.624,0.531}
> Out[4]= True

After I posted my solution I realized that the timing would most
likely differ if the list contained mixed types, i.e. not packed.

Apply becomes the fastest method in that case (incl. Peter's
solution):

tmp = RandomInteger[{1, 20}, {200000, 2}];
tmp[[1, 1]] = 1.0;
{t, r} = Transpose[{Timing[tmp /. {x_, y_} :> x | y],
    Timing[Apply[Alternatives, tmp, {1}]],
    Timing[Thread[tmp[[All, 1]] | tmp[[All, 2]]]],
    Timing[Thread[Alternatives @@ Transpose[tmp]]]}];
t
SameQ @@ r

Mike

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


#4596

FromPeter Pein <petsie@dordos.net>
Date2011-08-26 09:35 +0000
Message-ID<j37pdu$4n2$1@smc.vnet.net>
In reply to#4566
Am 25.08.2011 13:12, schrieb Sam Korn:
> Hi, another stupid question. I have a list, and each element in the list is
> a sublist with a pair of integers. How do I replace each element along the
> lines of "{5,6}" with "(5|6)"?
>
> I know it's a stupid question, and there's probably something really obvious
> I'm missing, but I'm really new to Mathematica.
>
> Thanks!
> -Sam

Hi Sam,

  apply the function {Alternatives[##]}&

In[1]:= lst = RandomInteger[9, {5, 2}]
Out[1]= {{0, 5}, {5, 6}, {7, 9}, {8, 8}, {9, 8}}

In[2]:= Apply[{Alternatives[##1]} & , lst, {1}]
Out[2]= {{0 | 5}, {5 | 6}, {7 | 9}, {8 | 8}, {9 | 8}}


Peter

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


#4617

FromMurray Eisenberg <murray@math.umass.edu>
Date2011-08-27 12:25 +0000
Message-ID<j3annj$hhc$1@smc.vnet.net>
In reply to#4566
The expression Alternatives @@@ data does not seem to give what the OP 
seems to have asked for, which is to have each pair of integers not only 
separated by the Alternatives symbol, but also to have each such
m | N surrounded by parentheses. (Which is why in my previously posted 
reply to the OP I asked exactly what sort of result he really wanted.)


On 8/26/11 5:23 AM, Ray Koopman wrote:
> On Aug 25, 4:12 am, Sam Korn<s...@kornfamily.com>  wrote:
>> Hi, another stupid question. I have a list, and each element in the list is
>> a sublist with a pair of integers. How do I replace each element along the
>> lines of "{5,6}" with "(5|6)"?
>>
>> I know it's a stupid question, and there's probably something really obvious
>> I'm missing, but I'm really new to Mathematica.
>>
>> Thanks!
>> -Sam
>
> Alternatives @@@ data
>

-- 
Murray Eisenberg                     murray@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

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


#4624

FromRay Koopman <koopman@sfu.ca>
Date2011-08-28 08:06 +0000
Message-ID<j3csum$5l7$1@smc.vnet.net>
In reply to#4617
On Aug 27, 5:25 am, Murray Eisenberg <mur...@math.umass.edu> wrote:
> The expression Alternatives @@@ data does not seem to give what the OP
> seems to have asked for, which is to have each pair of integers not
> only separated by the Alternatives symbol, but also to have each such
> m | N surrounded by parentheses. (Which is why in my previously posted
> reply to the OP I asked exactly what sort of result he really wanted.)

I thought about that but decided that "along the lines of" was the
operative phrase, since Mathematica would ignore the parentheses in
input.

>
> On 8/26/11 5:23 AM, Ray Koopman wrote:
>> On Aug 25, 4:12 am, Sam Korn<s...@kornfamily.com>  wrote:
>>> Hi, another stupid question. I have a list, and each element in
>>> the list is a sublist with a pair of integers. How do I replace
>>> each element along the lines of "{5,6}" with "(5|6)"?
>>>
>>> I know it's a stupid question, and there's probably something
>>> really obvious I'm missing, but I'm really new to Mathematica.
>>>
>>> Thanks!
>>> -Sam
>>
>> Alternatives @@@ data

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


#4637

FromMurray Eisenberg <murray@math.umass.edu>
Date2011-08-28 08:13 +0000
Message-ID<j3ctc2$5nu$1@smc.vnet.net>
In reply to#4566
The expression Alternatives @@@ data does not seem to give what the OP 
seems to have asked for, which is to have each pair of integers not only 
separated by the Alternatives symbol, but also to have each such
m | N surrounded by parentheses. (Which is why in my previously posted 
reply to the OP I asked exactly what sort of result he really wanted.)


On 8/26/11 5:23 AM, Ray Koopman wrote:
> On Aug 25, 4:12 am, Sam Korn<s...@kornfamily.com>  wrote:
>> Hi, another stupid question. I have a list, and each element in the list is
>> a sublist with a pair of integers. How do I replace each element along the
>> lines of "{5,6}" with "(5|6)"?
>>
>> I know it's a stupid question, and there's probably something really obvious
>> I'm missing, but I'm really new to Mathematica.
>>
>> Thanks!
>> -Sam
>
> Alternatives @@@ data
>

-- 
Murray Eisenberg                     murray@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

[toc] | [prev] | [standalone]


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


csiph-web