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 1510
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.000000000000 0002+1j) polar((2**0.5, 45.0/360*2*pi))
(1.000000000000 0002+1j) polar([2**0.5, 45.0/360*2*pi])
(1.000000000000 0002+1j) polar(1+1j)
(1.414213562373 0951, 0.7853981633974 4828)
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***********@a cm.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***********@a cm.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***********@a cm.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
across any examples of a construct like what I show below. Perhapes it's
simply a bad idea. If there is something fundamentally wrong with this
approach please let me know.
Can anybody tell me if there is a way to get the following to work? I...
|
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 list class must Ioverride in order for this
to work?
|
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: Untitled3.xsd
The message in XMLSPY in Untitled3.xsd is: The file is not valid: The
content model of complex type { no name } is not a valid restriction
of the content model of complex type 'Condiment_Type'
|
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
{...};
My concern is whether the standard might allow an implementation to use
a bare pointer as an 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 containers destructors virtual >
| |
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 derive from it ?
|
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 idea? I was under the
impression that deriving from std classes that do not have virtual methods
was bad. Is ostream designed to be derived from?
If, so are there any resources on how to do this properly? ostream has alot
if internals that look...
|
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 his
input buffer and trys to assign an iterator to a char pointer.
....
class winzoostreambuffer : private LoggerConsole, public
sts::basic_streambuf<char, std::char_traits<char>>
....
|
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. I am not satisfied with the underlying machinery of the solution though.
I am an advanced C programmer and most do object-based programming in C++. Please do not reply to this article with references to basic material or obvious tips.
An...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |