MathGroup Archive 2009

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

Search the Archive

Re: evaluate f[{x,y,z}] when f[x,y,z] defined

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103400] Re: evaluate f[{x,y,z}] when f[x,y,z] defined
  • From: pfalloon <pfalloon at gmail.com>
  • Date: Sun, 20 Sep 2009 06:22:05 -0400 (EDT)
  • References: <h9283i$lo6$1@smc.vnet.net>

On Sep 19, 7:26 pm, Murray Eisenberg <mur... at math.umass.edu> wrote:
> I ought to know how to do this (simply) but don't, or at least don't
> recall how...
>
> Often I have a function of several variables defined, for example:
>
>    f[x_, y_, z_] := Sqrt[x^2 + y^2 + z^2]
>
> And I have some list of numbers, e.g.:
>
>    a = {4, 3, 2};
>
> How can I evaluate the following...
>
>    f[a]
>
> ... without having to make a separate definition such as
>
>    f[{x_, y_, z_}] := f[x, y,z]
>
> ?
>
> --
> Murray Eisenberg                     mur... at math.umas=
s.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

Hi Murray,
There are probably a few ways to do this. My preference would be just
to Apply the function to the list:

   f@@a

If you have a list of such points you could use, e.g.

   f@@@{a,b,c}

to get f[a], f[b], f[c] (where each of a,b,c is assumed to be a List).


However, as a general stylistic issue, I think a good rule of thumb is
that if the three arguments (in this case x,y,z) form a vector in some
meaningful sense, then it is best to use a definition which takes the
vector, rather than individual points, as its argument. Thus,

f[vec_List] := ...

or

f[{xi__}] := ...

or

f[{x_,y_,z_}] := ....

etc

I find this extra structure makes code easier to read and errors
harder to make.

Cheers,
Peter.






  • Prev by Date: Re: Credit card balance transfer fee problem
  • Next by Date: Re: Capturing the output(s) from a Manipulate?
  • Previous by thread: Re: evaluate f[{x,y,z}] when f[x,y,z] defined
  • Next by thread: Capturing the output(s) from a Manipulate?