📄 matrix.h
字号:
/*
* FILE --- matrix.hh
*
* PURPOSE --- Header file for a class for implementing a matrix of complexs
* together with matrix operations such as multiplication, transpose, inverse,
* etc. It is intended to be used with the control library.
*/
// Prevent Multiple Inclusion
#ifndef MATRIX_HH
#define MATRIX_HH
// Include Files
#include <stdlib.h>
#include "complex.h"
#include <iostream.h>
//complex zero(0.0,0.0);
//complex 1(1.0,0.0);
// Local Definitions and Macros
#define MATRIX_NO_ERROR 0
#define MEMORY_ERROR 1
#define INVALID_DIMENTION 2
#define ILLEGAL_INDEX 3
#define DIVIDE_BY_ZERO 4
#define NONSQUARE_MATRIX 5
#define NONINVERTIBLE_MATRIX 6
#define SIZE_MISMATCH 7
#define INVALID_SET_MATRIX 8
/* Definition of the class */
class MATRIX
{
public:
/* Consrtuctor --- Fills the matrix with single value specified by _val*/
MATRIX(int _row = 2, int _col = 2, complex2 _val=0 );
/* Consrtuctor --- Fills the matrix with the values specified by _elements.
The dimension of _elements should be _row-by-_col */
MATRIX(int _row, int _col, complex2 **_elements);
/* Copy Constructor */
MATRIX(const MATRIX &_matrix);
// Destructor
~MATRIX();
void allocate_memory(void);
void free_memory(void);
// Return the # of rows or columns
inline int get_num_rows() const { return(row); }
inline int get_num_cols() const { return(col); }
// Assign all the elements of the matrix
// _elements should be of appropriate dimension
void set_matrix(const complex2 _val); // to the value given by _val
void set_matrix(const complex2 **_elements); // to values given by _elements
// The following is to be used for vectors (1-col or 1-row matrices)
void set_matrix(const complex2 *_elements); // to values given by _elements
// Assign the diagonal elements of the matrix
// _elements should be of appropriate dimension
void set_diagonal(complex2 _val); // to the value given by _val
void set_diagonal(const complex2 *_elements); // to values given by _elements
// Assign the elements of the specified row of the matrix
// _elements should be of appropriate dimension
void set_row(int _row, complex2 _val); // to the value given by _val
void set_row(int _row, const complex2 *_elements); // to values given by _elements
// Assign the elements of the specified column of the matrix
// _elements should be of appropriate dimension
void set_col(int _col, complex2 _val); // to the value given by _val
void set_col(int _col, const complex2 *_elements); // to values given by _elements
// Return the value of the element at specified index
complex2 get_element(int _row, int _col) const;
// Assign the element at specified index the value given by _val
void set_element(int _row, int _col, complex2 _val);
/* Add, subtract, multiply or devide the element at specified index
by the value given by _val */
void add_to_element(int _row, int _col, complex2 _val);
void subtract_from_element(int _row, int _col, complex2 _val);
void multiply_element(int _row, int _col, complex2 _val);
void divide_element(int _row, int _col, complex2 _val);
/* Add, subtract, multiply or devide all the elements of the specified
row by the value given by _val */
void add_to_row(int _row, complex2 _val);
void subtract_from_row(int _row, complex2 _val);
void multiply_row(int _row, complex2 _val);
void devide_row(int _row, complex2 _val);
/* Add, subtract, multiply or devide all the elements of the specified
column by the value given by _val */
void add_to_col(int _col, complex2 _val);
void subtract_from_col(int _col, complex2 _val);
void multiply_col(int _col, complex2 _val);
void devide_col(int _col, complex2 _val);
// Add a row (column) multiplied by a complex2 to another row (column)
void add_rows(int _source_row, int _destination_row, complex2 gain);
void add_cols(int _source_col, int _destination_col, complex2 gain);
// Overload some of the operators
MATRIX &operator=(const MATRIX &_matrix2);
const MATRIX &operator+=(const MATRIX &_matrix2);
const MATRIX &operator-=(const MATRIX &_matrix2);
const MATRIX &operator*=(const complex2 _val);
MATRIX operator+(const MATRIX &_matrix2) const;
MATRIX operator-(const MATRIX &_matrix2) const;
MATRIX operator*(const MATRIX &_matrix2) const;
MATRIX operator*(const complex2 _val);
friend MATRIX operator*(const complex2 _val, const MATRIX &_matrix);
int matrix_err; // Wariable to hold the error
void print_matrix(void);
private:
complex2** elements; // Two dimentional array that holds the elements
int row, col; // The # of rows and columns
};
MATRIX transpose(const MATRIX &_matrix); // Take the transpose
MATRIX transpose_col(const MATRIX &_matrix);//Take the coloum order;
MATRIX inv(const MATRIX &_matrix); // Take the inverse (square matrices)
MATRIX pinv(const MATRIX &_matrix); // Take the pseudo inverse
MATRIX hermite(const MATRIX &_matrix);// Take the conjugate transpose
complex2 trace(const MATRIX &_matrix); // Find the trace
complex2 norm(const MATRIX &_matrix); // Find the Frobenious norm
//MATRIX operator+(const MATRIX &_matrix1, const MATRIX &_matrix2);
//MATRIX operator-(const MATRIX &_matrix1, const MATRIX &_matrix2);
//MATRIX operator*(const MATRIX &_matrix1, const MATRIX &_matrix2);
MATRIX operator*(const complex2 _val, const MATRIX &_matrix);
//MATRIX operator*(const MATRIX &_matrix, const complex2 _val);
typedef MATRIX matrix;
#endif // MATRIX_HH
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -