Behavior of id()

14 views
Skip to first unread message

Guru Devanla

unread,
Jun 22, 2012, 4:13:48 PM6/22/12
to sy...@googlegroups.com
Hello All,

Can someone explain why 2 objects in the following statements give me the same id() value?


>>> A, B = symbols('A B', commutative=False)
>>> x = A*B
>>> print id(x)
169853492
>>> y = A*B
>>> print id(y)
169853492

I run into this problem while trying to implement some operation on Trace module. 

Say, I have  Tr(A*B*C) . Now, the user wants to create another instance of Tr(C*A*B).   But, both objects are getting the same id, meaning I am not able to create 2 different objects one which prints at Tr(A*B*C) and another which prints as Tr(C*A*B).  Note, that I have not yet overridden  _hashable_contents to compare the args ( using the  cyclic property of trace).

Am I missing something basic?   Or is there a way to override this  behavior, if necessary

Thanks
Guru




















krastano...@gmail.com

unread,
Jun 22, 2012, 4:22:46 PM6/22/12
to sy...@googlegroups.com
On 22 June 2012 22:13, Guru Devanla <grd...@gmail.com> wrote:
> Hello All,
>
> Can someone explain why 2 objects in the following statements give me the
> same id() value?
>
>
>>>> A, B = symbols('A B', commutative=False)
>>>> x = A*B
>>>> print id(x)
> 169853492
>>>> y = A*B
>>>> print id(y)
> 169853492

Because these are the same object A*B. x and y are just labels
pointing to an instance.

>
> I run into this problem while trying to implement some operation on Trace
> module.
>
> Say, I have  Tr(A*B*C) . Now, the user wants to create another instance of
> Tr(C*A*B).   But, both objects are getting the same id, meaning I am not
> able to create 2 different objects one which prints at Tr(A*B*C) and another
> which prints as Tr(C*A*B).  Note, that I have not yet overridden
>  _hashable_contents to compare the args ( using the  cyclic property of
> trace).
>
Stupid question: Did you correctly set commutable=False for A,B and C.
Does Tr somehow override them?

> Am I missing something basic?   Or is there a way to override this
>  behavior, if necessary
>
> Thanks
> Guru
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/lFlq7OJjqlkJ.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

krastano...@gmail.com

unread,
Jun 22, 2012, 4:34:15 PM6/22/12
to sy...@googlegroups.com
By the way, on my machine only the hashes get equal for commutative
symbols, not the ids:

In [12]: hash(A*B*C) == hash(C*A*B)
Out[12]: False

In [13]: id(A*B*C) == id(C*A*B)
Out[13]: False

In [14]: A,B,C = symbols('A B C', commutative=True)

In [15]: hash(A*B*C) == hash(C*A*B)
Out[15]: True

In [16]: id(A*B*C) == id(C*A*B)
Out[16]: False

So A*B*C and C*A*B generate different Mul instances even for
commutative symbols.

Guru Devanla

unread,
Jun 22, 2012, 4:35:40 PM6/22/12
to sy...@googlegroups.com
Stefan,

Thanks for the explanation on the first item.  My second item was typed in error ( and somehow the corrected post has not showed up yet). So, let me elaborate on the second issue.

I have a t1 = Tr(A*B*C),  and I set A,B,C = symbols('A B C', commutative=False).

Now, I have a method where I let the user do a cyclic permute on t1's arguments using t1.permute(1). This permute method returns t2 = Tr(C*A*B).  So far everything works fine. Until, this point I have not overridden the _hashable_content() method.


Now, I need to override my _hashable_contents() method so that I always return args  'A,B,C' ( in that order),  so that t1==t2 evaluates to True  (cycle property of trace).

But, once I implemented the _hashable_contents() method,  id(t2) is always equal to id(t1).  Which means I am not able to return Tr(C*A*B) when I call the permute method.


Makes sense?

Thanks
Guru
> sympy+unsubscribe@googlegroups.com.

Guru Devanla

unread,
Jun 22, 2012, 4:36:46 PM6/22/12
to sy...@googlegroups.com
Sorry about the second issue on my first mail. I have corrected my explanation in the followup email.

krastano...@gmail.com

unread,
Jun 22, 2012, 5:11:40 PM6/22/12
to sy...@googlegroups.com
I do not think that reimplementing hashable_content is the best idea.
These objects are different even though after some operations they can
be shown to evaluate to the same thing.

Why do you need the permutations? Can't you just add some degeneracy
factor if you need the sum of the permutations? And do you think that
your permutation method makes sense for expressions like Tr(A**n*B)?

I know that I am searching for workaround and not answering your exact
question, however this may turn out to be a better alternative.

Aaron Meurer

unread,
Jun 22, 2012, 5:43:30 PM6/22/12
to sy...@googlegroups.com
This is probably due to the cache. Does it also happen in isympy with the -C option?

Aaron Meurer
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/lFlq7OJjqlkJ.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.

Guru Devanla

unread,
Jun 22, 2012, 5:51:26 PM6/22/12
to sy...@googlegroups.com
Stefan,

You are right when you say that after some operations they can be equal. Irrespective of that,  I will have to evaluate and see if we need such a permute feature given that it's implementation gets tied into _hashable_content().

Thanks for taking the time and explaining your thoughts in detail.

-Guru

Guru Devanla

unread,
Jun 22, 2012, 5:52:11 PM6/22/12
to sy...@googlegroups.com
Aaron,

It does not happen when i start with -C option.  Out of general curiosity, are there modules in sympy that forgo this feature explicitly?
-G
To unsubscribe from this group, send email to sympy+unsubscribe@googlegroups.com.

Aaron Meurer

unread,
Jun 22, 2012, 6:33:02 PM6/22/12
to sy...@googlegroups.com
I don't think so. Any subclass of Basic is automatically cached, as far as I remember. 

If you're having this issue, it probably means that your hash is not consistent with you equality testing. 

I guess you have to ask if you want Trace(A*B*C) to be unequal to Trace(C*A*B) or not. You can't have it one way half the time and the other way the other half without running into problems. 

Aaron Meurer
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/4TxKSYHVJJcJ.

To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages