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: [mg107247] Re: Extra Data and Data structures
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Sat, 6 Feb 2010 03:26:48 -0500 (EST)
  • References: <hkgl4o$6ua$1@smc.vnet.net>

Hi,

> 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"
> 
> 
> 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?

I really don't know whether the following fits your needs, but it
probably is interesting anyway. It reflects my habit to misuse
DownValues for all kind of things :-)

SetAttributes[{getExtra, setExtra}, HoldFirst];

getExtra[x_Symbol] := ReleaseHold[
   	Hold[x["extra"]] /. DownValues[x]
   ];

setExtra[x_, val_] := (
  DownValues[x] = Append[
    DeleteCases[
     DownValues[x],
     Verbatim[RuleDelayed][_[_["extra"]], _],
     {1}
     ],
    HoldPattern[x["extra"]] :> val
    ];
  val
  )

now you can do:

x = {1, 2, 3, 4}

and use x as usual. If you want to add extra data you do:

setExtra[x,"cool"]

and to receive the extra data you'd do:

getExtra[x]

Of course you could create as many keys in the DownValues as you need...

> It would be particularly amazing if I could do something like this:
>
> g=3*f;
>
> getExtra[g]
> "cool"

I think something like this could be done with the above approach, but
probably not by using just this syntax (unless you overwrite Set, which
you usually don't want to touch). It would require some tricks though
and also a better sepecification like what should happen when the RHS
has more than one variable which has extra data? If it would be o.k. to
explicitly call copyExtra[f,g], then that should be easy enough
(assuming that the values of extra data will not contain references to
the symbol itself):

SetAttributes[copyExtra, HoldAll];
copyExtra[src_Symbol, trg_Symbol] :=
 DownValues[trg] = (DownValues[src] /. HoldPattern[src] :> trg)

hth,

albert


  • Prev by Date: Re: Re: Combining InterpolatingFunctions
  • Next by Date: Re: Obtain smooth plot of free-hand contour
  • Previous by thread: Re: Extra Data and Data structures
  • Next by thread: Obtain smooth plot of free-hand contour