On Sat, Nov 1, 2008 at 10:37 PM, Brian Granger <[EMAIL PROTECTED]> wrote:
>
> On Fri, Oct 31, 2008 at 1:03 AM, Andy Ray Terrel <[EMAIL PROTECTED]> wrote:
>>
>> On Fri, Oct 31, 2008 at 6:39 AM, Brian Granger <[EMAIL PROTECTED]> wrote:
>>>
>>> Hi,
>>>
>>> I am continuing some work that Ondrej and I started this week
>>> implementing quantum operators in sympy.  I am building a Dagger class
>>> that does the hermitian conjugate.  In the canonize method, the Dagger
>>> operator need to test what type of object has been passed to Dagger.
>>> From there it 1) either actually takes the dagger if it knows how or
>>> 2) just leaves Dagger unevaluated.  So, I need to test for various
>>> types of objects.
>>>
>>> The common way of testing the type of object you get in sympy is to
>>> use the attributes like is_number, is_real, etc.  But, I have two, no
>>> three problems with this:
>>>
>>> 1.  Only subclasses of Basic have these attributes.  Matrices (and any
>>> other immutable type) don't inherit from Basic and so they don't have
>>> all these attributes.
>>
>> I agree with Brian here.  This "some objects are basic others are not"
>> design is confusing, unfortunately there are not many design docs
>> around so I don't know if this was intentional or not.
>
> The logical things to do would be to have the following hierarchy of classes:
>
> Basic-------------ImmutableBasic
> |
> |
> MutableBasic
>
> Although I am still not clear where and how the immutability of the
> current Basic class shows up.

All Basic classes are immutable. It shows up in a way that once you
create an instance of any Basic subclass, it represents what you put
there a the creation time. If it changes later, it's a bug.

Example: x+x calls Add.__new__(x, x), which does canonicalization and
creates an instance of Mul(2, x).  Once Mul(2, x) is created, it never
changes.

It is however useful to have mutable Matrices. That's why they don't
inherit from Basic, because if they did, we get in trouble once you
start using Matrices in expressions.

If something is still not clear, let's discuss it.

>
>>>
>>> 2.  There is no is_complex attribute in basic.  This brings up another
>>> ?: how are complex number represented in sympy?
>>
>> The is_complex attribute comes from the assumptions, although this
>> seems to be broken.  You can simpify a built-in complex to play
>> around, in the example b.is_complex should be true:
>>
>> In [1]: a = complex(3,2)
>>
>> In [2]: a
>> Out[2]: (3+2j)
>>
>> In [3]: type(a)
>> Out[3]: <type 'complex'>
>>
>> In [4]: b = sympify(a)
>>
>> In [5]: type(b)
>> Out[5]: <class 'sympy.core.add.Add'>
>>
>> In [6]: b
>> Out[6]: 3 + 2*I
>>
>> In [7]: b.is_complex
>>
>> In [8]: b.args[1].args[1]
>> Out[8]: I
>>
>> In [9]: type(b.args[1].args[1])
>> Out[9]: <class 'sympy.core.numbers.ImaginaryUnit'>
>>
>> In [10]: i = b.args[1].args[1]
>>
>> In [11]: i.is_complex
>> Out[11]: True
>>
>>
>>>
>>> 3.  For custom object types, like my operators, I have to add custom
>>> code to my Dagger class that does things like:
>>>
>>> if isinstance(QuantumOperator):
>>>   # take Dagger of operator
>>>
>>> Is this the best way of going about this?
>>
>> Its the only way I know unless you add some new property
>> (is_QuantumOperator) to Basic.

Yes. Btw, you can ise isinstance for any other classes as well if you
like it. is_Mul is faster, because it's just an attribute lookup
(isinstance is quite slow). But for things like QuantumOperator, I
suggest to just use isinstance and only optimize when it is really
needed.

>
> Again, this seems like a perfect place to have objects themselves know
> how to take their dagger (transpose+conjugate), using a _dagger_
> method.  Otherwise, if I create a new type, I have to hack the Dagger
> class to know about this object.  This makes it difficult for people
> to extend Sympy.

Yes, I think _dagger_ is useful to have. See the other thread for
general discussion.

>
> Sorry for all these questions.  I am just trying to get a feel for the
> how and why of Sympy so I can hack on it.  It is quite cool.

Thanks for all these questions. It helps to improve the sympy's design.

Ondrej

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to