MathGroup Archive 1996

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

Search the Archive

Re: addressing matrix elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg3580] Re: addressing matrix elements
  • From: rjfrey at rentec.com (Robert J. Frey)
  • Date: Wed, 27 Mar 1996 03:25:22 -0500
  • Organization: Renaissance Technologies Corp.
  • Sender: owner-wri-mathgroup at wolfram.com

David Cabana (drc at emi.net) wrote:
: More generally, I would like to be able to apply a function f to each
: element in a matrix of arbitrary size and dimensions, without worrying
: about the particulars of the matrix representation via lists.

This function is quite general:

	pervade[f_, a_] := If[ListQ[a], Map[pervade[f,#]&,
		f[a]]

It applies the function f to the leaves of a.

For example, pervade[Length, {{1,2},3}] gives {{0,0},0} and
pervade[Range, {{1,2},3}] gives {{{1},{1,2}},{1,2,3}}. Write
out the input as a tree and apply the function in question to
the leaves to see why this is so.

Nested lists are essentially trees. As any good book on data
structures will tell you, recursive functions are a natural
way to traverse such objects.

I hope this is clear.
--
Regards,
Robert (rjfrey at rentec.com)

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: packages
  • Next by Date: Re: mathematica-macppp conflict
  • Previous by thread: Re: addressing matrix elements
  • Next by thread: Re: addressing matrix elements