稀疏矩阵相乘

#pragma once

class CMatrix
{
public:
	CMatrix(void);
	~CMatrix(void);
	bool InitMatrix(int row=3, int col=2, int count_of_number=3);

private:
	int m_nRow;
	int m_nCol;
	int m_nTotal;
	struct Node
	{
		int row;
		int col;
		int val;
	};
	Node *m_pNode;
public:
	void InputMatrix(void);
	void OutPutMatrix(void);
	void DefaultInit(void);
private:
	int Find(int temprow, int tempcol);
public:
	void FastReverse(void);
	int Multiply(const CMatrix& matrixSource, CMatrix& matrixDest);
	CMatrix(const CMatrix& matrix);
	void DefaultInit1(void);
};
#include "StdAfx.h"
#include "Matrix.h"

CMatrix::CMatrix(void)
{
	m_pNode = NULL;
}

CMatrix::~CMatrix(void)
{
	if (NULL != m_pNode)
	{
		delete []m_pNode;
	}
}

bool CMatrix::InitMatrix(int row, int col, int count_of_number)
{
	m_nCol = col;
	m_nRow = row;
	m_nTotal = count_of_number;
	m_pNode = new Node[count_of_number];
	if (NULL == m_pNode)
	{
		return false;
	}
	return true;
}

void CMatrix::InputMatrix(void)
{
	int row, col, val;
	for (int i=0; i<m_nTotal; i++)
	{
		cout<<"请输入行:";
		cin>>row;
		cout<<"请输入列:";
		cin>>col;
		cout<<"请输入元素值:";
		cin>>val;
		m_pNode[i].row = row;
		m_pNode[i].col = col;
		m_pNode[i].val = val;
	}
	//DefaultInit();
}

void CMatrix::OutPutMatrix(void)
{

	int nPos;
	for (int row=0; row<m_nRow; row++)
	{
		for (int col=0; col<m_nCol; col++)
		{
			if ((nPos=Find(row, col)) != -1)
			{
				cout<<setw(4)<<left<<m_pNode[nPos].val<<"  ";
			}
			else
			{
				cout<<setw(4)<<left<<0<<"  ";
			}
		}
		cout<<endl;
	}
}

void CMatrix::DefaultInit(void)
{
	m_pNode[0].row = 0;
	m_pNode[0].col = 1;
	m_pNode[0].val = 4;

	m_pNode[1].row = 1;
	m_pNode[1].col = 1;
	m_pNode[1].val = 5;

	m_pNode[2].row = 2;
	m_pNode[2].col = 1;
	m_pNode[2].val = 7;



}

int CMatrix::Find(int temprow, int tempcol)
{
	for (int i=0; i<m_nTotal; i++)
	{
		if (m_pNode[i].row == temprow && m_pNode[i].col == tempcol)
		{
			return i;
		}
	}
	return -1;
}

void CMatrix::FastReverse(void)
{
	Node *pTemp = new Node[m_nTotal];
	if (NULL == pTemp)
	{
		return;
	}
	
	//求稀疏矩阵每一列非零元素的个数
	int *parrCol = new int[m_nCol+1];
	if (NULL == parrCol)
	{
		return;
	}

	memset(parrCol, 0, sizeof(int)*(m_nCol+1));

	int i;
	for (i=0; i<m_nTotal; i++)
	{
		parrCol[m_pNode[i].col+1]++;  //将前一列的非零元素个数放到后一列去
	}

	for (i=1; i<m_nCol+1; i++)
	{
		parrCol[i] += parrCol[i-1];  //设置每一列的第一个元素在稀疏矩阵中的位置
	}

	for (i=0; i<m_nTotal; i++)
	{
		int pos = parrCol[m_pNode[i].col]++;
		pTemp[pos].row = m_pNode[i].col;
		pTemp[pos].col = m_pNode[i].row;
		pTemp[pos].val = m_pNode[i].val;
	}

	for (int i=0; i<m_nTotal; i++)
	{
		m_pNode[i].col = pTemp[i].col;
		m_pNode[i].row = pTemp[i].row;
		m_pNode[i].val = pTemp[i].val;
	}

	int temp = m_nRow;
	m_nRow = m_nCol;
	m_nCol = temp;

	delete []pTemp;
	delete []parrCol;

}

int CMatrix::Multiply(const CMatrix& matrixSource, CMatrix& matrixDest)
{
	if (m_nCol != matrixSource.m_nRow)
	{
		return 0;
	}
	
	//初始化目标矩阵
	matrixDest.m_pNode = new Node[m_nRow*matrixSource.m_nCol];
	matrixDest.m_nCol = matrixSource.m_nCol;
	matrixDest.m_nRow = m_nRow;
	CMatrix matrixReverse = matrixSource;
	matrixReverse.FastReverse();  //先将其倒转

	//求稀疏矩阵每一列非零元素的个数
	int *pArrRow = new int[m_nRow+1];
	if (NULL == pArrRow)
	{
		return 0;
	}

	memset(pArrRow, 0, sizeof(int)*(m_nRow+1));

	int i;
	for (i=0; i<m_nTotal; i++)
	{
		pArrRow[m_pNode[i].row+1]++;  //将前一行的非零元素个数放到后一行去
	}

	for (i=1; i<m_nRow+1; i++)
	{
		pArrRow[i] += pArrRow[i-1];  //设置每一行的第一个元素在稀疏矩阵中的位置
	}

	//倒转矩阵
	int *pArrRowOfReverse = new int[matrixReverse.m_nRow+1];
	if (NULL == pArrRowOfReverse)
	{
		return 0;
	}

	memset(pArrRowOfReverse, 0, sizeof(int)*(matrixReverse.m_nRow+1));


	for (i=0; i<matrixReverse.m_nTotal; i++)
	{
		pArrRowOfReverse[matrixReverse.m_pNode[i].row+1]++;  //将前一行的非零元素个数放到后一行去
	}

	for (i=1; i<matrixReverse.m_nRow+1; i++)
	{
		pArrRowOfReverse[i] += pArrRowOfReverse[i-1];  //设置每一行的第一个元素在稀疏矩阵中的位置
	}

	int pos;
	int posOfReverse;

	int indexMatrixDest=0;
	for (int row=0; row<matrixDest.m_nRow; row++)
	{

		for(int col=0; col<matrixDest.m_nCol; col++)
		{
			int result=0;
			pos = pArrRow[row];
			posOfReverse = pArrRowOfReverse[col];

			if (m_pNode[pos].row == row || matrixReverse.m_pNode[posOfReverse].row == col)
			{
				while(pos<pArrRow[row+1] && posOfReverse<pArrRowOfReverse[col+1])
				{
					if (m_pNode[pos].col == matrixReverse.m_pNode[posOfReverse].col)
					{
						result += m_pNode[pos].val*matrixReverse.m_pNode[posOfReverse].val;
						pos++;
						posOfReverse++;
					}
					else if (m_pNode[pos].col < matrixReverse.m_pNode[posOfReverse].col)
					{
						pos++;
					}
					else
					{
						posOfReverse++;
					}
				}
			}
			

			if (result != 0)
			{
				matrixDest.m_pNode[indexMatrixDest].val = result;
				matrixDest.m_pNode[indexMatrixDest].col = col;
				matrixDest.m_pNode[indexMatrixDest].row = row;
				indexMatrixDest++;
			}
		}
	}
	matrixDest.m_nTotal = indexMatrixDest;
	delete []pArrRow;
	delete []pArrRowOfReverse;
	return 1;
}



CMatrix::CMatrix(const CMatrix& matrix)
{
	m_pNode = new Node[matrix.m_nTotal];
	if (NULL == m_pNode)
	{
		return ;
	}

	m_nCol = matrix.m_nCol;
	m_nRow = matrix.m_nRow;
	m_nTotal = matrix.m_nTotal;
	memcpy(m_pNode, matrix.m_pNode, sizeof(Node)*m_nTotal);

}

void CMatrix::DefaultInit1(void)
{
	m_pNode[0].row = 0;
	m_pNode[0].col = 0;
	m_pNode[0].val = 4;

	m_pNode[1].row = 0;
	m_pNode[1].col = 2;
	m_pNode[1].val = 5;

	m_pNode[2].row = 0;
	m_pNode[2].col = 3;
	m_pNode[2].val = 4;

	m_pNode[3].row = 1;
	m_pNode[3].col = 0;
	m_pNode[3].val = 5;

	m_pNode[4].row = 1;
	m_pNode[4].col = 1;
	m_pNode[4].val = 3;

	m_pNode[5].row = 2;
	m_pNode[5].col = 0;
	m_pNode[5].val = 7;

	m_pNode[6].row = 2;
	m_pNode[6].col = 3;
	m_pNode[6].val = 7;
}

// SparseMatrix.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Matrix.h"

int _tmain(int argc, _TCHAR* argv[])
{
	CMatrix matrix;
	matrix.InitMatrix(5, 8, 4);
	matrix.InputMatrix();
	//matrix.DefaultInit();
	cout<<"稀疏矩阵如下:"<<endl;
	matrix.OutPutMatrix();
	matrix.FastReverse();
	cout<<"稀疏矩阵转置后如下:"<<endl;
	matrix.OutPutMatrix();
	CMatrix matrixSource, matrixDest;
	matrixSource.InitMatrix(5, 4, 3);
	matrixSource.InputMatrix();
	//matrixSource.DefaultInit1();
	cout<<"稀疏矩阵如下:"<<endl;
	matrixSource.OutPutMatrix();
	
	matrix.Multiply(matrixSource, matrixDest);
	cout<<"稀疏矩阵乘积如下:"<<endl;
	matrixDest.OutPutMatrix();
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值