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 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
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
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
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
thank you
I will have to take a closer look on __new__
Regards, Daniel This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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:...
|
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
{...};
...
|
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...
|
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...
|
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...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |