MathGroup Archive 2007

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

Search the Archive

Re: Creating a Listable Function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73579] Re: Creating a Listable Function
  • From: "dimitris" <dimmechan at yahoo.com>
  • Date: Thu, 22 Feb 2007 04:34:41 -0500 (EST)
  • References: <ergqjm$j0v$1@smc.vnet.net>

=CF/=C7 Gregory Lypny =DD=E3=F1=E1=F8=E5:
> Hello everyone,
>
> I've created a function for population variance, which does not make
> use of Mathematica's built-in Variance function.
>
> VarPop[x_] := Total[(x - Mean@x)^2]/Length@x
>
> How do I need to change the definition to have the function operate
> across lists much like Total, Mean, and Variance do?
>
> Regards,
>
> 	Greg


Hi Greg.

Sorry if I make mistake and I don't understood what you want, but
VarPop already behaves like the Built-in functions you mention when
applied on lists.

VarPop[x_] := Total[(x - Mean@x)^2]/Length@x

Consider now

lst1=Table[Random[],{10}]
Dimensions[%]

{0.647903,0.321486,0.0955869,0.731858,0.0143044,0.891193,0.174065,0.70159,0=
.\
183696,0.325517}
{10}

lst2 = Table[Random[], {10}, {10}];
Short[%, 5]
Dimensions[%]

{{0.821042,0.826185,0.56768,0.685286,0.834579,0.765259,0.395596,0.929556,
    0.00118294,0.0425669},\[LeftSkeleton]8\[RightSkeleton],
{0.717743,0.782238,\
0=2E619857,0.241115,0.619339,0.572299,0.515924,0.77904,0.492213,0.0594945}}
{10,10}

lst3 = Table[Random[], {10}, {10}, {10}];
Short[%, 3]
Dimensions[%]

{{{0.993049,0.137547,0.144427,0.109549,0.467162,0.838562,0.676214,0.0872068=
,0.\
751362,0.251411},{\[LeftSkeleton]1\[RightSkeleton]},\[LeftSkeleton]7\
\[RightSkeleton],{0.287858,0.424848,\[LeftSkeleton]7\[RightSkeleton],
0=2E620963}\
},\[LeftSkeleton]9\[RightSkeleton]}
{10,10,10}

Then

Through[{Variance,Mean,Total,VarPop}[lst1]]
{0.0652599,0.520712,5.20712,0.0587339}

Through[{Variance,Mean,Total,VarPop}[lst2]]//Short[#,3]&
{{0.107178,0.0667206,0.0814426,0.0859006,
    0.123088,0.0616136,0.0501627,0.0729217,0.0685926,0.0864332},{\
\[LeftSkeleton]1\[RightSkeleton]},\[LeftSkeleton]1\[RightSkeleton],
{0.113387,
    0.0716805,\[LeftSkeleton]7\[RightSkeleton],0.131527}}

(Short[#1, 4] & )[Through[{Variance, Mean, Total, VarPop}[lst3]]]
{{{0.133688,0.0882214,0.109247,0.101373,0.106272,0.0873485,0.0596831,0.\
0943123,0.111551,0.064193},\[LeftSkeleton]8\[RightSkeleton],
{0.0614513,0.\
0273284,0.0660795,0.0958829,\[LeftSkeleton]20\[RightSkeleton],\
[LeftSkeleton]\
20\[RightSkeleton],\[LeftSkeleton]20\[RightSkeleton],0.0814502,
      0.0779278,0.0791353}},\[LeftSkeleton]3\[RightSkeleton]}

Is not this behavior of VarPop what do you want (i.e. listability)?

Dimitris



  • Prev by Date: Re: Creating a Listable Function
  • Next by Date: Re: Diferent solution of integral in versions 4 and 5...
  • Previous by thread: Re: Creating a Listable Function
  • Next by thread: Map function which adds last two numbers of a list