OK, so you do not want an Add at all.  You want to create some class like

class Complex(Expr):

    __slots__ = ['re', 'im'] # you can call these whatever you want

    def __new__(cls, arg):
        # Biolerplate code.  Note that I haven't tested this, so there
are probably some typos.
        self.re, self.im = sympify(arg).as_real_imag()
        # Maybe some other stuff happens here.  I'm not sure.

Add is a class that takes in any number of arguments and combines
coefficients on terms (for example Add(x, 2*x, y) gives 3*x + y).  I
think you want a class to easily keep the real and imaginary parts of
an expression separate.  It doesn't make sense to combine the real
part and the imaginary part, and you want to keep the object Complex
even if one of the two is 0.

So I think you should create it as above.  Then, override the methods
you need.  .as_real_imag() would be trivial for this class, for
example.  You would also want to define a printer so that it prints as
re + im*I.

This class is very similar to the Rational class (see
sympy/core/numbers.py).  That class keeps track of two Integers,
self.p and self.q.  This class will keep track of two real Exprs, the
real and imaginary parts.

You should then make BeamParameter (or whatever) an instance of
Complex (or a subclass, depending on what you want to do).

By the way, your life might be easier if issue 754 were fixed.

Aaron Meurer

On Fri, Jun 10, 2011 at 3:06 PM, [email protected]
<[email protected]> wrote:
> BeamParameter should be just a complex number. As there is no Complex class
> in sympy I have tried to base it on Add.
>
> I need some additional fields and methods to implement the formalism, but if
> BeamParameter returns complex number created from the two arguments in the
> constructor the rest is easy.
>
> The nested example you gave should return x+I*y+I*z. This is complete
> nonsense from physical point of view, but the user is free to do it.
>
> I have tried to do it without __slots__ and __new__ and use __init__ instead
> but then isinstance(q, BeamParameter) returns False. I have no idea why it's
> not True?
>
>
> On 10 June 2011 22:34, Aaron Meurer <[email protected]> wrote:
>>
>> I'm not the best person to answer this, but probably you are doing
>> this the wrong way.
>
> I'm pretty sure you are right about me being wrong :)
>
>>
>>  I suppose to know how you should do it, I would
>> need to know more about how you expect this class to work.  What do
>> you expect BeamParamter(0, 1) to return?  How about
>> BeamParameter(BeamParameter(x, y), z) (nested classes)?
>>
>> Aaron Meurer
>>
>> On Fri, Jun 10, 2011 at 12:10 PM, [email protected]
>> <[email protected]> wrote:
>> > Hi,
>> >
>> > I am trying to create a class that is basically the sum of two arguments
>> > given in the constructor. Here is what I have done (simplified example
>> > with
>> > omitted arguments and slots):
>> >
>> > class BeamParameter(Add):
>> >
>> >     __slots__ = ['x', 'y']
>> >
>> >     def __new__(cls, x, y):
>> >         obj = Add.__new__(cls, x, I*y)
>> >         obj.x = x
>> >         obj.y = y
>> >         return obj
>> >
>> >
>> > It works ok when Add returns Add:
>> >>>>q=BeamParameter(1,1)
>> >>>>q
>> > 1+I*1
>> >
>> > But it fails when Add returns Mul:
>> >>>>q=BeamParameter(0,1)
>> > ---> 54         obj.x = x
>> > AttributeError: 'Mul' object has no attribute 'x'
>> >
>> >
>> > The __slots__ are lost somewhere for some reason. I'm afraid that I
>> > don't
>> > understand why. Maybe I should not use Add as a superclass. Do you have
>> > any
>> > solutions?
>> >
>> > Stefan
>> >
>> > --
>> > 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.
>> >
>>
>> --
>> 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.
>>
>
> --
> 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.
>

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