The Codes of Matrix Class

Matrix.h:
#ifndef MATRIX_H
#define MATRIX_H
#include<iostream>
#include<iomanip>
#include<cassert>
#include<cmath>
#include"MatrixTypedef.h"// declare typedef's header file 
class Matrix{
public:
Matrix(const un_int &r = 0, const un_int &c = 0, const double &number = 0);// The constructor with the parameters
Matrix(const Matrix &mat);// The copy constructor 
~Matrix();// The destructor 
un_int getRows(void) const { return m_rows; }// Accessor, get the value of rows 
un_int getCols(void) const { return m_cols; }// Accessor,get the value of cols

void resize(const un_int &r, const un_int &c, const double &number);// Reseting the values of rows, cols and matrix members

bool isEmpty(void) const;// The judgment if matrix is empty 
vec& operator [](const un_int &i) ;// Overloading the subscript operator[] 

const vec& operator [](const un_int &i) const;// Overloading the subscript operator[]
friend istream& operator >>(istream &sin, Matrix &mat);// Overloading  the operator >>
friend ostream& operator <<(ostream &sout, const Matrix &mat);// Overloading the operator <<       
friend Matrix operator +(const Matrix &l, const Matrix &r);// Overloading matrix addtion        
friend Matrix operator -(const Matrix &l, const Matrix &r);// Overloading matrix subtraction         
friend Matrix operator *(const double &x, const Matrix &mat);// Overloading matrix multiplication
friend Matrix operator *(const Matrix &mat, const double &x);// Overloading matrix multiplication 
friend Matrix operator *(const Matrix &l, const Matrix &r);// Overloading matrix multiplication 
Matrix operator !(void);// Solving the inverse of a matrix 
Matrix operator ~(void);// Solving the transpose of the matrix 
double Det(void);// Solving the Determinant

private:
un_int m_rows, m_cols;// The values of rows and cols 
Mat data;// The values of Matrix 
void Resize(Mat &mat, const un_int &r, const un_int &c);// Resetting vector<vector<double>>'s size

double GetDet(const Mat &mat, const un_int &n);// Solving the Determinan 
double GetCofactor(const un_int m, const un_int n);// Solving algebraic cofactor
};
#endif

Matrix.cpp:
#include"Matrix.h"

// The constructor with the parameters

Matrix::Matrix(const un_int &r, const un_int &c, const double &number){
// If cols and rows are greater than or equal to 0
assert(r >= 0);
assert(c >= 0);

m_rows = r; m_cols = c;

// Resetting and initialising the matrix

Resize(data, m_rows, m_cols);
for(un_int i = 0;i < m_rows;++i){
    for(un_int j = 0;j < m_cols;++j){
        data[i][j] = number;

    }

}

}

// The destructor 
Matrix::~Matrix(void){
// Using swap function forcing to release the memory space 
for(un_int i = 0;i < m_rows;++i){
    vec().swap(data[i]);

}
Mat().swap(data);

}

// The copy constructor 
Matrix::Matrix(const Matrix &mat){
if(!mat.isEmpty()){
m_rows = mat.getRows();
m_cols = mat.getCols();

// Resetting matrix's size and values 
Resize(data, m_rows, m_cols);
for(un_int i = 0;i < m_rows;++i){
    for(un_int j = 0;j < m_cols;++j){
        data[i][j] = mat[i][j];

}

}

}

}

// The definition of resize function
void Matrix::resize(const un_int &r, const un_int &c, const double &number){
// If cols and rows are greater than or equal to 0 
assert(r >= 0);
assert(c >= 0);

m_rows = r;
m_cols = c;
// Resetting matrix's size and values 
Resize(data, m_rows, m_cols);
for(un_int i = 0;i < m_rows;++i){
    for(un_int j = 0;j < m_cols;++j){
        data[i][j] = number;

}

}

}
// If matrix is empty

bool Matrix::isEmpty(void) const{
bool flag = true; // If all of the member of matrix is empty 
for(un_int i = 0;i < m_rows;++i){
    if(!data[i].empty()){

flag = false; break;

}

}
return flag;

}

// Overloading the subscript operator[] 
vec& Matrix::operator [](const un_int &i) {
// If memory accessing is out of bounds

assert(i >= 0);
assert(i < m_rows);
return data[i];

}

// Overloading the subscript operator[]
const vec& Matrix::operator [](const un_int &i) const {
// If memory accessing is out of bounds
assert(i >= 0);
assert(i < m_rows);
return data[i];

}

// Overloading  the operator >> 
istream& operator >>(istream &sin, Matrix &mat){
sin >> mat.m_rows >> mat.m_cols;
// If cols and rows are greater than 0
assert(mat.m_rows > 0);
assert(mat.m_cols > 0);
// Resetting matrix's size and values
mat.resize(mat.m_rows, mat.m_cols, 0);
for (un_int i = 0;i < mat.m_rows;++i) {
    for (un_int j = 0;j < mat.m_cols;++j) {
        sin >> mat[i][j];

}

}
return sin;

}

// Overloading the operator << 
ostream& operator <<(ostream &sout, const Matrix &mat){
// If cols and rows are greater than 0
assert(mat.m_rows > 0);
assert(mat.m_cols > 0);
// Outputting matrix 
for (un_int i = 0;i < mat.m_rows;++i){
    for(un_int j = 0;j < mat.m_cols;++j){
        sout.setf(ios::fixed);
        sout.precision(3);
        sout << right << setw(6) << mat[i][j] << (j == (mat.m_cols - 1)?'\n' : ' ');

}

}
        cout << endl;
return sout;

}

// Overloading matrix multiplication 
Matrix operator *(const double &x, const Matrix &mat){
// If cols and rows are greater than 0
assert(mat.m_rows > 0);
assert(mat.m_cols > 0);
Matrix temp(mat.m_rows, mat.m_cols, 0);
for(un_int i = 0;i < mat.m_rows;++i){
    for(un_int j = 0;j < mat.m_cols;++j){
        temp[i][j] = x * mat[i][j];

}

}
return temp;

}

// Overloading matrix multiplication 
Matrix operator *(const Matrix &mat, const double &x){
// If cols and rows are greater than 0
assert(mat.m_rows > 0);
assert(mat.m_cols > 0);
Matrix temp(mat.m_rows, mat.m_cols, 0);
for(un_int i = 0;i < mat.m_rows;++i){
    for(un_int j = 0;j < mat.m_cols;++j){
        temp[i][j] = x * mat[i][j];

}

}
return temp;

}

// Overloading matrix addtion
Matrix operator +(const Matrix &l, const Matrix &r){
// If the rows and cols between the two matrixs are equal
assert(l.m_rows == r.m_rows);
assert(l.m_cols == r.m_cols);
Matrix temp(l.m_rows, r.m_cols, 0);
for(un_int i = 0;i < l.m_rows;++i){
    for(un_int j = 0;j < l.m_cols;++j){
        temp[i][j] = l[i][j] + r[i][j];

}

}
return temp;

}

// Overloading matrix subtraction 
Matrix operator -(const Matrix &l, const Matrix &r){
// If the rows and cols between the two matrixs are equal
assert(l.m_rows == r.m_rows);
assert(l.m_cols == r.m_cols);
Matrix temp(l.m_rows, r.m_cols, 0);
for(un_int i = 0;i < l.m_rows;++i){
    for(un_int j = 0;j < l.m_cols;++j){
       temp[i][j] = l[i][j] - r[i][j];

}

}
return temp;

}

// Overloading matrix multiplication 
Matrix operator *(const Matrix &l, const Matrix &r){
// If  the left operand's cols is equal to the right operand's rows
assert(l.m_cols == r.m_rows);
// Using formula solving matrix multiplication 
Matrix temp(l.m_rows, r.m_cols, 0);
for(un_int i = 0;i < l.m_rows;++i){
    for(un_int j = 0;j < r.m_cols;++j){
        for(un_int k = 0;k < l.m_cols;++k){
            temp[i][j] += (l[i][k] * r[k][j]);

}

}

}
return temp;

}

// Solving the inverse of a matrix 
Matrix Matrix::operator !(void){
const double det = Det();// Solving the Determinan 
// If determinan is equal to 0 and matrix's rows is equal to cols 
assert(fabs(det) > EPS);
assert(m_rows == m_cols);
// Solving the inverse of a matrix 
Mat temp = data; for(un_int i = 0,k = 0;i < m_rows;++i, ++k){
for(un_int j = 0, l = 0;j < m_cols;++j, ++l){
    double n = GetCofactor(k, l) / det;

if(fabs(n) < EPS) n = 0.0; // If double value is equal to 0
        data[j][i] = n;

}

}
return *this;

}

// Solving the transpose of the matrix 
Matrix Matrix::operator ~(void){
// If cols and rows are greater than or equal to 0
assert(m_rows > 0);
assert(m_cols > 0);

// If rows is equal to cols

if(m_rows == m_cols){
for(un_int i = 1;i < m_rows;++i){
    for(un_int j = 0;j <= i;++j){
        swap(data[i][j],data[j][i]); } }
        return *this;

}

// If rows is diffierent from cols 
else { Matrix temp(m_cols, m_rows, 0);
    for(un_int i = 0;i < m_cols;++i){
        for(un_int j = 0;j < m_rows;++j){
            temp[i][j] = data[j][i];

}

}
            return temp;

}

}

// Solving the Determinant 
double Matrix::Det(void){
// If rows is equal to cols
assert(m_rows == m_cols);
return GetDet(data, m_rows);

}
// The definition of Resize function 
void Matrix::Resize(Mat &mat, const un_int &r, const un_int &c){

// Resetting cols
mat.resize(r);
// Resetting rows
for(un_int i = 0;i < r;++i){ mat[i].resize(c); }}
// The definition of GetDet function

GetDet double Matrix::GetDet(const Mat &mat,const un_int &n){ double ans = 0;
// Solving the determinant
if(n == 1) {return mat[0][0]; }
else { Mat temp; temp.resize(n);
for(un_int i = 0;i < n;++i){ temp[i].resize(n); }
for(un_int i = 0;i < n;++i){ // Getting algebraic cofactor of first row,j+1 th col 
    un_int p = 0; for(un_int j = 0;j < n;++j){
    if(j != 0){ un_int q = 0;
    for(un_int k = 0;k < n;++k){
        if(k != i){ temp[p][q] = mat[j][k]; ++q;

}

}
    ++p;

}

}
ans += pow(-1, i)*mat[0][i]*GetDet(temp, n - 1); }
return ans;

}

}

// The definition of GetCofactor function
double Matrix::GetCofactor(const un_int m, const un_int n){
Matrix temp(m_rows - 1, m_cols - 1, 0);
// Getting algebraic cofactor of m th row,n th col 
for(un_int i = 0, k = 0, l = 0;i < m_rows;++i){
     for(un_int j = 0;j < m_cols;++j){

if(i != m && j != n){
             temp[k][l] = data[i][j]; ++l;
             if(l == m_cols - 1){ l = 0; ++k;

}

}

}

}
const int sign = (((m + n + 2) & 1) == 0?1 : -1);
return sign * temp.Det();// Getting cofactor's determinant

}

MatrixTypedef.h:
#ifndef MATRIXTYPEDEF_H
#define MATRIXTYPEDEF_H
#include<vector>
using namespace std;
typedef vector<double> vec;
typedef vector<vec> Mat;
typedef unsigned int un_int;
const double EPS = 1e-6;
#endif

原文链接:http://user.qzone.qq.com/1043480007/main

矩阵类的代码(C++)的更多相关文章

  1. NPOI操作EXCEL(六)——矩阵类表头EXCEL模板的解析

    哈哈~~~很高兴还活着.总算加班加点的把最后一类EXCEL模板的解析做完了... 前面几篇文章介绍了博主最近项目中对于复杂excel表头的解析,写得不好,感谢园友们的支持~~~ 今天再简单讲诉一下另一 ...

  2. OpenGL矩阵类(C++)

    概述 创建&初始化 存取器 矩阵运算 变换函数 实例:模型视图矩阵 实例:投影矩阵 概述 OpenGL固定功能管线提供4个不同类型的矩阵(GL_MODELVIEW.GL_PROJECTION. ...

  3. 精解Mat类(一):基本数据类型-固定大小的 矩阵类(Matx) 向量类(Vector)

    一.基础数据类型 1.(基础)固定大小矩阵类 matx 说明: ①    基础矩阵是我个人增加的描述,相对于Mat矩阵类(存储图像信息的大矩阵)而言. ②    固定大小矩阵类必须在编译期间就知晓其维 ...

  4. 矩阵类的python实现

    科学计算离不开矩阵的运算.当然,python已经有非常好的现成的库:numpy. 我写这个矩阵类,并不是打算重新造一个轮子,只是作为一个练习,记录在此. 注:这个类的函数还没全部实现,慢慢在完善吧. ...

  5. OpenGL矩阵类(C++) 【转】

    http://www.cnblogs.com/hefee/p/3816727.html OpenGL矩阵类(C++) 概述 创建&初始化 存取器 矩阵运算 变换函数 实例:模型视图矩阵 实例: ...

  6. JS表单验证类HTML代码实例

    以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮 ...

  7. [Java]编写自己的Matrix矩阵类

    用java实现一个简单的矩阵类,可以实现简单的矩阵计算功能. class Matrix 1.向量点乘 public static double dot(double[] x,double[] y) 2 ...

  8. C#自定义FTP访问类的代码

    如下资料是关于C#自定义FTP访问类的代码,应该对各朋友有帮助. using System; using System.Collections.Generic; using System.Text; ...

  9. C# FTP操作类的代码

    如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...

随机推荐

  1. 200行PYTHON代码实现贪吃蛇

    200行Python代码实现贪吃蛇 话不多说,最后会给出全部的代码,也可以从这里Fork,正文开始: 目前实现的功能列表: 贪吃蛇的控制,通过上下左右方向键: 触碰到边缘.墙壁.自身则游戏结束: 接触 ...

  2. Codeforces 631 (Div. 2) D. Dreamoon Likes Sequences 位运算^ 组合数 递推

    https://codeforces.com/contest/1330/problem/D 给出d,m, 找到一个a数组,满足以下要求: a数组的长度为n,n≥1; 1≤a1<a2<⋯&l ...

  3. Spring-Cloud-Netflix-Eureka注册中心

    TOC 概述 eureka是Netflix的子模块之一,也是一个核心的模块 eureka里有2个组件: 一个是EurekaServer(一个独立的项目) 这个是用于定位服务以实现中间层服务器的负载平衡 ...

  4. MyBatis(四):SqlSession及其工厂类的作用域和生命周期

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出1便就懂!b站搜索狂神说即可 https://space.bilibili.com/95256449?spm_id_from=333.788 ...

  5. k8s集群搭建笔记(细节有解释哦)

    本文中所有带引号的命令,请手动输入引号,不知道为什么博客里输入引号,总是自动转换成了中文 基本组成 pod:k8s 最小单位,类似docker的容器(也许) 资源清单:资源.资源清单语法.pod生命周 ...

  6. python 函数--闭包函数

    一.闭包函数: 在一个外函数中定义一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用. 二.实例: def outer(a): #外函数 b = 10 #临时变量 def i ...

  7. mysql yum源安装

    部署服务器环境的时候经常要安装mysql,以下是常见的安装方式 源码安装 rpm包安装 yum源安装 这篇主要介绍yum源安装. yum源下载 进入 https://dev.mysql.com/dow ...

  8. matplotlib中的基本概念

    有外语基础的朋友看这里: matplotlib官方文档 Figure(图像): 组成部分

  9. linux 块设备简要介绍

    1. 块设备简单分类:SCSI块设备和LVM逻辑卷块设备: 2. 创建块设备需要两个linux内核函数:alloc_disk:add_disk; alloc_disk:用于分配一个gendisk结构体 ...

  10. MODIS系列之NDVI(MOD13Q1)四:MRT单次及批次处理数据

    前言: 本篇文章的出发点是因为之前接触过相关研究,困囧于该系列资料匮乏,想做一个系列.个人道行太浅,不足之处还请见谅.愿与诸君共勉. 数据准备: MODIS数据产品MOD13Q1—以2010年河南省3 ...