update matrix

This commit is contained in:
张壹 2025-02-21 09:51:44 +08:00
parent db71a9a47f
commit 99a2de65d6
2 changed files with 57 additions and 10 deletions

View File

@ -33,4 +33,4 @@ add_example(array_ex OFF)
add_example(gmt_ex OFF) add_example(gmt_ex OFF)
add_example(gnuplot_ex OFF) add_example(gnuplot_ex OFF)
add_example(stl_io_ex OFF) add_example(stl_io_ex OFF)
add_example(ply_io_ex ON) add_example(ply_io_ex OFF)

View File

@ -36,14 +36,14 @@ namespace gctl
template <typename MatValType> class matrix; template <typename MatValType> class matrix;
/* /*
* Here we define some commonly used array types. * Here we define some commonly used matrix types.
*/ */
typedef matrix<int> _2i_matrix; ///< 2-D integer array typedef matrix<int> _2i_matrix; ///< 2-D integer matrix
typedef matrix<float> _2f_matrix; ///< 2-D single-precision floating-point array typedef matrix<float> _2f_matrix; ///< 2-D single-precision floating-point matrix
typedef matrix<double> _2d_matrix; ///< 2-D double-precision floating-point array typedef matrix<double> _2d_matrix; ///< 2-D double-precision floating-point matrix
typedef matrix<std::string> _2s_matrix; ///< 2-D string array typedef matrix<std::string> _2s_matrix; ///< 2-D string matrix
typedef matrix<std::complex<float> > _2cf_matrix; ///< 1-D complex float array typedef matrix<std::complex<float> > _2cf_matrix; ///< 1-D complex float matrix
typedef matrix<std::complex<double> > _2cd_matrix; ///< 1-D complex double array typedef matrix<std::complex<double> > _2cd_matrix; ///< 1-D complex double matrix
/** /**
* @brief * @brief
@ -259,13 +259,20 @@ namespace gctl
* @param odr * @param odr
*/ */
void mean(array<MatValType> &m, matrix_order_e odr = RowMajor); void mean(array<MatValType> &m, matrix_order_e odr = RowMajor);
/**
* @brief
*
* @param odr
*/
void normalize(matrix_order_e odr = RowMajor);
/** /**
* @brief * @brief
* *
* @param n_type * @param n_type
*/ */
double norm(norm_type_e n_type = L2) const; double module(norm_type_e n_type = L2) const;
/** /**
* @brief Set elements' value as a sequent. * @brief Set elements' value as a sequent.
@ -829,7 +836,47 @@ namespace gctl
} }
template <typename MatValType> template <typename MatValType>
double matrix<MatValType>::norm(norm_type_e n_type) const void matrix<MatValType>::normalize(matrix_order_e odr)
{
if (odr == RowMajor)
{
for (size_t i = 0; i < row_length; i++)
{
MatValType sum = 0.0;
for (size_t j = 0; j < col_length; j++)
{
sum += val[i][j]*val[i][j];
}
sum = sqrt(sum);
for (size_t j = 0; j < col_length; j++)
{
val[i][j] /= sum;
}
}
}
else // ColMajor
{
for (size_t j = 0; j < col_length; j++)
{
MatValType sum = 0.0;
for (size_t i = 0; i < row_length; i++)
{
sum += val[i][j]*val[i][j];
}
sum = sqrt(sum);
for (size_t i = 0; i < row_length; i++)
{
val[i][j] /= sum;
}
}
}
return;
}
template <typename MatValType>
double matrix<MatValType>::module(norm_type_e n_type) const
{ {
MatValType nval = (MatValType) 0; MatValType nval = (MatValType) 0;
if (n_type == L0) if (n_type == L0)