Classes | |
| class | matrix |
| A wrapper class for GNU Scientific Library matrices. More... | |
| class | vector |
| A wrapper class for GSL vectors. More... | |
| class | vector_view |
| A vector that doesn't own its data; rather, points to data owned by another vector. More... | |
| class | slice |
| Vector slices corresponding to GNU Octave ranges. More... | |
Typedefs | |
| typedef vector | point |
| Useful alias, vectors are also points in space. | |
Functions | |
I/O | |
| std::ostream & | operator<< (std::ostream &os, const vector &v) |
| Stream insertion operator. | |
| vector | operator>> (std::istream &is, vector &v) |
| Stream extraction operator. | |
| std::ostream & | operator<< (std::ostream &os, const matrix &M) |
| Stream insertion operator. | |
| matrix | operator>> (std::istream &is, matrix &v) |
| Stream extraction operator. | |
Arithmetic | |
Some arithmetic functions for comfortable syntax. | |
| vector | operator* (double a, const vector &v) |
| Scale a vector. | |
| double | norm (const vector &v) |
| Euclidean norm of a vector. | |
| matrix | operator* (double a, const matrix &M) |
| Scale a matrix. | |
| matrix | inv (const matrix &A) |
| Matrix inverse, computed with LU factorisation. | |
| matrix | T (const matrix &A) |
| Return copy of transposed matrix. | |
| double | tr (const matrix &A) |
| Trace. | |
| double | det (matrix &A) |
| Determinant. | |
| double | cond (matrix &A) |
| L2 condition number, computed with SVD. | |
| typedef vector linalg::point |
Useful alias, vectors are also points in space.
| double linalg::cond | ( | matrix & | A | ) |
| double linalg::det | ( | matrix & | A | ) |
| matrix linalg::inv | ( | const matrix & | A | ) |
| double linalg::norm | ( | const vector & | v | ) |
| matrix linalg::operator* | ( | double | a, | |
| const matrix & | M | |||
| ) |
| vector linalg::operator* | ( | double | a, | |
| const vector & | v | |||
| ) |
| std::ostream & linalg::operator<< | ( | std::ostream & | os, | |
| const matrix & | M | |||
| ) |
Stream insertion operator.
00831 { 00832 os.setf(std::ios::scientific); 00833 os << std::setprecision(int(A.precision()) ); 00834 for(size_t i = 1; i <= A.rows(); i++){ 00835 for(size_t j = 1; j <= A.cols(); j++) 00836 os << " " << std::setw(int(A.precision()+6) ) << A(i,j) << " "; 00837 os << std::endl; 00838 } 00839 return os; 00840 }

| std::ostream & linalg::operator<< | ( | std::ostream & | os, | |
| const vector & | v | |||
| ) |
Stream insertion operator.
00769 { 00770 os.setf(std::ios::scientific); 00771 os << std::setprecision(int(v.precision()) ); 00772 for(size_t i = 1; i <= v.size(); i++){ 00773 os << " " << std::setw(int(v.precision()+6) ) << v(i) << " "; 00774 } 00775 os << std::endl; 00776 return os; 00777 }

| matrix linalg::operator>> | ( | std::istream & | is, | |
| matrix & | v | |||
| ) |
Stream extraction operator.
00842 { 00843 using namespace std; 00844 string line, token; 00845 bool rowset = false; 00846 list<double> data; 00847 00848 size_t rowsize = 0; 00849 size_t rows = 0; 00850 size_t cols = 0; 00851 while(getline(is, line)){ 00852 line = utils::trim(line); 00853 //Blank row or comment character. 00854 if(line[0] == '#' or line.length() == 0) 00855 continue; 00856 00857 stringstream ss_line; 00858 cols = 0; 00859 ss_line << line; 00860 while(ss_line >> token){ 00861 if(token[0] == '#'){ 00862 break; //Rest of line is comment. 00863 } 00864 00865 //The following may fail on a C++ implementation that doesn't 00866 //obey IEEE arithmetic (IEC 559). We could check for those, 00867 //but do we really want to compile Octave on C++ 00868 //implementations that don't follow IEEE arithmetic? 00869 else if(token == "NaN"){ 00870 double x = std::numeric_limits<double>::quiet_NaN(); 00871 data.push_back(x); 00872 cols++; 00873 } 00874 else if(token == "Inf"){ 00875 double x = std::numeric_limits<double>::infinity(); 00876 data.push_back(x); 00877 cols++; 00878 } 00879 else if(token == "-Inf"){ 00880 double x = -std::numeric_limits<double>::infinity(); 00881 data.push_back(x); 00882 cols++; 00883 } 00884 else if(token == ","){ 00885 ss_line >> token; 00886 } 00887 00888 //This also ignores commas and any other token. I think. If 00889 //there's garbage in the token, I have to see what happens 00890 //here. Do we also need to check for garbage? 00891 else{ 00892 double x; 00893 stringstream ss_token; 00894 ss_token << token; 00895 ss_token >> x; 00896 data.push_back(x); 00897 cols++; 00898 } 00899 00900 } 00901 00902 //First row gives the number of columns, and all successive rows 00903 //must have the same number of elements. 00904 if(!rowset){ 00905 rowset = true; 00906 rowsize = cols; 00907 } 00908 00909 if (cols != rowsize){ 00910 badArgument exc; 00911 exc.reason = "Cannot read matrix: bad format in input"; 00912 exc.file = __FILE__; 00913 exc.line = __LINE__; 00914 throw exc; 00915 } 00916 rows++; 00917 } 00918 if(rows == 0){ 00919 endOfFile exc; 00920 exc.reason = "Cannot read empty matrix from input."; 00921 exc.file = __FILE__; 00922 exc.line = __LINE__; 00923 throw exc; 00924 } 00925 00926 matrix M(rows,cols); 00927 typedef list<double>::iterator LI; 00928 00929 LI k = data.begin(); 00930 for(size_t i = 1; i <= rows; i++){ 00931 for(size_t j = 1; j <= cols; j++){ 00932 M(i,j) = *k; 00933 k++; 00934 } 00935 } 00936 00937 A = M; 00938 return A; 00939 }

| vector linalg::operator>> | ( | std::istream & | is, | |
| vector & | v | |||
| ) |
Stream extraction operator.
00779 { 00780 using namespace std; 00781 string s; 00782 list<double> data; 00783 bool colvector = true; 00784 bool shouldbedone = false; 00785 while(getline(is, s)){ 00786 s = utils::trim(s); 00787 if(s[0] == '#' or s.size() == 0) //Blank line or comment character 00788 continue; 00789 00790 stringstream ss; 00791 ss << s; 00792 00793 double x; 00794 size_t i = 0; 00795 while(ss >> x){ 00796 if( (i > 0 and colvector == false) or (shouldbedone == true)){ 00797 badArgument exc; 00798 exc.reason = "Cannot read vector: bad format in input"; 00799 exc.file = __FILE__; 00800 exc.line = __LINE__; 00801 throw exc; 00802 } 00803 data.push_back(x); 00804 i++; 00805 } 00806 if(i > 1){ 00807 colvector = false; //So it must be a row vector instead 00808 shouldbedone = true; 00809 } 00810 } 00811 00812 if(data.size() == 0){ 00813 endOfFile exc; 00814 exc.reason = "Cannot read empty vector from input."; 00815 exc.file = __FILE__; 00816 exc.line = __LINE__; 00817 throw exc; 00818 } 00819 00820 vector w(data.size()); 00821 typedef list<double>::iterator LI; 00822 size_t k = 1; 00823 for(LI i = data.begin(); i != data.end(); i++){ 00824 w(k) = *i; 00825 k++; 00826 } 00827 v = w; 00828 return v; 00829 }

| matrix linalg::T | ( | const matrix & | A | ) |
| double linalg::tr | ( | const matrix & | A | ) |
1.5.6