Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8022

Re: Tips for writing correct, non trivial Mathematica Libraries

From Andrzej Kozlowski <akoz@mimuw.edu.pl>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Tips for writing correct, non trivial Mathematica Libraries
Date 2012-01-20 06:46 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jfb2ku$hig$1@smc.vnet.net> (permalink)
References <201201191012.FAA03722@smc.vnet.net>

Show all headers | View raw


On 19 Jan 2012, at 11:12, Bill Rowe wrote:

>>
>> geom[list_] := Apply[Times, list]^(1/Length[list])
>
>> So, this does a bad thing for geom[ x+ y]  (returns (Sqrt[x y])
>
> What were you expecting here? This looks correct to me

Clearly he sees what actually is a powerful feature of Mathematica as a 
"bad thing". The feature is that a function whose arguments are not 
restricted by pattern maching can be put to a more general use. I think 
it's great and not "bad". If you want to restrict the function to work 
on lists you can do this:

geom[list_List] := Apply[Times, list]^(1/Length[list])

and now

geom[list_List] := Apply[Times, list]^(1/Length[list])

geom[x + y]

geom[x + y]

and if you really wish you can restrict it only to vectors you can do

geom[list_?VectorQ] := Apply[Times, list]^(1/Length[list])

and so on.

This is much more powerful and flexible than the "other" approach.

Andrzej Kozlowski

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Tips for writing correct, non trivial Mathematica Libraries Andrzej Kozlowski <akoz@mimuw.edu.pl> - 2012-01-20 06:46 +0000

csiph-web