矩阵类设计

GitHub

用C++类封装了一个矩阵的类,实现了加、减和乘法的运算符重载,同时复习了一遍拷贝、移动等构造函数和赋值运算符的重载。

Matrix.h

#pragma once
#include <stdio.h>
#include <iostream>

template<typename T>
class Matrix
{
public:
	Matrix()
	{
		Row = Column = 0;
		Item = nullptr;
	}
	Matrix(int row, int column)
	{
		Create(row, column);
	}
	Matrix(const Matrix<T>& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = new T * [Row];
		for (int i = 0; i < Row; i++)
		{
			Item[i] = new T[Column];
			::memcpy(Item[i], other.Item[i], sizeof(T) * Column);
		}
	}
	Matrix<T>& operator=(const Matrix<T>& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = new T * [Row];
		for (int i = 0; i < Row; i++)
		{
			Item[i] = new T[Column];
			::memcpy(Item[i], other.Item[i], sizeof(T) * Column);
		}
		return *this;
	}
	Matrix(Matrix<T>&& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = other.Item;
		other.Item = nullptr;
	}
	Matrix<T>& operator=(Matrix<T>&& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = other.Item;
		other.Item = nullptr;
	}
	~Matrix()
	{
		Release();
	}
	void Create(int row, int column)
	{
		Row = row;
		Column = column;
		Item = new T * [row];
		for (int i = 0; i < row; i++)
		{
			Item[i] = new T[column];
		}
	}
	void Release()
	{
		if (Item != nullptr)
		{
			for (int i = 0; i < Row; i++)
			{
				delete[] Item[i];
			}
			delete[] Item;
		}
		Item = nullptr;
		Row = 0;
		Column = 0;
	}
	void Resize(int row, int column)
	{
		if (row == Row && column == Column)
			return;
		Release();
		Create(row, column);
	}

	int* operator[](int i)
	{
		return Item[i];
	}
	Matrix<T> operator+(const Matrix<T>& other)
	{
		if (other.Row != Row || other.Column != Column)
			return Matrix<T>();
		Matrix<T> result(Row, Column);
		for (auto i = 0; i < Row; i++)
		{
			for (auto j = 0; j < Column; j++)
			{
				result[i][j] = Item[i][j] + other.Item[i][j];
			}
		}
		return std::move(result);
	}
	Matrix<T> operator-(const Matrix<T>& other)
	{
		if (other.Row != Row || other.Column != Column)
			return Matrix<T>();
		Matrix<T> result(Row, Column);
		for (auto i = 0; i < Row; i++)
		{
			for (auto j = 0; j < Column; j++)
			{
				result[i][j] = Item[i][j] - other.Item[i][j];
			}
		}
		return std::move(result);
	}
	Matrix<T> operator*(const Matrix<T>& other)
	{
		if (Column != other.Row)
			return Matrix<T>();
		Matrix<T> result(Row, other.Column);
		for (auto i = 0; i < Row; i++)
		{
			for (auto j = 0; j < other.Column; j++)
			{
				T key = 0;
				for (auto k = 0; k < Column; k++)
				{
					key += Item[i][k] * other.Item[k][j];
				}
				result[i][j] = key;
			}
		}
		return std::move(result);
	}
	int GetRow() { return Row; }
	int GetColumn() { return Column; }
	bool SetRowValue(int row, int* values)
	{
		if (row >= Row)
			return false;
		for (int i = 0; i < Column; i++)
		{
			Item[row][i] = values[i];
		}
		return true;
	}
	void SetValue(int** values)
	{
		for(auto i = 0; i < Row; i++)
			for (auto j = 0; j < Column; j++)
			{
				Item[i][j] = values[i][j];
			}
	}
	void Print(std::string name)
	{
		printf("%s:\n", name.c_str());
		for (int i = 0; i < Row; i++)
		{
			for (int j = 0; j < Column; j++)
			{
				printf("%d\t", Item[i][j]);
			}
			printf("\n");
		}
		printf("\n");
	}
private:
	int Row;
	int Column;

	T** Item;
};


void TestMatrix();

Matirx.cpp

#include "Matrix.h"

using namespace std;

void TestMatrix()
{
	Matrix<int> m1(2,2), m2(2, 2);
	int a[2] = { 1,2 };
	for (auto i = 0; i < 2;i++)
	{
		m1.SetRowValue(i, a);
		m2.SetRowValue(i, a);
	}
	m1.Print("m1");
	m2.Print("m2");
	Matrix<int> m3 = m1 + m2;
	m3.Print("m3");
	Matrix<int> m4 = m1 - m2;
	m4.Print("m4");

	Matrix<int> m5(2, 3), m6(3, 2);
	int b[3] = { 1, 2, 3 };
	int c[2] = { 1, 2 };
	m5.SetRowValue(0, b);
	m5.SetRowValue(1, b);
	m6.SetRowValue(0, c);
	m6.SetRowValue(1, c);
	m6.SetRowValue(2, c);
	Matrix<int> m7 = m5 * m6;
	m5.Print("m5");
	m6.Print("m6");
	m7.Print("m7");

	Matrix<int> m8(2, 3);
	int** p = new int* [2];
	p[0] = new int[3]{ 1, 2, 3 };
	p[1] = new int[3]{ 4, 5,6 };
	m8.SetValue(p);
	m8.Print("m8");

	Matrix<int> m9(m8), m10;
	m10 = m8;
	m9.Print("m9");
	m10.Print("m10");
}

测试用例输出结果:

m1:
1       2
1       2

m2:
1       2
1       2

m3:
2       4
2       4

m4:
0       0
0       0

m5:
1       2       3
1       2       3

m6:
1       2
1       2
1       2

m7:
6       12
6       12

m8:
1       2       3
4       5       6

m9:
1       2       3
4       5       6

m10:
1       2       3
4       5       6


E:\GitHub\IntroductionToAlgorithms\C++\build\Debug\IntroductionToAlgorithms.exe (进程 14088)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .


 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一份简单的 Java 矩阵 Matrix 的代码: ```java public class Matrix { private final int rows; private final int cols; private final double[][] data; public Matrix(int rows, int cols) { if (rows <= 0 || cols <= 0) { throw new IllegalArgumentException("rows and cols must be positive"); } this.rows = rows; this.cols = cols; this.data = new double[rows][cols]; } public int getRows() { return rows; } public int getCols() { return cols; } public double get(int i, int j) { checkBounds(i, j); return data[i][j]; } public void set(int i, int j, double x) { checkBounds(i, j); data[i][j] = x; } private void checkBounds(int i, int j) { if (i < 0 || i >= rows) { throw new IndexOutOfBoundsException("row index out of bounds"); } if (j < 0 || j >= cols) { throw new IndexOutOfBoundsException("col index out of bounds"); } } public static Matrix add(Matrix a, Matrix b) { if (a.rows != b.rows || a.cols != b.cols) { throw new IllegalArgumentException("matrices must have the same dimensions"); } Matrix c = new Matrix(a.rows, a.cols); for (int i = 0; i < a.rows; i++) { for (int j = 0; j < a.cols; j++) { c.data[i][j] = a.data[i][j] + b.data[i][j]; } } return c; } public static Matrix multiply(Matrix a, Matrix b) { if (a.cols != b.rows) { throw new IllegalArgumentException("number of columns in A must match number of rows in B"); } Matrix c = new Matrix(a.rows, b.cols); for (int i = 0; i < a.rows; i++) { for (int j = 0; j < b.cols; j++) { double sum = 0.0; for (int k = 0; k < a.cols; k++) { sum += a.data[i][k] * b.data[k][j]; } c.data[i][j] = sum; } } return c; } } ``` 这个矩阵支持以下操作: - 创建矩阵对象:`Matrix m = new Matrix(rows, cols)` - 获取矩阵的行数和列数:`m.getRows()` 和 `m.getCols()` - 获取矩阵的某个元素:`m.get(i, j)` - 设置矩阵的某个元素:`m.set(i, j, x)` - 矩阵加法:`Matrix.add(a, b)` - 矩阵乘法:`Matrix.multiply(a, b)`

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值