Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #16101 > unrolled thread
| Started by | Alexei Boulbitch <Alexei.Boulbitch@iee.lu> |
|---|---|
| First post | 2013-10-22 04:54 +0000 |
| Last post | 2013-10-24 03:33 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: Exercise of Programming with Mathematica Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2013-10-22 04:54 +0000
Re: Exercise of Programming with Mathematica mike.honeychurch@gmail.com - 2013-10-24 03:33 +0000
| From | Alexei Boulbitch <Alexei.Boulbitch@iee.lu> |
|---|---|
| Date | 2013-10-22 04:54 +0000 |
| Subject | Re: Exercise of Programming with Mathematica |
| Message-ID | <l450e6$shr$1@smc.vnet.net> |
Here is a rule designed to switch the order of each pair of expressions in a list. It works fine on the
first example, but fails on the second.
In[1]:= {{a, b}, {c, d}, {e, f}}/.{x_, y_} :> {y, x}
Out[1]= {{b, a}, {d, c}, {f, e}}
In[2]:= {{a, b}, {c, d}}/.{x_, y_} :> {y, x}
Out[2]= {{c, d}, {a, b}}
Explain what has gone wrong and rewrite this rule to correct the situation, that is, so that the second
example returns {{b, a}, {d, c}}
My solution is the most stupid one.
{{a, b}, {c, d}}/.{{x_, y_}, {w_, t_}} :> {{y, x}, {t, w}}
May I ask any elegant solutions?
Thanks.
You are right, this is a nice exercise in Mathematica. Considering it as such, I would like to offer few solutions that were not yet mentioned. Some of them are not shorter. What's the matter, is it not the exercise?
Here are two list to try with:
lst1 = {{a, b}, {c, d}, {e, f}};
lst2 = {{a, b}, {c, d}};
1. Mapping Permute onto a list:
Map[Permute[#, Cycles[{{2, 1}}]] &, lst1]
Map[Permute[#, Cycles[{{2, 1}}]] &, lst2]
{{b, a}, {d, c}, {f, e}}
{{b, a}, {d, c}}
2. Using Transpose + Rule:
Transpose[lst] /. {x_, y_} -> {y, x} // Transpose
{{b, a}, {d, c}}
3. Transpose and hands:
{Transpose[lst1][[2]], Transpose[lst1][[1]]} // Thread
{{b, a}, {d, c}}
Or {Transpose[lst1][[2]], Transpose[lst1][[1]]} // Transpose
4. Mapping a rule onto a list:
Map[ReplaceAll[#, {x_, y_} -> {y, x}] &, lst1]
Map[ReplaceAll[#, {x_, y_} -> {y, x}] &, lst2]
{{b, a}, {d, c}, {f, e}}
{{b, a}, {d, c}}
5. Using Thread+Rule+Thread or Transpose
Transpose[Thread[lst1] /. {x_, y_} -> {y, x}]
{{b, a}, {d, c}, {f, e}}
Thread[Thread[lst1] /. {x_, y_} -> {y, x}]
{{b, a}, {d, c}, {f, e}}
Have fun, Alexei
Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG
Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44
e-mail: alexei.boulbitch@iee.lu
[toc] | [next] | [standalone]
| From | mike.honeychurch@gmail.com |
|---|---|
| Date | 2013-10-24 03:33 +0000 |
| Message-ID | <l4a4en$aff$1@smc.vnet.net> |
| In reply to | #16101 |
On Tuesday, October 22, 2013 3:54:30 PM UTC+11, Alexei Boulbitch wrote:
> Here is a rule designed to switch the order of each pair of expressions in a list. It works fine on the
>
> first example, but fails on the second.
>
> In[1]:= {{a, b}, {c, d}, {e, f}}/.{x_, y_} :> {y, x}
>
> Out[1]= {{b, a}, {d, c}, {f, e}}
>
> In[2]:= {{a, b}, {c, d}}/.{x_, y_} :> {y, x}
>
> Out[2]= {{c, d}, {a, b}}
>
> Explain what has gone wrong and rewrite this rule to correct the situation, that is, so that the second
>
> example returns {{b, a}, {d, c}}
>
>
>
> My solution is the most stupid one.
>
>
>
> {{a, b}, {c, d}}/.{{x_, y_}, {w_, t_}} :> {{y, x}, {t, w}}
>
>
>
> May I ask any elegant solutions?
>
>
>
> Thanks.
>
>
>
>
>
> You are right, this is a nice exercise in Mathematica. Considering it as such, I would like to offer few solutions that were not yet mentioned. Some of them are not shorter. What's the matter, is it not the exercise?
>
>
>
> Here are two list to try with:
>
>
>
> lst1 = {{a, b}, {c, d}, {e, f}};
>
> lst2 = {{a, b}, {c, d}};
>
>
>
> 1. Mapping Permute onto a list:
>
>
>
> Map[Permute[#, Cycles[{{2, 1}}]] &, lst1]
>
> Map[Permute[#, Cycles[{{2, 1}}]] &, lst2]
>
>
>
> {{b, a}, {d, c}, {f, e}}
>
>
>
> {{b, a}, {d, c}}
>
>
>
> 2. Using Transpose + Rule:
>
>
>
> Transpose[lst] /. {x_, y_} -> {y, x} // Transpose
>
>
>
> {{b, a}, {d, c}}
>
>
>
> 3. Transpose and hands:
>
>
>
> {Transpose[lst1][[2]], Transpose[lst1][[1]]} // Thread
>
>
>
> {{b, a}, {d, c}}
>
>
>
> Or {Transpose[lst1][[2]], Transpose[lst1][[1]]} // Transpose
>
>
>
> 4. Mapping a rule onto a list:
>
>
>
> Map[ReplaceAll[#, {x_, y_} -> {y, x}] &, lst1]
>
> Map[ReplaceAll[#, {x_, y_} -> {y, x}] &, lst2]
>
>
>
> {{b, a}, {d, c}, {f, e}}
>
>
>
> {{b, a}, {d, c}}
>
>
>
> 5. Using Thread+Rule+Thread or Transpose
>
>
>
> Transpose[Thread[lst1] /. {x_, y_} -> {y, x}]
>
>
>
> {{b, a}, {d, c}, {f, e}}
>
>
>
> Thread[Thread[lst1] /. {x_, y_} -> {y, x}]
>
>
>
> {{b, a}, {d, c}, {f, e}}
>
>
>
> Have fun, Alexei
>
>
>
> Alexei BOULBITCH, Dr., habil.
>
> IEE S.A.
>
> ZAE Weiergewan,
>
> 11, rue Edmond Reuter,
>
> L-5326 Contern, LUXEMBOURG
>
>
>
> Office phone : +352-2454-2566
>
> Office fax: +352-2454-3566
>
> mobile phone: +49 151 52 40 66 44
>
>
>
> e-mail: alexei.boulbitch@iee.lu
While there are many ways to do this I usually use Part
lst1 = {{a, b}, {c, d}, {e, f}};
lst1[[All, {2, 1}]]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web