473,799 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why no arg, abs methods for comlex type?

Hello all,

I often have to deal with complex numbers
using python iteractive as calculator

I wonder why there are no methods like arg, abs

well one can use
c = 1+1j
abs(c)

In my opinion it would also be nice to have the
possibility to write it as
c.abs()
it looks more OO

unfortunately there is no arg method to get the angle
of the complex number
of course it easy to write one on your own
(that's what I have done for myself)
but differnt people will create and reinvite the wheel
over and over again
while reading alien code one will have to spend 2 seconds
remembering the function names etc

consider
c = 1+1j
c.arg(angle_mod e = cmath.GRAD) -> 45.0
or
c.arg(angle_mod e = cmath.RAD) -> 0.7853..

I would also like to see some more functions to make
calculations with complex number more convenient
e.g.
c = 27
c.pow(numerator = 1, denominator = 3,
mode = cmath.EULER, angle_mode = cmath.GRAD)
-> ((3,0), (3,120), (3,240))

what do you think about it?
maybe there exists some proposals aiming this goal?

--
Daniel
Aug 5 '05 #1
18 2207
> I would also like to see some more functions to make
calculations with complex number more convenient
e.g.
c = 27


c = 27+0j
Aug 5 '05 #2

"Daniel Schüle" <uv**@rz.uni-karlsruhe.de> wrote in message
news:dc******** **@news2.rz.uni-karlsruhe.de...
I wonder why there are no methods like arg, abs well one can use
c = 1+1j
abs(c)

In my opinion it would also be nice to have the
possibility to write it as
c.abs()
it looks more OO
Python is object based but not rigidly OO in syntax or looks. This is an
intentional design decision. Not being gratuitiously redundant is another.
unfortunately there is no arg method to get the angle
of the complex number
I agree that this is a deficiency. I would think .angle() should be a
no-param method like .conjugate(), though its internal implementation would
need access to the appropriate cmath functions. I think returning radians
might be enough though. You could submit to the SourceForge tracker a RFE
(Request For Enhancement) if not a patch.
I would also like to see some more functions to make
calculations with complex number more convenient
e.g.
c = 27
c.pow(numerator = 1, denominator = 3,
mode = cmath.EULER, angle_mode = cmath.GRAD)
-> ((3,0), (3,120), (3,240))
Wanting all roots is fairly specialized. sqrt(4) is 2, not (2, -2).
what do you think about it?
maybe there exists some proposals aiming this goal?


Have you looked for complex math functions in numpy, numarray, scipy or
similar packages? It is possible that a cmath2 module, written in Python,
could be useful.

Terry J. Reedy


Aug 5 '05 #3
On Fri, 05 Aug 2005 15:42:41 +0200,
Daniel Schüle <uv**@rz.uni-karlsruhe.de> wrote:
Hello all,
I often have to deal with complex numbers
using python iteractive as calculator I wonder why there are no methods like arg, abs well one can use
c = 1+1j
abs(c) In my opinion it would also be nice to have the
possibility to write it as
c.abs()
it looks more OO
I guess it's for the same reason that we are spared
from writing things like this:

z = x.squared().add ed_to(y.squared ()).squareroot( )
E = m.multiplied_by (c.squared())

More OO, yes. More readable, not IMO.
I would also like to see some more functions to make
calculations with complex number more convenient
[ ... ]
maybe there exists some proposals aiming this goal?


SciPy or Numeric?

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
Aug 5 '05 #4
Terry Reedy wrote:
"Daniel Schüle" <uv**@rz.uni-karlsruhe.de> wrote in message
news:dc******** **@news2.rz.uni-karlsruhe.de...

....
unfortunately there is no arg method to get the angle
of the complex number


I agree that this is a deficiency. I would think .angle() should be a
no-param method like .conjugate(), though its internal implementation would
need access to the appropriate cmath functions.


You need math, not cmath.

def arg(z):
"""The Argument of z, in radians."""
z += 0j # make it work on real inputs
return math.atan2(z.im ag, z.real)

Aug 5 '05 #5
On Fri, 05 Aug 2005 15:42:41 +0200, Daniel Schüle
<uv**@rz.uni-karlsruhe.de> declaimed the following in comp.lang.pytho n:

c = 1+1j
c.arg(angle_mod e = cmath.GRAD) -> 45.0
Is that right? The result looks more like Degrees...

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Aug 5 '05 #6
Hi Terry,
In my opinion it would also be nice to have the
possibility to write it as
c.abs()
it looks more OO

Python is object based but not rigidly OO in syntax or looks. This

is an intentional design decision. Not being gratuitiously redundant is another.

I agree, redundancy confuses people
unfortunate ly there is no arg method to get the angle
of the complex number

I agree that this is a deficiency. I would think .angle() should be a
no-param method like .conjugate(), though its internal implementation

would need access to the appropriate cmath functions. I think returning radians might be enough though.
I don't know what nomenclature is used in english speaking
mathematical world for angle of a complex number
I learned it in german as Arg(z) .. Arg standing for argument
you see, we would have named it differently, hence making
it difficult for the reader, eventually creating redundancy
You could submit to the SourceForge tracker a RFE
(Request For Enhancement) if not a patch.
I never did, but I will see
c = 1 + 1j
def arg(c, angle_mode = "RAD"): .... if angle_mode not in ("RAD", "GRAD"):
.... raise ValueError
.... import math
.... ret = math.atan2(c.im ag, c.real)
.... if angle_mode == "GRAD":
.... return ret / math.pi * 180
.... return ret

it's pretty easy and straightforward implementation
while writing this, I was struck by an idea and
reimplemented it as following
def arg(self, angle_mode = "RAD"): .... if angle_mode not in ("RAD", "GRAD"):
.... raise ValueError
.... import math
.... ret = math.atan2(self .imag, self.real)
.... if angle_mode == "GRAD":
.... return ret / math.pi * 180
.... return ret
.... class Complex(complex ): pass
Complex.arg = arg
c = Complex(1+1j)
c.arg(angle_mod e = "GRAD")
45.0

later I realized that this represents "yet another implementation"
which could be done by someone, thus again leading to possible
redundancy and confuse people

all this would not have happened when we would have
arg or angle builtin in complex type

I would also like to see some more functions to make
calculation s with complex number more convenient
e.g.
c = 27
c.pow(numerat or = 1, denominator = 3,
mode = cmath.EULER, angle_mode = cmath.GRAD)
-> ((3,0), (3,120), (3,240))

Wanting all roots is fairly specialized. sqrt(4) is 2, not (2, -2).


it could be named .allroots(n) -> ((), (), ())
with n whole number

indeed .pow() is not a good name for it, since z**x
always yields one complex number

if x is 2.5 or alike, it could be aproximated with n/m and use
res = []
for i in z.allroots(m):
res.append(i**n )

I am not sure, this is correct iterpretation of z**2.5
I will need to ask in math newsgroup :)
and approximation might not be good enough
Have you looked for complex math functions in numpy, numarray, scipy or
similar packages? It is possible that a cmath2 module, written in Python, could be useful.


I hope so
I will google for cmath2, I never heard about it
I am new to Numeric and numarray, I was playing with them and
ufunc-tionsand matplotlab, as for SciPy I couldnt find
any binary for 2.4 Python unfortunately :-/

Regards

--
Daniel
Aug 5 '05 #7
>>c = 1+1j
c.arg(angle_m ode = cmath.GRAD) -> 45.0

Is that right? The result looks more like Degrees...


maybe I confuse, in german one would say "45 Grad"
I took a freedom to translate it directly :)
well, my calculator shows a "D"
which most likely stands for Degree, I cannot tell for sure

--
Daniel
Aug 5 '05 #8
Daniel Schüle wrote:
what do you think about it?
maybe there exists some proposals aiming this goal?


Derive your own subclass of complex and define those methods.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I am not afraid / To be a lone Bohemian
-- Lamya
Aug 5 '05 #9
Daniel Schüle wrote:
maybe I confuse, in german one would say "45 Grad"
I took a freedom to translate it directly :)
well, my calculator shows a "D"
which most likely stands for Degree, I cannot tell for sure


Probably. In English, you have degrees and gradians, which aren't the
same thing; gradians are defined so that there are 400 gradians in a
circle (so 100 gradians in a right angle).

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I am not afraid / To be a lone Bohemian
-- Lamya
Aug 5 '05 #10

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

Similar topics

99
5928
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
5
2268
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release() around the existing code. So I made a metaclass that essentially replaces every method of a class with a 'wrapper' method, that does the locking, invocation, unlocking. Is this the right approach? It seems to work fine. But I have
51
7018
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or maybe even just declare an interface, with no methods implemented). Right now, you don't really have a way to do it. You can leave the methods with a "pass", or raise a NotImplementedError, but even in the best solution that I know of,
32
4529
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
3
2088
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
4
1511
by: Alex Sedow | last post by:
Method invocation will consist of the following steps: 1. Member lookup (14.3) - evaluate method group set (Base.f() and Derived.f()) .Standart say: "The compile-time processing of a method invocation of the form M(A), where M is a method group and A is an optional argument-list, consists of the following steps:  The set of candidate methods for the method invocation is constructed. Starting with the set of methods associated with M,...
33
3503
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able to re-generate the classes on level 2 I have a 3rd level that inherits from them and implements specific hand coded methods. 4. My colleague asked me to create a 4th level class that inherits the 3rd level class so he can write custom...
10
105204
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
318
11141
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html Just to keep the post on topic for my friends at comp.lang.c++, how do I play default windows sounds with C++?
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 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...
1
10228
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,...
0
10027
tracyyun
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...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
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...

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.