Add helper function `j()`. Fix bug. Refactor code.
This commit is contained in:
parent
84717bb121
commit
cf213d07c5
|
@ -9,6 +9,7 @@ if test "$PHP_ARMA" != "no"; then
|
||||||
|
|
||||||
ARMA_SRC=" \
|
ARMA_SRC=" \
|
||||||
src/php_arma.cc \
|
src/php_arma.cc \
|
||||||
|
src/functions.cc \
|
||||||
src/constants.cc \
|
src/constants.cc \
|
||||||
src/base.cc \
|
src/base.cc \
|
||||||
src/complex.cc \
|
src/complex.cc \
|
||||||
|
|
|
@ -23,8 +23,8 @@ namespace php_arma
|
||||||
zval *real, *imag;
|
zval *real, *imag;
|
||||||
ZEND_PARSE_PARAMETERS_START(0, 2)
|
ZEND_PARSE_PARAMETERS_START(0, 2)
|
||||||
Z_PARAM_OPTIONAL
|
Z_PARAM_OPTIONAL
|
||||||
Z_PARAM_ZVAL(real)
|
Z_PARAM_ZVAL_DEREF(real)
|
||||||
Z_PARAM_ZVAL(imag)
|
Z_PARAM_ZVAL_DEREF(imag)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
auto num_args = EX_NUM_ARGS();
|
auto num_args = EX_NUM_ARGS();
|
||||||
|
@ -54,8 +54,8 @@ namespace php_arma
|
||||||
{
|
{
|
||||||
zval *r, *theta;
|
zval *r, *theta;
|
||||||
ZEND_PARSE_PARAMETERS_START(2, 2)
|
ZEND_PARSE_PARAMETERS_START(2, 2)
|
||||||
Z_PARAM_ZVAL(r)
|
Z_PARAM_ZVAL_DEREF(r)
|
||||||
Z_PARAM_ZVAL(theta)
|
Z_PARAM_ZVAL_DEREF(theta)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
if (!zval_check_scalar<T>(r)) {
|
if (!zval_check_scalar<T>(r)) {
|
||||||
|
@ -74,7 +74,7 @@ namespace php_arma
|
||||||
{
|
{
|
||||||
zval *other;
|
zval *other;
|
||||||
ZEND_PARSE_PARAMETERS_START(1, 1)
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
||||||
Z_PARAM_ZVAL(other)
|
Z_PARAM_ZVAL_DEREF(other)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
if (!operators::add(getThis(), other, return_value)) {
|
if (!operators::add(getThis(), other, return_value)) {
|
||||||
|
@ -87,7 +87,7 @@ namespace php_arma
|
||||||
{
|
{
|
||||||
zval *other;
|
zval *other;
|
||||||
ZEND_PARSE_PARAMETERS_START(1, 1)
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
||||||
Z_PARAM_ZVAL(other)
|
Z_PARAM_ZVAL_DEREF(other)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
if (!operators::sub(getThis(), other, return_value)) {
|
if (!operators::sub(getThis(), other, return_value)) {
|
||||||
|
@ -106,7 +106,7 @@ namespace php_arma
|
||||||
{
|
{
|
||||||
zval *other;
|
zval *other;
|
||||||
ZEND_PARSE_PARAMETERS_START(1, 1)
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
||||||
Z_PARAM_ZVAL(other)
|
Z_PARAM_ZVAL_DEREF(other)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
if (!operators::mul(getThis(), other, return_value)) {
|
if (!operators::mul(getThis(), other, return_value)) {
|
||||||
|
@ -119,7 +119,7 @@ namespace php_arma
|
||||||
{
|
{
|
||||||
zval *other;
|
zval *other;
|
||||||
ZEND_PARSE_PARAMETERS_START(1, 1)
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
||||||
Z_PARAM_ZVAL(other)
|
Z_PARAM_ZVAL_DEREF(other)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
if (!operators::div(getThis(), other, return_value)) {
|
if (!operators::div(getThis(), other, return_value)) {
|
||||||
|
@ -186,7 +186,7 @@ namespace php_arma
|
||||||
{
|
{
|
||||||
zval *other;
|
zval *other;
|
||||||
ZEND_PARSE_PARAMETERS_START(1, 1)
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
||||||
Z_PARAM_ZVAL(other)
|
Z_PARAM_ZVAL_DEREF(other)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
if (!operators::pow(getThis(), other, return_value)) {
|
if (!operators::pow(getThis(), other, return_value)) {
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
//
|
||||||
|
// php-armadillo/functions.cc
|
||||||
|
//
|
||||||
|
// @Author CismonX
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "functions.hh"
|
||||||
|
#include "complex.hh"
|
||||||
|
|
||||||
|
#define PHP_ARMA_FE(func) \
|
||||||
|
ZEND_RAW_FENTRY("Arma\\" #func, ZEND_FN(func), nullptr, 0)
|
||||||
|
|
||||||
|
namespace php_arma
|
||||||
|
{
|
||||||
|
|
||||||
|
#ifdef PHP_ARMA_OPERATORS
|
||||||
|
PHP_FUNCTION(j)
|
||||||
|
{
|
||||||
|
double imag = 0.0;
|
||||||
|
ZEND_PARSE_PARAMETERS_START(0, 1)
|
||||||
|
Z_PARAM_OPTIONAL
|
||||||
|
Z_PARAM_DOUBLE_DEREF(imag)
|
||||||
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
|
zval_set_scalar(return_value, std::complex<double>(0.0, imag));
|
||||||
|
}
|
||||||
|
#endif // PHP_ARMA_OPERATORS
|
||||||
|
|
||||||
|
const zend_function_entry functions[] = {
|
||||||
|
#ifdef PHP_ARMA_OPERATORS
|
||||||
|
PHP_ARMA_FE(j)
|
||||||
|
#endif // PHP_ARMA_OPERATORS
|
||||||
|
PHP_FE_END
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
//
|
||||||
|
// php-armadillo/functions.hh
|
||||||
|
//
|
||||||
|
// @Author CismonX
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef PHP_ARMA_FUNCTIONS_HH
|
||||||
|
#define PHP_ARMA_FUNCTIONS_HH
|
||||||
|
|
||||||
|
#include "php_arma.hh"
|
||||||
|
|
||||||
|
namespace php_arma
|
||||||
|
{
|
||||||
|
extern const zend_function_entry functions[];
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !PHP_ARMA_FUNCTIONS_HH
|
|
@ -34,7 +34,7 @@ namespace php_arma
|
||||||
{
|
{
|
||||||
zval *val;
|
zval *val;
|
||||||
ZEND_PARSE_PARAMETERS_START(1, 1)
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
||||||
Z_PARAM_ZVAL(val)
|
Z_PARAM_ZVAL_DEREF(val)
|
||||||
ZEND_PARSE_PARAMETERS_END();
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
|
||||||
if (!zval_check_scalar<T>(val)) {
|
if (!zval_check_scalar<T>(val)) {
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include "complex.hh"
|
#include "complex.hh"
|
||||||
#include "base.hh"
|
#include "base.hh"
|
||||||
#include "mapval.hh"
|
#include "mapval.hh"
|
||||||
|
#include "functions.hh"
|
||||||
|
|
||||||
#ifdef PHP_ARMA_OPERATORS
|
#ifdef PHP_ARMA_OPERATORS
|
||||||
#include "operators.hh"
|
#include "operators.hh"
|
||||||
|
@ -16,39 +17,45 @@
|
||||||
|
|
||||||
#include <ext/standard/info.h>
|
#include <ext/standard/info.h>
|
||||||
|
|
||||||
PHP_MINIT_FUNCTION(arma)
|
namespace php_arma
|
||||||
{
|
{
|
||||||
php_arma::constants_init();
|
int module_init(INIT_FUNC_ARGS)
|
||||||
php_arma::complex_init();
|
{
|
||||||
php_arma::base_init();
|
constants_init();
|
||||||
php_arma::mapval_init();
|
complex_init();
|
||||||
|
base_init();
|
||||||
|
mapval_init();
|
||||||
|
|
||||||
#ifdef PHP_ARMA_OPERATORS
|
#ifdef PHP_ARMA_OPERATORS
|
||||||
php_arma::operators_init();
|
operators_init();
|
||||||
#endif // PHP_ARMA_OPERATORS
|
#endif // PHP_ARMA_OPERATORS
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
PHP_MINFO_FUNCTION(arma)
|
void module_info(ZEND_MODULE_INFO_FUNC_ARGS)
|
||||||
{
|
{
|
||||||
php_info_print_table_start();
|
php_info_print_table_start();
|
||||||
php_info_print_table_header(2, "armadillo support", "enabled");
|
php_info_print_table_header(2, "armadillo support", "enabled");
|
||||||
php_info_print_table_end();
|
php_info_print_table_end();
|
||||||
}
|
}
|
||||||
|
|
||||||
zend_module_entry arma_module_entry = {
|
zend_module_entry module_entry = {
|
||||||
STANDARD_MODULE_HEADER,
|
STANDARD_MODULE_HEADER,
|
||||||
"arma",
|
"arma",
|
||||||
nullptr,
|
functions,
|
||||||
PHP_MINIT(arma),
|
module_init,
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
PHP_MINFO(arma),
|
module_info,
|
||||||
PHP_ARMA_VERSION,
|
PHP_ARMA_VERSION,
|
||||||
STANDARD_MODULE_PROPERTIES
|
STANDARD_MODULE_PROPERTIES
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef COMPILE_DL_ARMA
|
#ifdef COMPILE_DL_ARMA
|
||||||
ZEND_GET_MODULE(arma)
|
extern "C" ZEND_DLEXPORT zend_module_entry *get_module()
|
||||||
#endif
|
{
|
||||||
|
return &php_arma::module_entry;
|
||||||
|
}
|
||||||
|
#endif // COMPILE_DL_ARMA
|
||||||
|
|
|
@ -22,8 +22,6 @@
|
||||||
#define ZEND_ACC_CTOR 0
|
#define ZEND_ACC_CTOR 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern zend_module_entry arma_module_entry;
|
|
||||||
|
|
||||||
/// Helper macros for method entry.
|
/// Helper macros for method entry.
|
||||||
|
|
||||||
#define PHP_ARMA_START_ME(cls, ...) \
|
#define PHP_ARMA_START_ME(cls, ...) \
|
||||||
|
@ -65,6 +63,13 @@ extern zend_module_entry arma_module_entry;
|
||||||
static constexpr const char val[] = name; \
|
static constexpr const char val[] = name; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper macros for parameter parsing.
|
||||||
|
|
||||||
|
#ifndef Z_PARAM_DOUBLE_DEREF
|
||||||
|
#define Z_PARAM_DOUBLE_DEREF(dest) \
|
||||||
|
Z_PARAM_DOUBLE_EX2(dest, _dummy, 0, 1, 0)
|
||||||
|
#endif // !Z_PARAM_DOUBLE_DEREF
|
||||||
|
|
||||||
#ifdef PHP_ARMA_OPERATORS
|
#ifdef PHP_ARMA_OPERATORS
|
||||||
|
|
||||||
/// Helper macros for handling operator overloading.
|
/// Helper macros for handling operator overloading.
|
||||||
|
@ -82,6 +87,8 @@ extern zend_module_entry arma_module_entry;
|
||||||
|
|
||||||
namespace php_arma
|
namespace php_arma
|
||||||
{
|
{
|
||||||
|
extern zend_module_entry module_entry;
|
||||||
|
|
||||||
/// Helpers for compile-time string concatenation for better module startup performance.
|
/// Helpers for compile-time string concatenation for better module startup performance.
|
||||||
namespace str
|
namespace str
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arma;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a CxDouble whose real part is zero.
|
||||||
|
*
|
||||||
|
* Helper function. Only defined when Features::OPERATORS is true.
|
||||||
|
*
|
||||||
|
* @param double $imag[optional]
|
||||||
|
* @return CxDouble
|
||||||
|
*/
|
||||||
|
function j($imag = 0.0) {}
|
|
@ -13,8 +13,10 @@ if (is_php_arma_loaded()) {
|
||||||
|
|
||||||
require_once 'includes/assert.php';
|
require_once 'includes/assert.php';
|
||||||
|
|
||||||
$foo = new Arma\CxDouble(1.2, 1.7);
|
use function Arma\j;
|
||||||
$bar = new Arma\CxDouble(2.3, 2.6);
|
|
||||||
|
$foo = 1.2 + j(1.7);
|
||||||
|
$bar = 2.3 + j(2.6);
|
||||||
|
|
||||||
batch_assert('operator overloading of Arma\\Complex',
|
batch_assert('operator overloading of Arma\\Complex',
|
||||||
[$foo->add($bar), $foo + $bar],
|
[$foo->add($bar), $foo + $bar],
|
||||||
|
|
Reference in New Issue