move matrix constructors to appropriate interfaces
This commit is contained in:
parent
60a8d88fb4
commit
272f30689c
|
@ -13,6 +13,24 @@
|
|||
|
||||
namespace php_arma
|
||||
{
|
||||
template <typename T, typename ChildT>
|
||||
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 <typename T, typename T1>
|
||||
PHP_ARMA_METHOD(dense_resizable_matrix, ones, T, T1)
|
||||
{
|
||||
|
@ -226,6 +244,7 @@ namespace php_arma
|
|||
|
||||
template <typename T, typename T1>
|
||||
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)
|
||||
|
|
|
@ -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);
|
||||
|
|
83
src/mat.cc
83
src/mat.cc
|
@ -16,91 +16,8 @@
|
|||
|
||||
namespace php_arma
|
||||
{
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
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 <typename T>
|
||||
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<T>(elem))) {
|
||||
goto end_loop;
|
||||
}
|
||||
native->at(idx_row, idx_col++) = zval_get_scalar<T>(elem);
|
||||
ZEND_HASH_FOREACH_END();
|
||||
++idx_row;
|
||||
idx_col = 0;
|
||||
ZEND_HASH_FOREACH_END();
|
||||
end_loop:
|
||||
RETVAL_OBJ(zobj);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
|
|
|
@ -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*);
|
||||
};
|
||||
|
||||
|
|
|
@ -11,6 +11,68 @@
|
|||
|
||||
namespace php_arma
|
||||
{
|
||||
template <typename T, typename ChildT>
|
||||
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 <typename T, typename ChildT>
|
||||
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<T>(elem))) {
|
||||
goto end_loop;
|
||||
}
|
||||
native->at(idx_row, idx_col++) = zval_get_scalar<T>(elem);
|
||||
ZEND_HASH_FOREACH_END();
|
||||
++idx_row;
|
||||
idx_col = 0;
|
||||
ZEND_HASH_FOREACH_END();
|
||||
end_loop:
|
||||
RETVAL_OBJ(zobj);
|
||||
}
|
||||
|
||||
template <typename T, typename T1>
|
||||
PHP_ARMA_METHOD(resizable_matrix, reshape, T, T1)
|
||||
{
|
||||
|
@ -153,16 +215,18 @@ namespace php_arma
|
|||
|
||||
template <typename T, typename T1>
|
||||
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()
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
||||
/**
|
||||
|
|
Reference in New Issue