diff --git a/easy3d/core/matrix.h b/easy3d/core/matrix.h index 838ed567..7a8682df 100644 --- a/easy3d/core/matrix.h +++ b/easy3d/core/matrix.h @@ -32,6 +32,8 @@ #include // for std::min, std::max #include // for std::sqrt +#include + namespace easy3d { @@ -155,6 +157,9 @@ namespace easy3d { /// @note This function also allow to set the elements on the diagonal to have values other than 1. void load_identity(FT v = FT(1)); + /// Set all elements to a random value (in the range [0.0, 1.0]) + void load_random(); + /// Return the transposed matrix. Matrix transpose() const; @@ -284,13 +289,20 @@ namespace easy3d { template Matrix mult_transpose(const std::vector &, const std::vector &); // a * b^T - /// unit and diagonal matrix + /// Generate a matrix with all its elements randomly initialized in the range [0.0, 1.0]. template - Matrix identity(int, const FT &); // Generate an identity matrix. + Matrix random(int row, int col); + + /// Generate an unity/identity matrix (i.e., all elements on the diagonal have a value of 1). + /// @note This function also allow to set the elements on the diagonal to have values other than 1. template - Matrix diagonal(const std::vector &); // Generate a diagonal matrix by given its diagonal elements. + Matrix identity(int N, const FT v = FT(1)); + + /// diagonal matrix template - std::vector diagonal(const Matrix &); // Get the diagonal entries of matrix. + Matrix diagonal(const std::vector &); // Generate a diagonal matrix by given its diagonal elements. + template + std::vector diagonal(const Matrix &); // Get the diagonal entries of matrix. /// the trace of this matrix, i.e. the sum of the coefficients on the main diagonal. /// NOTE: the matrix can be any matrix, not necessarily square. @@ -388,16 +400,19 @@ namespace easy3d { template FT dot(const std::vector &, const std::vector &); // Inner product for vectors. + /// Generate a vector with all its elements randomly initialized in the range [0.0, 1.0]. + template + std::vector random(int size); + /// utilities template - FT norm(const std::vector &); // Euclidean norm. + FT norm(const std::vector &); // Euclidean norm. template - FT norm(const std::vector > &); // Euclidean norm. + FT norm(const std::vector > &); // Euclidean norm. template - void swap(std::vector &, std::vector &);// Swap two vectors. + void swap(std::vector &, std::vector &); // Swap two vectors. template - std::vector - linspace(FT, FT, int);// Generates a vector of n points linearly spaced between and including a and b. + std::vector linspace(FT, FT, int);// Generates a vector of n points linearly spaced between and including a and b. template FT sum(const std::vector &); // vector sum. template @@ -640,14 +655,11 @@ namespace easy3d { return prow_[row][col]; } - /** - * Clears the matrix - * This resets all values to 0 (zero) - */ + * Clears the matrix such that all values are set to 0 (zero) + */ template - inline - void Matrix::load_zero() { + inline void Matrix::load_zero() { for (int i = 0; i < nRow_; i++) { for (int j = 0; j < nColumn_; j++) { prow_[i][j] = FT(0); @@ -656,13 +668,12 @@ namespace easy3d { } /** - * Sets the matrix to identity - * This sets all coefficients of this matrix to be equal to - * nROW x nCOL identity matrix. - */ + * Sets the matrix to identity + * This sets all coefficients of this matrix to be equal to + * nROW x nCOL identity matrix. + */ template - inline - void Matrix::load_identity(FT v /* = FT(1.0)*/) { + inline void Matrix::load_identity(FT v /* = FT(1.0)*/) { for (int i = 0; i < nRow_; i++) { for (int j = 0; j < nColumn_; j++) { prow_[i][j] = (i == j) ? v : FT(0); @@ -670,6 +681,15 @@ namespace easy3d { } } + /// Set all elements to a random value (in the range [0.0, 1.0]) + template + inline void Matrix::load_random() { + for (int i = 0; i < nRow_; i++) { + for (int j = 0; j < nColumn_; j++) { + prow_[i][j] = random_float(); + } + } + } template inline int Matrix::rows() const { @@ -1365,9 +1385,20 @@ namespace easy3d { } + /// Generate a matrix with its elements in the range [0.0, 1.0]. + template + Matrix random(int row, int col) { + Matrix tmp(row, col); + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + tmp[i][j] = random_float(); + } + } + } + /** Generate an identity matrix. */ template - Matrix identity(int N, const FT &x) { + Matrix identity(int N, const FT& x /*= FT(1)*/) { Matrix tmp(N, N); for (int i = 0; i < N; ++i) tmp[i][i] = x; @@ -1850,13 +1881,22 @@ namespace easy3d { /// std::vector's mean. template - FT mean(const std::vector &v) { + inline FT mean(const std::vector &v) { return sum(v) / FT(v.size()); } + /// Generate a vector with all its elements randomly initialized in the range [0.0, 1.0]. + template + inline std::vector random(int size) { + std::vector V(size); + for (auto& v : V) + v = random_float(); + return V; + } + /// std::vector's norm in Euclidean space. template - FT norm(const std::vector &v) { + inline FT norm(const std::vector &v) { FT sum = 0; typename std::vector::const_iterator itr = v.begin();