From 272f30689c2adccaef64fe5f697c4a9631b7cbd2 Mon Sep 17 00:00:00 2001 From: CismonX Date: Mon, 4 Nov 2019 15:27:48 +0800 Subject: [PATCH] move matrix constructors to appropriate interfaces --- src/dense_resizable_matrix.cc | 19 ++++++ src/dense_resizable_matrix.hh | 1 + src/mat.cc | 83 ----------------------- src/mat.hh | 4 -- src/resizable_matrix.cc | 84 +++++++++++++++++++++--- src/resizable_matrix.hh | 2 + stubs/Mat.php | 30 --------- stubs/internal/DenseResizableMatrix.php | 10 +++ stubs/internal/ResizableMatrix.php | 16 +++++ stubs/internal/SparseResizableMatrix.php | 15 +++++ 10 files changed, 137 insertions(+), 127 deletions(-) diff --git a/src/dense_resizable_matrix.cc b/src/dense_resizable_matrix.cc index b97ef90..39603bd 100644 --- a/src/dense_resizable_matrix.cc +++ b/src/dense_resizable_matrix.cc @@ -13,6 +13,24 @@ namespace php_arma { + template + PHP_ARMA_METHOD(dense_resizable_matrix, init, T, ChildT) + { + zend_long n_rows = 0; + zend_long n_cols = 0; + zend_long fill = fill::none; + ZEND_PARSE_PARAMETERS_START(0, 3) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(n_rows) + Z_PARAM_LONG(n_cols) + Z_PARAM_LONG(fill) + ZEND_PARSE_PARAMETERS_END(); + + auto zobj = ChildT::create(n_rows, n_cols); + fill::invoke(ZOBJ_NATIVE(zobj), fill); + RETVAL_OBJ(zobj); + } + template PHP_ARMA_METHOD(dense_resizable_matrix, ones, T, T1) { @@ -226,6 +244,7 @@ namespace php_arma template PHP_ARMA_START_ME(dense_resizable_matrix, T, T1) + PHP_ARMA_ME(init, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ARMA_ME(ones, ZEND_ACC_PUBLIC) PHP_ARMA_ME(randu, ZEND_ACC_PUBLIC) PHP_ARMA_ME(randn, ZEND_ACC_PUBLIC) diff --git a/src/dense_resizable_matrix.hh b/src/dense_resizable_matrix.hh index b3de721..aea2333 100644 --- a/src/dense_resizable_matrix.hh +++ b/src/dense_resizable_matrix.hh @@ -19,6 +19,7 @@ namespace php_arma PHP_ARMA_COMMON_DECLARE(); private: + static PHP_FUNCTION(init); static PHP_FUNCTION(ones); static PHP_FUNCTION(randu); static PHP_FUNCTION(randn); diff --git a/src/mat.cc b/src/mat.cc index 0b4e912..b668467 100644 --- a/src/mat.cc +++ b/src/mat.cc @@ -16,91 +16,8 @@ namespace php_arma { - template - PHP_ARMA_METHOD(mat, init, T) - { - zend_long n_rows = 0; - zend_long n_cols = 0; - zend_long fill = fill::none; - ZEND_PARSE_PARAMETERS_START(0, 3) - Z_PARAM_OPTIONAL - Z_PARAM_LONG(n_rows) - Z_PARAM_LONG(n_cols) - Z_PARAM_LONG(fill) - ZEND_PARSE_PARAMETERS_END(); - - auto zobj = mat::create(n_rows, n_cols); - fill::invoke(ZOBJ_NATIVE(zobj), fill); - RETVAL_OBJ(zobj); - } - - template - PHP_ARMA_METHOD(mat, fromString, T) - { - zend_string *text; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_STR(text) - ZEND_PARSE_PARAMETERS_END(); - - auto zobj = mat::create(ZSTR_VAL(text)); - RETVAL_OBJ(zobj); - } - - template - PHP_ARMA_METHOD(mat, fromArray, T) - { - zval *arr; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ARRAY(arr) - ZEND_PARSE_PARAMETERS_END(); - - auto num_rows = zend_hash_num_elements(Z_ARR_P(arr)); - if (UNEXPECTED(num_rows == 0)) { - RETURN_OBJ(mat::create()); - } - auto first_row = zend_hash_index_find(Z_ARR_P(arr), 0); - if (UNEXPECTED(first_row == nullptr || Z_TYPE_P(first_row) != IS_ARRAY)) { - throw_exception("bad input matrix"); - return; - } - auto num_cols = zend_hash_num_elements(Z_ARR_P(first_row)); - if (UNEXPECTED(num_rows == 0)) { - RETURN_OBJ(mat::create()); - } - - auto zobj = mat::create(num_rows, num_cols); - auto native = ZOBJ_NATIVE(zobj); - size_t idx_row = 0; - size_t idx_col = 0; - ZEND_HASH_FOREACH_VAL(Z_ARR_P(arr), zval *row) - if (idx_row > num_rows - 1) { - break; - } - if (UNEXPECTED(Z_TYPE_P(row) != IS_ARRAY)) { - throw_exception("bad input matrix"); - break; - } - ZEND_HASH_FOREACH_VAL(Z_ARR_P(row), zval *elem) - if (UNEXPECTED(idx_col > num_cols - 1)) { - break; - } - if (UNEXPECTED(!zval_check_scalar(elem))) { - goto end_loop; - } - native->at(idx_row, idx_col++) = zval_get_scalar(elem); - ZEND_HASH_FOREACH_END(); - ++idx_row; - idx_col = 0; - ZEND_HASH_FOREACH_END(); - end_loop: - RETVAL_OBJ(zobj); - } - template PHP_ARMA_START_ME(mat, T) - PHP_ARMA_ME(init, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ARMA_ME(fromString, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ARMA_ME(fromArray, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ARMA_END_ME(); template diff --git a/src/mat.hh b/src/mat.hh index ef066ce..c712bf5 100644 --- a/src/mat.hh +++ b/src/mat.hh @@ -31,10 +31,6 @@ namespace php_arma private: PHP_ARMA_COMMON_DECLARE(); - static PHP_FUNCTION(init); - static PHP_FUNCTION(fromString); - static PHP_FUNCTION(fromArray); - static void ce_init(zend_class_entry*); }; diff --git a/src/resizable_matrix.cc b/src/resizable_matrix.cc index ae6f9f0..d9f33f8 100644 --- a/src/resizable_matrix.cc +++ b/src/resizable_matrix.cc @@ -11,6 +11,68 @@ namespace php_arma { + template + PHP_ARMA_METHOD(resizable_matrix, fromString, T, ChildT) + { + zend_string *text; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STR(text) + ZEND_PARSE_PARAMETERS_END(); + + auto zobj = ChildT::create(ZSTR_VAL(text)); + RETVAL_OBJ(zobj); + } + + template + PHP_ARMA_METHOD(resizable_matrix, fromArray, T, ChildT) + { + zval *arr; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ARRAY(arr) + ZEND_PARSE_PARAMETERS_END(); + + auto num_rows = zend_hash_num_elements(Z_ARR_P(arr)); + if (UNEXPECTED(num_rows == 0)) { + RETURN_OBJ(ChildT::create()); + } + auto first_row = zend_hash_index_find(Z_ARR_P(arr), 0); + if (UNEXPECTED(first_row == nullptr || Z_TYPE_P(first_row) != IS_ARRAY)) { + throw_exception("bad input matrix"); + return; + } + auto num_cols = zend_hash_num_elements(Z_ARR_P(first_row)); + if (UNEXPECTED(num_rows == 0)) { + RETURN_OBJ(ChildT::create()); + } + + auto zobj = ChildT::create(num_rows, num_cols); + auto native = ZOBJ_NATIVE(zobj); + size_t idx_row = 0; + size_t idx_col = 0; + ZEND_HASH_FOREACH_VAL(Z_ARR_P(arr), zval *row) + if (idx_row > num_rows - 1) { + break; + } + if (UNEXPECTED(Z_TYPE_P(row) != IS_ARRAY)) { + throw_exception("bad input matrix"); + break; + } + ZEND_HASH_FOREACH_VAL(Z_ARR_P(row), zval *elem) + if (UNEXPECTED(idx_col > num_cols - 1)) { + break; + } + if (UNEXPECTED(!zval_check_scalar(elem))) { + goto end_loop; + } + native->at(idx_row, idx_col++) = zval_get_scalar(elem); + ZEND_HASH_FOREACH_END(); + ++idx_row; + idx_col = 0; + ZEND_HASH_FOREACH_END(); + end_loop: + RETVAL_OBJ(zobj); + } + template PHP_ARMA_METHOD(resizable_matrix, reshape, T, T1) { @@ -153,16 +215,18 @@ namespace php_arma template PHP_ARMA_START_ME(resizable_matrix, T, T1) - PHP_ARMA_ME(reshape, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(resize, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(setSize, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(zeros, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(eye, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(inRange, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(shedRow, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(shedCol, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(shedRows, ZEND_ACC_PUBLIC) - PHP_ARMA_ME(shedCols, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(fromString, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ARMA_ME(fromArray, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ARMA_ME(reshape, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(resize, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(setSize, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(zeros, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(eye, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(inRange, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(shedRow, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(shedCol, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(shedRows, ZEND_ACC_PUBLIC) + PHP_ARMA_ME(shedCols, ZEND_ACC_PUBLIC) PHP_ARMA_END_ME(); void resizable_matrix_init() diff --git a/src/resizable_matrix.hh b/src/resizable_matrix.hh index 5509726..176faf8 100644 --- a/src/resizable_matrix.hh +++ b/src/resizable_matrix.hh @@ -19,6 +19,8 @@ namespace php_arma PHP_ARMA_COMMON_DECLARE(); private: + static PHP_FUNCTION(fromString); + static PHP_FUNCTION(fromArray); static PHP_FUNCTION(reshape); static PHP_FUNCTION(resize); static PHP_FUNCTION(setSize); diff --git a/stubs/Mat.php b/stubs/Mat.php index 2950dcd..20b1871 100644 --- a/stubs/Mat.php +++ b/stubs/Mat.php @@ -9,35 +9,5 @@ namespace Arma; */ abstract class Mat implements Internal\DenseResizableMatrix { - /** - * Construct the matrix to have user specified dimensions and fill with specified pattern. - * - * @param int $n_rows[optional] - * @param int $n_cols[optional] - * @param int $fill[optional] - * @return Mat - */ - static function init($n_rows, $n_cols, $fill) { - return new static; - } - /** - * Create the matrix from a textual description. - * - * @param string $text - * @return Mat - */ - static function fromString($text) { - return new static; - } - - /** - * Create the matrix from a PHP array. - * - * @param array $arr - * @return Mat - */ - static function fromArray($arr) { - return new static; - } } diff --git a/stubs/internal/DenseResizableMatrix.php b/stubs/internal/DenseResizableMatrix.php index aa09352..36bd2c0 100644 --- a/stubs/internal/DenseResizableMatrix.php +++ b/stubs/internal/DenseResizableMatrix.php @@ -14,6 +14,16 @@ interface DenseResizableMatrix extends DenseMatrix, ResizableMatrix { // Initialization + /** + * Construct the matrix to have user specified dimensions and fill with specified pattern. + * + * @param int $n_rows[optional] + * @param int $n_cols[optional] + * @param int $fill[optional] + * @return Mat + */ + static function init($n_rows, $n_cols, $fill); + /** * Set all the elements of an object to one, optionally first changing the size to specified dimensions. * diff --git a/stubs/internal/ResizableMatrix.php b/stubs/internal/ResizableMatrix.php index 9f517a2..fb39724 100644 --- a/stubs/internal/ResizableMatrix.php +++ b/stubs/internal/ResizableMatrix.php @@ -9,6 +9,22 @@ namespace Arma\Internal; */ interface ResizableMatrix extends Resizable, Matrix { + /** + * Create the matrix from a textual description. + * + * @param string $text + * @return Mat + */ + static function fromString($text); + + /** + * Create the matrix from a PHP array. + * + * @param array $arr + * @return Mat + */ + static function fromArray($arr); + /** * Recreate the object according to given size specifications, with the elements taken from the * previous version of the object in a column-wise manner. diff --git a/stubs/internal/SparseResizableMatrix.php b/stubs/internal/SparseResizableMatrix.php index 241c6c9..08d0608 100644 --- a/stubs/internal/SparseResizableMatrix.php +++ b/stubs/internal/SparseResizableMatrix.php @@ -11,6 +11,21 @@ use Arma\SpMapVal; */ interface SparseResizableMatrix extends SparseMatrix, ResizableMatrix { + // Initialization + + /** + * Construct the matrix to have user specified dimensions and fill with specified pattern. + * + * $density should be specified when filling the matrix with random values. + * + * @param int $n_rows[optional] + * @param int $n_cols[optional] + * @param int $fill[optional] + * @param double $density[optional] + * @return Mat + */ + static function init($n_rows, $n_cols, $fill, $density); + // Subview /**