numpy.sum() returns 0-dimensional array, rather than a scalar

Issue #57 resolved
Eric Hermes created an issue

The numpy function sum() returns a 0-dimensional numpy array instead of a scalar. This results in breakage when interacting with objects and methods that expect a scalar type. I encountered this problem when using numpy with sympy:

import numpy as np
from sympy import symbols

x = symbols('x')

# Works
foo = np.array([1, 2, 3, 4]).sum() * x
print(foo.subs({x: 20}))

# Does not work
bar = np.sum([1, 2, 3, 4]) * x
print(bar.subs({x: 20}))

This script prints 200 twice when run in CPython (or when run in pypy using upstream numpy with cpyext), but only prints 200 once and fails with the following error message using Pypy numpy:

Traceback (most recent call last):
  File "pypy_test.py", line 14, in <module>
    print(bar.subs({x: 20}))
AttributeError: 'numpy.ndarray' object has no attribute 'subs'

Comments (2)

  1. Log in to comment