Changes between Initial Version and Version 2 of Ticket #29230


Ignore:
Timestamp:
Feb 22, 2020, 11:48:40 AM (3 years ago)
Author:
gh-garambois
Comment:

Thank you very much chapoton.

I have tried to follow your advice and also the advice given in the link you sent me.

So I modified the Ticket with the new code. And I enclose a modified "sigma_gauss_function.py" file. Sorry, I attached the .py file twice, it's useless, but I couldn't remove the second one.

To switch from sage code to python code, I also had to make some other changes.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #29230 – Description

    initial v2  
    1010
    1111{{{#!python
    12 #Calculation program sigma(z), z Gaussian integer.
    13 #Calculates the sum of the complex divisors of z.
    14 #The Gaussian integer prime factors must be taken in the first quadrant.
    15 #Observation: equivalent to the Mathematica's function DivisorSigma [1, z, GaussianIntegers -> True]
    16 #References http://mathworld.wolfram.com/DivisorFunction.html
    17 #References https://www.jstor.org/stable/2312472?seq=1
    18 #References https://encompass.eku.edu/etd/158/
     12r"""
     13sigma_gauss function (sum of divisors) applicable to a Gaussian integer.
     14
     15This function is an extension of the sigma function in number theory to Gaussian integers.
     16
     17
     18INPUT:
     19
     20    ''z'' -- GaussianInteger
     21
     22OUTPUT:
     23
     24    ''sigma_gauss(z)'' -- GaussianInteger, the sum of the Gaussian integer divisors of z
     25
     26    Caution: The Gaussian integer prime factors are only taken in the first quadrant,
     27    see why in the references below.
     28
     29EXAMPLES:
     30
     31The sum of the divisors of Gauss integers z = 2*I + 2, z = 3*I + 2, z = 13::
     32
     33    sage: sigma_gauss(2*I + 2)
     34    5*I
     35    sage: sigma_gauss(3*I + 2)
     36    3*I + 3
     37    sage: sigma_gauss(5)
     38    8*I + 4
     39
     40AUTHORS:
     41
     42    Paul Zimmermann and Jean-Luc Garambois (2019): initial version
     43
     44REFERENCES:
     45
     46    [1] http://mathworld.wolfram.com/DivisorFunction.html
     47        Equivalent on the Mathematica software to DivisorSigma [1, z, GaussianIntegers -> True]
     48    [2] https://www.jstor.org/stable/2312472?seq=1
     49    [3] https://encompass.eku.edu/etd/158/
     50"""
     51# ****************************************************************************
     52#       Copyright (C) 2019 Jean-Luc Garambois <jlgarambois@gmail.com>
     53#
     54# This program is free software: you can redistribute it and/or modify
     55# it under the terms of the GNU General Public License as published by
     56# the Free Software Foundation, either version 2 of the License, or
     57# (at your option) any later version.
     58#                  https://www.gnu.org/licenses/
     59# ****************************************************************************
     60
     61from sage.functions.other import real, imag
     62from sage.rings.number_field.order import GaussianIntegers
    1963
    2064R = GaussianIntegers()
    2165
    2266def sigma_gauss_aux (l):
    23    s = 1
    24    e=0
    25    while e<len(l):
    26       p = l[e][0]
    27       k = l[e][1]
    28       s = s * (p^(k+1)-1) / (p-1)
    29       e+=1
    30    return R(s)
     67    s = 1
     68    e = 0
     69    while e < len(l):
     70        p = l[e][0]
     71        k = l[e][1]
     72        s = s * (p**(k+1) - 1) / (p-1)
     73        e = e + 1
     74    return R(s)
    3175
    3276def sigma_gauss(z):
    33    ll=list(R(z).factor())
    34    l=[]
    35    i=0
     77    ll = list(R(z).factor())
     78    l = []
     79    i = 0
    3680
    37 #Gaussian prime factors must be selected in the first quadrant.
    38 #To understand why, refer to the references cited at the beginning of the program.
     81# ****************************************************************************
     82# Gaussian prime factors must be selected in the first quadrant.
     83# ****************************************************************************
    3984
    40    while i<len(ll):
    41       nz=ll[i][0]
    42       if real(nz)<0 and imag(nz)<0:
    43          nz=-nz
    44       if real(nz)<0 and imag(nz)>=0:
    45          nz=-I*nz
    46       if real(nz)>=0 and imag(nz)<0:
    47          nz=I*nz
    48       assert real(nz)>=0 and imag(nz)>=0
    49       lz=[nz,ll[i][1]]
    50       l.append(lz)
    51       i+=1
    52    r=0
     85    while i < len(ll):
     86        nz = ll[i][0]
     87        if real(nz) < 0 and imag(nz) < 0:
     88            nz = -nz
     89        if real(nz) < 0 and imag(nz) >= 0:
     90            nz = -I * nz
     91        if real(nz) >= 0 and imag(nz) < 0:
     92            nz = I * nz
     93        assert real(nz) >= 0 and imag(nz) >= 0
     94        lz = [nz, ll[i][1]]
     95        l.append(lz)
     96        i = i + 1
     97    r = 0
    5398
    54 #Be careful not to have the same factor twice in the list of factors after their selection in the first quadrant and before applying the "sigma_gauss_aux" function.
     99# ****************************************************************************
     100# Be careful not to have the same factor twice after their selection
     101# in the first quadrant and before applying the "sigma_gauss_aux" function.
     102# ****************************************************************************
    55103
    56    while r<len(l)-1:
    57       t=r+1
    58       while t<len(l):
    59          if l[r][0]==l[t][0]:
    60             l[r][1]=l[r][1]+l[t][1]
    61             del l[t]
    62             t-=1
    63          t+=1
    64       r+=1
    65    for x,e in l:
    66       assert e != 0
    67    return sigma_gauss_aux(l)
     104    while r < len(l) - 1:
     105        t = r + 1
     106        while t < len(l):
     107            if l[r][0] == l[t][0]:
     108                l[r][1] = l[r][1] + l[t][1]
     109                del l[t]
     110                t = t - 1
     111            t = t + 1
     112        r= r + 1
     113    for x, e in l:
     114        assert e != 0
     115    return sigma_gauss_aux(l)
    68116}}}