summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/_add_newdocs.py7
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src52
-rw-r--r--numpy/core/tests/test_longdouble.py13
3 files changed, 70 insertions, 2 deletions
diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py
index dbe3d226f..30379dfbe 100644
--- a/numpy/core/_add_newdocs.py
+++ b/numpy/core/_add_newdocs.py
@@ -1036,7 +1036,12 @@ add_newdoc('numpy.core.multiarray', 'fromstring',
A string containing the data.
dtype : data-type, optional
The data type of the array; default: float. For binary input data,
- the data must be in exactly this format.
+ the data must be in exactly this format. Supported dtypes are
+ byte, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong,
+ datetime, timedelta, float, double, longdouble, half, bool,
+ cfloat, and cdouble.
+
+ Complex dtypes are only supported since numpy 1.18.0
count : int, optional
Read this number of `dtype` elements from the data. If this is
negative (the default), the count will be determined from the
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index 5d9e990e8..53dfe7ca7 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -1849,7 +1849,57 @@ BOOL_fromstr(char *str, void *ip, char **endptr,
}
/**begin repeat
- * #fname = CFLOAT, CDOUBLE, CLONGDOUBLE,
+ * #fname = CFLOAT, CDOUBLE#
+ * #type = npy_cfloat, npy_cdouble#
+ */
+static int
+@fname@_fromstr(char *str, void *ip, char **endptr,
+ PyArray_Descr *NPY_UNUSED(ignore))
+{
+ double result;
+
+ result = NumPyOS_ascii_strtod(str, endptr);
+
+ @type@ output;
+
+ if (endptr && ((*endptr[0] == '+') || (*endptr[0] == '-'))) {
+ // Imaginary component specified
+
+ output.real = result;
+
+ // Reading imaginary component
+ str = *endptr;
+ result = NumPyOS_ascii_strtod(str, endptr);
+
+ output.imag = result;
+
+ // Skip j
+ ++*endptr;
+ }
+ else if (endptr && *endptr[0] == 'j') {
+ // Real component not specified
+
+ output.real = 0;
+ output.imag = result;
+
+ // Skip j
+ ++*endptr;
+ }
+ else {
+ // Imaginary component not specified
+
+ output.real = result;
+ output.imag = 0.;
+ }
+
+ *(@type@ *)ip = output;
+ return 0;
+}
+/**end repeat**/
+
+
+/**begin repeat
+ * #fname = CLONGDOUBLE,
* OBJECT, STRING, UNICODE, VOID#
*/
diff --git a/numpy/core/tests/test_longdouble.py b/numpy/core/tests/test_longdouble.py
index 59ac5923c..8e1c9d153 100644
--- a/numpy/core/tests/test_longdouble.py
+++ b/numpy/core/tests/test_longdouble.py
@@ -71,6 +71,19 @@ def test_fromstring():
err_msg="reading '%s'" % s)
+def test_fromstring_complex():
+ for ctypes in ["complex", "cdouble", "cfloat"]:
+ # Check spacing between separator
+ assert_equal(np.fromstring("1, 2 , 3 ,4",sep=",",dtype=ctypes),
+ np.array([1., 2., 3., 4.]))
+ # Real component not specified
+ assert_equal(np.fromstring("1j, -2j, 3j, 4e1j",sep=",",dtype=ctypes),
+ np.array([1.j, -2.j, 3.j, 40.j]))
+ # Both components specified
+ assert_equal(np.fromstring("1+1j,2-2j, -3+3j, -4e1", sep=",", dtype=ctypes),
+ np.array([1. + 1.j, 2. - 2.j, - 3. + 3.j, - 40.]))
+
+
def test_fromstring_bogus():
with assert_warns(DeprecationWarning):
assert_equal(np.fromstring("1. 2. 3. flop 4.", dtype=float, sep=" "),