Code: Selecteer alles
#ifndef _MATRIX_HPP
#define _MATRIX_HPP
#include <iostream>
#include <cassert>
#include <algorithm>
class matrix
{
public:
typedef double value_type;
typedef size_t size_type;
enum order_t { row_major, column_major };
// Constructor
matrix(size_type nb_rows, size_type nb_cols, value_type init=0, order_t order=row_major)
: n(nb_rows), m(nb_cols), data(new value_type[nb_rows*nb_cols]), ord(order)
{
value_type* temp=data;
for(size_type i=0;i<nb_rows*nb_cols;++i){
*temp=init;
++temp;
}
}
...
protected:
size_type n; // number of rows
size_type m; // number of columns
value_type* data;
order_t ord;
};
// Free functions operating on matrix:
// Add two matrices and return a new matrix
matrix operator+(const matrix& A, const matrix& B);
// multiply by a scalar: symmetric thus we need two functions
matrix operator*(const matrix& A, matrix::value_type alpha);
matrix operator*(matrix::value_type alpha, const matrix& A);
// Consider two matrix objects a and b as being two vectors of size n * m
// stacking all elements next to each other, then
// calculate their inner product
matrix::value_type vected_inner_product(const matrix& a, const matrix& b);
// calculate the 1-norm (maximum absolute column sum)
matrix::value_type norm_1(const matrix& A);
// calculate the Frobenius-norm (2-norm of the stacked matrix as a vector)
matrix::value_type norm_frob(const matrix& A);
// print the matrix in a nice format on screen
std::ostream& operator<<(std::ostream& out, const matrix& A);
#endif // _MATRIX_HPP
Code: Selecteer alles
#ifndef _MATRIX_HPP
#define _MATRIX_HPP
#include <iostream>
#include <cassert>
#include <algorithm>
template <typename T> class matrix
{
public:
typedef T value_type;
typedef size_t size_type;
enum order_t { row_major, column_major };
// Constructor
matrix(size_type nb_rows, size_type nb_cols, value_type init=0, order_t order=row_major)
: n(nb_rows), m(nb_cols), data(new value_type[nb_rows*nb_cols]), ord(order)
{
value_type* temp=data;
for(size_type i=0;i<nb_rows*nb_cols;++i){
*temp=init;
++temp;
}
}
...
protected:
size_type n; // number of rows
size_type m; // number of columns
value_type* data;
order_t ord;
};
// Free functions operating on matrix:
// Add two matrices and return a new matrix
matrix operator+(const matrix& A, const matrix& B); //(*)
// multiply by a scalar: symmetric thus we need two functions
matrix operator*(const matrix& A, matrix::value_type alpha);
matrix operator*(matrix::value_type alpha, const matrix& A);
// Consider two matrix objects a and b as being two vectors of size n * m
// stacking all elements next to each other, then
// calculate their inner product
matrix::value_type vected_inner_product(const matrix& a, const matrix& b);
// calculate the 1-norm (maximum absolute column sum)
matrix::value_type norm_1(const matrix& A);
// calculate the Frobenius-norm (2-norm of the stacked matrix as a vector)
matrix::value_type norm_frob(const matrix& A);
// print the matrix in a nice format on screen
std::ostream& operator<<(std::ostream& out, const matrix& A);
#endif // _MATRIX_HPP
bvb bij de lijn (*) krijg ik "expected constructor, destructor, or type conversion before "operator""
voor de echte liefhebbers heb ik de code bijgevoegd in bijlage