[Date Index]
[Thread Index]
[Author Index]
Re: copying a variable
*To*: mathgroup at smc.vnet.net
*Subject*: [mg92343] Re: copying a variable
*From*: ramiro <ramiro.barrantes at uvm.edu>
*Date*: Sat, 27 Sep 2008 22:24:17 -0400 (EDT)
*References*: <gbl303$m03$1@smc.vnet.net> <48DE179F.5090201@gmail.com> <48DE24D3.3010109@uvm.edu> <f831b3d60809270720j486e8eccxc0098ae2ce9c49c4@mail.gmail.com>
Hi Szabolcs,
I just tried with expressions and it looks great. I think I was just
treating the DownValues as a kind of hash table (inspired a bit by the
mathematica paclets) I think this is an important step in defining how
I will work with tree (I work with evolutionary trees in biology). So
here are two questions as I'm afraid I'm missing mathematica tricks:
1) If I want to find out the ancestor of a node, would it be best to a)
just traverse the tree every time (maybe the speed won't be an issue),
b) do some combination of Position, etc. to figure it out; c) keep a
separate record of definitions (like the one I had) of who's the
ancestor and the children of who.
2) What if I want to do an operation on a tree, I was thinking about
reading the tree and then making "definitions" of children, ancestors,
etc. (just because I am going to use those many times), then making the
operation somehow and outputting the tree again as an expression. e.g.
reroot[treeX_]:=Module[{definitions},
definition = "read all the tree data, ancestors, children, root,
etc." based on treeX
.... reroot the tree
make a new expression based on rerooted tree
output expression
]
Thanks for helping. Any suggestions or comments much appreciated.
Ramiro
HorvÃ¡t wrote:
> 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 uvm.edu> 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: copying a variable**
Next by Date:
**Variations on ShadowPlot3D**
Previous by thread:
**Re: copying a variable**
Next by thread:
**SparseArray**
| |