ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
small_matrix.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_DETAIL_SPAI_SMALL_MATRIX_HPP
2 #define VIENNACL_LINALG_DETAIL_SPAI_SMALL_MATRIX_HPP
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
27 #include <utility>
28 #include <iostream>
29 #include <fstream>
30 #include <string>
31 #include <algorithm>
32 #include <vector>
33 #include <math.h>
34 #include <map>
35 #include "boost/numeric/ublas/vector.hpp"
36 #include "boost/numeric/ublas/matrix.hpp"
37 #include "boost/numeric/ublas/matrix_proxy.hpp"
38 #include "boost/numeric/ublas/vector_proxy.hpp"
39 #include "boost/numeric/ublas/storage.hpp"
40 #include "boost/numeric/ublas/io.hpp"
41 #include "boost/numeric/ublas/lu.hpp"
42 #include "boost/numeric/ublas/triangular.hpp"
43 #include "boost/numeric/ublas/matrix_expression.hpp"
44 #include "boost/numeric/ublas/detail/matrix_assign.hpp"
45 
46 #include "viennacl/forwards.h"
47 
48 namespace viennacl
49 {
50 namespace linalg
51 {
52 namespace detail
53 {
54 namespace spai
55 {
56 
57 //
58 // Constructs an orthonormal sparse matrix M (with M^T M = Id). Is composed of elementary 2x2 rotation matrices with suitable renumbering.
59 //
60 template<typename MatrixT>
61 void make_rotation_matrix(MatrixT & mat,
62  vcl_size_t new_size,
63  vcl_size_t off_diagonal_distance = 4)
64 {
65  mat.resize(new_size, new_size, false);
66  mat.clear();
67 
68  double val = 1.0 / std::sqrt(2.0);
69 
70  for (vcl_size_t i=0; i<new_size; ++i)
71  mat(i,i) = val;
72 
73  for (vcl_size_t i=off_diagonal_distance; i<new_size; ++i)
74  {
75  mat(i-off_diagonal_distance, i) = val;
76  mat(i, i-off_diagonal_distance) = -val;
77  }
78 
79 }
80 
81 
82 //calcualtes matrix determinant
83 template<typename MatrixT>
84 double determinant(boost::numeric::ublas::matrix_expression<MatrixT> const & mat_r)
85 {
86  double det = 1.0;
87 
88  MatrixT mLu(mat_r());
89  boost::numeric::ublas::permutation_matrix<vcl_size_t> pivots(mat_r().size1());
90 
91  int is_singular = static_cast<int>(lu_factorize(mLu, pivots));
92 
93  if (!is_singular)
94  {
95  for (vcl_size_t i=0; i < pivots.size(); ++i)
96  {
97  if (pivots(i) != i)
98  det *= -1.0;
99 
100  det *= mLu(i,i);
101  }
102  }
103  else
104  det = 0.0;
105 
106  return det;
107 }
108 
109 }
110 }
111 }
112 }
113 #endif
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:163
This file provides the forward declarations for the main types used within ViennaCL.
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
void make_rotation_matrix(MatrixT &mat, vcl_size_t new_size, vcl_size_t off_diagonal_distance=4)
Definition: blas3.hpp:36
std::size_t vcl_size_t
Definition: forwards.h:75
double determinant(boost::numeric::ublas::matrix_expression< MatrixT > const &mat_r)
void lu_factorize(matrix< NumericT, viennacl::row_major > &A)
LU factorization of a row-major dense matrix.
Definition: lu.hpp:42