This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Element wise multiplication issue
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ivan-marroquin, mark.dickinson
Priority: normal Keywords:

Created on 2017-07-14 13:21 by ivan-marroquin, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Python_Multiplication.zip ivan-marroquin, 2017-07-14 13:21 It is a compressed text file containing the data used to report the issue
Messages (2)
msg298353 - (view) Author: Ivan Marroquin (ivan-marroquin) Date: 2017-07-14 13:21
Hi all,

I am using an anaconda 4.3.18 64 bits installation on windows 7. the version of python is 3.6.1 and numpy version is 1.12.1

In Python, I have this element wise multiplication:
import numpy as np

#read the input data from an ascii file into the attributes_data #variable

def rankorder(attribute):
    sorted_items= np.sort(attribute, axis= 0, kind= 'mergesort')
    sorted_ids= np.argsort(attribute,axis= 0, kind= 'mergesort')
    
    # Find where are repetitions
    repeat_ids= (np.diff(sorted_items) == 0).astype(int)
    t_repeat_ids= [0]
    t_repeat_ids.extend(repeat_ids)
    
    # Rank with tieds with/withou skipping
    rankNoSkip= np.logical_not(t_repeat_ids).astype(int)
    rankNoSkip= np.cumsum(rankNoSkip)

    # Pre-allocate rank
    rank= np.arange(1, len(attribute) + 1)

    # Adjust for tieds (and skips)
    for i in range(0, len(rank)):
        if (t_repeat_ids[i] == 1):
            rank[i]= rankNoSkip[i]

    new_rank= np.zeros((len(attribute),1), np.int)
    
    for i in range(0, len(rank)):
        new_rank[sorted_ids[i]]= rank[i]

    return new_rank

rows= np.shape(attributes_data)[0]

R= rankorder(attributes_data[:,0])
S= rankorder(attributes_data[:,1])

A= np.zeros((rows), np.float64)

A= (R - 1) * (R - 2) * (S - 1) * (S - 2)

which for the first 20 and last 20 lines, I get:
[[ -490512836 -2014671888 45454500 88877908 168997500 -999033092
234376500 -1459172492 274758300 -1621798592 302597100 -1745619092
323687100 -1807113092 337229100 -1843876292 345487500 -1819378592
339970800 -1782548792]]
[[ 269791376 -882944896 -1010138236 -969282796 -132998296 -2128999336
62900136 25346184 1914059608 -1246247072 -2047681696 357533664
-1795623696 1896988000 767556900 757120 2046171488 365504
360674604 499699800 41184000]]

In Octave, I have the equivalent script. However, the computed values right at this command line:
A= (R - 1) .* (R - 2) .* (S - 1) .* (S - 2)

I get these results:
Columns 1 through 8:
1.6272e+011 1.0870e+010 4.5454e+007 8.6788e+009 1.6900e+008 7.5909e+009 2.3438e+008 7.1308e+009
Columns 9 through 16:
2.7476e+008 6.9681e+009 3.0260e+008 6.8443e+009 3.2369e+008 6.7828e+009 3.3723e+008 6.7461e+009
Columns 17 through 20:
3.4549e+008 6.7706e+009 3.3997e+008 6.8074e+009

ans =
Columns 1 through 8:
4.5648e+009 3.4120e+009 3.2848e+009 3.3257e+009 4.1620e+009 2.1660e+009 6.2900e+007 2.5346e+007
Columns 9 through 16:
1.0504e+010 7.3437e+009 2.2473e+009 3.5753e+008 2.4993e+009 1.8970e+009 7.6756e+008 7.5712e+005
Columns 17 through 21:
6.3411e+009 4.2953e+009 4.6556e+009 4.9970e+008 4.1184e+007

For some reason the computed values in Python are wrong. Any suggestions?

In the attached file, the first column corresponds to R variable and the second column corresponds to S variable.

Many thanks,
Ivan
msg298356 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-07-14 14:03
This is really a question for a NumPy mailing list, not for the Python bug tracker.

But the answer is that you're doing computations using 32-bit integers, and those computations overflow, leading to the odd results you're seeing. It looks as though your Octave computation is performed using floats, hence the different results.

Closing here: this isn't a bug (not even a NumPy bug, I'm afraid), and it's unrelated to core Python.
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75113
2017-07-14 14:03:36mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg298356

resolution: not a bug
stage: resolved
2017-07-14 13:21:15ivan-marroquincreate