Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
lu_decompose.cpp File Reference

LU decomposition of a square matrix More...

#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>
Include dependency graph for lu_decompose.cpp:

Functions

int lu_decomposition (const std::vector< std::vector< double >> &A, std::vector< std::vector< double >> *L, std::vector< std::vector< double >> *U)
 
template<typename T >
std::ostreamoperator<< (std::ostream &out, std::vector< std::vector< T >> const &v)
 
int main (int argc, char **argv)
 

Detailed Description

LU decomposition of a square matrix

Author
Krishna Vedala

Function Documentation

◆ lu_decomposition()

int lu_decomposition ( const std::vector< std::vector< double >> &  A,
std::vector< std::vector< double >> *  L,
std::vector< std::vector< double >> *  U 
)

Perform LU decomposition on matrix

Parameters
[in]Amatrix to decompose
[out]Loutput L matrix
[out]Uoutput U matrix
Returns
0 if no errors
negative if error occurred
24  {
25  int row, col, j;
26  int mat_size = A.size();
27 
28  if (mat_size != A[0].size()) {
29  // check matrix is a square matrix
30  std::cerr << "Not a square matrix!\n";
31  return -1;
32  }
33 
34  // regularize each row
35  for (row = 0; row < mat_size; row++) {
36  // Upper triangular matrix
37 #ifdef _OPENMP
38 #pragma omp for
39 #endif
40  for (col = row; col < mat_size; col++) {
41  // Summation of L[i,j] * U[j,k]
42  double lu_sum = 0.;
43  for (j = 0; j < row; j++) lu_sum += L[0][row][j] * U[0][j][col];
44 
45  // Evaluate U[i,k]
46  U[0][row][col] = A[row][col] - lu_sum;
47  }
48 
49  // Lower triangular matrix
50 #ifdef _OPENMP
51 #pragma omp for
52 #endif
53  for (col = row; col < mat_size; col++) {
54  if (row == col) {
55  L[0][row][col] = 1.;
56  continue;
57  }
58 
59  // Summation of L[i,j] * U[j,k]
60  double lu_sum = 0.;
61  for (j = 0; j < row; j++) lu_sum += L[0][col][j] * U[0][j][row];
62 
63  // Evaluate U[i,k]
64  L[0][col][row] = (A[col][row] - lu_sum) / U[0][row][row];
65  }
66  }
67 
68  return 0;
69 }

◆ main()

int main ( int  argc,
char **  argv 
)

Main function

91  {
92  int mat_size = 3; // default matrix size
93  const int range = 50;
94  const int range2 = range >> 1;
95 
96  if (argc == 2)
97  mat_size = atoi(argv[1]);
98 
99  std::srand(std::time(NULL)); // random number initializer
100 
101  /* Create a square matrix with random values */
102  std::vector<std::vector<double>> A(mat_size);
103  std::vector<std::vector<double>> L(mat_size); // output
104  std::vector<std::vector<double>> U(mat_size); // output
105  for (int i = 0; i < mat_size; i++) {
106  // calloc so that all valeus are '0' by default
107  A[i] = std::vector<double>(mat_size);
108  L[i] = std::vector<double>(mat_size);
109  U[i] = std::vector<double>(mat_size);
110  for (int j = 0; j < mat_size; j++)
111  /* create random values in the limits [-range2, range-1] */
112  A[i][j] = static_cast<double>(std::rand() % range - range2);
113  }
114 
115  std::clock_t start_t = std::clock();
116  lu_decomposition(A, &L, &U);
117  std::clock_t end_t = std::clock();
118  std::cout << "Time taken: "
119  << static_cast<double>(end_t - start_t) / CLOCKS_PER_SEC << "\n";
120 
121  std::cout << "A = \n" << A << "\n";
122  std::cout << "L = \n" << L << "\n";
123  std::cout << "U = \n" << U << "\n";
124 
125  return 0;
126 }
Here is the call graph for this function:

◆ operator<<()

template<typename T >
std::ostream& operator<< ( std::ostream out,
std::vector< std::vector< T >> const &  v 
)

operator to print a matrix

76  {
77  const int width = 10;
78  const char separator = ' ';
79 
80  for (size_t row = 0; row < v.size(); row++) {
81  for (size_t col = 0; col < v[row].size(); col++)
82  out << std::left << std::setw(width) << std::setfill(separator)
83  << v[row][col];
84  out << std::endl;
85  }
86 
87  return out;
88 }
Here is the call graph for this function:
std::srand
T srand(T... args)
std::clock_t
std::vector
STL class.
std::vector::size
T size(T... args)
std::setfill
T setfill(T... args)
std::clock
T clock(T... args)
std::cerr
std::atoi
T atoi(T... args)
std::rand
T rand(T... args)
std::endl
T endl(T... args)
std::left
T left(T... args)
std::time
T time(T... args)
std::setw
T setw(T... args)
lu_decomposition
int lu_decomposition(const std::vector< std::vector< double >> &A, std::vector< std::vector< double >> *L, std::vector< std::vector< double >> *U)
Definition: lu_decompose.cpp:22