00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef __MATRIX_H__
00017 #define __MATRIX_H__
00018
00019
00020
00021 #include <iostream>
00022 #include <fstream>
00023 #include <sstream>
00024 #include <cstdlib>
00025 #include <climits>
00026 #include <cmath>
00027
00028
00029
00030 #include "Vector.h"
00031
00032 namespace Flood
00033 {
00034
00037
00038 template <class Type>
00039 class Matrix
00040 {
00041
00042 public:
00043
00044
00045
00046 explicit Matrix(void);
00047
00048 explicit Matrix(int, int);
00049
00050 explicit Matrix(int, int, const Type&);
00051
00052 explicit Matrix(const char*);
00053
00054 Matrix(const Matrix&);
00055
00056
00057
00058 virtual ~Matrix(void);
00059
00060
00061
00062
00063 Matrix<Type>& operator = (const Matrix<Type>&);
00064
00065
00066
00067
00068 inline Type* operator [] (int);
00069 inline const Type* operator [] (int) const;
00070
00071
00072
00073
00074 Matrix<Type> operator + (const Type&) const;
00075 Matrix<Type> operator + (const Matrix<Type>&) const;
00076
00077 Matrix<Type> operator - (const Type&) const;
00078 Matrix<Type> operator - (const Matrix<Type>&) const;
00079
00080 Matrix<Type> operator * (const Type&) const;
00081 Matrix<Type> operator * (const Matrix<Type>&) const;
00082
00083 Matrix<Type> operator / (const Type&) const;
00084 Matrix<Type> operator / (const Matrix<Type>&) const;
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 bool operator == (const Matrix<Type>&) const;
00105 bool operator == (const Type&) const;
00106
00107 bool operator != (const Matrix<Type>&) const;
00108 bool operator != (const Type&) const;
00109
00110 bool operator > (const Matrix<Type>&) const;
00111 bool operator > (const Type&) const;
00112
00113 bool operator < (const Matrix<Type>&) const;
00114 bool operator < (const Type&) const;
00115
00116 bool operator >= (const Matrix<Type>&) const;
00117 bool operator >= (const Type&) const;
00118
00119 bool operator <= (const Matrix<Type>&) const;
00120 bool operator <= (const Type&) const;
00121
00122
00123
00124
00125
00126
00127 inline int get_rows_number(void) const;
00128 inline int get_columns_number(void) const;
00129
00130 Vector<Type> get_row(int) const;
00131 Vector<Type> get_column(int) const;
00132
00133 Matrix<Type> get_submatrix(const Vector<int>&, const Vector<int>&) const;
00134
00135 Vector<Type> get_diagonal(void) const;
00136
00137 bool get_display(void) const;
00138
00139
00140
00141 void set(void);
00142 void set(int, int);
00143 void set(int, int, const Type&);
00144 void set(const Matrix&);
00145 void set(const char*);
00146
00147 void set_row(int, const Vector<Type>&) const;
00148 void set_column(int, const Vector<Type>&) const;
00149
00150 void set_rows_number(int);
00151 void set_columns_number(int);
00152
00153 void set_display(bool);
00154
00155
00156
00157 void resize(int, int);
00158
00159 void add_row(const Vector<Type>&);
00160 void add_column(const Vector<Type>&);
00161
00162 void subtract_row(int);
00163 void subtract_column(int);
00164
00165
00166
00167 void initialize(const Type&) const;
00168
00169 void initialize_uniform(void);
00170 void initialize_uniform(double, double);
00171 void initialize_uniform(const Matrix<double>&, const Matrix<double>&) ;
00172
00173 void initialize_normal(void);
00174 void initialize_normal(double, double);
00175 void initialize_normal(const Matrix<double>&, const Matrix<double>&);
00176
00177 void set_to_identity(void) const;
00178
00179
00180
00181 Vector<Type> dot(const Vector<Type>&) const;
00182 Matrix<Type> dot(const Matrix<Type>&) const;
00183
00184 Vector< Vector<double> > calculate_mean_standard_deviation(void) const;
00185 Vector< Vector<double> > calculate_mean_standard_deviation(const Vector<int>&) const;
00186 Vector< Vector<double> > calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const;
00187
00188 Vector< Vector<Type> > calculate_minimum_maximum(void) const;
00189 Vector< Vector<Type> > calculate_minimum_maximum(const Vector<int>&) const;
00190 Vector< Vector<Type> > calculate_minimum_maximum(const Vector<int>&, const Vector<int>&) const;
00191
00192 Type calculate_determinant(void) const;
00193 Matrix<Type> calculate_transpose(void) const;
00194 Matrix<Type> calculate_cofactor(void) const;
00195 Matrix<Type> calculate_inverse(void) const;
00196
00197 bool is_symmetric(void) const;
00198 bool is_antisymmetric(void) const;
00199
00200
00201
00202 void scale_mean_standard_deviation(const Vector<double>&, const Vector<double>&);
00203 void scale_minimum_maximum(const Vector<double>&, const Vector<double>&);
00204
00205
00206
00207 void unscale_mean_standard_deviation(const Vector<double>&, const Vector<double>&);
00208 void unscale_minimum_maximum(const Vector<double>&, const Vector<double>&);
00209
00210
00211
00212 std::string to_XML(bool);
00213
00214 void print(void);
00215 void print_data(void);
00216
00217 void save(const char*);
00218 void save_data(const char*);
00219
00220 void load(const char*);
00221 void load_data(const char*);
00222
00223 private:
00224
00226
00227 int rows_number;
00228
00230
00231 int columns_number;
00232
00233 bool display;
00234
00236
00237 Type** data;
00238
00239
00240
00241 double calculate_random_uniform(double, double);
00242 double calculate_random_normal(double, double);
00243 };
00244
00245
00246
00247
00249
00250 template <class Type>
00251 Matrix<Type>::Matrix(void)
00252 {
00253 rows_number = 0;
00254 columns_number = 0;
00255 display = true;
00256 data = NULL;
00257 }
00258
00259
00264
00265 template <class Type>
00266 Matrix<Type>::Matrix(int new_rows_number, int new_columns_number)
00267 {
00268
00269
00270 #ifdef _DEBUG
00271
00272 if(new_rows_number < 0)
00273 {
00274 std::cerr << "Flood Error: Matrix Template. " << std::endl
00275 << "Constructor Matrix(int, int)." << std::endl
00276 << "Number of rows must be equal or greater than zero." << std::endl;
00277
00278 exit(1);
00279 }
00280 else if(new_columns_number < 0)
00281 {
00282 std::cerr << "Flood Error: Matrix Template. " << std::endl
00283 << "Constructor Matrix(int, int)." << std::endl
00284 << "Number of columns must be equal or greater than zero." << std::endl;
00285
00286 exit(1);
00287 }
00288
00289 #endif
00290
00291 if(new_rows_number == 0 && new_columns_number == 0)
00292 {
00293 rows_number = 0;
00294 columns_number = 0;
00295 data = NULL;
00296 }
00297 else if(new_rows_number == 0)
00298 {
00299 std::cerr << "Flood Error: Matrix Template. " << std::endl
00300 << "Constructor Matrix(int, int)." << std::endl
00301 << "Number of rows must be greater than zero." << std::endl;
00302
00303 exit(1);
00304 }
00305 else if(new_columns_number == 0)
00306 {
00307 std::cerr << "Flood Error: Matrix Template. " << std::endl
00308 << "Constructor Matrix(int, int)." << std::endl
00309 << "Number of columns must be greater than zero." << std::endl;
00310
00311 exit(1);
00312 }
00313 else
00314 {
00315 rows_number = new_rows_number;
00316 columns_number = new_columns_number;
00317
00318 data = new Type*[rows_number];
00319 data[0] = new Type[rows_number*columns_number];
00320
00321 for(int i = 1; i < rows_number; i++)
00322 {
00323 data[i] = data[i-1] + columns_number;
00324 }
00325 }
00326
00327 display = true;
00328 }
00329
00330
00335
00336 template <class Type>
00337 Matrix<Type>::Matrix(int new_rows_number, int new_columns_number, const Type& type)
00338 {
00339
00340
00341 #ifdef _DEBUG
00342
00343 if(new_rows_number < 0)
00344 {
00345 std::cerr << "Flood Error: Matrix Template. " << std::endl
00346 << "Constructor Matrix(int, int, const Type&)." << std::endl
00347 << "Number of rows must be equal or greater than zero." << std::endl;
00348
00349 exit(1);
00350 }
00351 else if(new_columns_number < 0)
00352 {
00353 std::cerr << "Flood Error: Matrix Template. " << std::endl
00354 << "Constructor Matrix(int, int, const Type&)." << std::endl
00355 << "Number of columns must be equal or greater than zero." << std::endl;
00356
00357 exit(1);
00358 }
00359
00360 #endif
00361
00362 if(new_rows_number == 0 && new_columns_number == 0)
00363 {
00364 rows_number = 0;
00365 columns_number = 0;
00366 data = NULL;
00367 }
00368 else if(new_rows_number == 0)
00369 {
00370 std::cerr << "Flood Error: Matrix Template. " << std::endl
00371 << "Constructor Matrix(int, int, const Type&)." << std::endl
00372 << "Number of rows must be greater than zero." << std::endl;
00373
00374 exit(1);
00375 }
00376 else if(new_columns_number == 0)
00377 {
00378 std::cerr << "Flood Error: Matrix Template. " << std::endl
00379 << "Constructor Matrix(int, int, const Type&)." << std::endl
00380 << "Number of columns must be greater than zero." << std::endl;
00381
00382 exit(1);
00383 }
00384 else
00385 {
00386
00387
00388 rows_number = new_rows_number;
00389 columns_number = new_columns_number;
00390
00391 data = new Type*[new_rows_number];
00392 data[0] = new Type[rows_number*columns_number];
00393
00394 for(int i = 1; i < rows_number; i++)
00395 {
00396 data[i] = data[i-1] + columns_number;
00397 }
00398
00399
00400
00401 for(int i = 0; i < rows_number; i++)
00402 {
00403 for(int j = 0; j < columns_number; j++)
00404 {
00405 data[i][j] = type;
00406 }
00407 }
00408 }
00409
00410 display = true;
00411 }
00412
00413
00416
00417 template <class Type>
00418 Matrix<Type>::Matrix(const char* filename)
00419 {
00420 rows_number = 0;
00421 columns_number = 0;
00422 display = true;
00423 data = NULL;
00424
00425 load(filename);
00426 }
00427
00428
00431
00432 template <class Type>
00433 Matrix<Type>::Matrix(const Matrix& other_matrix)
00434 {
00435 int new_rows_number = other_matrix.rows_number;
00436 int new_columns_number = other_matrix.columns_number;
00437
00438 display = other_matrix.display;
00439 data = NULL;
00440
00441
00442
00443 #ifdef _DEBUG
00444
00445 if(new_rows_number < 0)
00446 {
00447 std::cerr << "Flood Error: Matrix Template. " << std::endl
00448 << "Copy constructor." << std::endl
00449 << "Number of rows must be equal or greater than zero." << std::endl;
00450
00451 exit(1);
00452 }
00453 else if(new_columns_number < 0)
00454 {
00455 std::cerr << "Flood Error: Matrix Template. " << std::endl
00456 << "Copy constructor." << std::endl
00457 << "Number of columns must be equal or greater than zero." << std::endl;
00458
00459 exit(1);
00460 }
00461
00462 #endif
00463
00464 if(new_rows_number == 0 && new_columns_number == 0)
00465 {
00466 rows_number = 0;
00467 columns_number = 0;
00468 data = NULL;
00469 }
00470 else if(new_rows_number == 0)
00471 {
00472 std::cerr << "Flood Error: Matrix Template. " << std::endl
00473 << "Copy constructor." << std::endl
00474 << "Number of rows must be greater than zero." << std::endl;
00475
00476 exit(1);
00477 }
00478 else if(new_columns_number == 0)
00479 {
00480 std::cerr << "Flood Error: Matrix Template. " << std::endl
00481 << "Copy constructor." << std::endl
00482 << "Number of columns must be greater than zero." << std::endl;
00483
00484 exit(1);
00485 }
00486 else
00487 {
00488 rows_number = new_rows_number;
00489 columns_number = new_columns_number;
00490
00491 data = new Type*[rows_number];
00492 data[0] = new Type[rows_number*columns_number];
00493
00494 for(int i = 1; i < rows_number; i++)
00495 {
00496 data[i] = data[i-1] + columns_number;
00497 }
00498 }
00499
00500 for(int i = 0; i < rows_number; i++)
00501 {
00502 for(int j = 0; j < columns_number; j++)
00503 {
00504 data[i][j] = other_matrix[i][j];
00505 }
00506 }
00507 }
00508
00509
00510
00511
00513
00514 template <class Type>
00515 Matrix<Type>::~Matrix(void)
00516 {
00517 if(data != NULL)
00518 {
00519 delete[] (data[0]);
00520 delete[] (data);
00521 }
00522 }
00523
00524
00525
00526
00529
00530 template <class Type>
00531 Matrix<Type>& Matrix<Type>::operator = (const Matrix<Type>& other_matrix)
00532 {
00533 if(this != &other_matrix)
00534 {
00535 if(rows_number != other_matrix.rows_number || columns_number != other_matrix.columns_number)
00536 {
00537 if(data != NULL)
00538 {
00539 delete[] (data[0]);
00540 delete[] (data);
00541 }
00542
00543 rows_number = other_matrix.rows_number;
00544 columns_number = other_matrix.columns_number;
00545
00546 data = new Type*[rows_number];
00547 data[0] = new Type[rows_number*columns_number];
00548
00549 for(int i = 1; i < rows_number; i++)
00550 {
00551 data[i] = data[i-1] + columns_number;
00552 }
00553 }
00554
00555
00556 for(int i = 0; i < rows_number; i++)
00557 {
00558 for(int j = 0; j < columns_number; j++)
00559 {
00560 data[i][j] = other_matrix[i][j];
00561 }
00562 }
00563 }
00564
00565 return(*this);
00566 }
00567
00568
00569
00570
00572
00573 template <class Type>
00574 inline Type* Matrix<Type>::operator [] (int i)
00575 {
00576
00577
00578 #ifdef _DEBUG
00579
00580 if(i < 0)
00581 {
00582 std::cerr << "Flood Error: Matrix Template. " << std::endl
00583 << "operator [] (int)." << std::endl
00584 << "Row index (" << i << ") must be equal or greater than zero." << std::endl;
00585
00586 exit(1);
00587 }
00588
00589 if(i >= rows_number)
00590 {
00591 std::cerr << "Flood Error: Matrix Template. " << std::endl
00592 << "operator [] (int)." << std::endl
00593 << "Row index (" << i << ") must be less than number of rows (" << rows_number << ")." << std::endl;
00594
00595 exit(1);
00596 }
00597
00598 #endif
00599
00600
00601
00602 return(data[i]);
00603 }
00604
00605
00607
00608 template <class Type>
00609 inline const Type* Matrix<Type>::operator [] (int i) const
00610 {
00611
00612
00613 #ifdef _DEBUG
00614
00615 if(i < 0)
00616 {
00617 std::cerr << "Flood Error: Matrix Template. " << std::endl
00618 << "operator [] (int) const." << std::endl
00619 << "Row index must be equal or greater than zero." << std::endl;
00620
00621 exit(1);
00622 }
00623
00624 if(i >= rows_number)
00625 {
00626 std::cerr << "Flood Error: Matrix Template. " << std::endl
00627 << "operator [] (int) const." << std::endl
00628 << "Row index must be less than number of rows." << std::endl;
00629
00630 exit(1);
00631 }
00632
00633 #endif
00634
00635
00636
00637 return(data[i]);
00638 }
00639
00640
00641
00642
00646
00647 template <typename Type>
00648 bool Matrix<Type>::operator == (const Matrix<Type>& other_matrix) const
00649 {
00650
00651
00652 #ifdef _DEBUG
00653
00654 int other_rows_number = other_matrix.get_rows_number();
00655 int other_columns_number = other_matrix.get_columns_number();
00656
00657 if(other_rows_number != rows_number)
00658 {
00659 std::cerr << "Flood Error: Matrix Template." << std::endl
00660 << "bool operator == (const Matrix<Type>&) const." << std::endl
00661 << "Both number of rows must be the same." << std::endl;
00662
00663 exit(1);
00664 }
00665 else if(other_columns_number != columns_number)
00666 {
00667 std::cerr << "Flood Error: Matrix Template." << std::endl
00668 << "bool operator == (const Matrix<Type>&) const." << std::endl
00669 << "Both number of columns must be the same." << std::endl;
00670
00671 exit(1);
00672 }
00673
00674 #endif
00675
00676 for(int i = 0; i < rows_number; i++)
00677 {
00678 for(int j = 0; j < columns_number; j++)
00679 {
00680 if(data[i][j] != other_matrix[i][j])
00681 {
00682 return(false);
00683 }
00684 }
00685 }
00686
00687 return(true);
00688 }
00689
00690
00691
00692
00696
00697 template <typename Type>
00698 bool Matrix<Type>::operator == (const Type& value) const
00699 {
00700 for(int i = 0; i < rows_number; i++)
00701 {
00702 for(int j = 0; j < columns_number; j++)
00703 {
00704 if(data[i][j] != value)
00705 {
00706 return(false);
00707 }
00708 }
00709 }
00710
00711 return(true);
00712 }
00713
00714
00715
00716
00720
00721 template <typename Type>
00722 bool Matrix<Type>::operator != (const Matrix<Type>& other_matrix) const
00723 {
00724
00725
00726 #ifdef _DEBUG
00727
00728 int other_rows_number = other_matrix.get_rows_number();
00729 int other_columns_number = other_matrix.get_columns_number();
00730
00731 if(other_rows_number != rows_number)
00732 {
00733 std::cerr << "Flood Error: Matrix Template." << std::endl
00734 << "bool operator != (const Matrix<Type>&) const." << std::endl
00735 << "Both numbers of rows must be the same." << std::endl;
00736
00737 exit(1);
00738 }
00739 else if(other_columns_number != columns_number)
00740 {
00741 std::cerr << "Flood Error: Matrix Template." << std::endl
00742 << "bool operator != (const Matrix<Type>&) const." << std::endl
00743 << "Both numbers of columns must be the same." << std::endl;
00744
00745 exit(1);
00746 }
00747
00748 #endif
00749
00750 for(int i = 0; i < rows_number; i++)
00751 {
00752 for(int j = 0; j < columns_number; j++)
00753 {
00754 if(data[i][j] != other_matrix[i][j])
00755 {
00756 return(true);
00757 }
00758 }
00759 }
00760
00761 return(false);
00762 }
00763
00764
00765
00766
00770
00771 template <typename Type>
00772 bool Matrix<Type>::operator != (const Type& value) const
00773 {
00774
00775
00776 for(int i = 0; i < rows_number; i++)
00777 {
00778 for(int j = 0; j < columns_number; j++)
00779 {
00780 if(data[i][j] != value)
00781 {
00782 return(true);
00783 }
00784 }
00785 }
00786
00787 return(false);
00788 }
00789
00790
00791
00792
00797
00798 template <typename Type>
00799 bool Matrix<Type>::operator > (const Matrix<Type>& other_matrix) const
00800 {
00801
00802
00803 #ifdef _DEBUG
00804
00805 int other_rows_number = other_matrix.get_rows_number();
00806 int other_columns_number = other_matrix.get_columns_number();
00807
00808 if(other_rows_number != rows_number)
00809 {
00810 std::cerr << "Flood Error: Matrix Template." << std::endl
00811 << "bool operator > (const Matrix<Type>&) const." << std::endl
00812 << "Both numbers of rows must be the same." << std::endl;
00813
00814 exit(1);
00815 }
00816 else if(other_columns_number != columns_number)
00817 {
00818 std::cerr << "Flood Error: Matrix Template." << std::endl
00819 << "bool operator > (const Matrix<Type>&) const." << std::endl
00820 << "Both numbers of columns must be the same." << std::endl;
00821
00822 exit(1);
00823 }
00824
00825 #endif
00826
00827 for(int i = 0; i < rows_number; i++)
00828 {
00829 for(int j = 0; j < rows_number; j++)
00830 {
00831 if(data[i][j] <= other_matrix[i][j])
00832 {
00833 return(false);
00834 }
00835 }
00836 }
00837
00838 return(true);
00839 }
00840
00841
00842
00843
00847
00848 template <typename Type>
00849 bool Matrix<Type>::operator > (const Type& value) const
00850 {
00851 for(int i = 0; i < rows_number; i++)
00852 {
00853 for(int j = 0; j < rows_number; j++)
00854 {
00855 if(data[i][j] <= value)
00856 {
00857 return(false);
00858 }
00859 }
00860 }
00861
00862 return(true);
00863 }
00864
00865
00866
00867
00872
00873 template <typename Type>
00874 bool Matrix<Type>::operator < (const Matrix<Type>& other_matrix) const
00875 {
00876
00877
00878 #ifdef _DEBUG
00879
00880 int other_rows_number = other_matrix.get_rows_number();
00881 int other_columns_number = other_matrix.get_columns_number();
00882
00883 if(other_rows_number != rows_number)
00884 {
00885 std::cerr << "Flood Error: Matrix Template." << std::endl
00886 << "bool operator < (const Matrix<Type>&) const." << std::endl
00887 << "Both numbers of rows must be the same." << std::endl;
00888
00889 exit(1);
00890 }
00891 else if(other_columns_number != columns_number)
00892 {
00893 std::cerr << "Flood Error: Matrix Template." << std::endl
00894 << "bool operator < (const Matrix<Type>&) const." << std::endl
00895 << "Both numbers of columns must be the same." << std::endl;
00896
00897 exit(1);
00898 }
00899
00900 #endif
00901
00902 for(int i = 0; i < rows_number; i++)
00903 {
00904 for(int j = 0; j < columns_number; j++)
00905 {
00906 if(data[i][j] >= other_matrix[i][j])
00907 {
00908 return(false);
00909 }
00910 }
00911 }
00912
00913 return(true);
00914 }
00915
00916
00917
00918
00922
00923 template <typename Type>
00924 bool Matrix<Type>::operator < (const Type& value) const
00925 {
00926 for(int i = 0; i < rows_number; i++)
00927 {
00928 for(int j = 0; j < columns_number; j++)
00929 {
00930 if(data[i][j] >= value)
00931 {
00932 return(false);
00933 }
00934 }
00935 }
00936
00937 return(true);
00938 }
00939
00940
00941
00942
00947
00948 template <typename Type>
00949 bool Matrix<Type>::operator >= (const Matrix<Type>& other_matrix) const
00950 {
00951
00952
00953 #ifdef _DEBUG
00954
00955 int other_rows_number = other_matrix.get_rows_number();
00956 int other_columns_number = other_matrix.get_columns_number();
00957
00958 if(other_rows_number != rows_number)
00959 {
00960 std::cerr << "Flood Error: Matrix Template." << std::endl
00961 << "bool operator >= (const Matrix<Type>&) const." << std::endl
00962 << "Both number of rows must be the same." << std::endl;
00963
00964 exit(1);
00965 }
00966 else if(other_columns_number != columns_number)
00967 {
00968 std::cerr << "Flood Error: Matrix Template." << std::endl
00969 << "bool operator >= (const Matrix<Type>&) const." << std::endl
00970 << "Both number of columns must be the same." << std::endl;
00971
00972 exit(1);
00973 }
00974
00975 #endif
00976
00977 for(int i = 0; i < rows_number; i++)
00978 {
00979 for(int j = 0; j < columns_number; j++)
00980 {
00981 if(data[i][j] < other_matrix[i][j])
00982 {
00983 return(false);
00984 }
00985 }
00986 }
00987
00988 return(true);
00989 }
00990
00991
00992
00993
00997
00998 template <typename Type>
00999 bool Matrix<Type>::operator >= (const Type& value) const
01000 {
01001 for(int i = 0; i < rows_number; i++)
01002 {
01003 for(int j = 0; j < columns_number; j++)
01004 {
01005 if(data[i][j] < value)
01006 {
01007 return(false);
01008 }
01009 }
01010 }
01011
01012 return(true);
01013 }
01014
01015
01016
01017
01022
01023 template <typename Type>
01024 bool Matrix<Type>::operator <= (const Matrix<Type>& other_matrix) const
01025 {
01026
01027
01028 #ifdef _DEBUG
01029
01030 int other_rows_number = other_matrix.get_rows_number();
01031 int other_columns_number = other_matrix.get_columns_number();
01032
01033 if(other_rows_number != rows_number)
01034 {
01035 std::cerr << "Flood Error: Matrix Template." << std::endl
01036 << "bool operator >= (const Matrix<Type>&) const." << std::endl
01037 << "Both numbers of rows must be the same." << std::endl;
01038
01039 exit(1);
01040 }
01041 else if(other_columns_number != columns_number)
01042 {
01043 std::cerr << "Flood Error: Matrix Template." << std::endl
01044 << "bool operator >= (const Matrix<Type>&) const." << std::endl
01045 << "Both numbers of columns must be the same." << std::endl;
01046
01047 exit(1);
01048 }
01049
01050 #endif
01051
01052 for(int i = 0; i < rows_number; i++)
01053 {
01054 for(int j = 0; j < columns_number; j++)
01055 {
01056 if(data[i][j] > other_matrix[i][j])
01057 {
01058 return(false);
01059 }
01060 }
01061 }
01062
01063 return(true);
01064 }
01065
01066
01067
01068
01072
01073 template <typename Type>
01074 bool Matrix<Type>::operator <= (const Type& value) const
01075 {
01076 for(int i = 0; i < rows_number; i++)
01077 {
01078 for(int j = 0; j < columns_number; j++)
01079 {
01080 if(data[i][j] > value)
01081 {
01082 return(false);
01083 }
01084 }
01085 }
01086
01087 return(true);
01088 }
01089
01090
01091
01092
01093
01094
01096
01097 template <class Type>
01098 inline int Matrix<Type>::get_rows_number(void) const
01099 {
01100 return(rows_number);
01101 }
01102
01103
01104
01105
01107
01108 template <class Type>
01109 inline int Matrix<Type>::get_columns_number(void) const
01110 {
01111 return(columns_number);
01112 }
01113
01114
01115
01116
01118
01119 template <class Type>
01120 bool Matrix<Type>::get_display(void) const
01121 {
01122 return(display);
01123 }
01124
01125
01126
01127
01129
01130 template <class Type>
01131 void Matrix<Type>::set(void)
01132 {
01133 if(data != NULL)
01134 {
01135 delete[] (data[0]);
01136 delete[] (data);
01137 }
01138
01139 rows_number = 0;
01140 columns_number = 0;
01141 data = NULL;
01142 }
01143
01144
01145
01146
01150
01151 template <class Type>
01152 void Matrix<Type>::set(int new_rows_number, int new_columns_number)
01153 {
01154
01155
01156 #ifdef _DEBUG
01157
01158 if(new_rows_number < 0)
01159 {
01160 std::cerr << "Flood Error: Matrix Template. " << std::endl
01161 << "void set(int, int) method." << std::endl
01162 << "Number of rows must be equal or greater than zero." << std::endl;
01163
01164 exit(1);
01165 }
01166 else if(new_columns_number < 0)
01167 {
01168 std::cerr << "Flood Error: Matrix Template. " << std::endl
01169 << "void set(int, int) method." << std::endl
01170 << "Number of columns must be equal or greater than zero." << std::endl;
01171
01172 exit(1);
01173 }
01174
01175 #endif
01176
01177 if(new_rows_number == rows_number && new_columns_number == columns_number)
01178 {
01179
01180 }
01181 else if(new_rows_number == 0 && new_columns_number == 0)
01182 {
01183 set();
01184 }
01185 else if(new_rows_number == 0)
01186 {
01187 std::cerr << "Flood Error: Matrix Template." << std::endl
01188 << "void set(int, int) method." << std::endl
01189 << "Number of rows must be greater than zero." << std::endl;
01190
01191 exit(1);
01192 }
01193 else if(new_columns_number == 0)
01194 {
01195 std::cerr << "Flood Error: Matrix Template." << std::endl
01196 << "void set(int, int) method." << std::endl
01197 << "Number of columns must be greater than zero." << std::endl;
01198
01199 exit(1);
01200 }
01201 else
01202 {
01203 rows_number = new_rows_number;
01204 columns_number = new_columns_number;
01205
01206 if(data != NULL)
01207 {
01208 delete[] (data[0]);
01209 delete[] (data);
01210 }
01211
01212 data = new Type*[rows_number];
01213 data[0] = new Type[rows_number*columns_number];
01214
01215 for(int i = 1; i < rows_number; i++)
01216 {
01217 data[i] = data[i-1] + columns_number;
01218 }
01219 }
01220 }
01221
01222
01223
01224
01230
01231 template <class Type>
01232 void Matrix<Type>::set(int new_rows_number, int new_columns_number, const Type& value)
01233 {
01234
01235
01236 #ifdef _DEBUG
01237
01238 if(new_rows_number < 0)
01239 {
01240 std::cerr << "Flood Error: Matrix Template. " << std::endl
01241 << "void set(int, int, const Type&) method." << std::endl
01242 << "Number of rows must be equal or greater than zero." << std::endl;
01243
01244 exit(1);
01245 }
01246 else if(new_columns_number < 0)
01247 {
01248 std::cerr << "Flood Error: Matrix Template. " << std::endl
01249 << "void set(int, int, const Type&) method." << std::endl
01250 << "Number of columns must be equal or greater than zero." << std::endl;
01251
01252 exit(1);
01253 }
01254
01255 #endif
01256
01257 if(new_rows_number == rows_number && new_columns_number == columns_number)
01258 {
01259
01260 }
01261 else if(new_rows_number == 0 && new_columns_number == 0)
01262 {
01263 set();
01264 }
01265 else if(new_rows_number == 0)
01266 {
01267 std::cerr << "Flood Error: Matrix Template." << std::endl
01268 << "void set(int, int, const Type&) method." << std::endl
01269 << "Number of rows must be greater than zero." << std::endl;
01270
01271 exit(1);
01272 }
01273 else if(new_columns_number == 0)
01274 {
01275 std::cerr << "Flood Error: Matrix Template." << std::endl
01276 << "void set(int, int, const Type&) method." << std::endl
01277 << "Number of columns must be greater than zero." << std::endl;
01278
01279 exit(1);
01280 }
01281 else
01282 {
01283 set(new_rows_number, new_columns_number);
01284 initialize(value);
01285 }
01286 }
01287
01288
01289
01290
01293
01294 template <class Type>
01295 void Matrix<Type>::set(const Matrix& other_matrix)
01296 {
01297 }
01298
01299
01300
01301
01304
01305 template <class Type>
01306 void Matrix<Type>::set(const char* filename)
01307 {
01308 load(filename);
01309 }
01310
01311
01312
01313
01316
01317 template <class Type>
01318 void Matrix<Type>::set_rows_number(int new_rows_number)
01319 {
01320
01321
01322 #ifdef _DEBUG
01323
01324 if(new_rows_number < 0)
01325 {
01326 std::cerr << "Flood Error: Matrix Template. " << std::endl
01327 << "void set_rows_number(int) method." << std::endl
01328 << "Number of rows must be equal or greater than zero." << std::endl;
01329
01330 exit(1);
01331 }
01332
01333 #endif
01334
01335 if(new_rows_number != rows_number)
01336 {
01337 if(data != NULL)
01338 {
01339 delete[] (data[0]);
01340 delete[] (data);
01341 }
01342
01343 if(new_rows_number == 0 && columns_number == 0)
01344 {
01345 rows_number = 0;
01346 columns_number = 0;
01347 data = NULL;
01348 }
01349 else if(new_rows_number == 0)
01350 {
01351 std::cerr << "Flood Error: Matrix Template. " << std::endl
01352 << "void set(int, int) method." << std::endl
01353 << "Number of rows must be greater than zero." << std::endl;
01354
01355 exit(1);
01356 }
01357 else if(columns_number == 0)
01358 {
01359 std::cerr << "Flood Error: Matrix Template. " << std::endl
01360 << "void set(int, int) method." << std::endl
01361 << "Number of columns must be greater than zero." << std::endl;
01362
01363 exit(1);
01364 }
01365 else
01366 {
01367 rows_number = new_rows_number;
01368
01369 data = new Type*[rows_number];
01370 data[0] = new Type[rows_number*columns_number];
01371
01372 for(int i = 1; i < rows_number; i++)
01373 {
01374 data[i] = data[i-1] + columns_number;
01375 }
01376 }
01377 }
01378 }
01379
01380
01381
01382
01385
01386 template <class Type>
01387 void Matrix<Type>::set_columns_number(int new_columns_number)
01388 {
01389
01390
01391 #ifdef _DEBUG
01392
01393 if(new_columns_number < 0)
01394 {
01395 std::cerr << "Flood Error: Matrix Template. " << std::endl
01396 << "void set_columns_number(int) method." << std::endl
01397 << "Number of columns must be equal or greater than zero." << std::endl;
01398
01399 exit(1);
01400 }
01401
01402 #endif
01403 }
01404
01405
01406
01407
01410
01411 template <class Type>
01412 void Matrix<Type>::set_display(bool new_display)
01413 {
01414 display = new_display;
01415 }
01416
01417
01418
01419
01424
01425 template <class Type>
01426 void Matrix<Type>::resize(int new_rows_number, int new_columns_number)
01427 {
01428
01429
01430 #ifdef _DEBUG
01431
01432 if(new_rows_number < 0)
01433 {
01434 std::cerr << "Flood Error: Matrix Template." << std::endl
01435 << "void resize(int, int) method." << std::endl
01436 << "Number of rows must be equal or greater than zero." << std::endl;
01437
01438 exit(1);
01439 }
01440 else if(new_columns_number < 0)
01441 {
01442 std::cerr << "Flood Error: Matrix Template." << std::endl
01443 << "void resize(int, int) method." << std::endl
01444 << "Number of columns must be equal or greater than zero." << std::endl;
01445
01446 exit(1);
01447 }
01448
01449 #endif
01450
01451
01452
01453 if(new_rows_number == rows_number && new_columns_number == columns_number)
01454 {
01455
01456 }
01457 else if(new_rows_number == 0 && new_columns_number == 0)
01458 {
01459 rows_number = 0;
01460 columns_number = 0;
01461
01462 if(data != NULL)
01463 {
01464 delete[] (data[0]);
01465 delete[] (data);
01466 }
01467
01468 data = NULL;
01469 }
01470 else if(new_rows_number == 0)
01471 {
01472 std::cerr << "Flood Error: Matrix Template." << std::endl
01473 << "void resize(int, int) method." << std::endl
01474 << "Number of rows must be greater than zero." << std::endl;
01475
01476 exit(1);
01477 }
01478 else if(new_columns_number == 0)
01479 {
01480 std::cerr << "Flood Error: Matrix Template." << std::endl
01481 << "void resize(int, int) method." << std::endl
01482 << "Number of columns must be greater than zero." << std::endl;
01483
01484 exit(1);
01485 }
01486 else
01487 {
01488 if(new_rows_number >= rows_number && new_columns_number >= columns_number)
01489 {
01490 Matrix<Type> copy(*this);
01491
01492 set(new_rows_number, new_columns_number);
01493
01494 for(int i = 0; i < copy.get_rows_number(); i++)
01495 {
01496 for(int j = 0; j < copy.get_columns_number(); j++)
01497 {
01498 data[i][j] = copy[i][j];
01499 }
01500 }
01501
01502 }
01503 else if(new_rows_number >= rows_number && new_columns_number <= columns_number)
01504 {
01505 Matrix<Type> copy(*this);
01506
01507 set(new_rows_number, new_columns_number);
01508
01509 for(int i = 0; i < copy.get_rows_number(); i++)
01510 {
01511 for(int j = 0; j < new_columns_number; j++)
01512 {
01513 data[i][j] = copy[i][j];
01514 }
01515 }
01516 }
01517 else if(new_rows_number <= rows_number && new_columns_number >= columns_number)
01518 {
01519 Matrix<Type> copy(*this);
01520
01521 set(new_rows_number, new_columns_number);
01522
01523 for(int i = 0; i < new_rows_number; i++)
01524 {
01525 for(int j = 0; j < copy.get_columns_number(); j++)
01526 {
01527 data[i][j] = copy[i][j];
01528 }
01529 }
01530 }
01531 else if(new_rows_number <= rows_number && new_columns_number >= columns_number)
01532 {
01533 Matrix<Type> copy(*this);
01534
01535 set(new_rows_number, new_columns_number);
01536
01537 for(int i = 0; i < new_rows_number; i++)
01538 {
01539 for(int j = 0; j < new_columns_number; j++)
01540 {
01541 data[i][j] = copy[i][j];
01542 }
01543 }
01544 }
01545 }
01546 }
01547
01548
01549
01550
01554
01555 template <class Type>
01556 Matrix<Type> Matrix<Type>::get_submatrix(const Vector<int>& row_indices, const Vector<int>& column_indices) const
01557 {
01558 int row_indices_size = row_indices.get_size();
01559 int column_indices_size = column_indices.get_size();
01560
01561 Matrix<Type> submatrix(row_indices_size, column_indices_size);
01562
01563 int row_index;
01564 int column_index;
01565
01566 for(int i = 0; i < row_indices_size; i++)
01567 {
01568 row_index = row_indices[i];
01569
01570 for(int j = 0; j < column_indices_size; j++)
01571 {
01572 column_index = column_indices[j];
01573
01574 submatrix[i][j] = data[row_index][column_index];
01575 }
01576 }
01577
01578 return(submatrix);
01579 }
01580
01581
01582
01583
01584
01587
01588 template <class Type>
01589 Vector<Type> Matrix<Type>::get_row(int i) const
01590 {
01591 Vector<Type> row(columns_number);
01592
01593 for(int j = 0; j < columns_number; j++)
01594 {
01595 row[j] = data[i][j];
01596 }
01597
01598 return(row);
01599 }
01600
01601
01602
01603
01606
01607 template <class Type>
01608 Vector<Type> Matrix<Type>::get_column(int j) const
01609 {
01610 Vector<Type> column(rows_number);
01611
01612 for(int i = 0; i < rows_number; i++)
01613 {
01614 column[i] = data[i][j];
01615 }
01616
01617 return(column);
01618 }
01619
01620
01621
01622
01624
01625 template <class Type>
01626 Vector<Type> Matrix<Type>::get_diagonal(void) const
01627 {
01628
01629
01630 #ifdef _DEBUG
01631
01632 if(rows_number != columns_number)
01633 {
01634 std::cerr << "Flood Error: Matrix Template." << std::endl
01635 << "Vector<Type> get_diagonal(void) const method." << std::endl
01636 << "Matrix must be squared." << std::endl;
01637
01638 exit(1);
01639 }
01640
01641 #endif
01642
01643 Vector<Type> diagonal(rows_number);
01644
01645 for(int i = 0; i < rows_number; i++)
01646 {
01647 diagonal[i] = data[i][i];
01648 }
01649
01650 return(diagonal);
01651 }
01652
01653
01654
01655
01659
01660 template <class Type>
01661 void Matrix<Type>::set_row(int row_index, const Vector<Type>& new_row) const
01662 {
01663
01664
01665 #ifdef _DEBUG
01666
01667 if(row_index >= rows_number)
01668 {
01669 std::cerr << "Flood Error: Matrix Template. " << std::endl
01670 << "set_row(int, const Vector<Type>&) const method." << std::endl
01671 << "Index must be less than number of rows." << std::endl;
01672
01673 exit(1);
01674 }
01675
01676 int size = new_row.get_size();
01677
01678 if(size != columns_number)
01679 {
01680 std::cerr << "Flood Error: Matrix Template. " << std::endl
01681 << "set_row(int, const Vector<Type>&) const method." << std::endl
01682 << "Size must be equal to number of columns." << std::endl;
01683
01684 exit(1);
01685 }
01686
01687 #endif
01688
01689
01690
01691 for(int i = 0; i < columns_number; i++)
01692 {
01693 data[row_index][i] = new_row[i];
01694 }
01695 }
01696
01697
01698
01699
01703
01704 template <class Type>
01705 void Matrix<Type>::set_column(int column_index, const Vector<Type>& new_column) const
01706 {
01707
01708
01709 #ifdef _DEBUG
01710
01711 if(column_index >= columns_number)
01712 {
01713 std::cerr << "Flood Error: Matrix Template. " << std::endl
01714 << "set_column(int, const Vector<Type>&) const." << std::endl
01715 << "Index must be less than number of columns." << std::endl;
01716
01717 exit(1);
01718 }
01719
01720 int size = new_column.get_size();
01721
01722 if(size != rows_number)
01723 {
01724 std::cerr << "Flood Error: Matrix Template. " << std::endl
01725 << "set_column(int, const Vector<Type>&) const." << std::endl
01726 << "Size must be equal to number of rows." << std::endl;
01727
01728 exit(1);
01729 }
01730
01731 #endif
01732
01733
01734
01735 for(int i = 0; i < rows_number; i++)
01736 {
01737 data[i][column_index] = new_column[i];
01738 }
01739 }
01740
01741
01742
01743
01748
01749 template <class Type>
01750 void Matrix<Type>::add_row(const Vector<Type>& new_row)
01751 {
01752 #ifdef _DEBUG
01753
01754 int size = new_row.get_size();
01755
01756 if(size != columns_number)
01757 {
01758 std::cerr << "Flood Error: Matrix Template. " << std::endl
01759 << "add_row(const Vector<Type>&) const." << std::endl
01760 << "Size must be equal to number of columns." << std::endl;
01761
01762 exit(1);
01763 }
01764
01765 #endif
01766
01767 resize(rows_number+1, columns_number);
01768
01769 set_row(rows_number-1, new_row);
01770 }
01771
01772
01773
01774
01779
01780 template <class Type>
01781 void Matrix<Type>::add_column(const Vector<Type>& new_column)
01782 {
01783 #ifdef _DEBUG
01784
01785 int size = new_column.get_size();
01786
01787 if(size != columns_number)
01788 {
01789 std::cerr << "Flood Error: Matrix Template. " << std::endl
01790 << "add_column(const Vector<Type>&) const." << std::endl
01791 << "Size must be equal to number of columns." << std::endl;
01792
01793 exit(1);
01794 }
01795
01796 #endif
01797
01798 resize(rows_number, columns_number+1);
01799
01800 set_column(columns_number-1, new_column);
01801 }
01802
01803
01804
01805
01809
01810 template <class Type>
01811 void Matrix<Type>::subtract_row(int row_index)
01812 {
01813 #ifdef _DEBUG
01814
01815 if(row_index < 0)
01816 {
01817 std::cerr << "Flood Error: Matrix Template. " << std::endl
01818 << "subtract_row(int) const." << std::endl
01819 << "Index of row must be equal or greater than zero." << std::endl;
01820
01821 exit(1);
01822 }
01823 else if(row_index >= rows_number)
01824 {
01825 std::cerr << "Flood Error: Matrix Template. " << std::endl
01826 << "subtract_row(int) const." << std::endl
01827 << "Index of row must be less than number of rows." << std::endl;
01828
01829 exit(1);
01830 }
01831 else if(rows_number < 2)
01832 {
01833 std::cerr << "Flood Error: Matrix Template. " << std::endl
01834 << "subtract_row(int) const." << std::endl
01835 << "Number of rows must be equal or greater than two." << std::endl;
01836
01837 exit(1);
01838 }
01839
01840 #endif
01841
01842 Matrix<Type> new_matrix(rows_number-1, columns_number);
01843
01844 for(int i = 0; i < row_index; i++)
01845 {
01846 for(int j = 0; j < columns_number; j++)
01847 {
01848 new_matrix[i][j] = data[i][j];
01849 }
01850 }
01851
01852 for(int i = row_index+1; i < rows_number; i++)
01853 {
01854 for(int j = 0; j < columns_number; j++)
01855 {
01856 new_matrix[i-1][j] = data[i][j];
01857 }
01858 }
01859
01860 *this = new_matrix;
01861 }
01862
01863
01864
01865
01869
01870 template <class Type>
01871 void Matrix<Type>::subtract_column(int column_index)
01872 {
01873 #ifdef _DEBUG
01874
01875 if(column_index < 0)
01876 {
01877 std::cerr << "Flood Error: Matrix Template. " << std::endl
01878 << "subtract_column(int) const." << std::endl
01879 << "Index of column must be equal or greater than zero." << std::endl;
01880
01881 exit(1);
01882 }
01883 else if(column_index >= columns_number)
01884 {
01885 std::cerr << "Flood Error: Matrix Template. " << std::endl
01886 << "subtract_column(int) const." << std::endl
01887 << "Index of column must be less than number of columns." << std::endl;
01888
01889 exit(1);
01890 }
01891 else if(columns_number < 2)
01892 {
01893 std::cerr << "Flood Error: Matrix Template. " << std::endl
01894 << "subtract_column(int) const." << std::endl
01895 << "Number of columns must be equal or greater than two." << std::endl;
01896
01897 exit(1);
01898 }
01899
01900 #endif
01901
01902 Matrix<Type> new_matrix(rows_number, columns_number-1);
01903
01904 for(int i = 0; i < rows_number; i++)
01905 {
01906 for(int j = 0; j < column_index; j++)
01907 {
01908 new_matrix[i][j] = data[i][j];
01909 }
01910 }
01911
01912 for(int i = 0; i < rows_number; i++)
01913 {
01914 for(int j = column_index+1; j < columns_number; j++)
01915 {
01916 new_matrix[i][j-1] = data[i][j];
01917 }
01918 }
01919
01920 *this = new_matrix;
01921 }
01922
01923
01924
01925
01928
01929 template <class Type>
01930 void Matrix<Type>::initialize(const Type& value) const
01931 {
01932 for(int i = 0; i < rows_number; i++)
01933 {
01934 for(int j = 0; j < columns_number; j++)
01935 {
01936 data[i][j] = value;
01937 }
01938 }
01939 }
01940
01941
01942
01943
01945
01946 template <class Type>
01947 void Matrix<Type>::initialize_uniform(void)
01948 {
01949 for(int i = 0; i < rows_number; i++)
01950 {
01951 for(int j = 0; j < columns_number; j++)
01952 {
01953 data[i][j] = calculate_random_uniform(-1.0, 1.0);
01954 }
01955 }
01956 }
01957
01958
01959
01960
01965
01966 template <class Type>
01967 void Matrix<Type>::initialize_uniform(double minimum, double maximum)
01968 {
01969
01970
01971 #ifdef _DEBUG
01972
01973 if(minimum > maximum)
01974 {
01975 std::cerr << "Flood Error: Matrix Template." << std::endl
01976 << "void initialize_uniform(double, double) method." << std::endl
01977 << "Minimum value must be less or equal than maximum value." << std::endl;
01978
01979 exit(1);
01980 }
01981
01982 #endif
01983
01984 for(int i = 0; i < rows_number; i++)
01985 {
01986 for(int j = 0; j < columns_number; j++)
01987 {
01988 data[i][j] = calculate_random_uniform(minimum, maximum);
01989 }
01990 }
01991 }
01992
01993
01994
01995
02000
02001 template <class Type>
02002 void Matrix<Type>::initialize_uniform(const Matrix<double>& minimum, const Matrix<double>& maximum)
02003 {
02004
02005
02006 #ifdef _DEBUG
02007
02008 if(minimum > maximum)
02009 {
02010 std::cerr << "Flood Error: Matrix Template." << std::endl
02011 << "void initialize_uniform(const Matrix<double>&, const Matrix<double>&) method."
02012 << std::endl
02013 << "Minimum values must be less or equal than their respective maximum values." << std::endl;
02014
02015 exit(1);
02016 }
02017
02018 #endif
02019
02020 for(int i = 0; i < rows_number; i++)
02021 {
02022 for(int j = 0; j < columns_number; j++)
02023 {
02024 data[i][j] = calculate_random_uniform(minimum[i][j], maximum[i][j]);
02025 }
02026 }
02027 }
02028
02029
02030
02031
02034
02035 template <class Type>
02036 void Matrix<Type>::initialize_normal(void)
02037 {
02038 for(int i = 0; i < rows_number; i++)
02039 {
02040 for(int j = 0; j < columns_number; j++)
02041 {
02042 data[i][j] = calculate_random_normal(0.0, 1.0);
02043 }
02044 }
02045 }
02046
02047
02048
02049
02054
02055 template <class Type>
02056 void Matrix<Type>::initialize_normal(double mean, double standard_deviation)
02057 {
02058
02059
02060 #ifdef _DEBUG
02061
02062 if(standard_deviation < 0.0)
02063 {
02064 std::cerr << "Flood Error: Matrix Template." << std::endl
02065 << "void initialize_normal(double, double) method." << std::endl
02066 << "Standard deviation must be equal or greater than zero." << std::endl;
02067
02068 exit(1);
02069 }
02070
02071 #endif
02072
02073 for(int i = 0; i < rows_number; i++)
02074 {
02075 for(int j = 0; j < columns_number; j++)
02076 {
02077 data[i][j] = calculate_random_normal(mean, standard_deviation);
02078 }
02079 }
02080 }
02081
02082
02083
02084
02089
02090 template <class Type>
02091 void Matrix<Type>::initialize_normal(const Matrix<double>& mean, const Matrix<double>& standard_deviation)
02092 {
02093
02094
02095 #ifdef _DEBUG
02096
02097 if(standard_deviation < 0.0)
02098 {
02099 std::cerr << "Flood Error: Matrix Template." << std::endl
02100 << "void initialize_normal(const Matrix<double>&, const Matrix<double>&) method."
02101 << std::endl
02102 << "Standard deviations must be equal or greater than zero." << std::endl;
02103
02104 exit(1);
02105 }
02106
02107 #endif
02108
02109 for(int i = 0; i < rows_number; i++)
02110 {
02111 for(int j = 0; j < columns_number; j++)
02112 {
02113 data[i][j] = calculate_random_uniform(mean[i][j], standard_deviation[i][j]);
02114 }
02115 }
02116 }
02117
02118
02119
02120
02123
02124 template <class Type>
02125 void Matrix<Type>::set_to_identity(void) const
02126 {
02127
02128
02129 #ifdef _DEBUG
02130
02131 if(rows_number != columns_number)
02132 {
02133 std::cout << "Flood Error: Matrix Template." << std::endl
02134 << "set_to_identity(void) const method." << std::endl
02135 << "Matrix must be square." << std::endl;
02136
02137 exit(1);
02138 }
02139
02140 #endif
02141
02142 for(int i = 0; i < rows_number; i++)
02143 {
02144 for(int j = 0; j < columns_number; j++)
02145 {
02146 if(i==j)
02147 {
02148 data[i][j] = 1;
02149 }
02150 else
02151 {
02152 data[i][j] = 0;
02153 }
02154 }
02155 }
02156 }
02157
02158
02159
02160
02164
02165 template <class Type>
02166 Vector< Vector<double> > Matrix<Type>::calculate_mean_standard_deviation(void) const
02167 {
02168
02169
02170 #ifdef _DEBUG
02171
02172 if(rows_number == 0)
02173 {
02174 std::cerr << "Flood Error: Matrix template." << std::endl
02175 << "Vector<double> calculate_mean_standard_deviation(void) const method." << std::endl
02176 << "Number of rows must be greater than one." << std::endl;
02177
02178 exit(1);
02179 }
02180
02181 #endif
02182
02183
02184
02185 Vector<double> mean(columns_number, 0.0);
02186
02187 for(int j = 0; j < columns_number; j++)
02188 {
02189 mean[j] = 0.0;
02190
02191 for(int i = 0; i < rows_number; i++)
02192 {
02193 mean[j] += data[i][j];
02194 }
02195
02196 mean[j] /= (double)rows_number;
02197 }
02198
02199
02200
02201 Vector<double> standard_deviation(columns_number, 0.0);
02202
02203 for(int j = 0; j < columns_number; j++)
02204 {
02205 standard_deviation[j] = 0.0;
02206
02207 for(int i = 0; i < rows_number; i++)
02208 {
02209 standard_deviation[j] += (data[i][j] - mean[j])*(data[i][j] - mean[j]);
02210 }
02211
02212 standard_deviation[j] = sqrt(standard_deviation[j]/(rows_number-1.0));
02213 }
02214
02215
02216
02217 Vector< Vector<double> > mean_standard_deviation(2);
02218
02219 mean_standard_deviation[0] = mean;
02220 mean_standard_deviation[1] = standard_deviation;
02221
02222 return(mean_standard_deviation);
02223 }
02224
02225
02226
02227
02232
02233 template <class Type>
02234 Vector< Vector<double> > Matrix<Type>::calculate_mean_standard_deviation(const Vector<int>& column_indices) const
02235 {
02236 int column_indices_size = column_indices.get_size();
02237
02238
02239
02240 #ifdef _DEBUG
02241
02242 if(column_indices_size < 0)
02243 {
02244 std::cerr << "Flood Error: Matrix template." << std::endl
02245 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&) const method."
02246 << std::endl
02247 << "Size of column indices must be equal or greater than zero." << std::endl;
02248
02249 exit(1);
02250 }
02251
02252 #endif
02253
02254 int column_index;
02255
02256
02257
02258 Vector<double> mean(column_indices_size, 0.0);
02259
02260 for(int j = 0; j < column_indices_size; j++)
02261 {
02262 column_index = column_indices[j];
02263
02264 mean[j] = 0.0;
02265
02266 for(int i = 0; i < rows_number; i++)
02267 {
02268 mean[j] += data[i][column_index];
02269 }
02270
02271 mean[j] /= (double)rows_number;
02272 }
02273
02274
02275
02276 Vector<double> standard_deviation(column_indices_size, 0.0);
02277
02278 for(int j = 0; j < column_indices_size; j++)
02279 {
02280 column_index = column_indices[j];
02281
02282 standard_deviation[j] = 0.0;
02283
02284 for(int i = 0; i < rows_number; i++)
02285 {
02286 standard_deviation[j] += (data[i][column_index] - mean[j])*(data[i][column_index] - mean[j]);
02287 }
02288
02289 standard_deviation[j] = sqrt(standard_deviation[j]/(rows_number-1.0));
02290 }
02291
02292
02293
02294 Vector< Vector<double> > mean_standard_deviation(2);
02295
02296 mean_standard_deviation[0] = mean;
02297 mean_standard_deviation[1] = standard_deviation;
02298
02299 return(mean_standard_deviation);
02300 }
02301
02302
02303
02304
02310
02311 template <class Type>
02312 Vector< Vector<double> > Matrix<Type>::
02313 calculate_mean_standard_deviation(const Vector<int>& row_indices, const Vector<int>& column_indices) const
02314 {
02315 int row_indices_size = row_indices.get_size();
02316 int column_indices_size = column_indices.get_size();
02317
02318
02319
02320 #ifdef _DEBUG
02321
02322
02323
02324 if(row_indices_size > rows_number)
02325 {
02326 std::cerr << "Flood Error: Matrix template." << std::endl
02327 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const method." << std::endl
02328 << "Row indices size must be equal or less than rows number." << std::endl;
02329
02330 exit(1);
02331 }
02332
02333 for(int i = 0; i < row_indices_size; i++)
02334 {
02335 if(row_indices[i] < 0)
02336 {
02337 std::cerr << "Flood Error: Matrix template." << std::endl
02338 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const method." << std::endl
02339 << "Row index " << i << " must be equal or greater than zero." << std::endl;
02340
02341 exit(1);
02342 }
02343 if(row_indices[i] >= rows_number)
02344 {
02345 std::cerr << "Flood Error: Matrix template." << std::endl
02346 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const method." << std::endl
02347 << "Row index " << i << " must be less than rows number." << std::endl;
02348
02349 exit(1);
02350 }
02351 }
02352
02353 if(row_indices_size == 0)
02354 {
02355 std::cerr << "Flood Error: Matrix template." << std::endl
02356 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const method." << std::endl
02357 << "Size of row indices must be greater than zero." << std::endl;
02358
02359 exit(1);
02360 }
02361
02362
02363
02364 if(column_indices_size > columns_number)
02365 {
02366 std::cerr << "Flood Error: Matrix template." << std::endl
02367 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const method." << std::endl
02368 << "Column indices size must be equal or less than columns number." << std::endl;
02369
02370 exit(1);
02371 }
02372
02373 for(int i = 0; i < column_indices_size; i++)
02374 {
02375 if(column_indices[i] < 0)
02376 {
02377 std::cerr << "Flood Error: Matrix template." << std::endl
02378 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const method." << std::endl
02379 << "Column index " << i << " must be equal or greater than zero." << std::endl;
02380
02381 exit(1);
02382 }
02383 if(column_indices[i] >= columns_number)
02384 {
02385 std::cerr << "Flood Error: Matrix template." << std::endl
02386 << "Vector<double> calculate_mean_standard_deviation(const Vector<int>&, const Vector<int>&) const method." << std::endl
02387 << "Column index " << i << " must be less than columns number." << std::endl;
02388
02389 exit(1);
02390 }
02391 }
02392
02393 #endif
02394
02395 int row_index;
02396 int column_index;
02397
02398
02399
02400 Vector<double> mean(column_indices_size, 0.0);
02401
02402 for(int j = 0; j < column_indices_size; j++)
02403 {
02404 column_index = column_indices[j];
02405
02406 mean[j] = 0.0;
02407
02408 for(int i = 0; i < row_indices_size; i++)
02409 {
02410 row_index = row_indices[i];
02411
02412 mean[j] += data[row_index][column_index];
02413 }
02414
02415 mean[j] /= (double)rows_number;
02416 }
02417
02418
02419
02420 Vector<double> standard_deviation(column_indices_size, 0.0);
02421
02422 for(int j = 0; j < column_indices_size; j++)
02423 {
02424 column_index = column_indices[j];
02425
02426 standard_deviation[j] = 0.0;
02427
02428 for(int i = 0; i < row_indices_size; i++)
02429 {
02430 row_index = row_indices[i];
02431
02432 standard_deviation[j] += (data[row_index][column_index] - mean[j])*(data[row_index][column_index] - mean[j]);
02433 }
02434
02435 standard_deviation[j] = sqrt(standard_deviation[j]/(rows_number-1.0));
02436 }
02437
02438
02439
02440 Vector< Vector<double> > mean_standard_deviation(2);
02441
02442 mean_standard_deviation[0] = mean;
02443 mean_standard_deviation[1] = standard_deviation;
02444
02445 return(mean_standard_deviation);
02446 }
02447
02448
02449
02450
02454
02455 template <class Type>
02456 Vector< Vector<Type> > Matrix<Type>::calculate_minimum_maximum(void) const
02457 {
02458 Vector< Vector<Type> > minimum_maximum(2);
02459
02460 Vector<Type> minimum(columns_number, 1.0e99);
02461 Vector<Type> maximum(columns_number, -1.0e99);
02462
02463 for(int j = 0; j < columns_number; j++)
02464 {
02465 for(int i = 0; i < rows_number; i++)
02466 {
02467 if(data[i][j] < minimum[j])
02468 {
02469 minimum[j] = data[i][j];
02470 }
02471
02472 if(data[i][j] > maximum[j])
02473 {
02474 maximum[j] = data[i][j];
02475 }
02476 }
02477 }
02478
02479
02480
02481 minimum_maximum[0] = minimum;
02482 minimum_maximum[1] = maximum;
02483
02484 return(minimum_maximum);
02485 }
02486
02487
02488
02489
02494
02495 template <class Type>
02496 Vector< Vector<Type> > Matrix<Type>::calculate_minimum_maximum(const Vector<int>& column_indices) const
02497 {
02498 int size = column_indices.get_size();
02499
02500 #ifdef _DEBUG
02501
02502 for(int i = 0; i < size; i++)
02503 {
02504 if(column_indices[i] < 0)
02505 {
02506 std::cerr << "Flood Error: Matrix template."
02507 << "Vector<Type> calculate_minimum_maximum(const Vector<int>&) const method." << std::endl
02508 << "Index of column must be equal or greater than zero." << std::endl;
02509
02510 exit(1);
02511 }
02512 else if(column_indices[i] >= columns_number)
02513 {
02514 std::cerr << "Flood Error: Matrix template."
02515 << "Vector<Type> calculate_minimum_maximum(const Vector<int>&) const method." << std::endl
02516 << "Index of column must be less than number of columns." << std::endl;
02517
02518 exit(1);
02519 }
02520 }
02521
02522 #endif
02523
02524 int column_index;
02525
02526 Vector<Type> minimum(size, 1.0e99);
02527 Vector<Type> maximum(size, -1.0e99);
02528
02529 for(int j = 0; j < size; j++)
02530 {
02531 column_index = column_indices[j];
02532
02533 for(int i = 0; i < rows_number; i++)
02534 {
02535 if(data[i][column_index] < minimum[j])
02536 {
02537 minimum[j] = data[i][column_index];
02538 }
02539
02540 if(data[i][column_index] > maximum[j])
02541 {
02542 maximum[j] = data[i][column_index];
02543 }
02544 }
02545 }
02546
02547
02548
02549 Vector< Vector<Type> > minimum_maximum(2);
02550
02551 minimum_maximum[0] = minimum;
02552 minimum_maximum[1] = maximum;
02553
02554 return(minimum_maximum);
02555 }
02556
02557
02558
02559
02565
02566 template <class Type>
02567 Vector< Vector<Type> > Matrix<Type>::
02568 calculate_minimum_maximum(const Vector<int>& row_indices, const Vector<int>& column_indices) const
02569 {
02570 int row_indices_size = row_indices.get_size();
02571 int column_indices_size = column_indices.get_size();
02572
02573 Vector<Type> minimum(column_indices_size, 1.0e99);
02574 Vector<Type> maximum(column_indices_size, -1.0e99);
02575
02576 int row_index;
02577 int column_index;
02578
02579 for(int j = 0; j < column_indices_size; j++)
02580 {
02581 column_index = column_indices[j];
02582
02583 for(int i = 0; i < row_indices_size; i++)
02584 {
02585 row_index = row_indices[i];
02586
02587 if(data[row_index][column_index] < minimum[j])
02588 {
02589 minimum[j] = data[row_index][column_index];
02590 }
02591
02592 if(data[row_index][column_index] > maximum[j])
02593 {
02594 maximum[j] = data[row_index][column_index];
02595 }
02596 }
02597 }
02598
02599
02600
02601 Vector< Vector<Type> > minimum_maximum(2);
02602
02603 minimum_maximum[0] = minimum;
02604 minimum_maximum[1] = maximum;
02605
02606 return(minimum_maximum);
02607 }
02608
02609
02610
02611
02617
02618 template <class Type>
02619 void Matrix<Type>::scale_mean_standard_deviation(const Vector<double>& mean, const Vector<double>& standard_deviation)
02620 {
02621 #ifdef _DEBUG
02622
02623 int mean_size = mean.get_size();
02624
02625 if(mean_size != columns_number)
02626 {
02627 std::cerr << "Flood Error: Matrix template."
02628 << "void scale_mean_standard_deviation(const Vector<double>&, const Vector<double>&) method." << std::endl
02629 << "Size of mean vector must be equal to number of columns." << std::endl;
02630
02631 exit(1);
02632 }
02633
02634 int standard_deviation_size = standard_deviation.get_size();
02635
02636 if(standard_deviation_size != columns_number)
02637 {
02638 std::cerr << "Flood Error: Matrix template."
02639 << "void scale_mean_standard_deviation(const Vector<double>&, const Vector<double>&) method." << std::endl
02640 << "Size of standard deviation vector must be equal to number of columns." << std::endl;
02641
02642 exit(1);
02643 }
02644
02645 #endif
02646
02647
02648
02649 for(int j = 0; j < columns_number; j++)
02650 {
02651 if(standard_deviation[j] < 1e-99)
02652 {
02653 if(display)
02654 {
02655 std::cout << "Flood Warning: Matrix class." << std::endl
02656 << "void scale_mean_standard_deviation(const Vector<double>&, const Vector<double>&) method." << std::endl
02657 << "Standard deviation of column " << j << " is zero." << std::endl
02658 << "Those values won't be scaled." << std::endl;
02659 }
02660
02661
02662 }
02663 else
02664 {
02665 for(int i = 0; i < rows_number; i++)
02666 {
02667 data[i][j] = (data[i][j] - mean[j])/standard_deviation[j];
02668 }
02669 }
02670 }
02671 }
02672
02673
02674
02675
02681
02682 template <class Type>
02683 void Matrix<Type>::scale_minimum_maximum(const Vector<double>& minimum, const Vector<double>& maximum)
02684 {
02685 #ifdef _DEBUG
02686
02687 int minimum_size = minimum.get_size();
02688
02689 if(minimum_size != columns_number)
02690 {
02691 std::cerr << "Flood Error: Matrix template."
02692 << "void scale_minimum_maximum(const Vector<double>&, const Vector<double>&) method." << std::endl
02693 << "Size of minimum vector must be equal to number of columns." << std::endl;
02694
02695 exit(1);
02696 }
02697
02698 int maximum_size = maximum.get_size();
02699
02700 if(maximum_size != columns_number)
02701 {
02702 std::cerr << "Flood Error: Matrix template."
02703 << "void scale_minimum_maximum(const Vector<double>&, const Vector<double>&) method." << std::endl
02704 << "Size of maximum vector must be equal to number of columns." << std::endl;
02705
02706 exit(1);
02707 }
02708
02709 #endif
02710
02711
02712
02713 for(int j = 0; j < columns_number; j++)
02714 {
02715 if(maximum[j] - minimum[j] < 1e-99)
02716 {
02717 if(display)
02718 {
02719 std::cout << "Flood Warning: Matrix class." << std::endl
02720 << "void scale_minimum_maximum(const Vector<double>&, const Vector<double>&) method." << std::endl
02721 << "Minimum and maximum values of column " << j << " are equal." << std::endl
02722 << "Those values won't be scaled." << std::endl;
02723 }
02724
02725
02726 }
02727 else
02728 {
02729 for(int i = 0; i < rows_number; i++)
02730 {
02731 data[i][j] = 2.0*(data[i][j] - minimum[j])/(maximum[j]-minimum[j])-1.0;
02732 }
02733 }
02734 }
02735 }
02736
02737
02738
02739
02745
02746 template <class Type>
02747 void Matrix<Type>::unscale_mean_standard_deviation(const Vector<double>& mean, const Vector<double>& standard_deviation)
02748 {
02749 #ifdef _DEBUG
02750
02751 int mean_size = mean.get_size();
02752
02753 if(mean_size != columns_number)
02754 {
02755 std::cerr << "Flood Error: Matrix template."
02756 << "void unscale_mean_standard_deviation(const Vector<double>&, const Vector<double>&) method." << std::endl
02757 << "Size of mean vector must be equal to number of columns." << std::endl;
02758
02759 exit(1);
02760 }
02761
02762 int standard_deviation_size = standard_deviation.get_size();
02763
02764 if(standard_deviation_size != columns_number)
02765 {
02766 std::cerr << "Flood Error: Matrix template." << std::endl
02767 << "void unscale_mean_standard_deviation(const Vector<double>&, const Vector<double>&) method." << std::endl
02768 << "Size of standard deviation vector must be equal to number of columns." << std::endl;
02769
02770 exit(1);
02771 }
02772
02773 #endif
02774
02775 for(int j = 0; j < columns_number; j++)
02776 {
02777 if(standard_deviation[j] < 1e-99)
02778 {
02779 if(display)
02780 {
02781 std::cout << "Flood Warning: Matrix template." << std::endl
02782 << "void unscale_mean_standard_deviation(const Vector<double>&, const Vector<double>&) method." << std::endl
02783 << "Standard deviation of column variable " << j << " is zero." << std::endl
02784 << "Those columns won't be scaled." << std::endl;
02785 }
02786
02787
02788 }
02789 else
02790 {
02791 for(int i = 0; i < rows_number; i++)
02792 {
02793 data[i][j] = data[i][j]*standard_deviation[j] + mean[j];
02794 }
02795 }
02796 }
02797 }
02798
02799
02800
02801
02807
02808 template <class Type>
02809 void Matrix<Type>::unscale_minimum_maximum(const Vector<double>& minimum, const Vector<double>& maximum)
02810 {
02811 #ifdef _DEBUG
02812
02813 int minimum_size = minimum.get_size();
02814
02815 if(minimum_size != columns_number)
02816 {
02817 std::cerr << "Flood Error: Matrix template."
02818 << "void unscale_minimum_maximum(const Vector<double>&, const Vector<double>&) method." << std::endl
02819 << "Size of minimum vector must be equal to number of columns." << std::endl;
02820
02821 exit(1);
02822 }
02823
02824 int maximum_size = maximum.get_size();
02825
02826 if(maximum_size != columns_number)
02827 {
02828 std::cerr << "Flood Error: Matrix template."
02829 << "void unscale_minimum_maximum(const Vector<double>&, const Vector<double>&) method." << std::endl
02830 << "Size of maximum vector must be equal to number of columns." << std::endl;
02831
02832 exit(1);
02833 }
02834
02835 #endif
02836
02837 for(int j = 0; j < columns_number; j++)
02838 {
02839 if(maximum[j] - minimum[j] < 1e-99)
02840 {
02841 if(display)
02842 {
02843 std::cout << "Flood Warning: Matrix template." << std::endl
02844 << "void unscale_minimum_maximum(const Vector<double>&, const Vector<double>&) method." << std::endl
02845 << "Minimum and maximum values of column " << j << " are equal." << std::endl
02846 << "Those columns won't be unscaled." << std::endl;
02847 }
02848
02849
02850 }
02851 else
02852 {
02853 for(int i = 0; i < rows_number; i++)
02854 {
02855 data[i][j] = 0.5*(data[i][j] + 1.0)*(maximum[j]-minimum[j]) + minimum[j];
02856 }
02857 }
02858 }
02859 }
02860
02861
02862
02863
02865
02866 template <class Type>
02867 Matrix<Type> Matrix<Type>::calculate_transpose(void) const
02868 {
02869 Matrix<Type> transpose(columns_number, rows_number);
02870
02871 for(int i = 0; i < columns_number; i++)
02872 {
02873 for(int j = 0; j < rows_number; j++)
02874 {
02875 transpose[i][j] = data[j][i];
02876 }
02877 }
02878
02879 return(transpose);
02880 }
02881
02882
02883
02884
02886
02887 template <class Type>
02888 Type Matrix<Type>::calculate_determinant(void) const
02889 {
02890
02891
02892 #ifdef _DEBUG
02893
02894 if(rows_number != columns_number)
02895 {
02896 std::cerr << "Flood Error: Matrix Template." << std::endl
02897 << "calculate_determinant(void) const method." << std::endl
02898 << "Matrix must be square." << std::endl;
02899
02900 exit(1);
02901 }
02902
02903 #endif
02904
02905 Type determinant = 0;
02906
02907 if(rows_number == 0)
02908 {
02909 std::cerr << "Flood Error: Matrix Template." << std::endl
02910 << "calculate_determinant(void) const method." << std::endl
02911 << "Size of matrix is zero." << std::endl;
02912
02913 exit(1);
02914 }
02915 else if(rows_number == 1)
02916 {
02917 determinant = data[0][0];
02918 }
02919 else if(rows_number == 2)
02920 {
02921 determinant = data[0][0]*data[1][1] - data[1][0]*data[0][1];
02922 }
02923 else
02924 {
02925 int sign;
02926
02927 for(int row_index = 0; row_index < rows_number; row_index++)
02928 {
02929
02930
02931 Matrix<Type> sub_matrix(rows_number-1, columns_number-1);
02932
02933 for(int i = 1; i < rows_number; i++)
02934 {
02935 int j2 = 0;
02936
02937 for(int j = 0; j < columns_number; j++)
02938 {
02939 if(j == row_index)
02940 {
02941 continue;
02942 }
02943
02944 sub_matrix[i-1][j2] = data[i][j];
02945
02946 j2++;
02947 }
02948 }
02949
02950 sign = (int)(pow(-1.0, row_index+2.0));
02951
02952 determinant += sign*data[0][row_index]*sub_matrix.calculate_determinant();
02953 }
02954 }
02955
02956 return(determinant);
02957 }
02958
02959
02960
02961
02963
02964 template <class Type>
02965 Matrix<Type> Matrix<Type>::calculate_cofactor(void) const
02966 {
02967 Matrix<double> cofactor(rows_number, columns_number);
02968
02969 Matrix<double> c(rows_number-1, columns_number-1);
02970
02971 for(int j = 0; j < rows_number; j++)
02972 {
02973 for(int i = 0; i < rows_number; i++)
02974 {
02975
02976
02977 int i1 = 0;
02978
02979 for(int ii = 0; ii < rows_number; ii++)
02980 {
02981 if(ii == i)
02982 {
02983 continue;
02984 }
02985
02986 int j1 = 0;
02987
02988 for(int jj = 0; jj < rows_number; jj++)
02989 {
02990 if(jj == j)
02991 {
02992 continue;
02993 }
02994
02995 c[i1][j1] = data[ii][jj];
02996 j1++;
02997 }
02998 i1++;
02999 }
03000
03001 double determinant = c.calculate_determinant();
03002
03003 cofactor[i][j] = pow(-1.0, i+j+2.0)*determinant;
03004 }
03005 }
03006
03007 return(cofactor);
03008 }
03009
03010
03011
03012
03015
03016 template <class Type>
03017 Matrix<Type> Matrix<Type>::calculate_inverse(void) const
03018 {
03019
03020
03021 #ifdef _DEBUG
03022
03023 if(rows_number != columns_number)
03024 {
03025 std::cerr << "Flood Error: Matrix Template." << std::endl
03026 << "calculate_inverse(void) const method." << std::endl
03027 << "Matrix must be square." << std::endl;
03028
03029 exit(1);
03030 }
03031
03032 #endif
03033
03034 double determinant = calculate_determinant();
03035
03036 if(determinant == 0.0)
03037 {
03038 std::cerr << "Flood Error: Matrix Template." << std::endl
03039 << "calculate_inverse(void) const method." << std::endl
03040 << "Matrix is singular." << std::endl;
03041
03042 exit(1);
03043 }
03044
03045
03046
03047 Matrix<double> cofactor = calculate_cofactor();
03048
03049
03050
03051 Matrix<double> adjoint = cofactor.calculate_transpose();
03052
03053
03054
03055 Matrix<double> inverse = adjoint/determinant;
03056
03057 return(inverse);
03058 }
03059
03060
03061
03062
03065
03066 template <typename Type>
03067 Matrix<Type> Matrix<Type>::operator + (const Type& scalar) const
03068 {
03069 Matrix<Type> sum(rows_number, columns_number);
03070
03071 for(int i = 0; i < rows_number; i++)
03072 {
03073 for(int j = 0; j < columns_number; j++)
03074 {
03075 sum[i][j] = data[i][j] + scalar;
03076 }
03077 }
03078
03079 return(sum);
03080 }
03081
03082
03083
03084
03087
03088 template <typename Type>
03089 Matrix<Type> Matrix<Type>::operator + (const Matrix<Type>& other_matrix) const
03090 {
03091
03092
03093 #ifdef _DEBUG
03094
03095 int other_rows_number = other_matrix.get_rows_number();
03096 int other_columns_number = other_matrix.get_columns_number();
03097
03098 if(other_rows_number != rows_number || other_columns_number != columns_number)
03099 {
03100 std::cerr << "Flood Error: Matrix Template." << std::endl
03101 << "Matrix<Type> operator + (const Matrix<Type>&) const." << std::endl
03102 << "Both matrix sizes must be the same." << std::endl;
03103
03104 exit(1);
03105 }
03106
03107 #endif
03108
03109 Matrix<Type> sum(rows_number, columns_number);
03110
03111 for(int i = 0; i < rows_number; i++)
03112 {
03113 for(int j = 0; j < columns_number; j++)
03114 {
03115 sum[i][j] = data[i][j] + other_matrix[i][j];
03116 }
03117 }
03118
03119 return(sum);
03120 }
03121
03122
03123
03124
03127
03128 template <typename Type>
03129 Matrix<Type> Matrix<Type>::operator - (const Type& scalar) const
03130 {
03131 Matrix<Type> difference(rows_number, columns_number);
03132
03133 for(int i = 0; i < rows_number; i++)
03134 {
03135 for(int j = 0; j < columns_number; j++)
03136 {
03137 difference[i][j] = data[i][j] - scalar;
03138 }
03139 }
03140
03141 return(difference);
03142 }
03143
03144
03145
03146
03149
03150 template <typename Type>
03151 Matrix<Type> Matrix<Type>::operator - (const Matrix<Type>& other_matrix) const
03152 {
03153
03154
03155 #ifdef _DEBUG
03156
03157 int other_rows_number = other_matrix.get_rows_number();
03158 int other_columns_number = other_matrix.get_columns_number();
03159
03160 if(other_rows_number != rows_number || other_columns_number != columns_number)
03161 {
03162 std::cerr << "Flood Error: Matrix Template." << std::endl
03163 << "Matrix<Type> operator - (const Matrix<Type>&) const method." << std::endl
03164 << "Both matrix sizes must be the same." << std::endl;
03165
03166 exit(1);
03167 }
03168
03169 #endif
03170
03171 Matrix<Type> difference(rows_number, columns_number);
03172
03173 for(int i = 0; i < rows_number; i++)
03174 {
03175 for(int j = 0; j < columns_number; j++)
03176 {
03177 difference[i][j] = data[i][j] - other_matrix[i][j];
03178 }
03179 }
03180
03181 return(difference);
03182 }
03183
03184
03185
03186
03189
03190 template <typename Type>
03191 Matrix<Type> Matrix<Type>::operator * (const Type& scalar) const
03192 {
03193 Matrix<Type> product(rows_number, columns_number);
03194
03195 for(int i = 0; i < rows_number; i++)
03196 {
03197 for(int j = 0; j < columns_number; j++)
03198 {
03199 product[i][j] = data[i][j]*scalar;
03200 }
03201 }
03202
03203 return(product);
03204 }
03205
03206
03207
03208
03211
03212 template <typename Type>
03213 Matrix<Type> Matrix<Type>::operator * (const Matrix<Type>& other_matrix) const
03214 {
03215
03216
03217 #ifdef _DEBUG
03218
03219 int other_rows_number = other_matrix.get_rows_number();
03220 int other_columns_number = other_matrix.get_columns_number();
03221
03222 if(other_rows_number != rows_number || other_columns_number != columns_number)
03223 {
03224 std::cerr << "Flood Error: Matrix Template." << std::endl
03225 << "Matrix<Type> operator * (const Matrix<Type>&) const method." << std::endl
03226 << "Both matrix sizes must be the same." << std::endl;
03227
03228 exit(1);
03229 }
03230
03231 #endif
03232
03233 Matrix<Type> product(rows_number, columns_number);
03234
03235 for(int i = 0; i < rows_number; i++)
03236 {
03237 for(int j = 0; j < columns_number; j++)
03238 {
03239 product[i][j] = data[i][j]*other_matrix[i][j];
03240 }
03241 }
03242
03243 return(product);
03244 }
03245
03246
03247
03248
03251
03252 template <typename Type>
03253 Matrix<Type> Matrix<Type>::operator / (const Type& scalar) const
03254 {
03255 Matrix<Type> cocient(rows_number, columns_number);
03256
03257 for(int i = 0; i < rows_number; i++)
03258 {
03259 for(int j = 0; j < columns_number; j++)
03260 {
03261 cocient[i][j] = data[i][j]/scalar;
03262 }
03263 }
03264
03265 return(cocient);
03266 }
03267
03268
03269
03270
03273
03274 template <typename Type>
03275 Matrix<Type> Matrix<Type>::operator / (const Matrix<Type>& other_matrix) const
03276 {
03277
03278
03279 #ifdef _DEBUG
03280
03281 int other_rows_number = other_matrix.get_rows_number();
03282 int other_columns_number = other_matrix.get_columns_number();
03283
03284 if(other_rows_number != rows_number || other_columns_number != columns_number)
03285 {
03286 std::cerr << "Flood Error: Matrix Template." << std::endl
03287 << "Matrix<Type> operator / (const Matrix<Type>&) const method." << std::endl
03288 << "Both matrix sizes must be the same." << std::endl;
03289
03290 exit(1);
03291 }
03292
03293 #endif
03294
03295 Matrix<Type> cocient(rows_number, columns_number);
03296
03297 for(int i = 0; i < rows_number; i++)
03298 {
03299 for(int j = 0; j < columns_number; j++)
03300 {
03301 cocient[i][j] = data[i][j]/other_matrix[i][j];
03302 }
03303 }
03304
03305 return(cocient);
03306 }
03307
03308
03309
03310
03314
03315 template <typename Type>
03316 Vector<Type> Matrix<Type>::dot(const Vector<Type>& vector) const
03317 {
03318
03319
03320 #ifdef _DEBUG
03321
03322 int size = vector.get_size();
03323
03324 if(size != columns_number)
03325 {
03326 std::cerr << "Flood Error: Matrix Template." << std::endl
03327 << "Vector<Type> dot(const Vector<Type>&) const method." << std::endl
03328 << "Vector size must be equal to matrix number of columns." << std::endl;
03329
03330 exit(1);
03331 }
03332
03333 #endif
03334
03335
03336
03337 Vector<Type> product(rows_number);
03338
03339 for(int i = 0; i < rows_number; i++)
03340 {
03341 product[i] = 0;
03342
03343 for(int j = 0; j < columns_number; j++)
03344 {
03345 product[i] += vector[j]*data[i][j];
03346 }
03347 }
03348
03349 return(product);
03350 }
03351
03352
03353
03354
03358
03359 template <typename Type>
03360 Matrix<Type> Matrix<Type>::dot(const Matrix<Type>& other_matrix) const
03361 {
03362 int other_columns_number = other_matrix.get_columns_number();
03363
03364
03365
03366 #ifdef _DEBUG
03367
03368
03369
03370 #endif
03371
03372 Matrix<Type> product(rows_number, other_columns_number, 0.0);
03373
03374 for(int i = 0; i < rows_number; i++)
03375 {
03376 for(int j = 0; j < other_columns_number; j++)
03377 {
03378 for(int k = 0; k < columns_number; k++)
03379 {
03380 product[i][j] += data[i][k]*other_matrix[k][j];
03381 }
03382 }
03383 }
03384
03385 return(product);
03386 }
03387
03388
03389
03390
03393
03394 template <typename Type>
03395 bool Matrix<Type>::is_symmetric(void) const
03396 {
03397
03398
03399 #ifdef _DEBUG
03400
03401 if(rows_number != columns_number)
03402 {
03403 std::cerr << "Flood Error: Matrix Template." << std::endl
03404 << "bool is_symmetric(void) const method." << std::endl
03405 << "Matrix must be squared." << std::endl;
03406
03407 exit(1);
03408 }
03409
03410 #endif
03411
03412 Matrix<Type> transpose = calculate_transpose();
03413
03414 for(int i = 0; i < rows_number; i++)
03415 {
03416 for(int j = 0; j < columns_number; j++)
03417 {
03418 if(data[i][j] != transpose[i][j])
03419 {
03420 return(false);
03421 }
03422 }
03423 }
03424
03425 return(true);
03426 }
03427
03428
03429
03430
03433
03434 template <typename Type>
03435 bool Matrix<Type>::is_antisymmetric(void) const
03436 {
03437
03438
03439 #ifdef _DEBUG
03440
03441 if(rows_number != columns_number)
03442 {
03443 std::cerr << "Flood Error: Matrix Template." << std::endl
03444 << "bool is_antisymmetric(void) const method." << std::endl
03445 << "Matrix must be squared." << std::endl;
03446
03447 exit(1);
03448 }
03449
03450 #endif
03451
03452 Matrix<Type> transpose = calculate_transpose();
03453
03454 for(int i = 0; i < rows_number; i++)
03455 {
03456 for(int j = 0; j < columns_number; j++)
03457 {
03458 if(i != j && -data[i][j] != transpose[i][j])
03459 {
03460 return(false);
03461 }
03462 }
03463 }
03464
03465 return(true);
03466 }
03467
03468
03473
03474 template<typename Type>
03475 std::istream& operator >> (std::istream& is, Matrix<Type>& m)
03476 {
03477 int rows_number = m.get_rows_number();
03478 int columns_number = m.get_columns_number();
03479
03480 for(int i = 0; i < rows_number; i++)
03481 {
03482 for(int j = 0; j < columns_number; j++)
03483 {
03484 is >> m[i][j];
03485 }
03486 }
03487
03488 return(is);
03489 }
03490
03491
03492
03493
03498
03499 template<typename Type>
03500 std::ostream& operator << (std::ostream& os, Matrix<Type>& m)
03501 {
03502 int rows_number = m.get_rows_number();
03503 int columns_number = m.get_columns_number();
03504
03505 for(int i = 0; i < rows_number; i++)
03506 {
03507 for(int j = 0; j < columns_number; j++)
03508 {
03509 os << m[i][j] << " ";
03510 }
03511
03512 os << std::endl;
03513 }
03514
03515 return(os);
03516 }
03517
03518
03519
03520
03522
03523 template <class Type>
03524 std::string Matrix<Type>::to_XML(bool show_declaration)
03525 {
03526 std::stringstream buffer;
03527
03528
03529
03530 if(show_declaration)
03531 {
03532 buffer << "<Flood version='3.0' class='Matrix'>" << std::endl;
03533 }
03534
03535
03536
03537 buffer << "<RowsNumber>" << std::endl
03538 << rows_number << std::endl
03539 << "</RowsNumber>" << std::endl;
03540
03541
03542
03543 buffer << "<ColumnsNumber>" << std::endl
03544 << columns_number << std::endl
03545 << "</ColumnsNumber>" << std::endl;
03546
03547
03548
03549 buffer << "<Display>" << std::endl
03550 << display << std::endl
03551 << "</Display>" << std::endl;
03552
03553
03554
03555 buffer << "<Data>" << std::endl;
03556
03557 for(int i = 0; i < rows_number; i++)
03558 {
03559 for(int j = 0; j < columns_number; j++)
03560 {
03561 buffer << data[i][j] << " ";
03562 }
03563 buffer << std::endl;
03564 }
03565
03566 buffer << "</Data>" << std::endl;
03567
03568 return(buffer.str());
03569 }
03570
03571
03572
03573
03575
03576 template <class Type>
03577 void Matrix<Type>::print(void)
03578 {
03579 std::cout << to_XML(true);
03580 }
03581
03582
03583
03584
03586
03587 template <class Type>
03588 void Matrix<Type>::print_data(void)
03589 {
03590 for(int i = 0; i < rows_number; i++)
03591 {
03592 for(int j = 0; j < columns_number; j++)
03593 {
03594 std::cout << data[i][j] << " ";
03595 }
03596 std::cout << std::endl;
03597 }
03598 }
03599
03600
03601
03602
03606
03607 template <class Type>
03608 void Matrix<Type>::load(const char* filename)
03609 {
03610 std::fstream file;
03611
03612
03613
03614 file.open(filename, std::ios::in);
03615
03616 if(!file.is_open())
03617 {
03618 std::cerr << "Flood Error: Matrix template." << std::endl
03619 << "void load(const char*) method." << std::endl
03620 << "Cannot open matrix XML-type file." << std::endl;
03621
03622 exit(1);
03623 }
03624
03625
03626
03627 std::string line;
03628 std::string word;
03629
03630
03631
03632 getline(file, line);
03633
03634 if(line != "<Flood version='3.0' class='Matrix'>")
03635 {
03636
03637
03638
03639
03640
03641 }
03642
03643
03644
03645 file >> word;
03646
03647 if(word != "<RowsNumber>")
03648 {
03649 std::cerr << "Flood Error: Matrix template." << std::endl
03650 << "void load(const char*) method." << std::endl
03651 << "Unknown rows number begin tag: " << word << std::endl;
03652
03653 exit(1);
03654 }
03655
03656 int new_rows_number;
03657
03658 file >> new_rows_number;
03659
03660 file >> word;
03661
03662 if(word != "</RowsNumber>")
03663 {
03664 std::cerr << "Flood Error: Matrix template." << std::endl
03665 << "void load(const char*) method." << std::endl
03666 << "Unknown rows number end tag: " << word << std::endl;
03667
03668 exit(1);
03669 }
03670
03671
03672
03673 file >> word;
03674
03675 if(word != "<ColumnsNumber>")
03676 {
03677 std::cerr << "Flood Error: Matrix template." << std::endl
03678 << "void load(const char*) method." << std::endl
03679 << "Unknown columns number begin tag: " << word << std::endl;
03680
03681 exit(1);
03682 }
03683
03684 int new_columns_number;
03685
03686 file >> new_columns_number;
03687
03688 file >> word;
03689
03690 if(word != "</ColumnsNumber>")
03691 {
03692 std::cerr << "Flood Error: Matrix template." << std::endl
03693 << "void load(const char*) method." << std::endl
03694 << "Unknown columns number end tag: " << word << std::endl;
03695
03696 exit(1);
03697 }
03698
03699
03700
03701 set(new_rows_number, new_columns_number);
03702
03703 while(!file.eof())
03704 {
03705 file >> word;
03706
03707 if(word == "<Display>")
03708 {
03709 bool new_display;
03710
03711 file >> new_display;
03712
03713 file >> word;
03714
03715 if(word != "</Display>")
03716 {
03717 std::cerr << "Flood Error: Matrix template." << std::endl
03718 << "void load(const char*) method." << std::endl
03719 << "Unknown display end tag: " << word << std::endl;
03720
03721 exit(1);
03722 }
03723 }
03724 else if(word == "<Data>")
03725 {
03726 for(int i = 0; i < rows_number; i++)
03727 {
03728 for(int j = 0; j < columns_number; j++)
03729 {
03730 file >> data[i][j];
03731 }
03732 }
03733
03734 file >> word;
03735
03736 if(word != "</Data>")
03737 {
03738 std::cerr << "Flood Error: Matrix template." << std::endl
03739 << "void load(const char*) method." << std::endl
03740 << "Unknown data end tag: " << word << std::endl;
03741
03742 exit(1);
03743 }
03744 }
03745 else
03746 {
03747
03748
03749
03750
03751
03752 }
03753 }
03754
03755
03756
03757
03758
03759 file.close();
03760 }
03761
03762
03763
03764
03768
03769 template <class Type>
03770 void Matrix<Type>::load_data(const char* filename)
03771 {
03772 std::fstream file;
03773
03774
03775
03776 file.open(filename, std::ios::in);
03777
03778 if(!file.is_open())
03779 {
03780 std::cerr << "Flood Error: Matrix template." << std::endl
03781 << "void load_data(const char*) method." << std::endl
03782 << "Cannot open data data file." << std::endl;
03783
03784 exit(1);
03785 }
03786
03787 for(int i = 0; i < rows_number; i++)
03788 {
03789 for(int j = 0; j < columns_number; j++)
03790 {
03791 file >> data[i][j];
03792 }
03793 }
03794
03795
03796
03797 file.close();
03798 }
03799
03800
03801
03802
03806
03807 template <class Type>
03808 void Matrix<Type>::save(const char* filename)
03809 {
03810 std::fstream file;
03811
03812
03813
03814 file.open(filename, std::ios::out);
03815
03816 if(!file.is_open())
03817 {
03818 std::cerr << "Flood Error: Matrix template." << std::endl
03819 << "void save(const char*) method." << std::endl
03820 << "Cannot open matrix XML-type file." << std::endl;
03821
03822 exit(1);
03823 }
03824
03825
03826
03827 file << to_XML(true) << std::endl;
03828
03829
03830
03831 file.close();
03832 }
03833
03834
03835
03836
03840
03841 template <class Type>
03842 void Matrix<Type>::save_data(const char* filename)
03843 {
03844 std::fstream file;
03845
03846
03847
03848 file.open(filename, std::ios::out);
03849
03850 if(!file.is_open())
03851 {
03852 std::cerr << "Flood Error: Matrix template." << std::endl
03853 << "void save_data(const char*) method." << std::endl
03854 << "Cannot open matrix data file." << std::endl;
03855
03856 exit(1);
03857 }
03858
03859
03860
03861 for(int i = 0; i < rows_number; i++)
03862 {
03863 for(int j = 0; j < columns_number; j++)
03864 {
03865 file << data[i][j] << " ";
03866 }
03867 file << std::endl;
03868 }
03869
03870
03871
03872 file.close();
03873 }
03874
03875
03876
03877
03882
03883 template <typename Type>
03884 double Matrix<Type>::calculate_random_uniform(double minimum, double maximum)
03885 {
03886 double random = (double)rand()/(RAND_MAX+1.0);
03887
03888 double random_uniform = minimum + (maximum-minimum)*random;
03889
03890 return(random_uniform);
03891 }
03892
03893
03894
03895
03900
03901 template <typename Type>
03902 double Matrix<Type>::calculate_random_normal(double mean, double standard_deviation)
03903 {
03904 double random_normal = 0.0;
03905
03906 const double pi = 4.0*atan(1.0);
03907
03908 double random_uniform_1;
03909 double random_uniform_2;
03910
03911 do
03912 {
03913 random_uniform_1 = (double)rand()/(RAND_MAX+1.0);
03914
03915 }while(random_uniform_1 == 0.0);
03916
03917 random_uniform_2 = (double)rand()/(RAND_MAX+1.0);
03918
03919
03920
03921 random_normal = mean + sqrt(-2.0*log(random_uniform_1))*sin(2.0*pi*random_uniform_2)*standard_deviation;
03922
03923 return(random_normal);
03924 }
03925
03926
03927 }
03928
03929 #endif
03930
03931
03932
03933
03934
03935
03936
03937
03938
03939
03940
03941
03942
03943
03944
03945
03946
03947