From bf348ed8f24f8245cf4c0a18a02b1b13d7db6c5f Mon Sep 17 00:00:00 2001 From: Ander Date: Fri, 5 Apr 2019 22:11:34 +1000 Subject: [PATCH 1/5] Fixing assignment between Array and TinyVector expression Add checking for conversion of a right side to Array::T_numtype (C++11 is used) --- blitz/array-impl.h | 8 +++ blitz/array/expr.h | 120 +++++++++++++++++++++++++++++++++++++++++++-- blitz/array/ops.cc | 6 ++- blitz/tinymat2.h | 13 ++--- blitz/tinyvec2.h | 13 ++--- 5 files changed, 142 insertions(+), 18 deletions(-) diff --git a/blitz/array-impl.h b/blitz/array-impl.h index 4bff205a..9426c571 100644 --- a/blitz/array-impl.h +++ b/blitz/array-impl.h @@ -62,6 +62,7 @@ #include #endif +#include BZ_NAMESPACE(blitz) @@ -2268,6 +2269,13 @@ class Array : public MemoryBlockReference isStorageContiguous when operator, is used. \todo We should do bounds checking, right now we will buffer overrun if the number of initializers in the list is larger than numElements. */ + template ::value>::type> + ListInitializationSwitch operator=(const T_expr &expr) + { + return ListInitializationSwitch(*this, T_numtype(expr)); + } + ListInitializationSwitch operator=(T_numtype x) { return ListInitializationSwitch(*this, x); diff --git a/blitz/array/expr.h b/blitz/array/expr.h index 65d160e3..4d0ce55e 100644 --- a/blitz/array/expr.h +++ b/blitz/array/expr.h @@ -65,6 +65,112 @@ BZ_NAMESPACE(blitz) #define BZ_MAX(a,b) (a)>(b) ? (a) : (b) #define BZ_MIN(a,b) (a)<(b) ? (a) : (b) +//NOTE: These template structures make to choose type of result binary operation between left and right side +//TODO: It's necessary to consider the cases of expressions with ArrayIndexMapping and/or IndexPlaceholder + +template struct TypeSwitch { typedef void Type; }; + +template +struct TypeSwitch, Array, RA> > { + typedef Array, RA> Type; +}; + +template +struct TypeSwitch, RA>, TinyVector > { + typedef Array, RA> Type; +}; + +template +struct TypeSwitch, TinyVector > { + typedef void Type; +}; + +template +struct TypeSwitch, TinyVector > { + typedef TinyVector Type; +}; + +template +struct TypeSwitch, Array > { + typedef Array Type; +}; + +template +struct TypeSwitch > { + typedef TinyVector Type; +}; + +template +struct TypeSwitch, T_expr2> { + typedef TinyVector Type; +}; + +template +struct TypeSwitch, T_expr2> { + typedef Array Type; +}; + +template +struct TypeSwitch > { + typedef Array Type; +}; + +//NOTE: These template structures define final type of an array expression + +template struct TypeInfo { typedef T_expr Type; }; + +template +struct TypeInfo > { + typedef typename FastTV2CopyIterator::T_vector Type; +}; + +template +struct TypeInfo > { + typedef typename FastTV2Iterator::T_vector Type; +}; + +template +struct TypeInfo > { + typedef typename FastArrayCopyIterator::T_array Type; +}; + +template +struct TypeInfo > { + typedef typename FastArrayIterator::T_array Type; +}; + +template +struct TypeInfo<_bz_ArrayExprBinaryOp > { + typedef typename TypeSwitch< + typename TypeInfo::Type, + typename TypeInfo::Type + >::Type Type; +}; + +template +struct TypeInfo<_bz_ArrayExprUnaryOp > { + typedef + typename TypeInfo::Type + Type; +}; + +template +struct TypeInfo<_bz_ArrayExprConstant > { + typedef + typename TypeInfo::Type + Type; +}; + +template +struct TypeInfo<_bz_ArrayExpr > { + typedef + typename TypeInfo::Type + Type; +}; + +//... + + template class _bz_ExprPair { public: @@ -108,6 +214,7 @@ class _bz_ArrayExpr { public: + typedef typename TypeInfo<_bz_ArrayExpr >::Type T_type; typedef P_expr T_expr; typedef _bz_typename T_expr::T_numtype T_numtype; // select return type @@ -500,6 +607,7 @@ class _bz_ArrayExpr template class _bz_ArrayExprUnaryOp { public: + typedef typename TypeInfo<_bz_ArrayExprUnaryOp >::Type T_type; typedef P_expr T_expr; typedef P_op T_op; typedef _bz_typename T_expr::T_numtype T_numtype1; @@ -775,6 +883,7 @@ class _bz_ArrayExprUnaryOp { template class _bz_ArrayExprBinaryOp { public: + typedef typename TypeInfo<_bz_ArrayExprBinaryOp >::Type T_type; typedef P_expr1 T_expr1; typedef P_expr2 T_expr2; typedef P_op T_op; @@ -1976,14 +2085,15 @@ class _bz_ArrayExprQuaternaryOp { template class _bz_ArrayExprConstant { public: + typedef typename TypeInfo<_bz_ArrayExprConstant >::Type T_type; typedef P_numtype T_numtype; - typedef typename opType::T_optype T_optype; - typedef typename asET::T_wrapped T_typeprop; - typedef typename unwrapET::T_unwrapped T_result; + typedef typename opType::T_optype T_optype; + typedef typename asET::T_wrapped T_typeprop; + typedef typename unwrapET::T_unwrapped T_result; typedef T_numtype T_ctorArg1; typedef int T_ctorArg2; // dummy - typedef _bz_ArrayExprConstant T_range_result; + typedef _bz_ArrayExprConstant T_range_result; static const int numArrayOperands = 0, numTVOperands = 0, @@ -1996,7 +2106,7 @@ class _bz_ArrayExprConstant { /** For the purpose of vectorizing across the container (as opposed to for operating on multicomponent types), a constant is always a constant. */ - template struct tvresult { + template struct tvresult { typedef _bz_ArrayExprConstant Type; }; diff --git a/blitz/array/ops.cc b/blitz/array/ops.cc index 4a00d33d..d4fc2080 100644 --- a/blitz/array/ops.cc +++ b/blitz/array/ops.cc @@ -50,7 +50,11 @@ Array& Array::initialize(T_numtype x) // we can't use asExpr here, because if we are initializing an array // whose components are also ETBase, it would parse as an array // expression, not as an initialization with a scalar. - (*this) = _bz_ArrayExpr<_bz_ArrayExprConstant >(x); + + //(*this) = _bz_ArrayExpr<_bz_ArrayExprConstant >(x); + iterator iter, last = end(); + for (iter = begin(); iter != last; ++ iter) *iter = x; + return *this; } diff --git a/blitz/tinymat2.h b/blitz/tinymat2.h index 3e9c8400..f174a3c9 100644 --- a/blitz/tinymat2.h +++ b/blitz/tinymat2.h @@ -75,12 +75,13 @@ class TinyMatrix : public ETBase > typedef P_numtype T_numtype; // typedef _bz_tinyMatrixRef // T_reference; - typedef TinyVector T_index; - typedef TinyMatrix T_matrix; - typedef FastTM2Iterator T_iterator; - typedef T_numtype* iterator; - typedef const T_numtype* const_iterator; - typedef FastTM2CopyIterator T_range_result; + typedef TinyVector T_index; + typedef TinyMatrix T_matrix; + typedef T_matrix T_type; + typedef FastTM2Iterator T_iterator; + typedef T_numtype* iterator; + typedef const T_numtype* const_iterator; + typedef FastTM2CopyIterator T_range_result; static const int //numArrayOperands = 1, diff --git a/blitz/tinyvec2.h b/blitz/tinyvec2.h index 3f1600bd..2f38c030 100644 --- a/blitz/tinyvec2.h +++ b/blitz/tinyvec2.h @@ -77,12 +77,13 @@ class TinyVector : public ETBase > // Public Types ////////////////////////////////////////////// - typedef P_numtype T_numtype; - typedef TinyVector T_vector; - typedef FastTV2Iterator T_iterator; - typedef T_numtype* iterator; - typedef const T_numtype* const_iterator; - typedef FastTV2CopyIterator T_range_result; + typedef P_numtype T_numtype; + typedef TinyVector T_vector; + typedef T_vector T_type; + typedef FastTV2Iterator T_iterator; + typedef T_numtype* iterator; + typedef const T_numtype* const_iterator; + typedef FastTV2CopyIterator T_range_result; static const int //numArrayOperands = 1, From 69b1780c66939de5af8a39210b35195e33dc807e Mon Sep 17 00:00:00 2001 From: Ander Date: Fri, 5 Apr 2019 22:12:22 +1000 Subject: [PATCH 2/5] Update test cases --- testsuite/Makefile.am | 3 +- testsuite/multicomponent-1.cpp | 128 +++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 testsuite/multicomponent-1.cpp diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am index abaa175f..cb397648 100644 --- a/testsuite/Makefile.am +++ b/testsuite/Makefile.am @@ -18,7 +18,7 @@ derrick-bass-1 derrick-bass-3 exprctor expression-slicing extract free \ gary-huber-1 indexexpr-base initialize int-math-func interlace iter \ Josef-Wagenhuber levicivita loop1 matthias-troyer-1 matthias-troyer-2 \ mattias-lindstroem-1 member_function minmax minsumpow module \ -multicomponent multicomponent-2 newet Olaf-Ronneberger-1 \ +multicomponent multicomponent-1 multicomponent-2 newet Olaf-Ronneberger-1 \ patrik-jonsson-1 peter-bienstman-1 peter-bienstman-2 peter-bienstman-3 \ peter-bienstman-4 peter-bienstman-5 peter-nordlund-1 peter-nordlund-2 \ peter-nordlund-3 preexisting promote pthread qcd reduce reindex \ @@ -73,6 +73,7 @@ minmax_SOURCES = minmax.cpp minsumpow_SOURCES = minsumpow.cpp module_SOURCES = module1.cpp module2.cpp multicomponent_SOURCES = multicomponent.cpp +multicomponent_1_SOURCES = multicomponent-1.cpp multicomponent_2_SOURCES = multicomponent-2.cpp newet_SOURCES = newet.cpp Olaf_Ronneberger_1_SOURCES = Olaf-Ronneberger-1.cpp diff --git a/testsuite/multicomponent-1.cpp b/testsuite/multicomponent-1.cpp new file mode 100644 index 00000000..536b5bba --- /dev/null +++ b/testsuite/multicomponent-1.cpp @@ -0,0 +1,128 @@ +#include "testsuite.h" +#include +#include +#include +#include +#include +#include + +BZ_USING_NAMESPACE(blitz) + +static const double eps = 0.0001; + +#define ISCLOSE(a, b) BZTEST(fabs((a)-(b)) T_matrix; +typedef TinyVector T_vector; +typedef Array T_array_2; +typedef Array T_array_1; +typedef Array T_array_1_of_vector; +typedef Array T_array_2_of_vector; +typedef Array T_array_1_of_matrix; +typedef Array T_array_2_of_matrix; + +#ifndef __TEST_FOR_VECTOR_EXPR +#define EXPR2(a) (T_vector(a * 2 + 1)) +#define EXPR3(a) (T_vector(a * 3 - 4)) +#else +#define EXPR2(a) (a * 2 + 1) +#define EXPR3(a) (a * 3 - 4) +#endif + +#define EXPR4(a, b) (a - b * 5 + 6) + +int main() { + + T_vector V1(1, 2, 3); + T_vector V2 = EXPR2(V1); + T_vector V3 = EXPR3(V1); + T_vector V4 = EXPR4(V2, V1); + cout << "V1 = " << V1 << endl; + cout << "V2 = " << V2 << endl; + cout << "V3 = " << V3 << endl; + cout << "V4 = " << V4 << endl; + + cout << "Testing for 1d-Array:" << endl; + + //Here TinyVector is composite object like as Array in the left side + T_array_1 A1(3); + A1 = EXPR2(V1); + cout << A1 << endl; + for (int k = 0; k < 3; ++ k) { ISCLOSE(A1(k), V2(k)); } + //Here we construct TinyVector as scalar value and fill Array + T_array_1_of_vector A1V(5); + A1V = EXPR2(V1); + cout << A1V << endl; + for (int i = 0; i < 5; ++ i) + for (int k = 0; k < 3; ++ k) { ISCLOSE(A1V(i)(k), V2(k)); } + //Partial assignment of a vector expression + A1V(Range(1, 3)) = EXPR3(V1); + cout << A1V << endl; + for (int i = 0; i < 5; ++ i) + for (int k = 0; k < 3; ++ k) { + if ((i >= 1) && (i <= 3)) { + ISCLOSE(A1V(i)(k), V3(k)); + } + else { + ISCLOSE(A1V(i)(k), V2(k)); + } + } + //Check for component wise assignment + for (int k = 0; k < 3; ++ k) { + A1V[k] = V4(k); + } + cout << A1V << endl; + for (int i = 0; i < 5; ++ i) + for (int k = 0; k < 3; ++ k) { ISCLOSE(A1V(i)(k), V4(k)); } + + cout << "Testing for 2d-Array:" << endl; + + //Here we construct TinyVector as scalar value and fill Array + T_array_2_of_vector A2V(5, 4); + A2V = EXPR2(V1); + cout << A2V << endl; + for (int i = 0; i < 5; ++ i) + for (int j = 0; j < 4; ++ j) + for (int k = 0; k < 3; ++ k) { ISCLOSE(A2V(i, j)(k), V2(k)); } + //Partial assignment of a vector expression + A2V(Range(2, 3), Range(1, 2)) = EXPR3(V1); + cout << A2V << endl; + for (int i = 0; i < 5; ++ i) + for (int j = 0; j < 4; ++ j) + for (int k = 0; k < 3; ++ k) { + if ((i >= 2) && (i <= 3) && + (j >= 1) && (j <= 2)) { + ISCLOSE(A2V(i, j)(k), V3(k)); + } + else { + ISCLOSE(A2V(i, j)(k), V2(k)); + } + } + //Check for component wise assignment + for (int k = 0; k < 3; ++ k) { + A2V[k] = V4(k); + } + cout << A2V << endl; + for (int i = 0; i < 5; ++ i) + for (int j = 0; j < 4; ++ j) + for (int k = 0; k < 3; ++ k) { ISCLOSE(A2V(i, j)(k), V4(k)); } + //Check for array expression + T_array_2_of_vector A(5, 4); + T_array_2_of_vector B(5, 4); + T_array_2_of_vector C(5, 4); + A = V1; + B = V2; + B(0, 3) = EXPR3(V1); + B(4, 0) = V3; + C = EXPR4(B, A); + cout << C << endl; + for (int i = 0; i < 5; ++ i) + for (int j = 0; j < 4; ++ j) + for (int k = 0; k < 3; ++ k) { + ISCLOSE(C(i, j)(k), EXPR4(B(i, j)(k), A(i, j)(k))); + } + + return 0; +} From f6d0dbe6dc4f298eb6cd0209b4aaf433cc1e4df9 Mon Sep 17 00:00:00 2001 From: Ander Date: Sun, 7 Apr 2019 10:19:26 +1000 Subject: [PATCH 3/5] Fixing assignment between Array and TinyMatrix expression --- blitz/array/expr.h | 96 ++++++++++++++++++++++++++++++++++++---------- blitz/tinymat2.h | 30 +++++++++++---- 2 files changed, 99 insertions(+), 27 deletions(-) diff --git a/blitz/array/expr.h b/blitz/array/expr.h index 4d0ce55e..8e085e60 100644 --- a/blitz/array/expr.h +++ b/blitz/array/expr.h @@ -70,39 +70,85 @@ BZ_NAMESPACE(blitz) template struct TypeSwitch { typedef void Type; }; -template -struct TypeSwitch, Array, RA> > { - typedef Array, RA> Type; +template +struct TypeSwitch, TinyMatrix > { + typedef void Type; }; -template -struct TypeSwitch, RA>, TinyVector > { - typedef Array, RA> Type; +template +struct TypeSwitch, TinyVector > { + typedef void Type; }; -template -struct TypeSwitch, TinyVector > { +template +struct TypeSwitch, TinyMatrix > { typedef void Type; }; -template -struct TypeSwitch, TinyVector > { - typedef TinyVector Type; +template +struct TypeSwitch, TinyVector > { + typedef void Type; +}; + +//TinyMatrix: + +template +struct TypeSwitch, Array, RA> > { + typedef Array, RA> Type; +}; + +template +struct TypeSwitch, RA>, TinyMatrix > { + typedef Array, RA> Type; +}; + +template +struct TypeSwitch, TinyMatrix > { + typedef TinyMatrix Type; +}; + +template +struct TypeSwitch > { + typedef TinyMatrix Type; +}; + +template +struct TypeSwitch, T_expr2> { + typedef TinyMatrix Type; }; -template -struct TypeSwitch, Array > { - typedef Array Type; +//TinyVector: + +template +struct TypeSwitch, Array, RA> > { + typedef Array, RA> Type; +}; + +template +struct TypeSwitch, RA>, TinyVector > { + typedef Array, RA> Type; +}; + +template +struct TypeSwitch, TinyVector > { + typedef TinyVector Type; +}; + +template +struct TypeSwitch > { + typedef TinyVector Type; }; -template -struct TypeSwitch > { - typedef TinyVector Type; +template +struct TypeSwitch, T_expr2> { + typedef TinyVector Type; }; -template -struct TypeSwitch, T_expr2> { - typedef TinyVector Type; +//Array: + +template +struct TypeSwitch, Array > { + typedef Array Type; }; template @@ -119,6 +165,16 @@ struct TypeSwitch > { template struct TypeInfo { typedef T_expr Type; }; +template +struct TypeInfo > { + typedef typename FastTM2CopyIterator::T_matrix Type; +}; + +template +struct TypeInfo > { + typedef typename FastTM2Iterator::T_matrix Type; +}; + template struct TypeInfo > { typedef typename FastTV2CopyIterator::T_vector Type; diff --git a/blitz/tinymat2.h b/blitz/tinymat2.h index f174a3c9..c9d3a8d6 100644 --- a/blitz/tinymat2.h +++ b/blitz/tinymat2.h @@ -75,13 +75,13 @@ class TinyMatrix : public ETBase > typedef P_numtype T_numtype; // typedef _bz_tinyMatrixRef // T_reference; - typedef TinyVector T_index; - typedef TinyMatrix T_matrix; - typedef T_matrix T_type; - typedef FastTM2Iterator T_iterator; - typedef T_numtype* iterator; - typedef const T_numtype* const_iterator; - typedef FastTM2CopyIterator T_range_result; + typedef TinyVector T_index; + typedef TinyMatrix T_matrix; + typedef T_matrix T_type; + typedef FastTM2Iterator T_iterator; + typedef T_numtype* iterator; + typedef const T_numtype* const_iterator; + typedef FastTM2CopyIterator T_range_result; static const int //numArrayOperands = 1, @@ -95,6 +95,22 @@ class TinyMatrix : public ETBase > template inline TinyMatrix(const TinyMatrix& x); + /** This constructor creates a TinyMatrix from another ETBase + object. It needs to be explicit to avoid all kinds of + ambiguities. */ + template + inline explicit TinyMatrix(const ETBase& expr) { + *this = expr; } + + /** This constructor creates a TinyMatrix specifically from an + expression. This one we do NOT want to be explicit because that + breaks simple construction assignments like "TinyMatrix v = a+b;", forcing the user to explicitly write it like a + construction. */ + template + inline TinyMatrix(const _bz_ArrayExpr& expr) { + *this = expr; } + inline TinyMatrix(T_numtype initValue); static TinyVector base() From aa199d7a5c27ca4b48de6370c3aca436d03df30c Mon Sep 17 00:00:00 2001 From: Ander Date: Sun, 7 Apr 2019 10:54:31 +1000 Subject: [PATCH 4/5] Update test cases --- testsuite/multicomponent-1.cpp | 200 ++++++++++++++++++++++++++------- 1 file changed, 158 insertions(+), 42 deletions(-) diff --git a/testsuite/multicomponent-1.cpp b/testsuite/multicomponent-1.cpp index 536b5bba..2c30c528 100644 --- a/testsuite/multicomponent-1.cpp +++ b/testsuite/multicomponent-1.cpp @@ -8,37 +8,143 @@ BZ_USING_NAMESPACE(blitz) -static const double eps = 0.0001; +static const double eps = 1.e-7; #define ISCLOSE(a, b) BZTEST(fabs((a)-(b)) T_matrix; -typedef TinyVector T_vector; +typedef TinyMatrix T_matrix; +typedef TinyVector T_vector; +typedef Array T_array_2_of_matrix; +typedef Array T_array_1_of_matrix; +typedef Array T_array_2_of_vector; +typedef Array T_array_1_of_vector; typedef Array T_array_2; typedef Array T_array_1; -typedef Array T_array_1_of_vector; -typedef Array T_array_2_of_vector; -typedef Array T_array_1_of_matrix; -typedef Array T_array_2_of_matrix; + +#define __TEST_FOR_MATRIX_EXPR +#define __TEST_FOR_VECTOR_EXPR + +#ifndef __TEST_FOR_MATRIX_EXPR +#define MAT_EXPR1(a) (T_matrix(a * 2 + 1)) +#define MAT_EXPR2(a) (T_matrix(a * 3 - 4)) +#else +#define MAT_EXPR1(a) (a * 2 + 1) +#define MAT_EXPR2(a) (a * 3 - 4) +#endif + +#define MAT_EXPR3(a, b) (a - b * 5 + 6) #ifndef __TEST_FOR_VECTOR_EXPR -#define EXPR2(a) (T_vector(a * 2 + 1)) -#define EXPR3(a) (T_vector(a * 3 - 4)) +#define VEC_EXPR1(a) (T_vector(a * 2 + 1)) +#define VEC_EXPR2(a) (T_vector(a * 3 - 4)) #else -#define EXPR2(a) (a * 2 + 1) -#define EXPR3(a) (a * 3 - 4) +#define VEC_EXPR1(a) (a * 2 + 1) +#define VEC_EXPR2(a) (a * 3 - 4) #endif -#define EXPR4(a, b) (a - b * 5 + 6) +#define VEC_EXPR3(a, b) (a - b * 5 + 6) -int main() { +void test_for_matrix() { + + cout << "Testing for Array:" << endl; + + T_matrix M1; + M1 = 1, 2, 3, 4, 5, 6; + T_matrix M2 = MAT_EXPR1(M1); + T_matrix M3 = MAT_EXPR2(M1); + T_matrix M4 = MAT_EXPR3(M2, M1); + cout << "M1 = " << M1 << endl; + cout << "M2 = " << M2 << endl; + cout << "M3 = " << M3 << endl; + cout << "M4 = " << M4 << endl; + + cout << "Testing for 1d-Array:" << endl; + + //Here TinyMatrix is composite object like as Array in the left side + T_array_2 A2(2, 3); + A2 = MAT_EXPR1(M1); + cout << A2 << endl; + for (int k = 0; k < ROW; ++ k) + for (int l = 0; l < COL; ++ l) { ISCLOSE(A2(k, l), M2(k, l)); } + + //Here we construct TinyMatrix as scalar value and fill Array + T_array_1_of_matrix A1M(5); + A1M = MAT_EXPR1(M1); + cout << A1M << endl; + for (int k = 0; k < ROW; ++ k) + for (int l = 0; l < COL; ++ l) + for (int i = 0; i < 5; ++ i) { ISCLOSE(A1M(i)(k, l), M2(k, l)); } + + //Partial assignment of a matrix expression + A1M(Range(1, 3)) = MAT_EXPR2(M1); + cout << A1M << endl; + for (int k = 0; k < ROW; ++ k) + for (int l = 0; l < COL; ++ l) + for (int i = 0; i < 5; ++ i) { + if ((i >= 1) && (i <= 3)) { + ISCLOSE(A1M(i)(k, l), M3(k, l)); + } + else { + ISCLOSE(A1M(i)(k, l), M2(k, l)); + } + } + + cout << "Testing for 2d-Array:" << endl; + + //Here we construct TinyMatrix as scalar value and fill Array + T_array_2_of_matrix A2M(5, 4); + A2M = MAT_EXPR1(M1); + cout << A2M << endl; + for (int k = 0; k < ROW; ++ k) + for (int l = 0; l < COL; ++ l) + for (int i = 0; i < 5; ++ i) + for (int j = 0; j < 4; ++ j) { ISCLOSE(A2M(i, j)(k, l), M2(k, l)); } + + //Partial assignment of a matrix expression + A2M(Range(2, 3), Range(1, 2)) = MAT_EXPR2(M1); + cout << A2M << endl; + for (int k = 0; k < ROW; ++ k) + for (int l = 0; l < COL; ++ l) + for (int i = 0; i < 5; ++ i) + for (int j = 0; j < 4; ++ j) { + if ((i >= 2) && (i <= 3) && + (j >= 1) && (j <= 2)) { + ISCLOSE(A2M(i, j)(k, l), M3(k, l)); + } + else { + ISCLOSE(A2M(i, j)(k, l), M2(k, l)); + } + } + + //Check for array expression + T_array_2_of_matrix A(5, 4); + T_array_2_of_matrix B(5, 4); + T_array_2_of_matrix C(5, 4); + A = M1; + B = M2; + B(0, 3) = MAT_EXPR2(M1); + B(4, 0) = M3; + C = MAT_EXPR3(B, A); + cout << C << endl; + for (int k = 0; k < ROW; ++ k) + for (int l = 0; l < COL; ++ l) + for (int i = 0; i < 5; ++ i) + for (int j = 0; j < 4; ++ j) { + ISCLOSE(C(i, j)(k, l), MAT_EXPR3(B(i, j)(k, l), A(i, j)(k, l))); + } +} + +void test_for_vector() { + + cout << "Testing for Array:" << endl; T_vector V1(1, 2, 3); - T_vector V2 = EXPR2(V1); - T_vector V3 = EXPR3(V1); - T_vector V4 = EXPR4(V2, V1); + T_vector V2 = VEC_EXPR1(V1); + T_vector V3 = VEC_EXPR2(V1); + T_vector V4 = VEC_EXPR3(V2, V1); cout << "V1 = " << V1 << endl; cout << "V2 = " << V2 << endl; cout << "V3 = " << V3 << endl; @@ -48,20 +154,22 @@ int main() { //Here TinyVector is composite object like as Array in the left side T_array_1 A1(3); - A1 = EXPR2(V1); + A1 = VEC_EXPR1(V1); cout << A1 << endl; - for (int k = 0; k < 3; ++ k) { ISCLOSE(A1(k), V2(k)); } + for (int k = 0; k < COL; ++ k) { ISCLOSE(A1(k), V2(k)); } + //Here we construct TinyVector as scalar value and fill Array T_array_1_of_vector A1V(5); - A1V = EXPR2(V1); + A1V = VEC_EXPR1(V1); cout << A1V << endl; - for (int i = 0; i < 5; ++ i) - for (int k = 0; k < 3; ++ k) { ISCLOSE(A1V(i)(k), V2(k)); } + for (int k = 0; k < COL; ++ k) + for (int i = 0; i < 5; ++ i) { ISCLOSE(A1V(i)(k), V2(k)); } + //Partial assignment of a vector expression - A1V(Range(1, 3)) = EXPR3(V1); + A1V(Range(1, 3)) = VEC_EXPR2(V1); cout << A1V << endl; - for (int i = 0; i < 5; ++ i) - for (int k = 0; k < 3; ++ k) { + for (int k = 0; k < COL; ++ k) + for (int i = 0; i < 5; ++ i) { if ((i >= 1) && (i <= 3)) { ISCLOSE(A1V(i)(k), V3(k)); } @@ -69,29 +177,31 @@ int main() { ISCLOSE(A1V(i)(k), V2(k)); } } + //Check for component wise assignment - for (int k = 0; k < 3; ++ k) { + for (int k = 0; k < COL; ++ k) { A1V[k] = V4(k); } cout << A1V << endl; - for (int i = 0; i < 5; ++ i) - for (int k = 0; k < 3; ++ k) { ISCLOSE(A1V(i)(k), V4(k)); } + for (int k = 0; k < COL; ++ k) + for (int i = 0; i < 5; ++ i) { ISCLOSE(A1V(i)(k), V4(k)); } cout << "Testing for 2d-Array:" << endl; //Here we construct TinyVector as scalar value and fill Array T_array_2_of_vector A2V(5, 4); - A2V = EXPR2(V1); + A2V = VEC_EXPR1(V1); cout << A2V << endl; + for (int k = 0; k < COL; ++ k) for (int i = 0; i < 5; ++ i) - for (int j = 0; j < 4; ++ j) - for (int k = 0; k < 3; ++ k) { ISCLOSE(A2V(i, j)(k), V2(k)); } + for (int j = 0; j < 4; ++ j) { ISCLOSE(A2V(i, j)(k), V2(k)); } + //Partial assignment of a vector expression - A2V(Range(2, 3), Range(1, 2)) = EXPR3(V1); + A2V(Range(2, 3), Range(1, 2)) = VEC_EXPR2(V1); cout << A2V << endl; + for (int k = 0; k < COL; ++ k) for (int i = 0; i < 5; ++ i) - for (int j = 0; j < 4; ++ j) - for (int k = 0; k < 3; ++ k) { + for (int j = 0; j < 4; ++ j) { if ((i >= 2) && (i <= 3) && (j >= 1) && (j <= 2)) { ISCLOSE(A2V(i, j)(k), V3(k)); @@ -100,29 +210,35 @@ int main() { ISCLOSE(A2V(i, j)(k), V2(k)); } } + //Check for component wise assignment - for (int k = 0; k < 3; ++ k) { + for (int k = 0; k < COL; ++ k) { A2V[k] = V4(k); } cout << A2V << endl; + for (int k = 0; k < COL; ++ k) for (int i = 0; i < 5; ++ i) - for (int j = 0; j < 4; ++ j) - for (int k = 0; k < 3; ++ k) { ISCLOSE(A2V(i, j)(k), V4(k)); } + for (int j = 0; j < 4; ++ j) { ISCLOSE(A2V(i, j)(k), V4(k)); } + //Check for array expression T_array_2_of_vector A(5, 4); T_array_2_of_vector B(5, 4); T_array_2_of_vector C(5, 4); A = V1; B = V2; - B(0, 3) = EXPR3(V1); + B(0, 3) = VEC_EXPR2(V1); B(4, 0) = V3; - C = EXPR4(B, A); + C = VEC_EXPR3(B, A); cout << C << endl; + for (int k = 0; k < COL; ++ k) for (int i = 0; i < 5; ++ i) - for (int j = 0; j < 4; ++ j) - for (int k = 0; k < 3; ++ k) { - ISCLOSE(C(i, j)(k), EXPR4(B(i, j)(k), A(i, j)(k))); + for (int j = 0; j < 4; ++ j) { + ISCLOSE(C(i, j)(k), VEC_EXPR3(B(i, j)(k), A(i, j)(k))); } +} +int main() { + test_for_matrix(); + test_for_vector(); return 0; } From b2c0bd909d1c72beecef2a851e1bc9bcc7f74698 Mon Sep 17 00:00:00 2001 From: Ander Date: Sun, 7 Apr 2019 16:14:48 +1000 Subject: [PATCH 5/5] Minor fixes --- blitz/array/expr.h | 130 ++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/blitz/array/expr.h b/blitz/array/expr.h index 8e085e60..7c6fea7c 100644 --- a/blitz/array/expr.h +++ b/blitz/array/expr.h @@ -70,129 +70,129 @@ BZ_NAMESPACE(blitz) template struct TypeSwitch { typedef void Type; }; -template -struct TypeSwitch, TinyMatrix > { +template +struct TypeSwitch, TinyMatrix > { typedef void Type; }; -template -struct TypeSwitch, TinyVector > { +template +struct TypeSwitch, TinyVector > { typedef void Type; }; -template -struct TypeSwitch, TinyMatrix > { +template +struct TypeSwitch, TinyMatrix > { typedef void Type; }; -template -struct TypeSwitch, TinyVector > { +template +struct TypeSwitch, TinyVector > { typedef void Type; }; //TinyMatrix: -template -struct TypeSwitch, Array, RA> > { - typedef Array, RA> Type; +template +struct TypeSwitch, Array, N_rank> > { + typedef Array, N_rank> Type; }; -template -struct TypeSwitch, RA>, TinyMatrix > { - typedef Array, RA> Type; +template +struct TypeSwitch, N_rank>, TinyMatrix > { + typedef Array, N_rank> Type; }; -template -struct TypeSwitch, TinyMatrix > { - typedef TinyMatrix Type; +template +struct TypeSwitch, TinyMatrix > { + typedef TinyMatrix Type; }; -template -struct TypeSwitch > { - typedef TinyMatrix Type; +template +struct TypeSwitch > { + typedef TinyMatrix Type; }; -template -struct TypeSwitch, T_expr2> { - typedef TinyMatrix Type; +template +struct TypeSwitch, T_expr2> { + typedef TinyMatrix Type; }; //TinyVector: -template -struct TypeSwitch, Array, RA> > { - typedef Array, RA> Type; +template +struct TypeSwitch, Array, N_rank> > { + typedef Array, N_rank> Type; }; -template -struct TypeSwitch, RA>, TinyVector > { - typedef Array, RA> Type; +template +struct TypeSwitch, N_rank>, TinyVector > { + typedef Array, N_rank> Type; }; -template -struct TypeSwitch, TinyVector > { - typedef TinyVector Type; +template +struct TypeSwitch, TinyVector > { + typedef TinyVector Type; }; -template -struct TypeSwitch > { - typedef TinyVector Type; +template +struct TypeSwitch > { + typedef TinyVector Type; }; -template -struct TypeSwitch, T_expr2> { - typedef TinyVector Type; +template +struct TypeSwitch, T_expr2> { + typedef TinyVector Type; }; //Array: -template -struct TypeSwitch, Array > { - typedef Array Type; +template +struct TypeSwitch, Array > { + typedef Array Type; }; -template -struct TypeSwitch, T_expr2> { - typedef Array Type; +template +struct TypeSwitch, T_expr2> { + typedef Array Type; }; -template -struct TypeSwitch > { - typedef Array Type; +template +struct TypeSwitch > { + typedef Array Type; }; //NOTE: These template structures define final type of an array expression template struct TypeInfo { typedef T_expr Type; }; -template -struct TypeInfo > { - typedef typename FastTM2CopyIterator::T_matrix Type; +template +struct TypeInfo > { + typedef typename FastTM2CopyIterator::T_matrix Type; }; -template -struct TypeInfo > { - typedef typename FastTM2Iterator::T_matrix Type; +template +struct TypeInfo > { + typedef typename FastTM2Iterator::T_matrix Type; }; -template -struct TypeInfo > { - typedef typename FastTV2CopyIterator::T_vector Type; +template +struct TypeInfo > { + typedef typename FastTV2CopyIterator::T_vector Type; }; -template -struct TypeInfo > { - typedef typename FastTV2Iterator::T_vector Type; +template +struct TypeInfo > { + typedef typename FastTV2Iterator::T_vector Type; }; -template -struct TypeInfo > { - typedef typename FastArrayCopyIterator::T_array Type; +template +struct TypeInfo > { + typedef typename FastArrayCopyIterator::T_array Type; }; -template -struct TypeInfo > { - typedef typename FastArrayIterator::T_array Type; +template +struct TypeInfo > { + typedef typename FastArrayIterator::T_array Type; }; template