MathGroup Archive 2010

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

Search the Archive

Re: Extra Data and Data structures

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107235] Re: [mg107203] Extra Data and Data structures
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sat, 6 Feb 2010 03:24:37 -0500 (EST)
  • Reply-to: hanlonr at cox.net

Clear[f, extraData]

extraData /:
 Times[x_, extraData[y_List, z_]] := extraData[x*y, z]

extraData /:
 Plus[x_, extraData[y_List, z_]] := extraData[x + y, z]

extraData /:
 getExtra[extraData[x_List, y_]] := y

f = extraData[{1, 2, 3, 4}, "cool"];

x*f

extraData({x,2 x,3 x,4 x},cool)

x*f == f*x

True

f/x

extraData[{1/x, 2/x, 3/x, 4/x}, 
   "cool"]

f/x == (1/x) f

True

x + f

extraData({x+1,x+2,x+3,x+4},cool)

x + f == f + x

True

x - f

extraData({x-1,x-2,x-3,x-4},cool)

x - f == -f + x

True

getExtra[f]

cool

g = 3*f

extraData({3,6,9,12},cool)

getExtra[g]

cool


Bob Hanlon

---- Uayeb <uayebswinburne at gmail.com> wrote: 

=============
Oh wisdom of the Mathematica list,

I occassionally would have a use for being able to carry extra data
around with something like a large list or array. Thinking more
carefully about this, I realise what I'm really looking for is some
way I can create a data structure of sorts in a Mathematica object,
and then somehow be able to use it in standard Mathematica constructs
where it would assume a "default value."

My initial thought was to create some kind of wrapper function which
remains unevaluated but which as appropriate UpSets such that it
returns its default value when Mathematica trys to do something with
it.

I'd like to be able to do something like this:
f=extraData[{1,2,34},"cool"];

3*f
{3,6,9,12}
(or even "extraData[{3,6,9,12},"cool"])

getExtra[f]
"cool"

It would be particularly amazing if I could do something like this:

g=3*f;

getExtra[g]
"cool"

I envision some set of rules assigned to the wrapper function
extraData which somehow knows to pass most functions through to the
first argument, but which behaives specially to a few things, such as
Set, and the function to retrieve the extra data, getExtra.

The general case would then be something like
f_[extraData[data_,extra_]]^:=extraData[f[data],extra]

Unfortunately, the few times I've tried this, I very quickly end up
with too many special cases, which must be dealt with individually.
(For instance, the above suggestion won't deal with functions which
take multiple arguments, and functions which automatically thread over
lists are even more difficult.)

Any ideas? Is there an alternate (possibly less/more elegant)
solution?

Cheers,
Andy





  • Prev by Date: Re: Weird vanishing syntax coloring
  • Next by Date: Re: Re: Combining InterpolatingFunctions
  • Previous by thread: Extra Data and Data structures
  • Next by thread: Re: Extra Data and Data structures