⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.h

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 H
字号:
// ENERGY211/CME211//// matrix.h - header file for Project 1//#include <stdexcept>	// Need this to use std::runtime_error						// class, for reporting errors#ifndef CLASS_MATRIX	// Make sure this isn't included#define CLASS_MATRIX	// more than once// This class is used to specify the rows and columns to// be included in a submatrix.  DO NOT MODIFY!class Range {public:	Range( int r1 = 0, int r2 = -1, int c1 = 0, int c2 = -1 )	{		// r1 = first row in submatrix, r2 = last row		// c1 = first column in submatrix, c2 = last column		// Default value -1 indicates that the range extends		// to the last row or column of the matrix, so when		// this Range object is actually used to extract		// a submatrix of a particular matrix, m_row2 or		// m_col2 needs to be changed to the correct value		m_row1 = r1;		m_row2 = r2;		m_col1 = c1;		m_col2 = c2;	}	Range( const Range& r )	// Copy constructor	{		m_row1 = r.get_row1();		m_row2 = r.get_row2();		m_col1 = r.get_col1();		m_col2 = r.get_col2();	}	~Range()	{	}		// Use these to obtain data members	inline int get_row1() const { return m_row1; }	inline int get_row2() const { return m_row2; }	inline int get_col1() const { return m_col1; }	inline int get_col2() const { return m_col2; }		// Use these to change data members	inline void set_row1(int row1) { m_row1 = row1; }	inline void set_row2(int row2) { m_row2 = row2; }	inline void set_col1(int col1) { m_col1 = col1; }	inline void set_col2(int col2) { m_col2 = col2; }	private:	int m_row1;	int m_row2;	int m_col1;	int m_col2;} ;// Output operator for Range objectsstd::ostream& operator<<( std::ostream& out, const Range& r );// Main class that you will implement in this project// DO NOT MODIFY, ADD OR DELETE ANY PUBLIC DECLARATIONSclass Matrix {public:	// Constructor used to initialize a matrix from an array	// of doubles	Matrix( int rows = 0, int cols = 0, double *data = NULL );	// Copy constructor	Matrix( const Matrix& A );		enum ErrorCode {		ERROR_INVALID_SIZE = 0,		ERROR_SIZE_MISMATCH = 1,		ERROR_INVALID_INDEX = 2,		ERROR_INVALID_RANGE = 3,		ERROR_INVALID_RESHAPE = 4,		ERROR_INVALID_ASSIGNMENT = 5	} ;		// Destructor, must de-allocate dynamically allocated	// memory	virtual ~Matrix();		// Use these to obtain the size of the matrix	inline int get_rows() const { return m_rows; }	inline int get_cols() const { return m_cols; }		// Set matrix to given size, and set elements to zero	// If second argument is not passed, square matrix is	// assumed, as in MATLAB's zeros function	void Zeros( int rows, int cols = 0 );	// Same as Zeros, except all elements set to 1	void Ones( int rows, int cols = 0 );	// This makes the matrix equal to the identity matrix	// of the indicated size	void Identity( int rows, int cols = 0 );		// Matrix arithmetic	Matrix operator+( const Matrix& B ) const;	Matrix operator-( const Matrix& B ) const;	Matrix operator*( const Matrix& B ) const;	// Scalar multiplication	Matrix operator*( double s ) const;	Matrix operator/( double s ) const;		// Assignment operators	virtual Matrix& operator=( const Matrix& A );	Matrix& operator+=( const Matrix& A );	Matrix& operator-=( const Matrix& A );	// To multiply or divide by scalar, then assign	Matrix& operator*=( double s );	Matrix& operator/=( double s );		// This extracts the ith row of the matrix	double *operator[]( int i ) const;	// This extracts a submatrix of the matrix.  The	// Range object indicates a range of rows and a	// range of columns to extract.  NOTE: the	// resulting submatrix must not allocate its own	// memory for storing the entries!  It must point	// to the data in the original matrix, so that 	// the submatrix can be used on the left side of	// an assignment and modify the original matrix	Matrix operator[]( Range r ) const;	// This is particularly useful to store all of the	// entries of a matrix in a single column, but it	// can be used to reshape the matrix into a matrix	// of any other consistent size (that is, the number	// of entries must not change)	void Reshape( const Matrix& A, int rows, int cols );			Matrix TriU( int diag = 0 ) const;	Matrix TriL( int diag = 0 ) const;	class ColVector GetDiag( int diag = 0 ) const;	void SetDiag( const class ColVector& d, int diag = 0 );	static double norm1( const Matrix& A );	protected:	// In this section, you may add functions or variables	// that will help you implement the public operations	void allocate();	void deallocate();	double *m_data;	double **m_rowdata;	int m_rows;	int m_cols;	bool m_issubmatrix;		void Set( int rows, int cols, double *data = NULL );	void Set( const Matrix& A );	// Leave these alone!  They are used for error reporting	static void ReportError( ErrorCode errorcode );	static char *ErrorMessages[];} ;// Output operatorstd::ostream& operator<<( std::ostream& out, const Matrix& A );// Input operatorstd::istream& operator>>( std::istream& in, Matrix& A );// For multiplying a scalar by a matrixMatrix operator*( double s, const Matrix& A );#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -