diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 7f3d7b2..3db6544 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -33,4 +33,4 @@ add_example(array_ex OFF) add_example(gmt_ex OFF) add_example(gnuplot_ex OFF) add_example(stl_io_ex OFF) -add_example(ply_io_ex ON) \ No newline at end of file +add_example(ply_io_ex OFF) \ No newline at end of file diff --git a/lib/core/matrix.h b/lib/core/matrix.h index 318ea17..8a11226 100644 --- a/lib/core/matrix.h +++ b/lib/core/matrix.h @@ -36,14 +36,14 @@ namespace gctl template class matrix; /* - * Here we define some commonly used array types. + * Here we define some commonly used matrix types. */ - typedef matrix _2i_matrix; ///< 2-D integer array - typedef matrix _2f_matrix; ///< 2-D single-precision floating-point array - typedef matrix _2d_matrix; ///< 2-D double-precision floating-point array - typedef matrix _2s_matrix; ///< 2-D string array - typedef matrix > _2cf_matrix; ///< 1-D complex float array - typedef matrix > _2cd_matrix; ///< 1-D complex double array + typedef matrix _2i_matrix; ///< 2-D integer matrix + typedef matrix _2f_matrix; ///< 2-D single-precision floating-point matrix + typedef matrix _2d_matrix; ///< 2-D double-precision floating-point matrix + typedef matrix _2s_matrix; ///< 2-D string matrix + typedef matrix > _2cf_matrix; ///< 1-D complex float matrix + typedef matrix > _2cd_matrix; ///< 1-D complex double matrix /** * @brief 二维数组模版 @@ -259,13 +259,20 @@ namespace gctl * @param odr 计算行或列的平均值 */ void mean(array &m, matrix_order_e odr = RowMajor); + + /** + * @brief 按行或列归一化矩阵元素 + * + * @param odr 计算行或列的平均值 + */ + void normalize(matrix_order_e odr = RowMajor); /** * @brief 计算矩阵的模长 * * @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. @@ -829,7 +836,47 @@ namespace gctl } template - double matrix::norm(norm_type_e n_type) const + void matrix::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 + double matrix::module(norm_type_e n_type) const { MatValType nval = (MatValType) 0; if (n_type == L0)