我是靠谱客的博主 体贴篮球,最近开发中收集的这篇文章主要介绍经典数据结构之矩阵的基本运算,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

用一维数组来表示矩阵其实质与二维数组没啥区别,只是多了下标一步映射而已。由于方法本身很简单,所以不多做介绍。很容易扩展转置等方法,从而实现一个更为全面的矩阵类型。

CMatrix.h

#ifndef CMATRIX_HHH
#define CMATRIX_HHH

#include <cassert>
#include <iostream>
#include <stdlib.h>
template<typename T>
class CMatrix{
private:
	// members;
    T* m_pElem;
	int m_nRow, m_nCol;   //the number of rows: m_nRow. So as to m_nCol;
public:
	// constructors;
    CMatrix(const int& row, const int& m_nCol);
	CMatrix(const CMatrix<T>& matrix);
	~CMatrix();
	// methods;
	// print the matrix out;
	void print(std::ostream& out);
    // operator+;
	CMatrix<T> operator+() const;
	CMatrix<T> operator+(const CMatrix<T>& matrix) const;
	// operator-;
	CMatrix<T> operator-() const;
	CMatrix<T> operator-(const CMatrix<T>& matrix) const;
	// operator*;
	CMatrix<T> operator*(const T& data) const;
	CMatrix<T> operator*(const CMatrix<T>& matrix) const;
	// operator ();
	T& operator()(const int& i, const int& j) const;
	// operator =;
	CMatrix<T>& operator=(const CMatrix<T>& matrix);
	// operator +=;
	CMatrix<T>& operator+=(const CMatrix<T>& matrix);
	// print;
	// row num;
	int mRows() const;
	// col num;
	int mCols() const;
};
// operator <<
	//friend std::ostream& operator<<(std::ostream& out, const CMatrix<T>& matrix);
	// row num;
template<typename T>
void CMatrix<T>::print(std::ostream& out){
    for(int i = 0; i < m_nRow * m_nCol; i ++){
		if( i % m_nCol == 0 )
			out << endl;
		out << m_pElem[i] << " ";
	}
	out <<endl;
}

template <typename T>
CMatrix<T>::CMatrix(const int& row, const int& col):
    m_nRow(row), m_nCol(col){
	assert((m_nRow > 0) && (m_nCol > 0));
    int length = row * col;
    m_pElem = new T[length];
}

template <typename T>
CMatrix<T>::CMatrix(const CMatrix<T>& matrix):
    m_nRow(matrix.m_nRow), m_nCol(matrix.m_nCol){
	assert((m_nRow > 0) && (m_nCol > 0));
	m_pElem = new T[m_nRow * m_nCol];
	for(int i = 0; i < m_nRow * m_nCol; i ++)
		m_pElem[i] = matrix.m_pElem[i];
}

template <typename T>
CMatrix<T>::~CMatrix(){
	delete [] m_pElem;
}

template <typename T>
T& CMatrix<T>::operator()(const int& i, const int& j) const{
    assert((i > 0 && i <= m_nRow) && (j > 0 && j <= m_nCol));
    return m_pElem[(i - 1) * m_nCol + j - 1];
}

template <typename T>
CMatrix<T> CMatrix<T>::operator+() const{
	CMatrix<T> tmpMatrix(m_nRow, m_nCol);
	for(int i = 0; i < m_nRow * m_nCol; i ++){
        tmpMatrix.m_pElem[i] = abs(m_pElem[i]);
	}
	return tmpMatrix;
}

template <typename T>
CMatrix<T> CMatrix<T>::operator+(const CMatrix<T>& matrix) const{
    assert((m_nRow == matrix.m_nRow) && (m_nCol == matrix.m_nCol));
	CMatrix<T> tmpMatrix(m_nRow, m_nCol);
    for(int i = 0; i < m_nRow * m_nCol; i ++){
        tmpMatrix.m_pElem[i] = m_pElem[i] + matrix.m_pElem[i];
	}
	return tmpMatrix;
}

template <typename T>
CMatrix<T> CMatrix<T>::operator-() const{
	CMatrix<T> tmpMatrix(m_nRow, m_nCol);
	for(int i = 0; i < m_nRow * m_nCol; i ++){
        tmpMatrix.m_pElem[i] = (-m_pElem[i]);
	}
	return tmpMatrix;
}

template <typename T>
CMatrix<T> CMatrix<T>::operator-(const CMatrix<T>& matrix) const{
    assert((m_nRow == matrix.m_nRow) && (m_nCol == matrix.m_nCol));
	CMatrix<T> tmpMatrix(m_nRow, m_nCol);
    for(int i = 0; i < m_nRow * m_nCol; i ++){
        tmpMatrix.m_pElem[i] = m_pElem[i] - matrix.m_pElem[i];
	}
	return tmpMatrix;
}
template<typename T>
CMatrix<T> CMatrix<T>::operator*(const T& data) const{
	CMatrix<T> tmpMatrix(m_nRow, m_nCol);
	for(int i = 0; i < m_nRow * m_nCol; i ++)
		tmpMatrix.m_pElem[i] = m_pElem[i]  * data;
	return tmpMatrix;
}
template <typename T>
CMatrix<T> CMatrix<T>::operator*(const CMatrix<T>& matrix) const{
    assert(matrix.m_nRow == m_nCol);
	CMatrix<T> tmpMatrix(m_nRow, matrix.m_nCol);
    
	for(int i = 0; i < m_nRow; i ++){
       for(int j = 0; j < matrix.m_nCol; j ++){
		   tmpMatrix.m_pElem[i * matrix.m_nCol + j] = 0;
		   for(int k = 0; k < matrix.m_nRow; k ++){
			   tmpMatrix.m_pElem[i * matrix.m_nCol + j] += m_pElem[i * m_nCol + k] * matrix.m_pElem[j + matrix.m_nCol * k];
		   }
	    }
	}
	return tmpMatrix;
        
}

template <typename T>
CMatrix<T>& CMatrix<T>::operator=(const CMatrix<T>& matrix){
	m_nRow = matrix.m_nRow;
	m_nCol = matrix.m_nCol;
	delete [] m_pElem;
	m_pElem = new T[m_nRow * m_nCol];
	for(int i = 0; i < m_nRow * m_nCol; i ++){
		m_pElem[i] = matrix.m_pElem[i];
	}
	return *this;
}

template <typename T>
CMatrix<T>& CMatrix<T>::operator+=(const CMatrix<T>& matrix){
    assert((m_nRow == matrix.m_nRow) && (m_nCol == matrix.m_nCol));
    for(int i = 0; i < m_nRow * m_nCol; i ++)
		m_pElem[i] += matrix.m_pElem[i];
	return *this;
}

template <typename T>
int CMatrix<T>::mRows() const{
	return m_nRow;
}

template <typename T>
int CMatrix<T>::mCols() const{
	return m_nCol;
}
#endif

main.cpp

#include "CMatrix.h"
#include <iostream>
using namespace std;
int main(){
	CMatrix<int> matrix(2,5);
    for(int i = 1; i <= 2; i ++){
		for(int j = 1; j <= 5; j ++)
			matrix(i,j) = -2*i;
	}
	cout << "print matrix" << endl;
	matrix.print(cout);

    CMatrix<int> tmp = matrix;
	tmp.print(cout);
    
	cout << "print -matrix" << endl;
	tmp = (-matrix);
	tmp.print(cout);
    
	cout << "print +matrix" << endl;
	tmp = +matrix;
	tmp.print(cout);

	cout << "print -matrix + matrix" << endl;
	tmp += matrix;
	tmp.print(cout);
    
	cout << "print k * matrix" << endl;
	tmp = matrix;
	tmp = tmp * 2;
	tmp.print(cout);

	cout << "print the sum of each row" << endl;
    CMatrix<int> tmpMul(5,1);
	for(int i = 1; i <= 5; i ++){
		tmpMul(i,1) = 1;
	}
	tmpMul = matrix * tmpMul;
	tmpMul.print(cout);

	system("pause");
	return 0;
}

测试输出

print matrix

-2 -2
-2 -2
-2 -4
-4 -4
-4 -4

-2 -2
-2 -2
-2 -4
-4 -4
-4 -4
print -matrix

2 2
2 2
2 4
4 4
4 4
print +matrix

2 2
2 2
2 4
4 4
4 4
print -matrix + matrix

0 0
0 0
0 0
0 0
0 0
print k * matrix

-4 -4
-4 -4
-4 -8
-8 -8
-8 -8

-10 -20
请按任意键继续. . .





最后

以上就是体贴篮球为你收集整理的经典数据结构之矩阵的基本运算的全部内容,希望文章能够帮你解决经典数据结构之矩阵的基本运算所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部