MathGroup Archive 2004

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

Search the Archive

RE: List arguments to functions - Use of Inner Command

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45564] RE: [mg45537] List arguments to functions - Use of Inner Command
  • From: "David Park" <djmp at earthlink.net>
  • Date: Wed, 14 Jan 2004 01:26:17 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Chris,

If one wants to do parallel operations on the elements of two equal length
lists, and then do something with the resulting outputs, the magic command
is Inner.

For example, to add the elements of the two lists and then leave them in a
list (not what you want)...

Inner[Plus, {1, 2, 3}, {4, 5, 6}, List]
{5, 7, 9}

To get what you want use...

Inner[Plus, {1, 2, 3}, {4, 5, 6}, Plus]
21

If you wanted to take 2 times the element in the first list and add it to
the square of the corresponding element in the second list and then multiply
the results, you could use...

Inner[2#1 + #2^2 &, {1, 2, 3}, {4, 5, 6}, Times]
21924


For some reason it took me a long time to get a simple understanding of the
Inner command. Basically it is a way to make a parallel combination of the
elements of two equal length lists and then do something with the results.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


From: Chris Rozell [mailto:crozell at rice.edu]
To: mathgroup at smc.vnet.net

I have basic experience with Mathematica, but this problem has stumped
me for the last day or so.  Any help would be appreciated.

I have a function that needs to take two lists as arguments, perform
operations on them, and then sum the resulting list.  However, by
including the Plus function, it operates on each list item separately
(which is exactly the opposite of what I need.  For a simple example,

 >myfunc[x_, y_] = Apply[Plus, x + y];
 >myfunc[{1, 2, 3}, {4, 5, 6}]
 >
 >{5, 7, 9}

When what I would really like is the number 21 (i.e., 5+7+9, perform the
list addition first, then sum the components).  In this simple example I
could perform the summation after the function returns, but in my real
problem this would not be possible.

When reading the documentation, it appears that the Plus function
behaves this way because it has the attribute "Flat".  But even after
removing that attribute it still behaves in the same way, so I may be
misunderstanding the description of "flat".  Can anyone suggest a way to
either modify the Plus function behavior, or do this another way?  Thank
you in advance!



  • Prev by Date: Re: List arguments to functions
  • Next by Date: Re: hyperlink to pdf files
  • Previous by thread: Re: Struggling with list element assignment in functions
  • Next by thread: Run in Windows