Implement `SvMat`.

This commit is contained in:
CismonX 2019-07-08 00:57:25 +08:00
parent 9cdc10dd3f
commit 1de7c70361
25 changed files with 480 additions and 55 deletions

View File

@ -13,6 +13,7 @@ if test "$PHP_ARMA" != "no"; then
src/php_arma.cc \
src/functions.cc \
src/constants.cc \
src/subview.cc \
src/base.cc \
src/complex.cc \
src/mapval.cc \
@ -20,9 +21,13 @@ if test "$PHP_ARMA" != "no"; then
src/matrix.cc \
src/dense_matrix.cc \
src/resizable.cc \
src/non_resizable.cc \
src/resizable_matrix.cc \
src/non_resizable_matrix.cc \
src/dense_resizable_matrix.cc \
src/mat.cc
src/dense_non_resizable_matrix.cc \
src/mat.cc \
src/subview_mat.cc \
"
if test "$PHP_ARMA_OPERATORS" != "no"; then

View File

@ -6,6 +6,7 @@
#include "base.hh"
#include "mat.hh"
#include "subview_mat.hh"
#include "stream_utils.hh"
#include <armadillo>
@ -205,4 +206,5 @@ namespace php_arma
}
PHP_ARMA_INSTANTIATE(base, mat);
PHP_ARMA_INSTANTIATE(base, subview_mat);
}

View File

@ -7,6 +7,7 @@
#include "dense.hh"
#include "base.hh"
#include "mat.hh"
#include "subview_mat.hh"
namespace php_arma
{
@ -106,4 +107,5 @@ namespace php_arma
}
PHP_ARMA_INSTANTIATE(dense, mat);
PHP_ARMA_INSTANTIATE(dense, subview_mat);
}

View File

@ -47,7 +47,7 @@ namespace php_arma
throw_error("no such comparation for complex elements");
return false;
} else {
using dest_t = typename ChildT::with_int_elem_t;
using dest_t = typename ChildT::with_int_elem_t::orig_t;
using dest_native_t = typename dest_t::native_t;
try {
auto ret = func(Z_NATIVE_OBJ_P(zv1), Z_NATIVE_OBJ_P(zv2)).eval();

View File

@ -9,6 +9,7 @@
#include "matrix.hh"
#include "mapval.hh"
#include "mat.hh"
#include "subview_mat.hh"
#include <armadillo>
@ -55,7 +56,7 @@ namespace php_arma
zend_object *zobj;
if (EX_NUM_ARGS() == 1) {
zobj = mapval_dense<T>::create(&native->at(i));
zobj = mapval_dense<T>::create(&native->operator[](i));
} else {
zobj = mapval_dense<T>::create(&native->at(i, j));
}
@ -74,16 +75,36 @@ namespace php_arma
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_matrix, cols, T, T1)
template <typename T, typename ChildT>
PHP_ARMA_METHOD(dense_matrix, cols, T, ChildT)
{
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();
using ret_t = typename ChildT::subview_t;
auto native = THIS_NATIVE;
auto zobj = ret_t::create(std::move(native->cols(first_col, last_col)));
RETVAL_OBJ(zobj);
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_matrix, rows, T, T1)
template <typename T, typename ChildT>
PHP_ARMA_METHOD(dense_matrix, rows, T, ChildT)
{
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();
using ret_t = typename ChildT::subview_t;
auto native = THIS_NATIVE;
auto zobj = ret_t::create(std::move(native->rows(first_row, last_row)));
RETVAL_OBJ(zobj);
}
template <typename T, typename T1>
@ -146,14 +167,16 @@ namespace php_arma
Z_PARAM_BOOL(inv_sympd)
ZEND_PARSE_PARAMETERS_END();
using ret_t = typename ChildT::orig_t;
auto native = THIS_NATIVE;
zend_object *zobj;
try {
if (inv_sympd) {
zobj = ChildT::create(std::move(arma::inv_sympd(*native).eval()));
zobj = ret_t::create(std::move(arma::inv_sympd(*native).eval()));
} else {
zobj = ChildT::create(std::move(native->i().eval()));
zobj = ret_t::create(std::move(native->i().eval()));
}
} catch (const std::logic_error& err) {
throw_error("matrix is not square sized");
@ -222,4 +245,5 @@ namespace php_arma
}
PHP_ARMA_INSTANTIATE(dense_matrix, mat);
PHP_ARMA_INSTANTIATE(dense_matrix, subview_mat);
}

View File

@ -0,0 +1,54 @@
//
// php-armadillo/dense_non_resizable_matrix.cc
//
// @Author CismonX
//
#include "dense_non_resizable_matrix.hh"
#include "non_resizable_matrix.hh"
#include "dense_matrix.hh"
#include "subview_mat.hh"
#include <armadillo>
namespace php_arma
{
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_non_resizable_matrix, ones, T, T1)
{
auto native = THIS_NATIVE;
native->ones();
RETVAL_THIS();
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_non_resizable_matrix, randu, T, T1)
{
auto native = THIS_NATIVE;
native->randu();
RETVAL_THIS();
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_non_resizable_matrix, randn, T, T1)
{
auto native = THIS_NATIVE;
native->randn();
RETVAL_THIS();
}
template <typename T, typename T1>
PHP_ARMA_START_ME(dense_non_resizable_matrix, T, T1)
PHP_ARMA_ME(ones, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(randu, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(randn, ZEND_ACC_PUBLIC)
PHP_ARMA_END_ME();
void dense_non_resizable_matrix_init()
{
dense_non_resizable_matrix_ce = interface_register<dense_non_resizable_matrix_php_name>(
non_resizable_matrix_ce, dense_matrix_ce);
}
PHP_ARMA_INSTANTIATE(dense_non_resizable_matrix, subview_mat);
}

View File

@ -0,0 +1,34 @@
//
// php-armadillo/dense_non_resizable_matrix.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_DENSE_NON_RESIZABLE_MATRIX_HH
#define PHP_ARMA_DENSE_NON_RESIZABLE_MATRIX_HH
#include "php_arma.hh"
namespace php_arma
{
template <typename T, typename ChildT>
struct dense_non_resizable_matrix
{
using native_t = typename ChildT::native_t;
PHP_ARMA_COMMON_DECLARE();
private:
static PHP_FUNCTION(ones);
static PHP_FUNCTION(randu);
static PHP_FUNCTION(randn);
};
void dense_non_resizable_matrix_init();
constexpr const char dense_non_resizable_matrix_php_name[] = "DenseNonResizableMatrix";
inline zend_class_entry *dense_non_resizable_matrix_ce;
}
#endif // !PHP_ARMA_DENSE_NON_RESIZABLE_MATRIX_HH

View File

@ -19,10 +19,15 @@ namespace arma
namespace php_arma
{
template <typename T>
struct subview_mat;
template <typename T>
struct mat
{
using native_t = arma::Mat<T>;
using orig_t = mat<T>;
using subview_t = subview_mat<T>;
using with_int_elem_t = mat<zend_long>;
friend void mat_init();
@ -48,7 +53,7 @@ namespace php_arma
void mat_init();
constexpr const char mat_php_name[] = "Base";
constexpr const char mat_php_name[] = "Mat";
inline zend_class_entry *mat_ce;
}

View File

@ -6,6 +6,7 @@
#include "matrix.hh"
#include "mat.hh"
#include "subview_mat.hh"
#include <armadillo>
@ -97,25 +98,6 @@ namespace php_arma
RETVAL_BOOL(native->is_hermitian(zval_get_scalar<base_t>(tol)));
}
template <typename T, typename T1>
PHP_ARMA_METHOD(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 <typename T, typename T1>
PHP_ARMA_METHOD(matrix, swapRows, T, T1)
{
@ -147,6 +129,8 @@ namespace php_arma
template <typename T, typename ChildT>
PHP_ARMA_METHOD(matrix, t, T, ChildT)
{
using ret_t = typename ChildT::orig_t;
auto native = THIS_NATIVE;
zend_object *zobj;
@ -158,12 +142,12 @@ namespace php_arma
ZEND_PARSE_PARAMETERS_END();
if (conj) {
zobj = ChildT::create(std::move(native->t().eval()));
zobj = ret_t::create(std::move(native->t().eval()));
} else {
zobj = ChildT::create(std::move(native->st().eval()));
zobj = ret_t::create(std::move(native->st().eval()));
}
} else {
zobj = ChildT::create(std::move(native->t().eval()));
zobj = ret_t::create(std::move(native->t().eval()));
}
RETVAL_OBJ(zobj);
@ -179,7 +163,6 @@ namespace php_arma
PHP_ARMA_ME(isSquare, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(isSymmetric, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(isHermitian, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(inRange, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(swapRows, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(swapCols, ZEND_ACC_PUBLIC)
PHP_ARMA_ME(t, ZEND_ACC_PUBLIC)
@ -191,4 +174,5 @@ namespace php_arma
}
PHP_ARMA_INSTANTIATE(matrix, mat);
PHP_ARMA_INSTANTIATE(matrix, subview_mat);
}

View File

@ -27,7 +27,6 @@ namespace php_arma
static PHP_FUNCTION(isSquare);
static PHP_FUNCTION(isSymmetric);
static PHP_FUNCTION(isHermitian);
static PHP_FUNCTION(inRange);
static PHP_FUNCTION(swapRows);
static PHP_FUNCTION(swapCols);
static PHP_FUNCTION(t);

33
src/non_resizable.cc Normal file
View File

@ -0,0 +1,33 @@
//
// php-armadillo/non_resizable.cc
//
// @Author CismonX
//
#include "non_resizable.hh"
#include "subview_mat.hh"
#include <armadillo>
namespace php_arma
{
template <typename T, typename T1>
PHP_ARMA_METHOD(non_resizable, zeros, T, T1)
{
auto native = THIS_NATIVE;
native->zeros();
RETVAL_THIS();
}
template <typename T, typename T1>
PHP_ARMA_START_ME(non_resizable, T, T1)
PHP_ARMA_ME(zeros, ZEND_ACC_PUBLIC)
PHP_ARMA_END_ME();
void non_resizable_init()
{
non_resizable_ce = interface_register<non_resizable_php_name>();
}
PHP_ARMA_INSTANTIATE(non_resizable, subview_mat);
}

32
src/non_resizable.hh Normal file
View File

@ -0,0 +1,32 @@
//
// php-armadillo/non_resizable.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_NON_RESIZABLE_HH
#define PHP_ARMA_NON_RESIZABLE_HH
#include "php_arma.hh"
namespace php_arma
{
template <typename T, typename ChildT>
struct non_resizable
{
using native_t = typename ChildT::native_t;
PHP_ARMA_COMMON_DECLARE();
private:
static PHP_FUNCTION(zeros);
};
void non_resizable_init();
constexpr const char non_resizable_php_name[] = "NonResizable";
inline zend_class_entry *non_resizable_ce;
}
#endif // !PHP_ARMA_NON_RESIZABLE_HH

View File

@ -0,0 +1,36 @@
//
// php-armadillo/non_resizable_matrix.cc
//
// @Author CismonX
//
#include "non_resizable_matrix.hh"
#include "non_resizable.hh"
#include "matrix.hh"
#include "subview_mat.hh"
#include <armadillo>
namespace php_arma
{
template <typename T, typename T1>
PHP_ARMA_METHOD(non_resizable_matrix, eye, T, T1)
{
auto native = THIS_NATIVE;
native->eye();
RETVAL_THIS();
}
template <typename T, typename T1>
PHP_ARMA_START_ME(non_resizable_matrix, T, T1)
PHP_ARMA_ME(eye, ZEND_ACC_PUBLIC)
PHP_ARMA_END_ME();
void non_resizable_matrix_init()
{
non_resizable_matrix_ce = interface_register<non_resizable_matrix_php_name>(
non_resizable_ce, matrix_ce);
}
PHP_ARMA_INSTANTIATE(non_resizable_matrix, subview_mat);
}

View File

@ -0,0 +1,32 @@
//
// php-armadillo/non_resizable_matrix.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_NON_RESIZABLE_MATRIX_HH
#define PHP_ARMA_NON_RESIZABLE_MATRIX_HH
#include "php_arma.hh"
namespace php_arma
{
template <typename T, typename ChildT>
struct non_resizable_matrix
{
using native_t = typename ChildT::native_t;
PHP_ARMA_COMMON_DECLARE();
private:
static PHP_FUNCTION(eye);
};
void non_resizable_matrix_init();
constexpr const char non_resizable_matrix_php_name[] = "NonResizableMatrix";
inline zend_class_entry *non_resizable_matrix_ce;
}
#endif // !PHP_ARMA_NON_RESIZABLE_MATRIX_HH

View File

@ -7,16 +7,21 @@
#include "php_arma.hh"
#include "constants.hh"
#include "complex.hh"
#include "subview.hh"
#include "base.hh"
#include "dense.hh"
#include "matrix.hh"
#include "dense_matrix.hh"
#include "resizable.hh"
#include "resizable_matrix.hh"
#include "non_resizable.hh"
#include "non_resizable_matrix.hh"
#include "dense_resizable_matrix.hh"
#include "dense_non_resizable_matrix.hh"
#include "mapval.hh"
#include "functions.hh"
#include "mat.hh"
#include "subview_mat.hh"
#ifdef PHP_ARMA_OPERATORS
#include "operators.hh"
@ -30,6 +35,7 @@ namespace php_arma
{
constants_init();
complex_init();
subview_init();
base_init();
mapval_init();
dense_init();
@ -37,8 +43,12 @@ namespace php_arma
dense_matrix_init();
resizable_init();
resizable_matrix_init();
non_resizable_init();
non_resizable_matrix_init();
dense_resizable_matrix_init();
dense_non_resizable_matrix_init();
mat_init();
subview_mat_init();
#ifdef PHP_ARMA_OPERATORS
operators_init();

View File

@ -84,6 +84,25 @@ namespace php_arma
RETVAL_THIS();
}
template <typename T, typename T1>
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 <typename T, typename T1>
PHP_ARMA_METHOD(resizable_matrix, shedRow, T, T1)
{
@ -141,6 +160,7 @@ namespace php_arma
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)

View File

@ -24,6 +24,7 @@ namespace php_arma
static PHP_FUNCTION(setSize);
static PHP_FUNCTION(zeros);
static PHP_FUNCTION(eye);
static PHP_FUNCTION(inRange);
static PHP_FUNCTION(shedRow);
static PHP_FUNCTION(shedCol);
static PHP_FUNCTION(shedRows);

15
src/subview.cc Normal file
View File

@ -0,0 +1,15 @@
//
// php-armadillo/subview.cc
//
// @Author CismonX
//
#include "subview.hh"
namespace php_arma
{
void subview_init()
{
subview_ce = interface_register<subview_php_name>();
}
}

21
src/subview.hh Normal file
View File

@ -0,0 +1,21 @@
//
// php-armadillo/subview.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_SUBVIEW_HH
#define PHP_ARMA_SUBVIEW_HH
#include "php_arma.hh"
namespace php_arma
{
void subview_init();
constexpr const char subview_php_name[] = "Subview";
inline zend_class_entry *subview_ce;
}
#endif // !PHP_ARMA_SUBVIEW_HH

58
src/subview_mat.cc Normal file
View File

@ -0,0 +1,58 @@
//
// php-armadillo/subview_mat.cc
//
// @Author CismonX
//
#include "subview_mat.hh"
#include "subview.hh"
#include "base.hh"
#include "dense.hh"
#include "matrix.hh"
#include "dense_matrix.hh"
#include "non_resizable.hh"
#include "non_resizable_matrix.hh"
#include "dense_non_resizable_matrix.hh"
namespace php_arma
{
template <typename T>
PHP_ARMA_START_ME(subview_mat, T)
PHP_ARMA_END_ME();
template <typename T>
void subview_mat<T>::ce_init(zend_class_entry *parent_ce)
{
ce = class_register<php_name::val>(parent_ce, fentry_list_concat(
PHP_ARMA_FENTRY((base <T, subview_mat>::me)),
PHP_ARMA_FENTRY((dense <T, subview_mat>::me)),
PHP_ARMA_FENTRY((matrix <T, subview_mat>::me)),
PHP_ARMA_FENTRY((dense_matrix <T, subview_mat>::me)),
PHP_ARMA_FENTRY((non_resizable <T, subview_mat>::me)),
PHP_ARMA_FENTRY((non_resizable_matrix <T, subview_mat>::me)),
PHP_ARMA_FENTRY((dense_non_resizable_matrix<T, subview_mat>::me)),
PHP_ARMA_FENTRY(me)
));
ce->create_object = object_non_constructible;
object_handlers_init(&handlers);
handlers.offset = sizeof(native_t);
handlers.clone_obj = nullptr;
handlers.dtor_obj = object_destroy<native_t>;
handlers.count_elements = base<T, subview_mat>::count_elements;
}
PHP_ARMA_NAME_DECLARE(subview_mat, "DSvMat", double);
PHP_ARMA_NAME_DECLARE(subview_mat, "ISvMat", zend_long);
PHP_ARMA_NAME_DECLARE(subview_mat, "CxDSvMat", cx_double);
void subview_mat_init()
{
subview_mat_ce = abstract_class_register<subview_mat_php_name>(
dense_non_resizable_matrix_ce, subview_ce);
subview_mat<double >::ce_init(subview_mat_ce);
subview_mat<zend_long>::ce_init(subview_mat_ce);
subview_mat<cx_double>::ce_init(subview_mat_ce);
}
}

56
src/subview_mat.hh Normal file
View File

@ -0,0 +1,56 @@
//
// php-armadillo/subview_mat.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_SUBVIEW_MAT_HH
#define PHP_ARMA_SUBVIEW_MAT_HH
#include "php_arma.hh"
#include "complex.hh"
namespace arma
{
template <typename T>
class subview;
}
namespace php_arma
{
template <typename T>
struct mat;
template <typename T>
struct subview_mat
{
using native_t = arma::subview<T>;
using orig_t = mat<T>;
using subview_t = subview_mat<T>;
using with_int_elem_t = subview_mat<zend_long>;
friend void subview_mat_init();
template <typename... Ts>
zend_always_inline
static zend_object *create(Ts&&... args)
{
return object_create_ctor<native_t>(ce, &handlers, args...);
}
PHP_ARMA_CE_HANDLRES_DECLARE();
private:
PHP_ARMA_COMMON_DECLARE();
static void ce_init(zend_class_entry*);
};
void subview_mat_init();
constexpr const char subview_mat_php_name[] = "SvMat";
inline zend_class_entry *subview_mat_ce;
}
#endif // !PHP_ARMA_SUBVIEW_MAT_HH

View File

@ -69,15 +69,6 @@ interface Matrix
*/
function isHermitian($tol);
/**
* Check whether the given location is currently valid.
*
* @param int $row
* @param int $col[optional]
* @return bool
*/
function inRange($row, $col);
// Subview
/**

View File

@ -59,6 +59,17 @@ interface ResizableMatrix extends Resizable, Matrix
*/
function eye($n_rows, $n_cols);
// Characteristics
/**
* Check whether the given location is currently valid.
*
* @param int $row
* @param int $col[optional]
* @return bool
*/
function inRange($row, $col);
// Reduction
/**

View File

@ -33,4 +33,14 @@ interface ResizableVector extends Resizable, Vector
* @return $this
*/
function zeros($n_elem);
// Characteristics
/**
* Check whether the given index is currently valid.
*
* @param int $idx
* @return bool
*/
function inRange($idx);
}

View File

@ -94,14 +94,4 @@ interface Vector
* @return Vector
*/
function t($conj = true);
// Characteristics
/**
* Check whether the given index is currently valid.
*
* @param int $idx
* @return bool
*/
function inRange($idx);
}