Repository: ivy-llc/ivy

Issue:
asmatrix



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/numpy/manipulation_routines/changing_kind_of_array.py b/ivy/functional/frontends/numpy/manipulation_routines/changing_kind_of_array.py
--- a/ivy/functional/frontends/numpy/manipulation_routines/changing_kind_of_array.py
+++ b/ivy/functional/frontends/numpy/manipulation_routines/changing_kind_of_array.py
@@ -0,0 +1,7 @@
+# local
+import ivy.functional.frontends.numpy as np_frontend
+import ivy
+
+
+def asmatrix(data, dtype=None):
+    return np_frontend.matrix(ivy.array(data), dtype=dtype, copy=False)
diff --git a/ivy/functional/frontends/numpy/matrix/methods.py b/ivy/functional/frontends/numpy/matrix/methods.py
--- a/ivy/functional/frontends/numpy/matrix/methods.py
+++ b/ivy/functional/frontends/numpy/matrix/methods.py
@@ -11,9 +11,9 @@
 
 class matrix:
     def __init__(self, data, dtype=None, copy=True):
-        self._init_data(data, dtype)
+        self._init_data(data, dtype, copy)
 
-    def _init_data(self, data, dtype):
+    def _init_data(self, data, dtype, copy):
         if isinstance(data, str):
             self._process_str_data(data, dtype)
         elif isinstance(data, (list, ndarray)) or ivy.is_array(data):
@@ -21,7 +21,7 @@ def _init_data(self, data, dtype):
                 data = data.ivy_array
             if ivy.is_array(data) and dtype is None:
                 dtype = data.dtype
-            data = ivy.array(data, dtype=dtype)
+            data = ivy.array(data, dtype=dtype, copy=copy)
             self._data = data
         else:
             raise ivy.utils.exceptions.IvyException(
@@ -35,6 +35,7 @@ def _init_data(self, data, dtype):
 
     def _process_str_data(self, data, dtype):
         is_float = "." in data or "e" in data
+        is_complex = "j" in data
         data = data.replace(",", " ")
         data = " ".join(data.split())
         data = data.split(";")
@@ -42,9 +43,15 @@ def _process_str_data(self, data, dtype):
             row = row.strip().split(" ")
             data[i] = row
             for j, elem in enumerate(row):
-                data[i][j] = float(elem) if is_float else int(elem)
+                if is_complex:
+                    data[i][j] = complex(elem)
+                else:
+                    data[i][j] = float(elem) if is_float else int(elem)
         if dtype is None:
-            dtype = ivy.float64 if is_float else ivy.int64
+            if is_complex:
+                dtype = ivy.complex128
+            else:
+                dtype = ivy.float64 if is_float else ivy.int64
         self._data = ivy.array(data, dtype=dtype)
 
     # Properties #


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_changing_kind_of_array.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_changing_kind_of_array.py
--- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_changing_kind_of_array.py
+++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_changing_kind_of_array.py
@@ -0,0 +1,20 @@
+# global
+import numpy as np
+
+# local
+import ivy
+import ivy.functional.frontends.numpy as np_frontend
+import ivy_tests.test_ivy.helpers as helpers
+from ivy_tests.test_ivy.helpers import handle_frontend_test
+
+
+@handle_frontend_test(
+    fn_tree="numpy.asmatrix",
+    arr=helpers.dtype_and_values(min_num_dims=2, max_num_dims=2),
+)
+def test_numpy_asmatrix(arr):
+    dtype, x = arr
+    ret = np_frontend.asmatrix(x[0])
+    ret_gt = np.asmatrix(x[0])
+    assert ret.shape == ret_gt.shape
+    assert ivy.all(ivy.flatten(ret._data) == np.ravel(ret_gt))
diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_matrix/test_methods.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_matrix/test_methods.py
--- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_matrix/test_methods.py
+++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_matrix/test_methods.py
@@ -1,12 +1,12 @@
 # global
-from hypothesis import given, assume, strategies as st
+from hypothesis import assume, strategies as st
 import numpy as np
 import sys
 
 # local
 import ivy
 import ivy_tests.test_ivy.helpers as helpers
-from ivy_tests.test_ivy.helpers import handle_frontend_method
+from ivy_tests.test_ivy.helpers import handle_frontend_method, handle_frontend_test
 import ivy.functional.frontends.numpy as ivy_np
 
 
@@ -51,7 +51,10 @@ def _property_helper(draw):
     return data, data_gt
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_A(matrices):
     data, data_gt = matrices
     ret = np.ravel(data.A)
@@ -63,7 +66,10 @@ def test_numpy_matrix_property_A(matrices):
     )
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_A1(matrices):
     data, data_gt = matrices
     helpers.value_test(
@@ -73,7 +79,10 @@ def test_numpy_matrix_property_A1(matrices):
     )
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_I(matrices):
     data, data_gt = matrices
     assume(
@@ -89,7 +98,10 @@ def test_numpy_matrix_property_I(matrices):
     )
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_T(matrices):
     data, data_gt = matrices
     ret = np.ravel(data.T)
@@ -101,7 +113,10 @@ def test_numpy_matrix_property_T(matrices):
     )
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_data(matrices):
     data, data_gt = matrices
     # sanity test
@@ -110,7 +125,10 @@ def test_numpy_matrix_property_data(matrices):
     )
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_dtype(matrices):
     data, data_gt = matrices
     ivy.utils.assertions.check_equal(
@@ -118,19 +136,28 @@ def test_numpy_matrix_property_dtype(matrices):
     )
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_ndim(matrices):
     data, data_gt = matrices
     ivy.utils.assertions.check_equal(data.ndim, data_gt.ndim, as_array=False)
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_shape(matrices):
     data, data_gt = matrices
     ivy.utils.assertions.check_equal(data.shape, data_gt.shape, as_array=False)
 
 
-@given(matrices=_property_helper())
+@handle_frontend_test(
+    fn_tree="numpy.add",  # dummy fn_tree
+    matrices=_property_helper(),
+)
 def test_numpy_matrix_property_size(matrices):
     data, data_gt = matrices
     ivy.utils.assertions.check_equal(data.size, data_gt.size, as_array=False)
@@ -159,6 +186,7 @@ def test_numpy_matrix_argmax(
     method_flags,
     frontend_method_data,
     frontend,
+    on_device,
 ):
     input_dtype, x, axis = dtype_x_axis
     x = _get_x_matrix(x, to_str)
@@ -178,6 +206,7 @@ def test_numpy_matrix_argmax(
         },
         frontend=frontend,
         frontend_method_data=frontend_method_data,
+        on_device=on_device,
     )
 
 
@@ -204,6 +233,7 @@ def test_numpy_matrix_any(
     method_flags,
     frontend_method_data,
     frontend,
+    on_device,
 ):
     input_dtype, x, axis = dtype_x_axis
     x = _get_x_matrix(x, to_str)
@@ -223,4 +253,5 @@ def test_numpy_matrix_any(
         },
         frontend=frontend,
         frontend_method_data=frontend_method_data,
+        on_device=on_device,
     )