MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Problems solved - Using Map or Thread over selected parts of nested lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95949] Problems solved - Using Map or Thread over selected parts of nested lists
  • From: "Paul Ellsmore" <paul.ellsmore at nanion.co.uk>
  • Date: Fri, 30 Jan 2009 05:46:33 -0500 (EST)

Thanks to everyone who responded - I now have several good solutions to my
two problems, and understand Mathematica a bit better to boot!

Cheers,

Paul.

  _____  

From: Paul Ellsmore [mailto:paul.ellsmore at nanion.co.uk] 
Sent: 27 January 2009 16:20
To: 'mathgroup at smc.vnet.net'
Subject: [mg95949] Using Map or Thread over selected parts of nested lists

Hi,

I am currently working with a trial version of Mathematica 7, particularly
with an interest in building a graphical user interface. I don't have a
great familiarity with Mathematica, but I am trying hard to get to grips
with a Mathenatica-style of thinking, and the way lists can be manipulated
in particular. I have two questions, answers to which would help me a lot:

 

1)      My GUI allows the user to select some data files to work with. I use
the code:

 

source = SystemDialogInput[FileOpen]

 

This returns the full path for each file selected by the user - very
convenient and user-friendly. My problem is that "source" is returned as a
string (ie an atom) if one file is selected, but a nested list if multiple
files are selected. I have similar instances in many parts of my program
where the user might be working with one list of data or multiple lists, and
I think the same issue will arise. So I would like to know if there is a way
of forcing (in this case) "source" to be returned as a nestedlist type, even
if the length of that list is one?

 

2)      I have data in the format:

 

List1={{header,header},{header,header},{header,header},{frequency1,
complexnumber1},{frequency2, complexnumber2},{frequency3, complexnumber4}.}

 

On very many occasions I want to deal with :

 

List2={{frequency1, realpart1},{frequency2, realpart2},{frequency3,
realpart4}.}

 

Or some other trivial transform of complexnumbern. I have been through the
VirtualBook, and Documentation centre, and tried various forms of Map and
Thread, but I cannot find a way of "mapping" or "threading" a function like
Re[expr] over one element of part of a list, as in:

 

List2={{frequency1, Re[complexnumber1]},{frequency2,
Re[complexnumber2]},{frequency3, Re[complexnumber4]}.}

 

If someone could give me a pointer to the correct syntax, I would be very
grateful.

 

At present, I am getting the results I want by using Ifs and Do loops, to
deconstruct and reconstruct my lists, but I feel that is the wrong way to
approach things - I want to use Mathematica's strengths, not force it to use
a C type approach, just because that's what I am comfortable with.

 

Any suggestions gratefully received,

 

 

Cheers in advance,

 

Paul.

 

 

 

Dr. Paul A. Ellsmore

 

Nanion Limited

Oxford Centre for Innovation

Mill Street

Oxford

United Kingdom

OX2 0JX

 

Tel: +44 (0) 1865 811175

Fax: +44 (0) 1865 248594

 


------=_NextPart_000_0022_01C98210.8F32F9A0
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
X-Sun-Content-Length: 17450

<html xmlns:v="urn:schemas-microsoft-com:vml" =
xmlns:o="urn:schemas-microsoft-com:office:office" =
xmlns:w="urn:schemas-microsoft-com:office:word" =
xmlns:x="urn:schemas-microsoft-com:office:excel" =
xmlns:p="urn:schemas-microsoft-com:office:powerpoint" =
xmlns:a="urn:schemas-microsoft-com:office:access" =
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" =
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" =
xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" =
xmlns:b="urn:schemas-microsoft-com:office:publisher" =
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" =
xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet" =
xmlns:odc="urn:schemas-microsoft-com:office:odc" =
xmlns:oa="urn:schemas-microsoft-com:office:activation" =
xmlns:html="http://www.w3.org/TR/REC-html40"; =
xmlns:q="http://schemas.xmlsoap.org/soap/envelope/"; xmlns:D="DAV:" =
xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml"; =
xmlns:ois="http://schemas.microsoft.com/sharepoint/soap/ois/"; =
xmlns:dir="http://schemas.microsoft.com/sharepoint/soap/directory/"; =
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"; =
xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp"; =
xmlns:udc="http://schemas.microsoft.com/data/udc"; =
xmlns:xsd="http://www.w3.org/2001/XMLSchema"; =
xmlns:sub="http://schemas.microsoft.com/sharepoint/soap/2002/1/alerts/"=
 xmlns:ec="http://www.w3.org/2001/04/xmlenc#"; =
xmlns:sp="http://schemas.microsoft.com/sharepoint/"; =
xmlns:sps="http://schemas.microsoft.com/sharepoint/soap/"; =
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; =
xmlns:udcs="http://schemas.microsoft.com/data/udc/soap"; =
xmlns:udcxf="http://schemas.microsoft.com/data/udc/xmlfile"; =
xmlns:udcp2p="http://schemas.microsoft.com/data/udc/parttopart"; =
xmlns:st="&#1;" =
xmlns:st1="urn:schemas-microsoft-com:office:smarttags" =
xmlns="http://www.w3.org/TR/REC-html40";>

<head>
<meta http-equiv=Content-Type content="text/html; =
charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><o:SmartTagType
 namespaceuri="urn:schemas-microsoft-com:office:smarttags" =
name="country-region"/>
<o:SmartTagType =
namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="Street"/>
<o:SmartTagType =
namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="PersonName"/>
<o:SmartTagType =
namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="address"/>
<o:SmartTagType =
namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="place"/>
<o:SmartTagType =
namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="City"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal;
	font-family:Arial;
	color:windowtext;}
span.EmailStyle18
	{mso-style-type:personal-reply;
	font-family:Arial;
	color:navy;}
@page Section1
	{size:612.0pt 792.0pt;
	margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
	{page:Section1;}
 /* List Definitions */
 @list l0
	{mso-list-id:1288390089;
	mso-list-type:hybrid;
	mso-list-template-ids:510956088 67698705 67698713 67698715 67698703 =
67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
	{mso-level-text:"%1\)";
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level2
	{mso-level-tab-stop:72.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level3
	{mso-level-tab-stop:108.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level4
	{mso-level-tab-stop:144.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level5
	{mso-level-tab-stop:180.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level6
	{mso-level-tab-stop:216.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level7
	{mso-level-tab-stop:252.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level8
	{mso-level-tab-stop:288.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
@list l0:level9
	{mso-level-tab-stop:324.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;}
ol
	{margin-bottom:0cm;}
ul
	{margin-bottom:0cm;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><font size=2 color=navy face=Arial><span =
style='font-size:
10.0pt;font-family:Arial;color:navy'>Thanks to everyone who responded =
&#8211; I
now have several good solutions to my two problems, and understand =
Mathematica
a bit better to boot!<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span =
style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span =
style='font-size:
10.0pt;font-family:Arial;color:navy'>Cheers,<o:p></o:p></span></font></p>=


<p class=MsoNormal><font size=2 color=navy face=Arial><span =
style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span =
style='font-size:
10.0pt;font-family:Arial;color:navy'>Paul.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span =
style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p>&nbsp;</o:p></span></font></p>

<div>

<div class=MsoNormal align=center style='text-align:center'><font =
size=3
face="Times New Roman"><span style='font-size:12.0pt'>

<hr size=2 width="100%" align=center tabindex=-1>

</span></font></div>

<p class=MsoNormal><b><font size=2 face=Tahoma><span =
style='font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:</span></font></b><font =
size=2
face=Tahoma><span style='font-size:10.0pt;font-family:Tahoma'> Paul =
Ellsmore
[mailto:paul.ellsmore at nanion.co.uk] <br>
<b><span style='font-weight:bold'>Sent:</span></b> 27 January 2009 =
16:20<br>
<b><span style='font-weight:bold'>To:</span></b> '<st1:PersonName =
w:st="on">mathgroup at smc.vnet.net</st1:PersonName>'<br>
<b><span style='font-weight:bold'>Subject:</span></b> Using Map or =
Thread over
selected parts of nested lists</span></font><o:p></o:p></p>

</div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
style='font-size:
12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>Hi,<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>I am currently working with a trial version =
of
Mathematica 7, particularly with an interest in building a graphical =
user
interface. I don&#8217;t have a great familiarity with Mathematica, but =
I am
trying hard to get to grips with a Mathenatica-style of thinking, and =
the way
lists can be manipulated in particular. I have two questions, answers to =
which
would help me a lot:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal =
style='margin-left:36.0pt;text-indent:-18.0pt;mso-list:l0 level1 =
lfo2'><![if !supportLists]><font
size=3 face="Times New Roman"><span lang=EN-GB =
style='font-size:12.0pt'><span
style='mso-list:Ignore'>1)<font size=1 face="Times New =
Roman"><span
style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
</span></font></span></span></font><![endif]><span
lang=EN-GB>My GUI allows the user to select some data files to work =
with. I use
the code:<o:p></o:p></span></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>source = =
SystemDialogInput[FileOpen]<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>This returns the full path for each file =
selected by
the user &#8211; very convenient and user-friendly. My problem is that
&#8220;source&#8221; is returned as a string (ie an atom) if one file is
selected, but a nested list if multiple files are selected. I have =
similar
instances in many parts of my program where the user might be working =
with one
list of data or multiple lists, and I think the same issue will arise. =
So I
would like to know if there is a way of forcing (in this case)
&#8220;source&#8221; to be returned as a nestedlist type, even if the =
length of
that list is one?<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal =
style='margin-left:36.0pt;text-indent:-18.0pt;mso-list:l0 level1 =
lfo2'><![if !supportLists]><font
size=3 face="Times New Roman"><span lang=EN-GB =
style='font-size:12.0pt'><span
style='mso-list:Ignore'>2)<font size=1 face="Times New =
Roman"><span
style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
</span></font></span></span></font><![endif]><span
lang=EN-GB>I have data in the format:<o:p></o:p></span></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>List1={{header,header},{header,header},{head=
er,header},{frequency1,
complexnumber1},{frequency2, complexnumber2},{frequency3,
complexnumber4}&#8230;}<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>On very many occasions I want to deal with =
:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>List2={{frequency1, realpart1},{frequency2, =
realpart2},{frequency3,
realpart4}&#8230;}<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>Or some other trivial transform of =
complexnumbern. I
have been through the VirtualBook, and Documentation centre, and tried =
various
forms of Map and Thread, but I cannot find a way of =
&#8220;mapping&#8221; or
&#8220;threading&#8221; a function like Re[expr] over one element of =
part of a
list, as in:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>List2={{frequency1, =
Re[complexnumber1]},{frequency2,
Re[complexnumber2]},{frequency3, =
Re[complexnumber4]}&#8230;}<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>If someone could give me a pointer to the =
correct
syntax, I would be very grateful.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>At present, I am getting the results I want =
by using
Ifs and Do loops, to deconstruct and reconstruct my lists, but I feel =
that is
the wrong way to approach things &#8211; I want to use =
Mathematica&#8217;s
strengths, not force it to use a C type approach, just because =
that&#8217;s
what I am comfortable with.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>Any suggestions gratefully =
received,<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>Cheers in =
advance,<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'>Paul.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
lang=EN-GB
style='font-size:12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span =
style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span =
style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span =
style='font-size:10.0pt;
font-family:Arial'>Dr. Paul A. Ellsmore</span></font><o:p></o:p></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
style='font-size:
12.0pt'>&nbsp;<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span =
style='font-size:10.0pt;
font-family:Arial'>Nanion Limited</span></font><o:p></o:p></p>

<p class=MsoNormal><st1:place w:st="on"><st1:City w:st="on"><font =
size=2
  face=Arial><span =
style='font-size:10.0pt;font-family:Arial'>Oxford</span></font></st1:Ci=
ty></st1:place><font
size=2 face=Arial><span =
style='font-size:10.0pt;font-family:Arial'> Centre for
Innovation</span></font><o:p></o:p></p>

<p class=MsoNormal><st1:Street w:st="on"><st1:address =
w:st="on"><font size=2
  face=Arial><span style='font-size:10.0pt;font-family:Arial'>Mill =
Street</span></font></st1:address></st1:Street><o:p></o:p></p>

<p class=MsoNormal><st1:place w:st="on"><st1:City w:st="on"><font =
size=2
  face=Arial><span =
style='font-size:10.0pt;font-family:Arial'>Oxford</span></font></st1:Ci=
ty></st1:place><o:p></o:p></p>

<p class=MsoNormal><st1:place w:st="on"><st1:country-region =
w:st="on"><font
  size=2 face=Arial><span =
style='font-size:10.0pt;font-family:Arial'>United
  Kingdom</span></font></st1:country-region></st1:place><o:p></o:p></p>

<p class=MsoNormal><font size=2 face=Arial><span =
style='font-size:10.0pt;
font-family:Arial'>OX2 0JX</span></font><o:p></o:p></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
style='font-size:
12.0pt'>&nbsp;<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span =
style='font-size:10.0pt;
font-family:Arial'>Tel: +44 (0) 1865 811175</span></font><o:p></o:p></p>

<p class=MsoNormal><font size=2 face=Arial><span =
style='font-size:10.0pt;
font-family:Arial'>Fax: +44 (0) 1865 248594</span></font><o:p></o:p></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span =
style='font-size:
12.0pt'><o:p>&nbsp;</o:p></span></font></p>

</div>

</body>

</html>

------=_NextPart_000_0022_01C98210.8F32F9A0--


  • Prev by Date: Re: Re: Looping
  • Next by Date: Re: Re: Significant slow-down with Mathematica 7 (vs 6).
  • Previous by thread: voices for version 7 text-to-speech
  • Next by thread: MCMC in Mathematica