473,809 Members | 2,722 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
18 2209
Erik Max Francis wrote:
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).


In German, they're "Altgrad" (degrees) and "Neugrad" or "Gon" (gradians).

Reinhold
Aug 5 '05 #11

"Daniel Schüle" <uv**@rz.uni-karlsruhe.de> wrote in message
news:dd******** **@news2.rz.uni-karlsruhe.de...
I agree that this is a deficiency. I would think .angle() should be a


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


I am aware of the usage of argument to mean the angle in polar
representation, but I don't like it. The word argument already has two
other meanings, one in common English, the other in math/CS. The latter
meaning is the inputs to a function, and that is how the word is used in
Python (though the former applies more to many c.l.p threads ;-) To me,
the polar angle has no connection with either meaning and so the usage is
'like Greek' to me. Whereas angle is exactly what it is.

As for Greek: I first learned r(adius),theta (versus x,y or real,imag) as
the names for polar coordinates or the polar representation for complex
numbers and only ran into arg much later in some contexts. And I have seen
complex number implementations that use the equivalent of c.r() and
c.theta(). But I did not suggest that for one of the reasons I don't like
'lambda': its fine if you already know it and arbitrary if you don't. (Is
theta used in Germany?)
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


That is because we have not written it yet. The allroots function could be
the first addition, if it is not present elsewhere. The 'could be' was
meant in the sense of 'if someone were to write it' rather than 'if you
were to read it' ;-)

Terry J. Reedy

Aug 5 '05 #12
[...]
Derive your own subclass of complex and define those methods.


I think something as basic as an angle/arg of complex number
definetly belongs to the interface, and it would not even require a
great effort to put it there

most complex formulas out there use Euler representation
it's a waste of code lines (and programmers time)
to write 3 liner functions for the transformion between
a+bj <-> (r,angle)
Python makes things covenient
so we have complex numbers in the core language
the calculations where perfectly possible without them
using (re, im) tupels and many many sin/cos in the code
but imagine how ugly it would be ..
I would like see Python as a competitor to Matlab etc

Regards

--
Daniel

Aug 5 '05 #13
[...]
I am aware of the usage of argument to mean the angle in polar
representation, but I don't like it. The word argument already has two
other meanings, one in common English, the other in math/CS. The latter
meaning is the inputs to a function, and that is how the word is used in
Python (though the former applies more to many c.l.p threads ;-) To me,
the polar angle has no connection with either meaning and so the usage is
'like Greek' to me. Whereas angle is exactly what it is.
..angle() would be also alright, as it is easy to grasp
As for Greek: I first learned r(adius),theta (versus x,y or real,imag) as
the names for polar coordinates or the polar representation for complex
numbers and only ran into arg much later in some contexts. And I have seen
complex number implementations that use the equivalent of c.r() and
c.theta(). But I did not suggest that for one of the reasons I don't like
'lambda': its fine if you already know it and arbitrary if you don't. (Is
theta used in Germany?)


yes, of course
the entire mathematic is full of them :)
as for the complex numbers, in our lessons we used R(adius) and
Phi for the angle for polar representation
I guess it's more a question of teacher's preference than a national
norm
Aug 5 '05 #14
On Fri, 05 Aug 2005 18:24:26 +0200, Daniel Schüle
<uv**@rz.uni-karlsruhe.de> declaimed the following in comp.lang.pytho n:
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


45 Degrees => 50 Grad

2PI => 360 Degree => 400 Grad

(and military protractors are the only place I've seen Grads
used)
-- =============== =============== =============== =============== == <
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 6 '05 #15
Daniel Schüle wrote:
[...]
> Derive your own subclass of complex and define those methods.
I think something as basic as an angle/arg of complex number
definetly belongs to the interface, and it would not even require a
great effort to put it there


<shrug> Okay. Write a patch. Personally, I would prefer that it be a
function in cmath rather than a method because then it could be made to
work on integers and regular floats, too.
most complex formulas out there use Euler representation
it's a waste of code lines (and programmers time)
to write 3 liner functions for the transformion between
a+bj <-> (r,angle)
It's a very, very tiny outlay of effort on the programmer's part that
only has to happen once in their career.
Python makes things covenient
so we have complex numbers in the core language
the calculations where perfectly possible without them
using (re, im) tupels and many many sin/cos in the code
but imagine how ugly it would be ..
I don't have to imagine. It's not all that ugly.
I would like see Python as a competitor to Matlab etc


I think it is already.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 6 '05 #16
[...]
<shrug> Okay. Write a patch. Personally, I would prefer that it be a
function in cmath rather than a method because then it could be made to
work on integers and regular floats, too.


Ok, but what semantic should angle/arg have, say for 3 respectively
for 3.0?
the same as for arg(3+0j)?
Aug 6 '05 #17
Daniel Schüle wrote:
<shrug> Okay. Write a patch. Personally, I would prefer that it be a
function in cmath rather than a method because then it could be made
to work on integers and regular floats, too.


Ok, but what semantic should angle/arg have, say for 3 respectively
for 3.0?
the same as for arg(3+0j)?


As a potential user, that's what I would expect.

See Dan Bishop's implementation (earlier in this thread).
Aug 6 '05 #18
On Sat, 06 Aug 2005 06:44:03 GMT, Dennis Lee Bieber <wl*****@ix.net com.com> wrote:
On Fri, 05 Aug 2005 18:24:26 +0200, Daniel Schüle
<uv**@rz.uni-karlsruhe.de> declaimed the following in comp.lang.pytho n:
>>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


45 Degrees => 50 Grad

2PI => 360 Degree => 400 Grad

(and military protractors are the only place I've seen Grads
used)

No one seems to have mentioned 2PI => 1 circle as in unit circles.
IIRC, back in the day before math chips, we implemented all the trig
functions in terms of angles where some number of bits respresented
one circle, for best resolution of angles with a given number of bits.
Also for compatibility with angular input devices. Also for the natural
modulo 2PI effect of representing an angle as an int, with unsigned interval
[0..2PI) or signed interval [-PI..PI). IIRC, FFTs involve phase in steps of
2PI/2**n also (for power-of-2 decimation effectively dividing the unit circle
in terms of powers of e**-i*(2*pi/2**n)) (where i is imaginary 0+1j). Hm, let' see,
for 45-degree deltas (2PI/2**3) ...
import cmath
(cmath.e+0j)**(-2j*cmath.pi/2**3) (0.707106781186 54757-0.7071067811865 4746j)
for c in [((cmath.e+0j)** (-2j*cmath.pi/2**3))**i for i in xrange(8)]: print c

...
(1+0j)
(0.707106781187-0.707106781187j )
(1.5701957963e-016-1j)
(-0.707106781187-0.707106781187j )
(-1-3.1403915926e-016j)
(-0.707106781187+ 0.707106781187j )
(-4.71058738891e-016+1j)
(0.707106781187 +0.707106781187 j)

Regards,
Bengt Richter
Aug 6 '05 #19

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

Similar topics

99
5934
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
2271
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
7028
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
4533
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
3505
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
105211
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
11164
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
9721
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
9602
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,...
0
10376
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
10383
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
10120
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...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.