Making it print correctly is just a matter of defining a printer for
your class.  In this case, just make the printer print the combined
expression.

Making it work with other sympy stuff is a matter of over-ridding the
right methods in your class, so that other SymPy classes/functions
know how your class "works".  Again, for most stuff, you can just make
Complex.whatever() return (Complex.re + Complex.im*I).whatever() (but
some stuff, like .as_real_imag() and _eval_expand_complex() can be
optimized).

One thing that I'm not sure if you can get to work correctly is something like

x, y = symbols('x y')
x + Complex(x, y) # or Complex(x + y*I), depending on how you plan to
implement it

Because Add won't know to combine the x into the real part of the
Complex.  See issue 1941.

Aaron Meurer

On Fri, Jun 10, 2011 at 3:18 PM, [email protected]
<[email protected]> wrote:
> Here is a workaround that I dislike (but it works):
>
> class BeamParameter(Expr):
>
>     __slots__ = ['z', 'z_r']
>
>     def __new__(cls, z, z_r):
>         inst = Expr.__new__(cls, z, z_r)
>         inst.z = z
>         inst.z_r = z_r
>         return inst
>
>     @property
>     def q(self):
>         return self.z+I*self.z_r
>
> Now I can do:
>>>>bp = BeamParameter(1,2)
>>>>bp.q
> 1+I*2
>
> But I would like to have:
>>>>bp
> 1+I*2
>
>
> On 10 June 2011 23:06, [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