Skip to content

sum along axis gives different answer for C v F ordered array #13240

@dvincentwest

Description

@dvincentwest

when doing a sum along an axis, I get different answers for C versus Fortran ordered arrays.

In the example below I have a large 2D array where all values are zero except for the first row. Slicing the first row and summing gives the same answer for both arrays. However, summing along the second axis and taking the first element of the result gives a different answer for the F ordered array while taking the total sum of the array gives a third answer, while for the C-ordered array, all three methods yield the expected same answer

def test_array_sum_function(arr):
    idx=0
    val1 = arr[idx, :].sum()
    val2 = arr.sum(axis=(1))[idx]
    print('axis sums:', val1)
    print('          ', val2)
    print('    equal:', val1 == val2)
    print('total sum:', arr.sum())

n = 2_000_000
np.random.seed(42)
rnd = np.random.random(n)

print('Fortran order:')
arrF = np.zeros((2, n), order='F')
arrF[0, :] = rnd
test_array_sum_function(arrF)

print('\nC order:')
arrC = np.zeros((2, n), order='C')
arrC[0, :] = rnd
test_array_sum_function(arrC)

prints:

Fortran order:
axis sums: 999813.1414744433
           999813.1414744079
    equal: False
total sum: 999813.1414744424

C order:
axis sums: 999813.1414744433
           999813.1414744433
    equal: True
total sum: 999813.1414744433

Numpy/Python version information:

1.15.4
3.7.1 (default, Dec 14 2018, 19:28:38)
[GCC 7.3.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions