473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

deriving from complex

Hello

I am trying to customize the handling of complex numbers
what I am missing is a builtin possibility to create
complex numbers in polar coordinates

so first I wrote a standalone function
def polar(r,arg): .... re, im = r*cos(arg), r*sin(arg)
.... return re + im*1j

then I tried to extend this to a class
from math import * class Complex(complex): .... def __init__(self,x,y,polar=False):
.... if not polar:
.... self.re, self.im = x,y
.... else:
.... self.re, self.im = x*cos(y), x*sin(y)
....
c=Complex(1,1)
c (1+1j) p=Complex(10,45.0/360*2*pi,True) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: complex() takes at most 2 arguments (3 given)
and got stuck with this error
it seems that last argument is rejected
because complex wants to have 2 arguments
but this works well ..
class X(object): .... def __init__(self,a):
.... self.a = a
.... class Y(X): .... def __init__(self,a,b):
.... self.a = a
.... self.b = b
.... y=Y(1,2)
what's causing the above exception?

one more question
class Complex(complex): .... def __init__(self,x,y):
.... self.real = x
.... self.imag = y
.... c=Complex(1,1) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in __init__
TypeError: readonly attribute


how can I work around this problem?

Regards, Daniel

Mar 8 '06 #1
7 1495
what do you think of this design?
def polar(x,y=None): .... if type(x) in (list,tuple) and len(x) == 2 and y is None:
.... return complex(x[0]*cos(x[1]), x[0]*sin(x[1]))
.... if type(x) is complex and y is None:
.... return (abs(x), atan2(x.imag,x.real))
.... if type(x) in (float, int, long) and type(y) in (float, int, long):
.... return complex(x*cos(y), x*sin(y))
.... polar(2**0.5, 45.0/360*2*pi) (1.0000000000000002+1j) polar((2**0.5, 45.0/360*2*pi)) (1.0000000000000002+1j) polar([2**0.5, 45.0/360*2*pi]) (1.0000000000000002+1j) polar(1+1j) (1.4142135623730951, 0.78539816339744828)


btw I like how Ruby handles the creation of complex numbers

c = Complex(1,1)
p = Complex.polar(1,45.0/360*2*PI)

Regards, Daniel

Mar 8 '06 #2
Schüle Daniel wrote:
I am trying to customize the handling of complex numbers
what I am missing is a builtin possibility to create
complex numbers in polar coordinates.... I wrote...:
>>> def polar(r,arg): ... re, im = r*cos(arg), r*sin(arg)
... return re + im*1j
then I tried to extend this to a class >>> class Complex(complex):

... def __init__(self,x,y,polar=False):
... if not polar:
... self.re, self.im = x,y
... else:
... self.re, self.im = x*cos(y), x*sin(y)


What you are missing is that complex is an immutable type.
You need to fiddle with __new__, not __init__.

class Complex(complex):
def __new__(class_, x, y=0.0, polar=False):
if polar:
return complex.__new__(class_, x * cos(y), x * sin(y))
else:
return complex.__new__(class_, x, y)

Which will produce instances of Complex, or:

class Complex(complex):
def __new__(class_, x, y=0.0, polar=False):
if polar:
return complex(x * cos(y), x * sin(y))
else:
return complex(class_, x, y)

Which will produce instances of complex.

--Scott David Daniels
sc***********@acm.org
Mar 8 '06 #3
Scott David Daniels wrote:
Schüle Daniel wrote:
...
And, of course, I reply with a cutto-pasto (pre-success code). ...
Which will produce instances of Complex, or:
class Complex(complex):
def __new__(class_, x, y=0.0, polar=False):
if polar:
return complex(x * cos(y), x * sin(y))
else:
return complex(class_, x, y)

This last line should, of course, be:
return complex(x, y)

--Scott David Daniels
sc***********@acm.org
Mar 8 '06 #4
Schüle Daniel wrote:
....
btw I like how Ruby handles the creation of complex numbers

c = Complex(1,1)
p = Complex.polar(1,45.0/360*2*PI)


class Complex(complex):
@classmethod
def polar(class_, radius, angle):
return class_(radius * cos(angle), radius * sin(angle))

--Scott David Daniels
sc***********@acm.org
Mar 8 '06 #5
thank you
I will have to take a closer look on __new__

Regards, Daniel

Mar 8 '06 #6

Schüle Daniel wrote:
thank you
I will have to take a closer look on __new__

Regards, Daniel


The pypy implementation of the complex builtin may give you some
clues.

I found it helpful.

http://codespeak.net/svn/pypy/dist/pypy/lib/cmath.py

Art

Mar 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Steven T. Hatton | last post by:
This may be another question having an obvious answer, but I'm not seeing it. I'm trying to create a class that derives from std::valarray<std::string>. I don't need a template, and I haven't come...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: bclark76 | last post by:
I am getting a strange error, maybe someone knows why it is occurring.. I get the following error when I try to validate Untitled8.xml in Altova XMLSPY: Validation error in another file:...
12
by: Mark P | last post by:
Is it guaranteed that STL iterators are aggregate types and can thus be derived from? I'm thinking along the lines of, for example: class DerIter : public std::vector<int>::iterator {...}; ...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
17
by: mosfet | last post by:
Could someone tell me why it's considered as bad practice to inherit from STL container ? And when you want to customize a STL container, do you mean I need to write tons of code just to avoid to...
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
7
by: Christopher Pisz | last post by:
I found an article http://spec.winprog.org/streams/ as a starting point, but it seems to do alot of things that aren't very standard at all. One particular problem is, that he is using a vector as...
3
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.