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

📄 cdmat.cpp

📁 大规模定制中评价系统源码!对其中配置后的产品进行优化,寻优!
💻 CPP
字号:
#include "CdMat.h"
#include <iostream>
#include <math.h>
using namespace std;
const double ESP = 0.0001;


CdMat::CdMat(int nRow, int nCol)
{
	if(nCol == 0 || 0 == nRow)
	{
		m_nCol = 0;
		m_nRow = 0;
		m_pdData = NULL;
	}
	else
	{
		m_nCol = nCol;
		m_nRow = nRow;
		m_pdData = new double[nCol * nRow];
		for (int i = 0; i < m_nRow; i++)
		{
			for (int j = 0; j < m_nCol; j++)
			{
				m_pdData[i * m_nCol + j] = 0;
			}
		}
	}
	
}

CdMat::CdMat(const CdMat &mat)
{
	m_nCol = mat.m_nCol;
	m_nRow = mat.m_nRow;
	m_pdData = new double [m_nCol * m_nRow];
	for (int i = 0; i < m_nRow; i++)
		{
			for (int j = 0; j < m_nCol; j++)
			{
				m_pdData[i * m_nCol + j] = mat.m_pdData[i * m_nCol + j];
			}
		}
}

void CdMat::Init()
{
	cout << "please input the number of Mat:\n";
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			cin >> m_pdData[i * m_nCol + j];
			cout << ",\t";
		}
		cout << endl;
	}
}

CdMat::~CdMat(void)
{
	delete [] m_pdData;
}

void CdMat::Display()
{
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			cout << m_pdData[i * m_nCol + j] << ",\t";
		}
		cout << endl;
	}
}

void CdMat::SetCol(int nCol)
{
	if(nCol < 0)
		return;
	m_nCol = nCol;
	if (m_pdData != NULL)
	{
		delete [] m_pdData;
	}
	m_pdData = new double [m_nCol * m_nRow];
}

int CdMat::GetCol()
{
	return m_nCol;
}

void CdMat::SetRow(int nRow)
{
	if(nRow < 0)
		return ;
	m_nRow = nRow;
	if (m_pdData != NULL)
	{
		delete [] m_pdData;
	}
	m_pdData = new double [m_nCol * m_nRow];
}

int CdMat::GetRow()
{
	return m_nRow;
}

void CdMat::SetData(int nRow, int nCol, double dData)
{
	if (nRow < 0 || nRow > m_nRow || nCol < 0 || nCol > m_nCol)
	return;
	m_pdData[(nRow - 1) * m_nCol + nCol] = dData; 
}

 CdMat  CdMat::operator =(const CdMat& otherMat)
{
	if (this == &otherMat)
	{
		return *this;
	}
	else
	{
		m_nCol = otherMat.m_nCol;
		m_nRow = otherMat.m_nRow;
		if (m_pdData != NULL)
		{
			delete [] m_pdData;
		}
		m_pdData = new double [m_nCol * m_nRow];
		for (int i = 0; i < m_nRow; i++)
		{
			for (int j = 0; j < m_nCol; j++)
			{
				m_pdData[i * m_nCol + j] = otherMat.m_pdData[i * m_nCol + j];
			}
		}
		return *this;

	}
}

CdMat CdMat::operator *(const CdMat &mat)
{
	
	if (m_nCol != mat.m_nRow)
	{
		return *this;
	}
	
	double sum = 0;
	CdMat tempmat(m_nRow, mat.m_nCol);
	for (int i = 0; i < m_nRow ; i++)
	{
		for (int j = 0; j < mat.m_nCol; j++)
		{
			sum = 0;
			for (int k = 0; k < m_nCol; k++)
			{
				sum += m_pdData[i * m_nCol + k] * mat.m_pdData[k * mat.m_nCol + j]; 
			}
			tempmat.m_pdData[i * mat.m_nCol + j] = sum;
		}
	}
	return tempmat;

}

CdMat CdMat::operator +(const CdMat &mat)
{
	
	if (m_nRow != mat.m_nRow || m_nCol != mat.m_nCol)
	{
		return *this;
	}
	CdMat tempmat(m_nRow, m_nCol);
	for (int i = 0; i < m_nRow ; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			tempmat.m_pdData[i * m_nCol + j] = m_pdData[i * m_nCol + j] 
											   + mat.m_pdData[i * m_nCol + j];
		}
	}
	return tempmat;

}

CdMat CdMat::operator -(const CdMat &mat)
{
	
	if (m_nRow != mat.m_nRow || m_nCol != mat.m_nCol)
	{
		return *this;
	}
	CdMat tempmat(m_nRow, m_nCol);
	for (int i = 0; i < m_nRow ; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			tempmat.m_pdData[i * m_nCol + j] = m_pdData[i * m_nCol + j] 
											   - mat.m_pdData[i * m_nCol + j];
		}
	}
	return tempmat;

}

CdMat CdMat::operator +(double d)
{
	CdMat tempmat(m_nRow, m_nCol);
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			tempmat.m_pdData[i * m_nCol + j] += d;
		} 
	}
	return tempmat;
}

CdMat CdMat::operator -(double d)
{
	CdMat tempmat(m_nRow, m_nCol);
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			tempmat.m_pdData[i * m_nCol + j] -= d;
		} 
	}
	return tempmat;
}

CdMat CdMat::operator *(double d)
{
	CdMat tempmat(m_nRow, m_nCol);
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			tempmat.m_pdData[i * m_nCol + j] *= d;
		} 
	}
	return tempmat;
}

CdMat CdMat::operator /(double d)
{
	if (d < 0.001)
	{
		return *this;
	}
	CdMat tempmat(*this);
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			tempmat.m_pdData[i * m_nCol + j] /= d;
		} 
	}
	return tempmat;
}

double CdMat::GetMax()
{
	double tempmax = m_pdData[0];
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nCol; j++)
		{
			if( tempmax < m_pdData[i * m_nCol + j])
			{
				tempmax = m_pdData[i * m_nCol + j];
			}
		}
	}
	return tempmax;
}

double CdMat::GetMaxEigbyPow(CdMat &vMat)
{
	if (vMat.m_nCol != 1 || vMat.m_nRow != m_nCol || m_nRow != m_nCol)
	{
		return 0;
	}
	else
	{
		double MaxEig, tempEig = 0;
		vMat = *this * vMat;
		MaxEig = vMat.GetMax();
		if (MaxEig > ESP)
		{
			vMat = vMat / MaxEig;
		}
		else
		{
			return MaxEig;
		}
		while (fabs(MaxEig - tempEig) > ESP)
		{
			tempEig = MaxEig;
			vMat = *this * vMat;
			MaxEig = vMat.GetMax();
			if (MaxEig > ESP)
			{
				vMat = vMat / MaxEig;
			}
			else
			{
				break;
			}
		}
		return MaxEig;
	}
}

double CdMat::GetCI(CdMat &vMat)
{
	double Eig = GetMaxEigbyPow(vMat);
	if (Eig != 0)
	{
		double CI = (Eig - m_nRow) /(m_nRow -1);
		return CI;
	}
	else
	{
		return 1;
	}
}


⌨️ 快捷键说明

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