// // php-armadillo/resizable_matrix.cc // // @Author CismonX // #include "resizable_matrix.hh" #include "resizable.hh" #include "matrix.hh" #include "mat.hh" 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) { zend_long n_rows, n_cols; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(n_rows) Z_PARAM_LONG(n_cols) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->reshape(n_rows, n_cols); } template PHP_ARMA_METHOD(resizable_matrix, resize, T, T1) { zend_long n_rows, n_cols; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(n_rows) Z_PARAM_LONG(n_cols) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->resize(n_rows, n_cols); } template PHP_ARMA_METHOD(resizable_matrix, setSize, T, T1) { zend_long n_rows, n_cols; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(n_rows) Z_PARAM_LONG(n_cols) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->set_size(n_rows, n_cols); } template PHP_ARMA_METHOD(resizable_matrix, zeros, T, T1) { zend_long n_rows = 0; zend_long n_cols = 0; ZEND_PARSE_PARAMETERS_START(0, 2) Z_PARAM_OPTIONAL Z_PARAM_LONG(n_rows) Z_PARAM_LONG(n_cols) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->zeros(n_rows, n_cols); RETVAL_THIS(); } template PHP_ARMA_METHOD(resizable_matrix, eye, T, T1) { zend_long n_rows = 0; zend_long n_cols = 0; ZEND_PARSE_PARAMETERS_START(0, 2) Z_PARAM_OPTIONAL Z_PARAM_LONG(n_rows) Z_PARAM_LONG(n_cols) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->eye(n_rows, n_cols); RETVAL_THIS(); } template PHP_ARMA_METHOD(resizable_matrix, inRange, T, T1) { zend_long row; zend_long col; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_LONG(row) Z_PARAM_OPTIONAL Z_PARAM_LONG(col) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; if (EX_NUM_ARGS() == 1) { RETURN_BOOL(native->in_range(row)); } RETVAL_BOOL(native->in_range(row, col)); } template PHP_ARMA_METHOD(resizable_matrix, shedRow, T, T1) { zend_long row_idx; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(row_idx) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->shed_row(row_idx); } template PHP_ARMA_METHOD(resizable_matrix, shedCol, T, T1) { zend_long col_idx; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(col_idx) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->shed_col(col_idx); } template PHP_ARMA_METHOD(resizable_matrix, shedRows, T, T1) { zend_long first_row, last_row; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(first_row) Z_PARAM_LONG(last_row) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->shed_rows(first_row, last_row); } template PHP_ARMA_METHOD(resizable_matrix, shedCols, T, T1) { zend_long first_col, last_col; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(first_col) Z_PARAM_LONG(last_col) ZEND_PARSE_PARAMETERS_END(); auto native = THIS_NATIVE; native->shed_rows(first_col, last_col); } template PHP_ARMA_START_ME(resizable_matrix, T, T1) 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() { resizable_matrix_ce = interface_register(resizable_ce, matrix_ce); } PHP_ARMA_INSTANTIATE(resizable_matrix, mat); }