lu_decompose.cpp File Reference

LU decomposition of a square matrix More...

`#include <cassert>`
`#include <ctime>`
`#include <iomanip>`
`#include <iostream>`
`#include "./lu_decomposition.h"`
Include dependency graph for lu_decompose.cpp:

## Functions

template<typename T >
std::ostream & | operator<< (std::ostream &out, matrix< T > const &v)
void | test1 ()
void | test2 ()
int | main (int argc, char **argv)

## Detailed Description

LU decomposition of a square matrix

Author
  Krishna Vedala

## Function Documentation

## main()

int main | ( | int | argc,
|  | char ** | argv
| ) |  |

Main function

{
std::srand(std::time(NULL)); // random number initializer
test1();
test2();
return 0;
}
test2
void test2()
Definition: lu_decompose.cpp:66
test1
void test1()
Definition: lu_decompose.cpp:36
std::srand
T srand(T... args)
std::time
T time(T... args)
Here is the call graph for this function:

## operator<<()

template<typename T >

std::ostream& operator<< | ( | std::ostream & | out,
|  | matrix< T > const & | v
| ) |  |

operator to print a matrix

{
const int width = 10;
const char separator = ' ';
for (size_t row = 0; row < v.size(); row++) {
for (size_t col = 0; col < v[row].size(); col++)
out << std::left << std::setw(width) << std::setfill(separator)
<< v[row][col];
out << std::endl;
}
return out;
}
std::endl
T endl(T... args)
std::left
T left(T... args)
std::setfill
T setfill(T... args)
std::setw
T setw(T... args)
std::vector::size
T size(T... args)
Here is the call graph for this function:

## test1()

void test1 | ( |  | ) |

Test LU decomposition

Todo:
  better ways to self-check a matrix output?

{
int mat_size = 3; // default matrix size
const int range = 50;
const int range2 = range >> 1;
/* Create a square matrix with random values */
matrix<double> A(mat_size, std::valarray<double>(mat_size));
matrix<double> L(mat_size, std::valarray<double>(mat_size)); // output
matrix<double> U(mat_size, std::valarray<double>(mat_size)); // output
for (int i = 0; i < mat_size; i++) {
// calloc so that all valeus are '0' by default
for (int j = 0; j < mat_size; j++)
/* create random values in the limits [-range2, range-1] */
A[i][j] = static_cast<double>(std::rand() % range - range2);
}
std::clock_t start_t = std::clock();
lu_decomposition(A, &L, &U);
std::clock_t end_t = std::clock();
std::cout << "Time taken: "
<< static_cast<double>(end_t - start_t) / CLOCKS_PER_SEC << "\n";
std::cout << "A = \n" << A << "\n";
std::cout << "L = \n" << L << "\n";
std::cout << "U = \n" << U << "\n";
}
std::cout
std::clock
T clock(T... args)
std::clock_t
lu_decomposition
int lu_decomposition(const matrix< T > &A, matrix< double > *L, matrix< double > *U)
Definition: lu_decomposition.h:29
mat_size
ll mat_size
Definition: matrix_exponentiation.cpp:45
std::rand
T rand(T... args)
std::valarray< double >
std::vector
Here is the call graph for this function:

## test2()

void test2 | ( |  | ) |

Test determinant computation using LU decomposition

{
std::cout << "Determinant test 1...";
matrix<int> A1({{1, 2, 3}, {4, 9, 6}, {7, 8, 9}});
assert(determinant_lu(A1) == -48);
std::cout << "passed\n";
std::cout << "Determinant test 2...";
matrix<int> A2({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
assert(determinant_lu(A2) == 0);
std::cout << "passed\n";
std::cout << "Determinant test 3...";
matrix<float> A3({{1.2, 2.3, 3.4}, {4.5, 5.6, 6.7}, {7.8, 8.9, 9.0}});
assert(determinant_lu(A3) == 3.63);
std::cout << "passed\n";
}
determinant_lu
double determinant_lu(const matrix< T > &A)
Definition: lu_decomposition.h:90
Here is the call graph for this function: