summaryrefslogtreecommitdiff
path: root/mtxmatrix.h
blob: e2f8c1078dcbf5a2f2c92f86f001ef3bd2ea4479 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* $Id$ */

/*
 * Copyright (C) 2006 Richard Braun
 * 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef _MTXMATRIX_H
#define _MTXMATRIX_H

#include <string>
#include <iostream>

#include "mtxvalue.h"
#include "mtxrational.h"

/**
 * A matrix.
 * 
 * This class allows to create matrices which consist of an array of
 * MtxRational objects, the number of rows, the number of columns, and
 * the length of the array which is rows * columns.
 */
class MtxMatrix: public MtxValue
{
  private:
    /**
     * Array of rationals.
     */
    MtxRational *rationals;

    /**
     * The length of the array of rationals.
     * 
     * It is maintained internally to avoid computing rows * columns.
     * 
     * @see getLength()
     */
    size_t length;

    /**
     * Number of rows.
     * 
     * @see getRows()
     */
    size_t rows;

    /**
     * Number of columns.
     * 
     * @see getColumns()
     */
    size_t columns;

    /**
     * Throw an MtxException exception if the rows of the current matrix and
     * the rows of the given matrix differ.
     * 
     * This check if performed mainly before augmenting matrices.
     * 
     * @see checkDimensions()
     */
    void checkRows(const MtxMatrix &matrix) const;

    /**
     * Check that both dimensions of the current matrix and the given matrix
     * match.
     * 
     * A MtxException exception is thrown if one of the dimensions differ.
     * Mainly used for addition like operations.
     * 
     * @see checkRows()
     */
    void checkDimensions(const MtxMatrix &matrix) const;

    /**
     * Check compatibility of the current matrix with the given matrix.
     * 
     * Two matrices are compatible if the number of columns of the first
     * is equal to the number of rows of the second. If matrices are not
     * compatible, an MtxException exception is raised. Mainly used for
     * matrix multiplication.
     */
    void checkCompatibility(const MtxMatrix &matrix) const;

    /**
     * Check that the current matrix is a square matrix.
     * 
     * Throw an MtxException exception if the number of rows differs from
     * the number of columns. Mainly used for LU decomposition.
     */
    void checkSquare() const;

    /**
     * Add two rows.
     * 
     * @param dst_row the index of the row which is updated
     * @param src_row the index of the row which is added to dst_row
     * @param rational a coefficient which is applied to each element of
     *                 src_row before addition
     */
    void addRows(size_t dst_row, size_t src_row, const MtxRational &rational);

    /**
     * Subtract two rows.
     * 
     * @param dst_row the index of the row which is updated
     * @param src_row the index of the row which is subtracted from dst_row
     * @param rational a coefficient which is applied to each element of
     *                 src_row before subtraction
     */
    void subtractRows(size_t dst_row, size_t src_row,
                      const MtxRational &rational);

    /**
     * Multiply a row by a rational.
     * 
     * @param row the index of the row which is updated
     * @param rational a coefficient which is applied to each element of
     *                 row
     */
    void multiplyRow(size_t row, const MtxRational &rational);

    /**
     * Divide a row by a rational.
     * 
     * @param row the index of the row which is updated
     * @param rational a coefficient which inverse is applied to each element
     *                 of row
     */
    void divideRow(size_t row, const MtxRational &rational);

    /**
     * Switch two rows.
     * 
     * @param r1 the index of the first row
     * @param r2 the index of the second row
     */
    void switchRows(size_t r1, size_t r2);

    /**
     * Inverse the current matrix if this matrix is a unit lower triangular
     * matrix.
     * 
     * Used for inversion of matrices.
     */
    MtxMatrix l_inverted() const;

    /**
     * Inverse the current matrix if this matrix is an upper triangular matrix.
     * 
     * Used for inversion of matrices.
     */
    MtxMatrix u_inverted() const;

  public:
    /**
     * Default constructor.
     */
    MtxMatrix();

    /**
     * Create a matrix from an array of rationals, the number of rows and the
     * number of columns. The caller must ensure coherent values are passed.
     */
    MtxMatrix(MtxRational *rationals, size_t rows, size_t columns);

    MtxMatrix(const MtxMatrix &matrix);
    virtual ~MtxMatrix();

    MtxMatrix & operator=(const MtxMatrix &matrix);

    /*
     * Some operators are available for easiness.
     */

    /**
     * Return the address of the line at requested index.
     * 
     * NULL is returned if index is invalid. Usage example :
     * MtxRational r;
     * r = matrix[1][2];
     */
    MtxRational * operator[](size_t index) const;
    MtxMatrix operator+() const;
    MtxMatrix operator-() const;
    bool operator==(const MtxMatrix &matrix) const;
    bool operator!=(const MtxMatrix &matrix) const;
    MtxMatrix operator+(const MtxMatrix &matrix) const;
    MtxMatrix operator+(const MtxRational &rational) const;
    MtxMatrix & operator+=(const MtxMatrix &matrix);
    MtxMatrix operator-(const MtxMatrix &matrix) const;
    MtxMatrix operator-(const MtxRational &rational) const;
    MtxMatrix & operator-=(const MtxMatrix &matrix);
    MtxMatrix operator*(const MtxMatrix &matrix) const;
    MtxMatrix & operator*=(const MtxMatrix &matrix);
    MtxMatrix operator*(const MtxRational &rational) const;
    MtxMatrix & operator*=(const MtxRational &rational);
    MtxMatrix operator/(const MtxMatrix &matrix) const;
    MtxMatrix operator/(const MtxRational &rational) const;

    friend std::ostream & operator<<(std::ostream &cout,
                                     const MtxMatrix &matrix);

    /**
     * Return length.
     * 
     * @see length
     */
    size_t getLength() const;

    /**
     * Return number of rows.
     * 
     * @see rows
     */
    size_t getRows() const;

    /**
     * Return number of columns.
     * 
     * @see columns
     */
    size_t getColumns() const;

    /**
     * Normalize each rational of the matrix.
     */
    MtxMatrix & normalize();

    /**
     * Return the transpose of the matrix.
     */
    MtxMatrix transpose() const;

    /**
     * Return the current matrix augmented with the given matrix.
     */
    MtxMatrix augmented(const MtxMatrix &matrix) const;

    /**
     * Apply an LU decomposition.
     * 
     * @param L set to the unit lower triangular matrix
     * @param U set to the upper triangular matrix
     */
    void luDecomposition(MtxMatrix &L, MtxMatrix &U) const;

    /**
     * Return the determinant.
     */
    MtxRational determinant() const;

    /**
     * Return the inverted matrix.
     */
    MtxMatrix inverted() const;

    /**
     * Return a string representation of the matrix.
     * 
     * If float_string is true, matrix elements are returned in floating
     * point representation, otherwise, they are returned in fractional
     * representation.
     * 
     * @param float_string control rendering of the matrix elements
     * @see toString()
     * @see toFloatString()
     */
    std::string toString(bool float_string) const;

    /**
     * Return a string representation of the matrix.
     * 
     * Similar to toString(false).
     * 
     * @see toString(bool float_string) const
     * @see toFloatString()
     */
    virtual std::string toString() const;

    /**
     * Return a string representation of the matrix.
     * 
     * Similar to toString(true).
     * 
     * @see toString(bool float_string) const
     * @see toString()
     */
    std::string toFloatString() const;

    /*
     * See mtxvalue.h.
     */
    MTXVALUE_CLONE_METHOD_DECLARATION(MtxMatrix);
    virtual MtxValue * add(const MtxValue *value) const;
    virtual MtxValue * subtract(const MtxValue *value) const;
    virtual MtxValue * multiply(const MtxValue *value) const;
    virtual MtxValue * divide(const MtxValue *value) const;
    virtual MtxValue * opposite() const;

    /**
     * Return a zero matrix.
     * 
     * @param rows number of rows of the zero matrix
     * @param columns number of columns of the zero matrix
     */
    static MtxMatrix getZeroMatrix(size_t rows, size_t columns);

    /**
     * Return an identity matrix.
     * 
     * Note that an identity matrix is a square matrix.
     * 
     * @param n dimension of identity matrix
     */
    static MtxMatrix getIdentityMatrix(size_t n);
};

#endif /* _MTXMATRIX_H */