diff --git a/include/matrix.hpp b/include/matrix.hpp index a44b20b..cfda46f 100644 --- a/include/matrix.hpp +++ b/include/matrix.hpp @@ -1,18 +1,59 @@ +/** @file matrix.hpp + * @brief Function prototypes for creating tridiagonal matrices. + * + * + * + * @author Cory Alexander Balaton (coryab) + * @author Janita Ovidie Sandtrøen Willumsen (janitaws) + * @bug No known bugs + */ #ifndef __MATRIX__ #define __MATRIX__ #include - +/** @brief Create a tridiagonal matrix. + * + * @param a Vector for the lower diagonal of size N-1 + * @param d Vector for the main diagonal of size N + * @param e Vector for the upper diagonal of size N-1 + * + * @return arma::matrix + * */ arma::mat create_tridiagonal( const arma::vec& a, const arma::vec& d, const arma::vec& e); +/** @brief Create a tridiagonal matrix. + * + * @param n The dimensions of the tridiagonal matrix + * @param a The signature for the lower diagonal + * @param d The signature for the main diagonal + * @param e The signature for the upper diagonal + * + * @return arma::matrix + * */ arma::mat create_tridiagonal(int n, double a, double d, double e); +/** @brief Create a symmetric tridiagonal matrix. + * + * @param n The dimensions of the tridiagonal matrix + * @param a The signature for the off diagonals + * @param d The signature for the main diagonal + * + * @return arma::matrix + * */ arma::mat create_symmetric_tridiagonal(int n, double a, double d); +/** @brief Find the off-diagonal element with the largest absolute value. + * + * @param A Symmetric matrix + * @param k Variable to store the row of the return value + * @param l Variable to store the column of the return value + * + * @return Double + * */ double max_offdiag_symmetric(arma::mat& A, int& k, int& l); #endif diff --git a/include/utils.hpp b/include/utils.hpp index 4dbbb1a..eea12de 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,10 +1,60 @@ +/** @file utils.hpp + * @brief Function prototypes and macros that are useful. + * + * These utility function are mainly for convenience and aren't directly + * related to the project. + * + * @author Cory Alexander Balaton (coryab) + * @author Janita Ovidie Sandtrøen Willumsen (janitaws) + * @bug No known bugs + */ #ifndef __UTILS__ #define __UTILS__ +#include +#include +#include + +/** @def DEBUG(msg) + * @brief Writes a debug message + * + * This function writes a debug message that includes the filename, + * line number, and a custom message. The function is wrapped in an ifdef + * that checks if DBG is defined, so one can choose to display the debug + * messages by adding the -DDBG flag when compiling. + * */ #ifdef DBG - #define DEBUG(msg) std::cout << __FILE__ << " " << __LINE__ << ": " << msg << std::endl + #define DEBUG(msg) std::cout << __FILE__ << " " << __LINE__ << ": " \ + << msg << std::endl #else #define DEBUG(msg) #endif +/** Code stolen from https://github.com/anderkve/FYS3150 + * Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp + * Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp + * */ + +/** @brief Turns a double into a string written in scientific format. + * + * @param d The number to stringify + * @param width The reserved width of the string + * @param prec The precision of the stringified number + * + * @return String + * */ +std::string scientific_format(double d, int width=20, int prec=10); + +/** @brief Turns a vector of doubles into a string written in scientific format. + * + * @param v The vector to stringify + * @param width The reserved width of the string + * @param prec The precision of the stringified number + * + * @return String + * */ +std::string scientific_format(const std::vector& v, + int width=20, + int prec=10); + #endif diff --git a/src/matrix.cpp b/src/matrix.cpp index 53734d8..66b8073 100644 --- a/src/matrix.cpp +++ b/src/matrix.cpp @@ -1,3 +1,12 @@ +/** @file matrix.cpp + * @brief Function prototypes for creating tridiagonal matrices. + * + * + * + * @author Cory Alexander Balaton (coryab) + * @author Janita Ovidie Sandtrøen Willumsen (janitaws) + * @bug No known bugs + */ #include "matrix.hpp" arma::mat create_tridiagonal( @@ -8,17 +17,17 @@ arma::mat create_tridiagonal( int n = d.n_elem; arma::mat A = arma::mat(n, n).fill(0.); - A(0,0) = d(0); - A(0,1) = e(0); + A(0, 0) = d(0); + A(0, 1) = e(0); for (int i = 1; i < n-1; i++) { A(i, i-1) = a(i-1); A(i, i) = d(i); A(i, i+1) = e(i); } - + A(n-1, n-2) = a(n-2); - A(n-1,n-1) = d(n-1); + A(n-1, n-1) = d(n-1); return A; } @@ -42,6 +51,7 @@ double max_offdiag_symmetric(arma::mat& A, int& k, int& l) int m = A.n_rows; int n = A.n_cols; double item, res = 0.; + for (int i=0; i < m; i++) { for (int j=i+1; j < n; j++) { if ((item = std::abs(A(i,j))) > res) { @@ -51,5 +61,6 @@ double max_offdiag_symmetric(arma::mat& A, int& k, int& l) } } } + return res; } diff --git a/src/utils.cpp b/src/utils.cpp index e69de29..dab9d65 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -0,0 +1,25 @@ +/** @file utils.cpp + * @brief Implementation of the utils + * + * @author Cory Alexander Balaton (coryab) + * @author Janita Ovidie Sandtrøen Willumsen (janitaws) + * @bug No known bugs + */ +#include "utils.hpp" + +std::string scientific_format(double d, int width, int prec) +{ + std::stringstream ss; + ss << std::setw(width) << std::setprecision(prec) << std::scientific << d; + return ss.str(); +} + + +std::string scientific_format(const std::vector& v, int width, int prec) +{ + std::stringstream ss; + for(double elem : v) { + ss << scientific_format(elem, width, prec); + } + return ss.str(); +}