-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.hpp
182 lines (152 loc) · 4.79 KB
/
matrix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
isl-cpp: C++ bindings to the ISL (Integer Set Library)
Copyright (C) 2014 Jakob Leben <jakob.leben@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef ISL_CPP_MATRIX_INCLUDED
#define ISL_CPP_MATRIX_INCLUDED
#include "object.hpp"
#include "context.hpp"
#include "value.hpp"
#include "printer.hpp"
#include <isl/mat.h>
namespace isl {
template<>
struct object_behavior<isl_mat>
{
static isl_mat * copy( isl_mat * obj )
{
return isl_mat_copy(obj);
}
static void destroy( isl_mat *obj )
{
isl_mat_free(obj);
}
static isl_ctx * get_context( isl_mat * obj )
{
return isl_mat_get_ctx(obj);
}
};
class matrix : public object<isl_mat>
{
public:
class element
{
friend class matrix;
isl_mat *matrix;
int row;
int column;
public:
element & operator=( const value & v )
{
isl_mat_set_element_val(matrix, row, column, v.copy());
return *this;
}
element & operator=( int i )
{
isl_mat_set_element_si(matrix, row, column, i);
return *this;
}
isl::value value() const
{
return isl_mat_get_element_val(matrix, row, column);
}
private:
element( isl_mat *matrix, int row, int column ):
matrix(matrix), row(row), column(column)
{}
};
matrix(isl_mat *ptr): object(ptr) {}
// Does not initialize!
matrix( const context & ctx, unsigned rows, unsigned columns ):
object(ctx, isl_mat_alloc(ctx.get(), rows, columns))
{}
matrix( const context & ctx, unsigned rows, unsigned columns, int val ):
object(ctx, isl_mat_alloc(ctx.get(), rows, columns))
{
for(int row = 0; row < row_count(); ++row)
{
for (int col = 0; col < column_count(); ++col)
{
isl_mat_set_element_si(get(), row, col, val);
}
}
}
context get_context() const { return isl_mat_get_ctx(get()); }
int row_count() const { return isl_mat_rows(get()); }
int column_count() const { return isl_mat_cols(get()); }
element operator() (int row, int column)
{
return element(get(), row, column);
}
const element operator() (int row, int column) const
{
return element(get(), row, column);
}
matrix right_kernel() const
{
return isl_mat_right_kernel(copy());
}
matrix nullspace() const
{
return right_kernel();
}
void drop_column(int col)
{
int n_rows = row_count();
int n_cols = column_count() - 1;
auto result = isl_mat_alloc(ctx().get(),
n_rows, n_cols);
for (int r = 0; r < n_rows; ++r)
{
for (int c = 0; c < col; ++c)
{
auto val = isl_mat_get_element_val(get(), r, c);
result = isl_mat_set_element_val(result, r, c, val);
}
for (int c = col; c < n_cols; ++c)
{
auto val = isl_mat_get_element_val(get(), r, c+1);
result = isl_mat_set_element_val(result, r, c, val);
}
}
isl_mat_free(m_object);
m_object = result;
}
static matrix concatenate_vertically(const matrix & a, const matrix & b)
{
if (a.column_count() != b.column_count())
throw error("Matrices do not have equal number of columns");
auto result = isl::matrix(a.get_context(),
a.row_count() + b.row_count(),
a.column_count());
for (int row = 0; row < a.row_count(); ++row)
{
for (int col = 0; col < a.column_count(); ++col)
{
result(row,col) = a(row,col).value();
}
}
for (int row = 0; row < b.row_count(); ++row)
{
for (int col = 0; col < b.column_count(); ++col)
{
result(a.row_count() + row, col) = b(row,col).value();
}
}
return result;
}
};
void print( const matrix & m, int field_width = 4 );
}
#endif // ISL_CPP_MATRIX_INCLUDED