17 # define ERR_MATRIX_RANGE (char *)"Error: Matrix: index is out of range !"
18 # define ERR_MATRIX_INVALID (char *)"Error: Matrix: size is invalid !"
19 # define ERR_MATRIX_INVOP (char *)"Error: Matrix: invalid operation !"
21 # define RANGE(i, j, m_i, m_j)((i) >= 0 && (j) >= 0 && (i) < (m_i) && (j) < (m_j) ? true : false)
34 Matrix(
unsigned int rows,
unsigned int cols, T
const& elem)
36 if (rows == 0 || cols == 0)
38 std::cerr << ERR_MATRIX_INVALID << std::endl;
48 for (
unsigned int i = 0; i < rows * cols; i++)
57 for (
unsigned int i = 0; i < _rows * _cols; i++)
58 _m.push_back(copy[i]);
64 unsigned int rows(
void)
const {
return (_rows); }
65 unsigned int cols(
void)
const {
return (_cols); }
66 T
const& init(
void)
const {
return (_init); }
67 Matrix<T> identity(
unsigned int size, T
const& null, T
const&
id)
71 for (
unsigned int i = 0; i < size; i++)
84 for (
unsigned int i = 0; i < _rows * _cols; i++)
88 const T& operator[](
unsigned int i)
const
90 if (i < _rows * _cols)
93 std::cerr << ERR_MATRIX_RANGE << std::endl;
94 return (_m[_rows * _cols - 1]);
96 T& operator[](
unsigned int i)
98 if (i < _rows * _cols)
101 std::cerr << ERR_MATRIX_RANGE << std::endl;
102 return (_m[_rows * _cols - 1]);
104 const T& operator()(
unsigned int i,
unsigned int j)
const
106 if (RANGE(i, j, _rows, _cols))
107 return (_m[i * _cols + j]);
109 std::cerr << ERR_MATRIX_RANGE << std::endl;
110 return (_m[_cols * _cols - 1]);
112 T& operator()(
unsigned int i,
unsigned int j)
114 if (RANGE(i, j, _rows, _cols))
115 return (_m[i * _cols + j]);
117 std::cerr << ERR_MATRIX_RANGE << std::endl;
118 return (_m[_cols * _cols - 1]);
136 if (m1.rows() == m2.rows() && m1.cols() == m2.cols())
138 result =
Matrix<T>(m1.rows(), m1.cols(), m1.init());
139 for (
unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
140 result[i] = m1[i] + m2[i];
143 std::cerr << ERR_MATRIX_INVOP << std::endl;
148 Matrix<T> operator-(Matrix<T>
const& m1, Matrix<T>
const& m2)
152 if (m1.rows() == m2.rows() && m1.cols() == m2.cols())
154 result = Matrix<T>(m1.rows(), m1.cols(), m1.init());
155 for (
unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
156 result[i] = m1[i] - m2[i];
159 std::cerr << ERR_MATRIX_INVOP << std::endl;
164 Matrix<T> operator+(Matrix<T>
const& m1, T
const& s)
168 result = Matrix<T>(m1.rows(), m1.cols(), m1.init());
169 for (
unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
170 result[i] = m1[i] + s;
175 Matrix<T> operator*(Matrix<T>
const& m1, T
const& s)
179 result = Matrix<T>(m1.rows(), m1.cols(), m1.init());
180 for (
unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
181 result[i] = m1[i] * s;
186 Matrix<T> operator*(Matrix<T>
const& m1, Matrix<T>
const& m2)
190 if (m1.cols() == m2.rows())
192 result = Matrix<T>(m1.rows(), m2.cols(), m1.init());
193 for (
unsigned int i = 0; i < result.rows(); i++)
195 for (
unsigned int j = 0; j < result.cols(); j++)
197 for (
unsigned int k = 0; k < m1.cols(); k++)
200 result(i, j) = m1(i, k) * m2(k, j);
202 result(i, j) += m1(i, k) * m2(k, j);
208 std::cerr << ERR_MATRIX_INVOP << std::endl;
213 bool operator==(Matrix<T>
const& m1, Matrix<T>
const& m2)
215 if (m1.cols() == m2.rows())
217 for (
unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
225 std::cerr << ERR_MATRIX_INVOP << std::endl;
230 bool operator==(Matrix<T>& m1, Matrix<T>& m2)
232 if (m1.cols() == m2.rows())
234 for (
unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
242 std::cerr << ERR_MATRIX_INVOP << std::endl;
247 std::ostream& operator<<(std::ostream& flux, Matrix<T>
const& m)
249 for (
unsigned int i = 0; i < m.rows(); i++)
251 for (
unsigned j = 0; j < m.cols(); j++)
252 flux << m(i, j) <<
' ';
Contains all the objects that provide the management for the genetic algorithm with neural networks...
Definition: ann_exception.h:16