OOCholmod
 All Classes Functions
dense_matrix.h
1 //
2 // dense_vector.h
3 // OOCholmod
4 //
5 // Created by Morten Nobel-Jørgensen / Asger Nyman Christiansen
6 // Copyright (c) 2013 DTU Compute. All rights reserved.
7 // License: LGPL 3.0
8 
9 #pragma once
10 
11 #include <iostream>
12 #include <cassert>
13 #include <cmath>
14 #include <string>
15 #include <cholmod.h>
16 
17 #ifdef WIN32
18 #ifndef NAN
19  static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
20  #define NAN (*(const float *) __nan)
21 #endif
22 #endif
23 
24 
25 namespace oocholmod {
26 
27  // forward declaration
28  class SparseMatrix;
29  class Factor;
30 
31  class DenseMatrix {
32  public:
33  // In debug the matrix will be initialized to NAN
34  // In release mode, NAN will leave the matrix uninitialized
35  DenseMatrix(unsigned int rows = 0, unsigned int cols = 1, double value = NAN);
36 
37  DenseMatrix(cholmod_dense *x);
38 
39  DenseMatrix(DenseMatrix&& move);
40 
41  DenseMatrix& operator=(DenseMatrix&& other);
42 
43  ~DenseMatrix();
44 
45 
46  double& operator()(unsigned int row, unsigned int col = 0);
47 
48  double operator()(unsigned int row, unsigned int col = 0) const;
49 
50  double *begin();
51  double *end();
52 
53  // OPERATORS
54 
55 
56  // Addition
57  friend DenseMatrix operator+(const DenseMatrix& LHS, const DenseMatrix& RHS);
58  friend DenseMatrix&& operator+(DenseMatrix&& LHS, const DenseMatrix& RHS);
59  friend DenseMatrix&& operator+(const DenseMatrix& LHS, DenseMatrix&& RHS);
60  friend DenseMatrix&& operator+(DenseMatrix&& LHS, DenseMatrix&& RHS);
61 
62  DenseMatrix& operator+=(const DenseMatrix& RHS);
63 
64  // Subtraction
65  friend DenseMatrix operator-(const DenseMatrix& LHS, const DenseMatrix& RHS);
66  friend DenseMatrix&& operator-(DenseMatrix&& LHS, const DenseMatrix& RHS);
67  friend DenseMatrix&& operator-(const DenseMatrix& LHS, DenseMatrix&& RHS);
68  friend DenseMatrix&& operator-(DenseMatrix&& LHS, DenseMatrix&& RHS);
69 
70  DenseMatrix& operator-=(const DenseMatrix& RHS);
71 
72  friend DenseMatrix operator-(const DenseMatrix& M);
73  friend DenseMatrix&& operator-(DenseMatrix&& M);
74 
75  // Multiplication
76  friend DenseMatrix operator*(const DenseMatrix& LHS, double RHS);
77  friend DenseMatrix&& operator*(DenseMatrix&& LHS, double RHS);
78  friend DenseMatrix operator*(double LHS, const DenseMatrix& RHS);
79  friend DenseMatrix&& operator*(double LHS, DenseMatrix&& RHS);
80 
81  DenseMatrix& operator*=(double RHS);
82 
83  friend DenseMatrix operator*(const DenseMatrix& LHS, const DenseMatrix& RHS);
84 
85  friend DenseMatrix operator*(const DenseMatrix& LHS, const SparseMatrix& RHS);
86  friend DenseMatrix operator*(const SparseMatrix& LHS, const DenseMatrix& RHS);
87 
90  double norm(int norm) const;
91 
92  // Returns the determinant. For square matrices ONLY.
93  double determinant() const;
94 
95  // Transpose
96  void transpose();
97  friend DenseMatrix transposed(const DenseMatrix& M);
98  friend DenseMatrix&& transposed(DenseMatrix&& M);
99 
100  // Inverse
101  void inverse();
102  friend DenseMatrix inversed(const DenseMatrix& M);
103  friend DenseMatrix&& inversed(DenseMatrix&& M);
104 
105  // Solve
106  friend DenseMatrix solve(const DenseMatrix& A, const DenseMatrix& b);
107  friend DenseMatrix solve(DenseMatrix&& A, const DenseMatrix& b);
108  friend DenseMatrix&& solve(const DenseMatrix& A, DenseMatrix&& b);
109  friend DenseMatrix&& solve(DenseMatrix&& A, DenseMatrix&& b);
110 
111  friend DenseMatrix solve(const SparseMatrix& A, const DenseMatrix& b);
112  friend DenseMatrix solve(const Factor& F, const DenseMatrix& b);
113 
114  friend class SparseMatrix;
115 
116  // Print
117  friend std::ostream& operator<<(std::ostream& os, const DenseMatrix& A);
118 
119  double *getData();
120  const double *getData() const;
121 
122  unsigned int getRows() const;
123 
124  unsigned int getColumns() const;
125 
126  DenseMatrix copy() const;
127  void zero();
128 
129  // C = alpha * A + C
130  void multiplyAddTo(double alpha, DenseMatrix& C);
131 
132  // Computes perform one of the matrix-matrix operations C := alpha*op( A )*op( B ) + beta*C,
133  // this is matrix A
134  // op(N) is transposed if transposeN is true
135  void multiply(bool transposeA, bool transposeB, double alpha, double beta, const DenseMatrix& B, DenseMatrix& C);
136  DenseMatrix multiply(bool transposeA, bool transposeB, double alpha, const DenseMatrix& B);
137 
138  // assumes that matrix is 1xN or Mx1
139  // computes the L^2 norm of the vector
140  double length() const;
141 
142  // elementwise division
143  void elemDivide(const DenseMatrix& b);
144  void elemDivide(const DenseMatrix& b, DenseMatrix& dest) const;
145 
146  // elementwise multiplication
147  void elemMultiply(const DenseMatrix& b);
148  void elemMultiply(const DenseMatrix& b , DenseMatrix& dest) const;
149 
150  SparseMatrix toSparse() const;
151 
152  double dot(const DenseMatrix& b) const;
153  void fill(double value);
154  void set(float *data);
155  void set(double *data);
156  void get(double *outData) const;
157  void get(float *outData) const;
158 
159  void swap(DenseMatrix& other);
160 
161  bool operator==(const DenseMatrix& RHS) const;
162  bool operator!=(const DenseMatrix& RHS) const;
163  private:
164  DenseMatrix(const DenseMatrix& that); // prevent copy constructor (no implementation)
165  DenseMatrix operator=(const DenseMatrix& other); // prevent copy assignment operator (no implementation)
166  cholmod_dense *dense;
167  unsigned int nrow;
168  unsigned int ncol;
169  };
170 
171  // ---------- inline functions -----------
172 
173  inline double& DenseMatrix::operator()(unsigned int row, unsigned int col)
174  {
175 #ifdef DEBUG
176  assert(dense);
177  assert(row < nrow && col < ncol);
178 #endif
179  return ((double*)dense->x)[col*nrow + row];
180  }
181 
182  inline double DenseMatrix::operator()(unsigned int row, unsigned int col) const
183  {
184 #ifdef DEBUG
185  assert(dense);
186  assert(row < nrow && col < ncol);
187 #endif
188  return ((double*)dense->x)[col*nrow + row];
189  }
190 
191  inline double *DenseMatrix::getData(){ return (double *)(dense->x); };
192  inline const double *DenseMatrix::getData() const { return (double *)(dense->x); };
193 
194  inline unsigned int DenseMatrix::getRows() const{ return nrow; }
195 
196  inline unsigned int DenseMatrix::getColumns() const{ return ncol; }
197 
198 }
199 
200 
201 
202 
Definition: dense_matrix.h:31
double norm(int norm) const
Definition: dense_matrix.cpp:120
Definition: factor.h:22
Definition: sparse_matrix.h:62