This commit is contained in:
CismonX 2019-04-04 16:07:37 +08:00
parent c73f9039ec
commit c3c900db48
19 changed files with 173 additions and 70 deletions

View File

@ -15,7 +15,7 @@ namespace php_arma
zend_always_inline
void const_declare(zend_class_entry *ce, Ts... constants)
{
for (auto &&[name, val] : { constants... }) {
for (auto [name, val] : { constants... }) {
zend_declare_class_constant_long(ce, name, strlen(name), val);
}
}
@ -38,7 +38,8 @@ namespace php_arma
std::tuple("RAW_ASCII", file_type::raw_ascii),
std::tuple("CSV_ASCII", file_type::csv_ascii),
std::tuple("COORD_ASCII", file_type::coord_ascii),
std::tuple("PGM_BINARY", file_type::pgm_binary)
std::tuple("PGM_BINARY", file_type::pgm_binary),
std::tuple("HDF5_BINARY", file_type::hdf5_binary)
);
const_declare(sort_direction_ce = abstract_class_register<sort_direction_php_name>(),
std::tuple("ASCEND", sort_direction::ascend),
@ -46,5 +47,10 @@ namespace php_arma
std::tuple("STRICT_ASCEND", sort_direction::strict_ascend),
std::tuple("STRICT_DESCEND", sort_direction::strict_descend)
);
const_declare(hdf5_opts_ce = abstract_class_register<hdf5_opts_php_name>(),
std::tuple("TRANS", hdf5_opts::trans),
std::tuple("APPEND", hdf5_opts::append),
std::tuple("REPLACE", hdf5_opts::replace)
);
}
}

View File

@ -31,6 +31,7 @@ namespace php_arma
static constexpr auto csv_ascii = 5;
static constexpr auto coord_ascii = 6;
static constexpr auto pgm_binary = 7;
static constexpr auto hdf5_binary = 8;
};
struct sort_direction
@ -41,15 +42,24 @@ namespace php_arma
static constexpr auto strict_descend = 3;
};
struct hdf5_opts
{
static constexpr auto trans = 1u << 0;
static constexpr auto append = 1u << 1;
static constexpr auto replace = 1u << 2;
};
void constants_init();
constexpr const char fill_php_name[] = "Fill";
constexpr const char file_type_php_name[] = "FileType";
constexpr const char sort_direction_php_name[] = "SortDirection";
constexpr const char hdf5_opts_php_name[] = "HDF5Opts";
inline zend_class_entry *fill_ce;
inline zend_class_entry *file_type_ce;
inline zend_class_entry *sort_direction_ce;
inline zend_class_entry *hdf5_opts_ce;
}
#endif //!PHP_ARMA_CONSTANTS_HH

View File

@ -148,8 +148,8 @@ namespace php_arma
zend_always_inline
zend_class_entry *class_register(zend_class_entry *parent, const zend_function_entry *methods)
{
using std::placeholders::_1;
return ce_init<Name>(methods, std::bind(zend_register_internal_class_ex, _1, parent));
auto init_func = std::bind(zend_register_internal_class_ex, std::placeholders::_1, parent);
return ce_init<Name>(methods, init_func);
}
template <const char *Name>

View File

@ -68,4 +68,16 @@ abstract class FileType
* in the [0,255] interval, otherwise the resulting image may not display correctly.
*/
const PGM_BINARY = 7;
/**
* Numerical data stored in portable HDF5 binary format.
*
* For saving, the default dataset name within the HDF5 file is "dataset". For loading, the order of
* operations is: (1) try loading a dataset named "dataset", (2) try loading a dataset named "value",
* (3) try loading the first available dataset.
*
* To explicitly control the dataset name, specify it by passing an array with the following format to the
* first argument: ['filename' => string, 'dataset' => string(default='dataset'), 'settings' => int(default=0)]
*/
const HDF5_BINARY = 8;
}

28
stubs/HDF5Opts.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace Arma;
/**
* Options when saving/loading data in HDF5 format.
*
* @package Arma
*/
abstract class HDF5Opts
{
/**
* Save/load the data with columns transposed to rows (and vice versa)
*/
const TRANS = 1 << 0;
/**
* Instead of overwriting the file, append the specified dataset to the file.
*
* The specified dataset must not already exist in the file
*/
const APPEND = 1 << 1;
/**
* Instead of overwriting the file, replace the specified dataset in the file.
*/
const REPLACE = 1 << 2;
}

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\Complex;
/**
* Interface for all container objects.
*
@ -68,8 +70,8 @@ interface Base
*
* For SpMat, replacement is not done when $old_value == 0.
*
* @param number|\Arma\Complex $old_value
* @param number|\Arma\Complex $new_value
* @param number|Complex $old_value
* @param number|Complex $new_value
* @return void
*/
function replace($old_value, $new_value);
@ -97,14 +99,14 @@ interface Base
/**
* Returns the minimum value within this object.
*
* @return number|\Arma\Complex
* @return number|Complex
*/
function min();
/**
* Returns the maximum value within this object.
*
* @return number|\Arma\Complex
* @return number|Complex
*/
function max();

View File

@ -2,6 +2,9 @@
namespace Arma\Internal;
use Arma\IMat;
use Arma\Complex;
/**
* Interface for objects with a dense layout (Mat, Col and Row).
*
@ -14,7 +17,7 @@ interface Dense extends Base
*
*
* @param static $other
* @return \Arma\IMat
* @return IMat
*/
function equals($other);
@ -22,7 +25,7 @@ interface Dense extends Base
* Element-wise non-equality evaluation of two objects.
*
* @param static $other
* @return \Arma\IMat
* @return IMat
*/
function notEquals($other);
@ -30,7 +33,7 @@ interface Dense extends Base
* Element-wise "greater than" evaluation of two objects.
*
* @param static $other
* @return \Arma\IMat
* @return IMat
*/
function greaterThan($other);
@ -38,7 +41,7 @@ interface Dense extends Base
* Element-wise "smaller than" evaluation of two objects.
*
* @param static $other
* @return \Arma\IMat
* @return IMat
*/
function smallerThan($other);
@ -46,7 +49,7 @@ interface Dense extends Base
* Element-wise "not greater than" evaluation of two objects.
*
* @param static $other
* @return \Arma\IMat
* @return IMat
*/
function notGreaterThan($other);
@ -54,7 +57,7 @@ interface Dense extends Base
* Element-wise "not smaller than" evaluation of two objects.
*
* @param static $other
* @return \Arma\IMat
* @return IMat
*/
function notSmallerThan($other);
@ -64,7 +67,7 @@ interface Dense extends Base
*
* For matrices, filling is done column-by-column.
*
* @param number|\Arma\Complex $value
* @param number|Complex $value
* @return void
*/
function fill($value);

View File

@ -2,6 +2,12 @@
namespace Arma\Internal;
use Arma\ {
MapVal,
SvCol, SvRow, SvMat,
DiagView
};
/**
* Interface for dense matrices.
*
@ -16,7 +22,7 @@ interface DenseMatrix extends Dense, Matrix
*
* @param int $i
* @param int $j[optional]
* @return \Arma\MapVal
* @return MapVal
*/
function __invoke($i, $j);;
@ -25,7 +31,7 @@ interface DenseMatrix extends Dense, Matrix
*
* @param int $i
* @param int $j[optional]
* @return \Arma\MapVal
* @return MapVal
*/
function at($i, $j);
@ -33,7 +39,7 @@ interface DenseMatrix extends Dense, Matrix
* {@inheritdoc}
*
* @param int $col_number
* @return \Arma\SvCol
* @return SvCol
*/
function col($col_number);
@ -41,7 +47,7 @@ interface DenseMatrix extends Dense, Matrix
* {@inheritdoc}
*
* @param int $row_number
* @return \Arma\SvRow
* @return SvRow
*/
function row($row_number);
@ -50,7 +56,7 @@ interface DenseMatrix extends Dense, Matrix
*
* @param int $first_col
* @param int $last_col
* @return \Arma\SvMat
* @return SvMat
*/
function cols($first_col, $last_col);
@ -59,7 +65,7 @@ interface DenseMatrix extends Dense, Matrix
*
* @param int $first_row
* @param int $last_row
* @return \Arma\SvMat
* @return SvMat
*/
function rows($first_row, $last_row);
@ -70,7 +76,7 @@ interface DenseMatrix extends Dense, Matrix
* @param int $first_col
* @param int $last_row
* @param int $last_col
* @return \Arma\SvMat
* @return SvMat
*/
function submat($first_row, $first_col, $last_row, $last_col);
@ -78,7 +84,7 @@ interface DenseMatrix extends Dense, Matrix
* {@inheritdoc}
*
* @param int $n_cols
* @return \Arma\SvMat
* @return SvMat
*/
function head_cols($n_cols);
@ -86,7 +92,7 @@ interface DenseMatrix extends Dense, Matrix
* {@inheritdoc}
*
* @param int $n_rows
* @return \Arma\SvMat
* @return SvMat
*/
function head_rows($n_rows);
@ -94,7 +100,7 @@ interface DenseMatrix extends Dense, Matrix
* {@inheritdoc}
*
* @param int $n_cols
* @return \Arma\SvMat
* @return SvMat
*/
function tail_cols($n_cols);
@ -102,7 +108,7 @@ interface DenseMatrix extends Dense, Matrix
* {@inheritdoc}
*
* @param int $n_rows
* @return \Arma\SvMat
* @return SvMat
*/
function tail_rows($n_rows);
@ -110,7 +116,7 @@ interface DenseMatrix extends Dense, Matrix
* {@inheritdoc}
*
* @param int $k[optional]
* @return \Arma\DiagView
* @return DiagView
*/
function diag($k = 0);
@ -119,7 +125,7 @@ interface DenseMatrix extends Dense, Matrix
/**
* Apply a callback function to each row of the matrix.
*
* @param callable $action (\Arma\SvRow) -> void
* @param callable $action (SvRow) -> void
* @return void
*/
function eachRow($action);
@ -127,7 +133,7 @@ interface DenseMatrix extends Dense, Matrix
/**
* Apply a callback function to each column of the matrix.
*
* @param callable $action (\Arma\SvCol) -> void
* @param callable $action (SvCol) -> void
* @return void
*/
function eachCol($action);

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\SortDirection;
/**
* Interface for resizable (non-subview) dense matrices.
*
@ -89,5 +91,5 @@ interface DenseResizableMatrix extends DenseMatrix, ResizableMatrix
* @param int $dim[optional]
* @return bool
*/
function isSorted($sort_direction = \Arma\SortDirection::ASCEND, $dim = 0);
function isSorted($sort_direction = SortDirection::ASCEND, $dim = 0);
}

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\SortDirection;
/**
* Interface for resizable (non-subview) dense vectors.
*
@ -59,5 +61,5 @@ interface DenseResizableVector extends DenseVector, ResizableVector
* @param int $sort_direction[optional]
* @return bool
*/
function isSorted($sort_direction = \Arma\SortDirection::ASCEND);
function isSorted($sort_direction = SortDirection::ASCEND);
}

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\MapVal;
/**
* Interface for vectors with a Dense Layout (Row and Col).
*
@ -15,7 +17,7 @@ interface DenseVector extends Dense, Vector
* {@inheritdoc}
*
* @param int $idx
* @return \Arma\MapVal
* @return MapVal
*/
function __invoke($idx);
@ -23,7 +25,7 @@ interface DenseVector extends Dense, Vector
* {@inheritdoc}
*
* @param int $idx
* @return \Arma\MapVal
* @return MapVal
*/
function at($idx);
}

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\FileType;
/**
* Interface for resizable objects (non-subview objects)
*
@ -55,20 +57,20 @@ interface Resizable
/**
* Store data in a file or stream (the stream must be opened in binary mode).
*
* @param resource|string $dest
* @param int $file_type[optional]
* @param resource|string|array $dest
* @param int $file_type[optional]
* @return bool
*/
function save($dest, $file_type = \Arma\FileType::ARMA_BINARY);
function save($dest, $file_type = FileType::ARMA_BINARY);
/**
* Retrieve data from a file or stream (the stream must be opened in binary mode).
*
* On failure, the object is reset so that it has no elements.
*
* @param resource|string $from
* @param int $file_type[optional]
* @param resource|string|array $from
* @param int $file_type[optional]
* @return bool
*/
function load($from, $file_type = \Arma\FileType::AUTO_DETECT);
function load($from, $file_type = FileType::AUTO_DETECT);
}

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\Complex;
/**
* Interface for objects which wraps a single scalar value.
*
@ -12,14 +14,14 @@ interface Scalar
/**
* Return a copy of the representing scalar value of this object.
*
* @return number|\Arma\Complex
* @return number|Complex
*/
function val();
/**
* Set the representing value to the specified new value.
*
* @param number|\Arma\Complex $value
* @param number|Complex $value
* @return void
*/
function setTo($value);

View File

@ -2,6 +2,9 @@
namespace Arma\Internal;
use Arma\SpSvMat;
use Arma\SpDiagView;
/**
* Interface for sparse matrices.
*
@ -15,7 +18,7 @@ interface SparseMatrix extends Sparse, Matrix
* {@inheritdoc}
*
* @param int $col_number
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function col($col_number);
@ -23,7 +26,7 @@ interface SparseMatrix extends Sparse, Matrix
* {@inheritdoc}
*
* @param int $row_number
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function row($row_number);
@ -32,7 +35,7 @@ interface SparseMatrix extends Sparse, Matrix
*
* @param int $first_col
* @param int $last_col
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function cols($first_col, $last_col);
@ -41,7 +44,7 @@ interface SparseMatrix extends Sparse, Matrix
*
* @param int $first_row
* @param int $last_row
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function rows($first_row, $last_row);
@ -52,7 +55,7 @@ interface SparseMatrix extends Sparse, Matrix
* @param int $first_col
* @param int $last_row
* @param int $last_col
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function submat($first_row, $first_col, $last_row, $last_col);
@ -60,7 +63,7 @@ interface SparseMatrix extends Sparse, Matrix
* {@inheritdoc}
*
* @param int $n_cols
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function head_cols($n_cols);
@ -68,7 +71,7 @@ interface SparseMatrix extends Sparse, Matrix
* {@inheritdoc}
*
* @param int $n_rows
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function head_rows($n_rows);
@ -76,7 +79,7 @@ interface SparseMatrix extends Sparse, Matrix
* {@inheritdoc}
*
* @param int $n_cols
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function tail_cols($n_cols);
@ -84,7 +87,7 @@ interface SparseMatrix extends Sparse, Matrix
* {@inheritdoc}
*
* @param int $n_rows
* @return \Arma\SpSvMat
* @return SpSvMat
*/
function tail_rows($n_rows);
@ -92,7 +95,7 @@ interface SparseMatrix extends Sparse, Matrix
* {@inheritdoc}
*
* @param int $k[optional]
* @return \Arma\SpDiagView
* @return SpDiagView
*/
function diag($k = 0);
}

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\SpSvMapVal;
/**
* Interface for resizable (non-subview) sparse matrices.
*
@ -16,7 +18,7 @@ interface SparseNonResizableMatrix extends SparseMatrix, NonResizableMatrix
*
* @param int $i
* @param int $j[optional]
* @return \Arma\SpSvMapVal
* @return SpSvMapVal
*/
function __invoke($i, $j);
@ -25,7 +27,7 @@ interface SparseNonResizableMatrix extends SparseMatrix, NonResizableMatrix
*
* @param int $i
* @param int $j[optional]
* @return \Arma\SpSvMapVal
* @return SpSvMapVal
*/
function at($i, $j);
}

View File

@ -2,6 +2,13 @@
namespace Arma\Internal;
use Arma\SpMapVal;
/**
* Interface for resizable (non-subview) sparse matrices.
*
* @package Arma\Internal
*/
interface SparseResizableMatrix extends SparseMatrix, ResizableMatrix
{
// Subview
@ -11,7 +18,7 @@ interface SparseResizableMatrix extends SparseMatrix, ResizableMatrix
*
* @param int $i
* @param int $j[optional]
* @return \Arma\SpMapVal
* @return SpMapVal
*/
function __invoke($i, $j);
@ -20,7 +27,7 @@ interface SparseResizableMatrix extends SparseMatrix, ResizableMatrix
*
* @param int $i
* @param int $j[optional]
* @return \Arma\SpMapVal
* @return SpMapVal
*/
function at($i, $j);
}

View File

@ -2,6 +2,8 @@
namespace Arma\Internal;
use Arma\SpMapVal;
interface SparseVector extends Sparse, Vector
{
/// Subview
@ -10,7 +12,7 @@ interface SparseVector extends Sparse, Vector
* {@inheritdoc}
*
* @param int $idx
* @return \Arma\SpMapVal
* @return SpMapVal
*/
function __invoke($idx);
@ -18,7 +20,7 @@ interface SparseVector extends Sparse, Vector
* {@inheritdoc}
*
* @param int $idx
* @return \Arma\SpMapVal
* @return SpMapVal
*/
function at($idx);
}

View File

@ -4,25 +4,31 @@ Test for constants.
<?php require_once 'includes/loaded.php'; ?>
--FILE--
<?php
require_once 'includes/assert.php';
$fill = [
Arma\Fill::NONE, Arma\Fill::ZEROS, Arma\Fill::ONES,
Arma\Fill::EYE, Arma\Fill::RANDU, Arma\Fill::RANDN
];
use Arma\ {
Fill, FileType, SortDirection, HDF5Opts
};
$fill = [Fill::NONE, Fill::ZEROS, Fill::ONES, Fill::EYE, Fill::RANDU, Fill::RANDN];
$file_type = [
Arma\FileType::AUTO_DETECT, Arma\FileType::ARMA_BINARY,
Arma\FileType::ARMA_ASCII, Arma\FileType::RAW_BINARY,
Arma\FileType::RAW_ASCII, Arma\FileType::CSV_ASCII,
Arma\FileType::COORD_ASCII, Arma\FileType::PGM_BINARY
FileType::AUTO_DETECT, FileType::ARMA_BINARY, FileType::ARMA_ASCII,
FileType::RAW_BINARY, FileType::RAW_ASCII, FileType::CSV_ASCII,
FileType::COORD_ASCII, FileType::PGM_BINARY, FileType::HDF5_BINARY
];
$sort_direction = [
Arma\SortDirection::ASCEND, Arma\SortDirection::DESCEND,
Arma\SortDirection::STRICT_ASCEND, Arma\SortDirection::STRICT_DESCEND
SortDirection::ASCEND, SortDirection::DESCEND,
SortDirection::STRICT_ASCEND, SortDirection::STRICT_DESCEND
];
$hdf5_opts = [HDF5Opts::TRANS >> 0, HDF5Opts::APPEND >> 1, HDF5Opts::REPLACE >> 2];
batch_assert('constants',
[range(0, 5), $fill],
[range(0, 7), $file_type],
[range(0, 3), $sort_direction]
[range(0, 5), $fill],
[range(0, 8), $file_type],
[range(0, 3), $sort_direction],
[array_fill(0, 3, 1), $hdf5_opts]
);
?>
--EXPECT--

View File

@ -4,17 +4,23 @@ Test for `Arma\Complex`.
<?php require_once 'includes/loaded.php'; ?>
--FILE--
<?php
require_once 'includes/assert.php';
$cx_double = new Arma\CxDouble(1.2, 2.4);
batch_assert('Arma\\Complex',
[1.2, $cx_double->real],
[2.4, $cx_double->imag]
);
$cx_double->real *= 1.1;
$cx_double->imag *= 1.3;
batch_assert('Arma\\Complex',
[1.2 * 1.1, $cx_double->real],
[2.4 * 1.3, $cx_double->imag]
);
?>
--EXPECT--