[Tutor] Floor and modulus for complex arguments

Angus Rodgers angusr at bigfoot.com
Fri Jul 3 21:37:09 CEST 2009


>Date: Sat, 04 Jul 2009 00:25:13 +1000
>From: Lie Ryan <lie.1296 at gmail.com>
>Message-ID: <h2l4bm$mh0$1 at ger.gmane.org>
>
>Angus Rodgers wrote:
>
>> I /think/ I would be naturally inclined to define:
>> 
>>  floor(x + yj) = floor(x) + floor(y)j     for all real x, y
>> 
>>  z % w = z - floor(z / w) * w    for all complex z, w (!= 0)
>
>I'm not a mathematician, and I understand zilch about complex numbers.
>But looking literally at your definition, python 2.5 seems to define
>complex mod complex as you have defined:
>
>>>> import math
>>>> def floor(comp):
>...     return math.floor(comp.real) + math.floor(comp.imag) * 1j
>...
>>>> def mod(z, w):
>...     return z - floor(z / w) * w
>...
>>>> mod(10.4j+5.1, 5.2j+3.2)
>(1.8999999999999995+5.2000000000000002j)
>>>> (10.4j+5.1) % (5.2j+3.2)
>__main__:1: DeprecationWarning: complex divmod(), // and % are deprecated
>(1.8999999999999995+5.2000000000000002j)

mod(z, w) and (z % w) will give the same result whenever (z / w).imag
is >= 0 and < 1.  In particular, if also (z / w).real is >= 1 and < 2,
both expressions will be equal to z - w (as in this example).  But in
most cases they will give different results, e.g.:

>>> z = 1.5 + 1.5j
>>> mod(z, 1)
(0.5+0.5j)
>>> z % 1
(0.5+1.5j)

>> These seem like they would be mathematically useful definitions
>> (e.g. in algebraic number theory, where one has to find the
>> "nearest" Gaussian integer multiple of one Gaussian integer to
>> another - I forget the details, but it has something to do with
>> norms and Euclidean domains) [...]
>> 
>> Has there been heated debate about this (e.g. in the context
>> of Python 3, where the change to the division operator has 
>> apparently already provoked heated debate)?
>
>There is this:
>http://mail.python.org/pipermail/python-dev/2002-April/023251.html
>http://mail.python.org/pipermail/python-dev/2002-April/023259.html

I agree with Tim Peters that the original definition of complex
// and % makes no sense.  At least it makes no more sense to me
than it does to him; and that was indeed the reason for my post.

But I won't wax evangelical over my off-the-cuff definitions of
floor (and implicitly //) and % for complex numbers, especially
as the one application I only vaguely hinted at would actually 
need this instead:

 round(x + yj) = round(x) + round(y)j     for all real x, y

Or, in Python, something more like this (because unfortunately
'round' is built in):

def cround(z):
    return complex(round(z.real), round(z.imag))

The details I omitted are here, in case anyone wants to know!
<http://en.wikipedia.org/wiki/Gaussian_integer>
"It is easy to see graphically that every complex number is within
\frac{\sqrt 2}{2} units of a Gaussian integer. Put another way, 
every complex number (and hence every Gaussian integer) has a 
maximal distance of \frac{\sqrt 2}{2}\sqrt{N(z)} units to some
multiple of z, where z is any Gaussian integer; this turns Z[i]
into a Euclidean domain, where v(z) = N(z)."

>But it seems the reason is because when the decision was made, nobody in
>the devteam understand the reasoning behind complex numbers implementing
>the divmod, //, and % as such, and caused it to be removed due to being
>"makes no sense". Perhaps if you can convince the devteam about the math
>behind complex mod complex, this feature can be reintroduced. FWIW it
>has been 5+ years and nobody seems to complain before, it seems complex
>mod complex must have been very rarely used.

If I started to agitate for changes to a marginal and little-used
feature of the language within days of starting to learn it, might
I not quickly earn a reputation as a crank?  8-P

I don't know how useful it would be to [re]define floor(), //, % and
cround() for complex numbers.  Not very, if nobody uses them!  All 
I can say is that my suggested definitions seem to make more sense
than the original definitions seem to.  They do at least have an
easily pictured geometrical interpretation.  Imagine all multiples
of the given non-zero complex number w by complex numbers of the 
form m + nj, where m and n are integers: they form an infinite grid
of squares in the complex plane, and we are interested in which of
these squares the other given complex number z falls into, or else
we are interested in which vertex of the grid z lies closest to.
-- 
Angus Rodgers


More information about the Tutor mailing list