Skip to content

Commit a90e4a3

Browse files
author
Release Manager
committed
sagemathgh-38833: Respect sparse=True in vector constructor when passed ndarray Self-explanatory. (Of course user would not normally do this but… just in case.) ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: sagemath#38833 Reported by: user202729 Reviewer(s): Kwankyu Lee
2 parents 2304cfd + 05e55e2 commit a90e4a3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/sage/modules/free_module_element.pyx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,16 @@ def vector(arg0, arg1=None, arg2=None, sparse=None, immutable=False):
486486
sage: vector(S, 3) # needs sage.rings.finite_rings
487487
...
488488
(0, 0, 0)
489+
490+
We check that ``sparse`` is respected for numpy arrays::
491+
492+
sage: # needs numpy
493+
sage: import numpy
494+
sage: a = numpy.array([1,2,3], dtype=numpy.float64)
495+
sage: v = vector(a, sparse=True); v
496+
(1.0, 2.0, 3.0)
497+
sage: v.is_sparse()
498+
True
489499
"""
490500
from sage.modules.free_module import FreeModule
491501
# We first efficiently handle the important special case of the zero vector
@@ -563,7 +573,7 @@ def vector(arg0, arg1=None, arg2=None, sparse=None, immutable=False):
563573
except ImportError:
564574
pass
565575
else:
566-
if isinstance(v, ndarray):
576+
if isinstance(v, ndarray) and not sparse:
567577
if len(v.shape) != 1:
568578
raise TypeError("cannot convert %r-dimensional array to a vector" % len(v.shape))
569579
from sage.modules.free_module import VectorSpace

0 commit comments

Comments
 (0)