MathGroup Archive 2005

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

Search the Archive

Re: 'Good' or 'Proper' Mathematica coding habits question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62052] Re: [mg62028] 'Good' or 'Proper' Mathematica coding habits question
  • From: "Carl K. Woll" <carl at woll2woll.com>
  • Date: Thu, 10 Nov 2005 02:50:33 -0500 (EST)
  • References: <200511090846.DAA17466@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Matt wrote:
> Hello,
>   Due to the great help I've received from here, I've made limited
> progress with Mathematica.  As I continue my journey, I am trying to adopt the
> philosophy:  "When working with Mathematica, shy away from your long-learned
> habits of using loops, and instead try a functional or rule-based
> approach."  To this end, I have coded up a few functions, which I'd
> appreciate any feedback on, as to whether I'm breaking an inviolate
> rule of Mathematica, or I'm using an egregious memory model, I'm duplicating
> some functionality that already exists in Mathematica proper, etc.  Please bear
> in mind that I am by training and thought process an assembly and C/C++
> developer that worries about details such as whether to use
> post-increment or post-increment when dealing with objects due to the
> unnecessary construction of temporary objects on the stack.  I want to
> learn good Mathematica habits from the get-go.
> 
> Here are the functions with short explanations for each:
> 

[snip]

Matt first wants a function which extracts the nth element from each 
sublist. The simplest way is to use Part with All:

testlist = {{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}}

testlist[[All,1]]
testlist[[All,-1]]

etc.

Matt then wants a function which finds the maximum or minimum of a 
column. Use Max or Min:

Max[testlist[[All,2]]]

etc.

> 
> Here is a table of 20 pairs of numbers.
> values = Table[{Random[Real, {-2, 2}] i, Random[Real, {-2,2}] i}, {i,
> 1, 20}];
> 
> I want to extract all elements from 'values' where each of the values
> of a particular element are negative.  This was my first approach:
> negElems = {};
> (If[Negative[#[[1]]] && Negative[#[[2]]], negElems = {negElems, #}]) &
> /@ values
> negElems
> Flatten[%]
> Partition[%, 2]
> 
> I didn't like that approach particularly because it created a
> 'temporary' list with many Nulls embedded in it.  So, then I tried a
> Fold approach:
> 
> negElems = {};
> Fold[If[Negative[#2[[1]]] && Negative[#2[[2]]], negElems = {negElems,
> #2}, negElems] &, values[[1]], values];
> Flatten[%]
> Partition[%, 2]
> 

For this problem, Cases seems like the simplest approach:

Cases[ values, {_?Negative,_?Negative} ]

> That seems 'nicer', but I'm sure there's even better and more efficient
> ways of doing this (or perhaps, what I've done is as bad as bad can
> be).
> 

Remember that Mathematica has many builtin functions, and of course, in 
general it is best to make use of the appropriate function whenever you 
can. Part of the process of learning Mathematica is becoming familiar 
with various subsets of these builtin functions.

> All feedback is appreciated.
> 
> Thanks,
> 
> Matt

Carl Woll
Wolfram Research


  • Prev by Date: Re: Timing runs for the last part of my previous post
  • Next by Date: Re: Re: Following Help Links with the Keyboard
  • Previous by thread: 'Good' or 'Proper' Mathematica coding habits question
  • Next by thread: Re: 'Good' or 'Proper' Mathematica coding habits question