b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2matrix_structured.H
1//------------------------------------------------------------------------
2// b2matrix_structured.H --
3//
4// A framework for generic linear algebraic (matrix) computations.
5//
6// written by Mathias Doreille
7//
8// Copyright (c) 2004-2012 SMR Engineering & Development SA
9// 2502 Bienne, Switzerland
10//
11// All Rights Reserved. Proprietary source code. The contents of
12// this file may not be disclosed to third parties, copied or
13// duplicated in any form, in whole or in part, without the prior
14// written permission of SMR.
15//------------------------------------------------------------------------
16
17#ifndef __B2MATRIX_STRUCTURED_H__
18#define __B2MATRIX_STRUCTURED_H__
19
20#include "b2linear_algebra_def.H"
21
22namespace b2000::b2linalg {
23
24template <typename MTYPE>
25struct Mstructured_constref {
26 using base = Mstructured_constref;
27 using const_base = Mstructured_constref;
28 using copy = typename MTYPE::copy;
29};
30
31template <typename T, typename MTYPE>
32class Matrix<T, Mstructured_constref<MTYPE>> {
33public:
34 Matrix() : m(Matrix<T, MTYPE>::null), s(0), s2(0), index(0), index2(0) {}
35
36 Matrix(size_t size_, const size_t* index_, const Matrix<T, MTYPE>& m_)
37 : m(m_), s(size_), s2(size_), index(index_), index2(0) {}
38
39 Matrix(
40 size_t size_1, size_t size_2, const size_t* index1_, const size_t* index2_,
41 const Matrix<T, MTYPE>& m_)
42 : m(m_), s(size_1), s2(size_2), index(index1_), index2(index2_) {}
43
44 Matrix(const Matrix& m_) : m(m_.m), s(m_.s), s2(m_.s2), index(m_.index), index2(m_.index2) {}
45
46 std::pair<size_t, size_t> size() const { return std::pair<size_t, size_t>(s, s2); }
47
48 size_t size1() const { return s; }
49
50 size_t size2() const { return s2; }
51
52 size_t esize() const { return s * (s + 1) / 2; }
53
54 T operator()(size_t i, size_t j) const {
55 const size_t* ii = std::find(index, index + m.size1(), i);
56 if (ii == index + m.size1()) { return 0; }
57 const size_t* jj;
58 if (index2) {
59 jj = std::find(index2, index2 + m.size1(), j);
60 if (jj == index2 + m.size1()) { return 0; }
61 } else {
62 jj = std::find(index, index + m.size1(), j);
63 if (jj == index + m.size1()) { return 0; }
64 }
65 return m(ii - index, jj - index);
66 }
67
68private:
69 const Matrix<T, MTYPE> m;
70 size_t s;
71 size_t s2;
72 const size_t* index;
73 const size_t* index2;
74 MVFRIEND;
75};
76
77template <typename T, typename MTYPE>
78Matrix<T, Mstructured_constref<typename MTYPE::const_base>> StructuredMatrix(
79 size_t size, const Index& index, const Matrix<T, MTYPE>& m) {
80 if (index.size() != m.size1()) {
81 Exception() << "The index size differ from the matrix size." << THROW;
82 }
83 return Matrix<T, Mstructured_constref<typename MTYPE::const_base>>(size, &index[0], m);
84}
85
86template <typename T, typename MTYPE>
87Matrix<T, Mstructured_constref<typename MTYPE::const_base>> StructuredMatrix(
88 size_t size1, size_t size2, const Index& index1, const Index& index2,
89 const Matrix<T, MTYPE>& m) {
90 if (index1.size() != m.size1() && index2.size() != m.size2()) {
91 Exception() << "The index size differ from the matrix size." << THROW;
92 }
93 return Matrix<T, Mstructured_constref<typename MTYPE::const_base>>(
94 size1, size2, &index1[0], &index2[0], m);
95}
96
97} // namespace b2000::b2linalg
98
99#endif
#define THROW
Definition b2exception.H:198