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

📄 matrix.h

📁 在线支持向量机C++程序,程序中包含了应用的例子
💻 H
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
*                       ONLINE SUPPORT VECTOR REGRESSION                      *
*                      Copyright 2006 - Francesco Parrella                    *
*                                                                             *
*This program is distributed under the terms of the GNU General Public License*
******************************************************************************/


#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
#include "Vector.h"

using namespace std;

namespace onlinesvr
{

	template<class T>
	class Matrix
	{
	public:
		// Attributes
		Vector<Vector<T>*>* Values;

		// Initialization
		Matrix ();
		Matrix (double** X, int Rows, int Cols);
		~Matrix ();
		Matrix<T>* Clone ();
		int GetLengthRows ();
		int GetLengthCols ();

		// Selection Operations
		Vector<T>* GetRowRef (int Index);
		Vector<T>* GetRowCopy (int Index);
		Vector<T>* GetColCopy (int Index);
		T GetValue (int RowIndex, int ColIndex);
		void SetValue (int RowIndex, int ColIndex, T Value);
		int IndexOf (Vector<T> *V);

		// Add/Remove Operations
		void Clear ();
		void AddRowRef (Vector<T>* V);
		void AddRowCopy (Vector<T>* V);
		void AddRowCopy (T* V, int N);
		void AddRowRefAt (Vector<T>* V, int Index);
		void AddRowCopyAt (Vector<T>* V, int Index);
		void AddRowCopyAt (T* V, int N, int Index);
		void AddColCopy (Vector<T>* V);
		void AddColCopy (T* V, int N);
		void AddColCopyAt (Vector<T>* V, int Index);
		void AddColCopyAt (T* V, int N, int Index);
		void RemoveRow (int Index);
		void RemoveCol (int Index);
		Matrix<T>* ExtractRows (int FromRowIndex, int ToRowIndex);
		Matrix<T>* ExtractCols (int FromColIndex, int ToColIndex);

		// Pre-built Matrix
		static Matrix<double>* ZeroMatrix (int RowsNumber, int ColsNumber);
		static Matrix<double>* RandMatrix (int RowsNumber, int ColsNumber);

		// Mathematical Operations
		void SumScalar (T X);
		void ProductScalar (T X);
		void DivideScalar (T X);
		void PowScalar (T X);
		void SumMatrix (Matrix<T>* M);
		void SubtractMatrix (Matrix<T>* M);
		Vector<T>* ProductVector (Vector<T>* V);
		static Vector<T>* ProductVector (Matrix* M, Vector<T>* V);
		static Matrix<T>* ProductVectorVector (Vector<T>* V1, Vector<T>* V2);
		static Matrix<T>* ProductMatrixMatrix (Matrix<T>* M1, Matrix<T>* M2);

		// I/O Operations
		static Matrix<double>* Load(char* Filename);
		void Save (char* Filename);	
		void Print ();
		void Print (char* MatrixName);
		
		// Operators Redefinition	
		Vector<T> operator [] (int Index);

	private:
		// Private Attributes
		int	StepSize;

		// Private Methods
		void Resize ();
		void Resize (int NewRowNumber, int NewColNumber);
	};




		
	////////////////////////////
	// METHODS IMPLEMENTATION //
	////////////////////////////

		
	// INITIALIZATION
	template<class T>
	Matrix<T>::Matrix ()
	{
		this->StepSize = 100;
		this->Values = new Vector<Vector<T>*>();
		this->Values->SetStepSize(this->StepSize);
	}

	template<class T>
	Matrix<T>::Matrix (double** X, int Rows, int Cols)
	{
		this->StepSize = 100;
		this->Values = new Vector<Vector<T>*>(Rows);
		this->Values->SetStepSize(this->StepSize);
		for (int i=0; i<Rows; i++) {
			this->Values->Add(new Vector<T>(X[i],Cols));
		}
	}

	template<class T>
	Matrix<T>::~Matrix ()
	{
		this->Clear();
		delete this->Values;
	}

	template<class T>
	Matrix<T>* Matrix<T>::Clone ()
	{
		int i;
		Matrix<T>* M = new Matrix<T>();
		for (i=0; i<this->GetLengthRows(); i++)
			M->AddRowCopy(this->GetRowRef(i));		
		return M;
	}

	template<class T>
	int Matrix<T>::GetLengthRows ()
	{
		return this->Values->GetLength();
	}

	template<class T>
	int Matrix<T>::GetLengthCols ()
	{
		if (this->Values->GetLength()==0) {
			return 0;
		}
		else {
			return this->Values->Values[0]->GetLength();
		}
	}

		
	// Selection Operations
	template<class T>
	Vector<T>* Matrix<T>::GetRowRef (int Index)
	{
		if (Index>=0 && Index<this->GetLengthRows()) {
			return this->Values->Values[Index];
		}
		else {
			cerr << "Error! It's impossible to get an row from the matrix that doesn't exist." << endl;
			return new Vector<T>();
		}
	}

	template<class T>
	Vector<T>* Matrix<T>::GetRowCopy (int Index)
	{
		if (Index>=0 && Index<this->GetLengthRows()) {
			return this->Values->Values[Index]->Clone();
		}
		else {
			cerr << "Error! It's impossible to get an row from the matrix that doesn't exist." << endl;
			return new Vector<T>();
		}
	}

	template<class T>
	Vector<T>* Matrix<T>::GetColCopy (int Index)
	{
		if (Index>=0 && Index<this->GetLengthCols()) {
			Vector<T>* V = new Vector<T>();
			for (int i=0; i<this->GetLengthRows(); i++) {
				V->Add(this->Values->Values[i]->Values[Index]);
			}
			return V;
		}
		else {
			cerr << "Error! It's impossible to get an row from the matrix that doesn't exist." << endl;
			return new Vector<T>();
		}
	}

	template<class T>
	T Matrix<T>::GetValue (int RowIndex, int ColIndex)
	{
		return this->Values->Values[RowIndex]->Values[ColIndex];
	}

	template<class T>
	void Matrix<T>::SetValue (int RowIndex, int ColIndex, T Value)
	{
		this->Values->Values[RowIndex]->Values[ColIndex] = Value;
	}
	
	template<class T>
	int Matrix<T>::IndexOf (Vector<T> *V)
	{
		for (int i=0; i<this->GetLengthRows(); i++) {
			bool Found = true;
			for (int j=0; j<this->GetLengthCols(); j++) {
				if (V->GetValue(j) != this->GetValue(i,j)) {
					Found = false;
					break;
				}
			}
			if (Found)
				return i;
		}
		return -1;
	}

	// Add/Remove Operations
	template<class T>
	void Matrix<T>::Clear ()
	{
		for (int i=0; i<this->GetLengthRows(); i++) {
			delete this->Values->Values[i];		
		}	
		this->Values->Clear();		
	}

	template<class T>
	void Matrix<T>::AddRowRef (Vector<T>* V)
	{
		if (this->GetLengthRows()==0 && this->GetLengthCols()==0) {
			this->Values->Add(V);
			this->Values->Values[this->Values->GetLength()-1]->SetStepSize(this->StepSize);
		}
		else if (this->GetLengthCols() == V->GetLength()) {
			this->Values->Add(V);
			this->Values->Values[this->Values->GetLength()-1]->SetStepSize(this->StepSize);
		}
		else {
			cerr << "Error! It's impossible to add a row of different length." << endl;		
		}
	}

	template<class T>
	void Matrix<T>::AddRowCopy (Vector<T>* V)
	{
		if (this->GetLengthRows()==0 && this->GetLengthCols()==0) {
			this->Values->Add(V->Clone());
			this->Values->Values[this->Values->GetLength()-1]->SetStepSize(this->StepSize);
		}
		else if (this->GetLengthCols() == V->GetLength()) {
			this->Values->Add(V->Clone());
			this->Values->Values[this->Values->GetLength()-1]->SetStepSize(this->StepSize);
		}
		else {
			cerr << "Error! It's impossible to add a row of different length." << endl;		
		}
	}
	template<class T>
	void Matrix<T>::AddRowCopy (T* V, int N)
	{
		if (this->GetLengthRows()==0 && this->GetLengthCols()==0) {
			Vector<T>* NewV = new Vector<T>(V,N);
			this->Values->Add(NewV);
			this->Values->Values[this->Values->GetLength()-1]->SetStepSize(this->StepSize);
		}
		else if (this->GetLengthCols() == V->Length) {
			Vector<T>* NewV = new Vector<T>(V,N);
			this->Values->Add(NewV);	
			this->Values->Values[this->Values->GetLength()-1]->SetStepSize(this->StepSize);
		}
		else {
			cerr << "Error! It's impossible to add a row of different length." << endl;
		}
	}

	template<class T>
	void Matrix<T>::AddRowRefAt (Vector<T>* V, int Index)
	{
		if (this->GetLengthRows()==0 && this->GetLengthCols()==0 && Index==0) {
			this->Values->AddAt(V,Index);
			this->Values->Values[Index]->SetStepSize(this->StepSize);
		}
		else if (this->GetLengthCols()==V->GetLength() && Index>=0 && Index<=this->GetLengthRows()) {
			this->Values->AddAt(V,Index);
			this->Values->Values[Index]->SetStepSize(this->StepSize);
		}
		else {
			cerr << "Error! It's impossible to add a row of different length or in a bad index." << endl;
		}
	}

	template<class T>
	void Matrix<T>::AddRowCopyAt (Vector<T>* V, int Index)
	{
		if (this->GetLengthRows()==0 && this->GetLengthCols()==0 && Index==0) {
			this->Values->AddAt(V->Clone(),Index);
			this->Values->Values[Index]->SetStepSize(this->StepSize);
		}
		else if (this->GetLengthCols()==V->GetLength() && Index>=0 && Index<=this->GetLengthRows()) {
			this->Values->AddAt(V->Clone(),Index);
			this->Values->Values[Index]->SetStepSize(this->StepSize);
		}
		else {
			cerr << "Error! It's impossible to add a row of different length or in a bad index." << endl;
		}
	}

	template<class T>
	void Matrix<T>::AddRowCopyAt (T* V, int N, int Index)
	{
		if (this->GetLengthRows()==0 && this->GetLengthCols()==0 && Index==0) {
			this->Values->AddAt(new Vector<T>(V,N),Index);
			this->Values->Values[Index]->SetStepSize(this->StepSize);
		}
		else if (this->GetLengthCols()==V->Length && Index>=0 && Index<=this->GetLengthRows()) {
			this->Values->AddAt(new Vector<T>(V,N),Index);
			this->Values->Values[Index]->SetStepSize(this->StepSize);
		}
		else {
			cerr << "Error! It's impossible to add a row of different length or in a bad index." << endl;
		}
	}

	template<class T>
	void Matrix<T>::AddColCopy (Vector<T>* V)
	{
		if (this->GetLengthRows()==0 && this->GetLengthCols()==0) {
			for (int i=0; i<V->GetLength(); i++) {
				Vector<T>* V3 = new Vector<T>();
				V3->Add(V->Values[i]);
				V3->SetStepSize(this->StepSize);
				this->Values->Add(V3);
			}
		}
		else if (this->GetLengthRows() == V->GetLength()) {
			for (int i=0; i<V->GetLength(); i++) {
				this->Values->Values[i]->Add(V->Values[i]);
			}

⌨️ 快捷键说明

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