#include <math.h> #include <complex> #include <sstream> #include <iostream> #include <string> #include <exception> #define array_length(array) (sizeof(array)/sizeof(array[0])) #define min(x, y) ((x<=y)?x:y) #define max(x, y) ((x>=y)?x:y) #define ERROR -1 #define ATTEMPT 3 //class to simply keep track of current matrices and such class ___s {public: int ___x; ___s(int x){___x = x;};} ___s(0); //actual class template template <class math_class = long double> class matrix { protected: //matrix dimensions int ROW_, COLUMN_; //whether or not the matrix has been set to desired size bool matrix_set; //actual multidimensional array used to store the matrix math_class **MATRIX_STO; public: int matrix_number; //simple ID number matrix() : ROW_(0), COLUMN_(0), matrix_set(false) { //basic declaration renders sizeless matrix MATRIX_STO = new math_class*[0]; MATRIX_STO[0] = new math_class[0]; matrix_number = ___s.___x++; } matrix(int nRows, int nColumns, bool try_again = true) { using namespace std; try { MATRIX_STO = new math_class*[nRows]; for (int x = 0; x < nRows; x++) { MATRIX_STO[x] = new math_class[nColumns]; } ROW_ = nRows; COLUMN_ = nColumns; matrix_set = true; matrix_number = ___s.___x++; } catch (bad_alloc&) { if (try_again == true) { _try_set(nRows, nColumns, 3, true, false); } matrix_set = false; } } void _try_set(int nRows, int nColumns, int try_number = 1, bool display_error = true, bool for_operator = false) { if (matrix_set == false) { using namespace std; try { MATRIX_STO = new math_class*[nRows]; for (int x = 0; x < nRows; x++) { MATRIX_STO[x] = new math_class[nColumns]; } ROW_ = nRows; COLUMN_ = nColumns; matrix_set = true; cannot_create = false; matrix_number = ___s.___x++; } catch (bad_alloc&) { for (int current_try = 0; current_try < try_number; current_try++) {_try_set(nRows, nColumns, 0, false);} if (display_error) { if (!for_operator) { cerr << "Could not allocate enough memory to create matrix of desired size:\n" << nRows << " by " << nColumns << ". "<< \ try_number << ((try_number > 1) ? " attempts were made." : " attempt was made.") << endl; } if (for_operator) { cerr << "Could not allocate requested amount of memory to perform this operation" << ". "<< \ try_number << ((try_number > 1) ? " attempts were made." : " attempt was made.") << endl; } }; matrix_set = false; cannot_create = true; } } } void set_row(int row, math_class a_row[], int number_of_elements) { using namespace std; if (matrix_set) { if ((row >= ROW_) || ((number_of_elements - 1) >= COLUMN_)) { bool exist = true; string __error__ = ""; if ((row >= ROW_)) { __error__.append("Row does not exist.\n"); exist = false; } if ((number_of_elements - 1) >= COLUMN_) { if (exist) {__error__.append("Input row is too large and will be truncated.\n");} } cerr << __error__; } else { for (int current_element=0; current_element < number_of_elements; current_element++) { MATRIX_STO[row][current_element] = a_row[current_element]; } } } else {std::cerr << "Matrix not set\n";} } void set_column(int column, math_class a_column[], int number_of_elements) { using namespace std; if (matrix_set) { if ((column >= COLUMN_) || ((number_of_elements - 1) >= ROW_)) { bool exist = true; string __error__ = ""; if ((column >= COLUMN_)) { __error__.append("Column does not exist.\n"); exist = false; } if ((number_of_elements - 1) >= ROW_) { if (exist) {__error__.append("Input row is too large and will be truncated.\n");} } cerr << __error__; } else { for (int current_element=0; current_element < number_of_elements; current_element++) { MATRIX_STO[column][current_element] = a_column[current_element]; } } } else {std::cerr << "Matrix not set\n";} } math_class get_location(int nRow, int nColumn) { try { if ((nRow > ROW_) || (nColumn > COLUMN_)) { throw "Location does not exist "; } if (matrix_set) { return MATRIX_STO[nRow][nColumn]; } else throw "Matrix not set "; } catch (const char* caught) {std::cerr << caught; return ERROR;} } void delete_matrix(bool recreate = false, bool show_message = false) { for(int x = 0; x < ROW_; x++) { delete[] MATRIX_STO[x]; } delete[] MATRIX_STO; matrix_set = false; if (show_message) {std::cout << "Deletion succesful\n";} if (recreate) { std::cout << "Recreating Matrix\n"; _try_set(ROW_, COLUMN_, ATTEMPT, true, true); for (int CR = 0; CR < ROW_; CR++) {for (int CC = 0; CC < COLUMN_; CC++) {MATRIX_STO[CR][CC] = 0;};} } } //resize... well, F*cking hard to design with error handling, thats all I have to say :P/> I did write this myself though, barely any online help. void resize(int nRows, int nColumns) { ___temp_set(nRows, nColumns, 3, true); for (int x = 0; x < min(ROW_, nRows); x++) { for (int y = 0; y < min(COLUMN_, nColumns); y++) { TEMP_MATRIX[x][y] = MATRIX_STO[x][y]; } } if (can_resize) { delete_matrix(); _try_set(nRows, nColumns, ATTEMPT, false); if (!cannot_create) { for (int x = 0; x < nRows; x++) { for (int y = 0; y < nColumns; y++) { MATRIX_STO[x][y] = TEMP_MATRIX[x][y]; } } std::cout << "Resize to " << nRows << " by " << nColumns << " succesful\n"; } if (cannot_create) { _try_set(ROW_, COLUMN_, ATTEMPT, false); for (int x = 0; x < ROW_; x++) { for (int y = 0; y < COLUMN_; y++) { MATRIX_STO[x][y] = TEMP_MATRIX[x][y]; } } std::cerr << "Could not resize after " << ATTEMPT << ((ATTEMPT>1)?" attempts were made. ":" attempt was made. ") << "Data has been recreated unchanged.\n"; } } for(int x = 0; x < nRows; x++) { delete[] TEMP_MATRIX[x]; } delete[] TEMP_MATRIX; } //simple destructor to avoid memory leaks. ~matrix() { for(int x = 0; x < ROW_; x++) { delete[] MATRIX_STO[x]; } delete[] MATRIX_STO; ___s.___x--; } private: bool can_resize, cannot_create; //used in the resize function math_class **TEMP_MATRIX; //temporary matrix used in the resize function //to create the TEMP_MATRIX, used in the resize function void ___temp_set(int nRows, int nColumns, int try_number = ATTEMPT, bool display_error = true) { using namespace std; try { TEMP_MATRIX = new math_class*[nRows]; for (int x = 0; x < nRows; x++) {TEMP_MATRIX[x] = new math_class[nColumns];} can_resize = true; } catch (bad_alloc&) { for (int current_try = 0; current_try < try_number; current_try++) {___temp_set(nRows, nColumns, 0, false);} if (display_error) { cerr << "Could not allocate requested amount of memory to perform this operation" << ".\n"<< \ try_number << ((try_number > 1) ? " attempts were made." : " attempt was made.") << endl; } can_resize = false; } } };

Btw, if you find an error, or would simply like to help me improve this, tell me please. And, of course feel free to use this yourself.

This post has been edited by **novacrazy**: 13 March 2011 - 09:45 PM