Repository: STEllAR-GROUP/phylanx (language: python)

Issue:
Iteration on arrays doesn't work
The functions foo1() and foo2() ought to produce the same output. They do not.
```
from phylanx import Phylanx
from numpy import zeros

@Phylanx
def foo1():
    sh = [2,2]
    z = constant(0, sh, dtype="float")
    for a in z:
        print("a:",a)

def foo2():
    sh = [2,2]
    z = zeros(sh, dtype=float)
    for a in z:
        print("a:",a)

foo1()
foo2()
```


Discussion / hints:

Fix patch:
diff --git a/src/plugins/controls/for_each.cpp b/src/plugins/controls/for_each.cpp
index 5e6f27e07..d737e3a20 100644
--- a/src/plugins/controls/for_each.cpp
+++ b/src/plugins/controls/for_each.cpp
@@ -132,15 +132,17 @@ namespace phylanx { namespace execution_tree { namespace primitives {
         {
             auto m = value.matrix();
 
-            using phylanx::util::matrix_iterator;
-            matrix_iterator<decltype(m)> begin(m, 0);
-            matrix_iterator<decltype(m)> end(m, m.rows());
+            using phylanx::util::matrix_row_iterator;
+            matrix_row_iterator<decltype(m)> begin(m, 0);
+            matrix_row_iterator<decltype(m)> end(m, m.rows());
 
             for (auto it = begin; it != end; ++it)
             {
-                blaze::DynamicVector<T> row(std::move(*it));
+                blaze::CustomVector<T, blaze::aligned, blaze::padded> row(
+                    it->data(), it->size(), it->spacing());
                 auto result = p->eval(hpx::launch::sync,
-                    primitive_argument_type{std::move(row)}, ctx);
+                    primitive_argument_type{ir::node_data{std::move(row)}},
+                    ctx);
 
                 if (is_boolean_operand_strict(result))
                 {
@@ -215,9 +217,8 @@ namespace phylanx { namespace execution_tree { namespace primitives {
         }
 
         HPX_THROW_EXCEPTION(hpx::bad_parameter, "for_each::iterate_over_array",
-            generate_error_message("the iteration space has an unsupported "
-                                   "numeric type (dimension)",
-                ctx));
+            generate_error_message(
+                "the iteration space has an unsupported dimension", ctx));
     }
 
     hpx::future<primitive_argument_type> for_each::eval(


Test patch:
diff --git a/tests/regressions/python/1312_matrix_iteration.py b/tests/regressions/python/1312_matrix_iteration.py
new file mode 100644
index 000000000..d035708b2
--- /dev/null
+++ b/tests/regressions/python/1312_matrix_iteration.py
@@ -0,0 +1,32 @@
+#  Copyright (c) 2020 Steven R. Brandt
+#
+#  Distributed under the Boost Software License, Version 1.0. (See accompanying
+#  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+# 1312: Iteration on arrays doesn't work
+
+from phylanx import Phylanx
+import numpy as np
+
+
+# silence flake
+def append(lst, x):
+    return lst
+
+
+def constant(val, sh, dtype):
+    return val
+
+
+@Phylanx
+def foo():
+    sh = [2, 2]
+    z = constant(0, sh, dtype="float")
+    result = []
+    for a in z:
+        result = append(result, a)
+    return result
+
+
+result = foo()
+assert (result == np.array([[0, 0], [0, 0]])).all(), result
diff --git a/tests/regressions/python/CMakeLists.txt b/tests/regressions/python/CMakeLists.txt
index 65bd30ed7..12aa2bc23 100644
--- a/tests/regressions/python/CMakeLists.txt
+++ b/tests/regressions/python/CMakeLists.txt
@@ -72,6 +72,7 @@ set(tests
     1302_array
     1303_iterate_over_array
     1306_indexing
+    1312_matrix_iteration
     array_len_494
     array_shape_486
     array_subscript_403