MathGroup Archive 2008

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

Search the Archive

Re: copying a variable

Hello Ramiro,

Have you considered representing trees simply by Mathematica
expressions instead of making DownValue definitions like this?  All
Mathematica expressions are already trees.  Creating data structures
using definitions is not suited to Mathematica, and is generally a
very bad idea.

Just represent your data structure with an expression.  Do you see any
disadvantage in doing this?

On Sat, Sep 27, 2008 at 14:19, ramiro <ramiro.barrantes at> wrote:
> Hi,
> Thanks for answering, let me try to explain again in a bit more detail.  I
> have a tree object, which contains definitions about ancestors, children,
> root,  especial values, etc.  Let's call that tree, and I put data in it as
> definitions:
> tree["children", nodeX] = children of node X
> tree["ancestor", nodeY] = ancestor of node Y
> tree["root"] = root of tree
> However, I would like to do operations on this tree that change its
> topology, and return a new tree, for example reroot
> reroot[treeX,newRoot] = "a new tree just like treeX but with the root in
> newRoot"
> Therefore, I would like to have a "copy" of treeX with all its definitions
> inside this reRoot function, and thus  I would return a new tree very
> similar to the old one, with a new root, but the definitions of the old tree
> would remain unchanged..
> The example I wrote was just meant to illustrate the problem, but I hope the
> context I just mentioned might help explain the problem better.
> Thanks,
> ramiro
> Szabolcs Horvát wrote:
>> ramiro wrote:
>>> Hello,
>>> This must be very simple.  Consider the following code:
>>> k[1] = 2;
>>> sub[x_] := With[{z = x}, z[1] = 4; z]
>>> r = sub[k];
>>> r[1]
>>> k[1]
>>>  The output is
>>> 4
>>> 4
>>> I would like to have the assignment inside the function (on z) don't
>>> affect the parameter (x).  I would like to copy x so that whatever
>>> assignment I do on z doesn't affect it, in other words, I would like to
>>> return _a copy_ of x. So that the output should be:
>>> 4
>>> 2
>> I don't understand your question fully.  What is the purpose of having two
>> symbols with the same definition?  Why couldn't you just do r[1] = 4 instead
>> of r = sub[k]?
>> Are you trying to transfer the other definitions associated with k to r,
>> so that if k[2]=Pi was defined then after evaluating r = sub[k], r[2]=Pi
>> would be automatically defined too?
>> I suspect that there might be better solutions to your *actual* problem
>> than copying definitions ...   But if you really want to do this, you could
>> try something like DownValues[r] = DownValues[k] /. k :> r for copying ...
>> Be warned that this is very bug prone and is very likely to cause trouble
>> with non-trivial definitions.

  • Prev by Date: Re: SparseArray
  • Next by Date: Re: External packages, Manipulate, and MathPlayer
  • Previous by thread: Re: copying a variable
  • Next by thread: Re: copying a variable