g2o
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Attributes | List of all members
g2o::MatrixStructure Class Reference

representing the structure of a matrix in column compressed structure (only the upper triangular part of the matrix) More...

#include <matrix_structure.h>

Public Member Functions

 MatrixStructure ()
 
 ~MatrixStructure ()
 
void alloc (int n_, int nz)
 
void free ()
 
bool write (std::string_view filename) const
 
int nzMax () const
 max number of non-zeros blocks
 

Public Attributes

int n
 A is m-by-n. n must be >= 0.
 
int m
 A is m-by-n. m must be >= 0.
 
int * Ap
 column pointers for A, of size n+1
 
int * Aii
 row indices of A, of size nz = Ap [n]
 

Protected Attributes

int maxN
 size of the allocated memory
 
int maxNz
 size of the allocated memory
 

Detailed Description

representing the structure of a matrix in column compressed structure (only the upper triangular part of the matrix)

Definition at line 40 of file matrix_structure.h.

Constructor & Destructor Documentation

◆ MatrixStructure()

g2o::MatrixStructure::MatrixStructure ( )

Definition at line 45 of file matrix_structure.cpp.

46 : n(0), m(0), Ap(0), Aii(0), maxN(0), maxNz(0) {}
int maxNz
size of the allocated memory
int maxN
size of the allocated memory
int * Aii
row indices of A, of size nz = Ap [n]
int m
A is m-by-n. m must be >= 0.
int n
A is m-by-n. n must be >= 0.
int * Ap
column pointers for A, of size n+1

◆ ~MatrixStructure()

g2o::MatrixStructure::~MatrixStructure ( )

Definition at line 48 of file matrix_structure.cpp.

References free().

Member Function Documentation

◆ alloc()

void g2o::MatrixStructure::alloc ( int  n_,
int  nz 
)

allocate space for the Matrix Structure. You may call this on an already allocated struct, it will then reallocate the memory + additional space (double the required space).

Definition at line 50 of file matrix_structure.cpp.

50 {
51 if (n == 0) {
52 maxN = n = n_;
53 maxNz = nz;
54 Ap = new int[maxN + 1];
55 Aii = new int[maxNz];
56 } else {
57 n = n_;
58 if (maxNz < nz) {
59 maxNz = 2 * nz;
60 delete[] Aii;
61 Aii = new int[maxNz];
62 }
63 if (maxN < n) {
64 maxN = 2 * n;
65 delete[] Ap;
66 Ap = new int[maxN + 1];
67 }
68 }
69}

References Aii, Ap, maxN, maxNz, and n.

Referenced by g2o::SparseBlockMatrix< MatrixType >::fillBlockStructure().

◆ free()

void g2o::MatrixStructure::free ( )

Definition at line 71 of file matrix_structure.cpp.

71 {
72 n = 0;
73 m = 0;
74 maxN = 0;
75 maxNz = 0;
76 delete[] Aii;
77 Aii = 0;
78 delete[] Ap;
79 Ap = 0;
80}

References Aii, Ap, m, maxN, maxNz, and n.

Referenced by ~MatrixStructure().

◆ nzMax()

int g2o::MatrixStructure::nzMax ( ) const
inline

◆ write()

bool g2o::MatrixStructure::write ( std::string_view  filename) const

Write the matrix pattern to a file. File is also loadable by octave, e.g., then use spy(matrix)

Definition at line 82 of file matrix_structure.cpp.

82 {
83 const int& cols = n;
84 const int& rows = m;
85
86 const string_view name = [&filename]() {
87 const std::string::size_type lastDot = filename.find_last_of('.');
88 if (lastDot != std::string_view::npos) return filename.substr(0, lastDot);
89 return filename;
90 }();
91
92 vector<pair<int, int> > entries;
93 for (int i = 0; i < cols; ++i) {
94 const int& rbeg = Ap[i];
95 const int& rend = Ap[i + 1];
96 for (int j = rbeg; j < rend; ++j) {
97 entries.push_back(make_pair(Aii[j], i));
98 if (Aii[j] != i) entries.push_back(make_pair(i, Aii[j]));
99 }
100 }
101
102 sort(entries.begin(), entries.end(), ColSort());
103
104 const string output_filename(filename);
105 std::ofstream fout(output_filename);
106 fout << "# name: " << name << std::endl;
107 fout << "# type: sparse matrix" << std::endl;
108 fout << "# nnz: " << entries.size() << std::endl;
109 fout << "# rows: " << rows << std::endl;
110 fout << "# columns: " << cols << std::endl;
111 for (vector<pair<int, int> >::const_iterator it = entries.begin();
112 it != entries.end(); ++it) {
113 const pair<int, int>& entry = *it;
114 fout << entry.first << " " << entry.second << " 0"
115 << std::endl; // write a constant value of 0
116 }
117
118 return fout.good();
119}

References Aii, Ap, m, and n.

Member Data Documentation

◆ Aii

int* g2o::MatrixStructure::Aii

◆ Ap

int* g2o::MatrixStructure::Ap

◆ m

int g2o::MatrixStructure::m

A is m-by-n. m must be >= 0.

Definition at line 60 of file matrix_structure.h.

Referenced by g2o::SparseBlockMatrix< MatrixType >::fillBlockStructure(), free(), and write().

◆ maxN

int g2o::MatrixStructure::maxN
protected

size of the allocated memory

Definition at line 68 of file matrix_structure.h.

Referenced by alloc(), and free().

◆ maxNz

int g2o::MatrixStructure::maxNz
protected

size of the allocated memory

Definition at line 69 of file matrix_structure.h.

Referenced by alloc(), and free().

◆ n

int g2o::MatrixStructure::n

The documentation for this class was generated from the following files: