Re: Re: multiple outputs from a function

*To*: mathgroup at smc.vnet.net*Subject*: [mg52875] Re: [mg52834] Re: multiple outputs from a function*From*: yehuda ben-shimol <benshimo at bgu.ac.il>*Date*: Wed, 15 Dec 2004 04:27:14 -0500 (EST)*References*: <cpekeh$744$1@smc.vnet.net> <200412141059.FAA24637@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

This does not change the basic idea: The external header is still List. You may replace the list with an arbitrary header of your own, as long as it does not try to modify its content. The usage of rules prevents you to remember the position of the results but enforces you to remember their names as appear in the rule. yehuda John Jowett wrote: >Hello, > >As several people have pointed out, returning a list of values is probably >the most straightforward thing to do. It's fine as long as you know what the >elements in the list are supposed to be. > >But another technique that is often useful is to return a list of rules. To >give a trivial example: > >In[2]:= >f[x_,y_]:={sum->x+y,difference->x-y,product->x y, quotient-> x/y} > >In[3]:= >f[1,2] > >Out[3]= >{sum -> 3, difference -> -1, product -> 2, quotient -> 1/2} > >These results can then be used in other expressions: > >In[5]:= >(difference/sum)/.f[1,2] > >Out[5]= >-1/3 > >For less trivial examples see the built-in function Solve or functions like >LocationReport in the standard package Statistics`DescriptiveStatistics`. > >This is a way to provide a number of "named" results without worrying about >order in the list (in some other programming languages something similar is >achieved with so-called "associative arrays"). > >John Jowett > >"Ben Barrowes" <barrowes at alum.mit.edu> wrote in message >news:cpekeh$744$1 at smc.vnet.net... > > >>I feel I must be missing something fundamental... >> >>How do you write a function (or ?) in Mathematica which produces more >>than one output? >> >>Let's say I have some data and I want a single function to calculate the >>mean, variance, std, etc, more than one thing and return those? I know >>that there are builtin functions for those parameters, but the point is >>I want to define functions with more than one output. >> >>The only examples I can find are along the lines of: >> >>f[x_,y_]:=x^2+y^2; >> >>which have only a single result. >> >>Is there a different structure altogether, such as a Subroutine, which >>allows multiple results from a single subunit? >> >>One thought I had was that because Mathematica treats everything as >>global unless defined specifically local (e.g. in a module), that >>variables used in a procedure would be accessible and would thus be a >>"result", but it seems scoping problems would arise if this was used too >>often. For example: >> >>In[67]:= >>t1[x_,y_]:=(a1=x^2+y;a2=x+y^2;x+y) >> >>In[68]:= >>t2=t1[5,6] >> >>Out[68]= >>11 >> >>In[69]:= >>a1 >> >>Out[69]= >>31 >> >>In[70]:= >>a2 >> >>Out[70]= >>41 >> >>Is this the accepted method for extracting multiple results from one >>function definition? >> >> >>Ben Barrowes >> >> >> > > > > --Boundary_(ID_zhw7Zg8IIxMmPWqsLSw1dQ) <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> <title></title> </head> <body bgcolor="#ffffff" text="#000000"> This does not change the basic idea: The external header is still List. You may replace the list with an arbitrary header of your own, as long as it does not try to modify its content. The usage of rules prevents you to remember the position of the results but enforces you to remember their names as appear in the rule.<br> yehuda<br> <br> John Jowett wrote: <blockquote cite="mid200412141059.FAA24637 at smc.vnet.net" type="cite"> <pre wrap="">Hello, As several people have pointed out, returning a list of values is probably the most straightforward thing to do. It's fine as long as you know what the elements in the list are supposed to be. But another technique that is often useful is to return a list of rules. To give a trivial example: In[2]:= f[x_,y_]:={sum->x+y,difference->x-y,product->x y, quotient-> x/y} In[3]:= f[1,2] Out[3]= {sum -> 3, difference -> -1, product -> 2, quotient -> 1/2} These results can then be used in other expressions: In[5]:= (difference/sum)/.f[1,2] Out[5]= -1/3 For less trivial examples see the built-in function Solve or functions like LocationReport in the standard package Statistics`DescriptiveStatistics`. This is a way to provide a number of "named" results without worrying about order in the list (in some other programming languages something similar is achieved with so-called "associative arrays"). John Jowett "Ben Barrowes" <a class="moz-txt-link-rfc2396E" href="mailto:barrowes at alum.mit.edu"><barrowes at alum.mit.edu></a> wrote in message <a class="moz-txt-link-freetext" href="news:cpekeh$744$1 at smc.vnet.net">news:cpekeh$744$1 at smc.vnet.net</a>... </pre> <blockquote type="cite"> <pre wrap="">I feel I must be missing something fundamental... How do you write a function (or ?) in Mathematica which produces more than one output? Let's say I have some data and I want a single function to calculate the mean, variance, std, etc, more than one thing and return those? I know that there are builtin functions for those parameters, but the point is I want to define functions with more than one output. The only examples I can find are along the lines of: f[x_,y_]:=x^2+y^2; which have only a single result. Is there a different structure altogether, such as a Subroutine, which allows multiple results from a single subunit? One thought I had was that because Mathematica treats everything as global unless defined specifically local (e.g. in a module), that variables used in a procedure would be accessible and would thus be a "result", but it seems scoping problems would arise if this was used too often. For example: In[67]:= t1[x_,y_]:=(a1=x^2+y;a2=x+y^2;x+y) In[68]:= t2=t1[5,6] Out[68]= 11 In[69]:= a1 Out[69]= 31 In[70]:= a2 Out[70]= 41 Is this the accepted method for extracting multiple results from one function definition? Ben Barrowes </pre> </blockquote> <pre wrap=""><!----> </pre> </blockquote> </body> </html> --Boundary_(ID_zhw7Zg8IIxMmPWqsLSw1dQ)--

**References**:**Re: multiple outputs from a function***From:*"John Jowett" <John.Jowett@cern.ch>