矩阵类的代码(C++)

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

时间: 2024-11-08 19:32:08

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

【甘道夫】MapReduce实现矩阵乘法--实现代码

之前写了一篇分析MapReduce实现矩阵乘法算法的文章:[甘道夫]Mapreduce实现矩阵乘法的算法思路 为了让大家更直观的了解程序执行,今天编写了实现代码供大家参考. 编程环境: java version "1.7.0_40" Eclipse Kepler Windows7 x64 Ubuntu 12.04 LTS Hadoop2.2.0 Vmware 9.0.0 build-812388 输入数据: A矩阵存放地址:hdfs://singlehadoop:8020/wordsp

矩阵类的python实现

科学计算离不开矩阵的运算.当然,python已经有非常好的现成的库:numpy. 我写这个矩阵类,并不是打算重新造一个轮子,只是作为一个练习,记录在此. 注:这个类的函数还没全部实现,慢慢在完善吧. 全部代码: 1 import copy 2 3 class Matrix: 4 '''矩阵类''' 5 def __init__(self, row, column, fill=0.0): 6 self.shape = (row, column) 7 self.row = row 8 self.co

第4次作业类测试代码+105032014166+张珍珍

第4次作业:准备类测试代码 类测试代码的具体要求如下: (1)设计三角形完整程序 已经完成的方法是:  String triangle(int a,int b,int c) 现在要求继续增加新的功能: 建立界面,至少包含以下元素,但不限于此: 完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c) 要求: 1.        画出类图: 2.        完成界面和相应的功能

JS表单验证类HTML代码实例

以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮点数验证.日期验证.邮件检查.网址验证.固定电话和手机号码验证.IP地址验证.邮编和QQ号码验证. MSN和身份证验证等. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.

矩阵类(基本)

以下为矩阵类 1 namespace KUNKUN_MATRIX{ 2 const int MOD = 1000000007; 3 template<class E> 4 class Matrix{ 5 public: 6 Matrix(size_t _m,size_t _n); 7 Matrix(const Matrix& copy); 8 Matrix operator*(const Matrix &b); 9 E* operator[](size_t i); 10 Mat

CUDA 矩阵相乘完整代码

#include "cuda_runtime.h"#include "device_launch_parameters.h" #include <stdio.h>#include <stdlib.h>#include <time.h>#include "cublas_v2.h" #define BLOCK_SIZE 16 cudaError_t multiCuda(float *c, float *a, flo

第4次作业类测试代码+098+吴超

一.类图 二.代码与界面 简单的分层思想,代码目录如下: 计算日期的业务类操作代码:printDate.java;具体包括如下方法. 2.1  增加计算星期几的方法weekDay(),利用蔡勒(Zeller)公式.即w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1: 公式中的符号含义如下,w:星期:c:世纪-1:y:年(两位数):m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1.2月要看作上一年的13.14月来计算,比如2003年1月1日要看作2002年的13月

1,OC语言的前世今生 ,2,OC语言入门,3,OC语言与C的差异,4,面向对象,5,类和对象的抽象关系,6,类的代码创建,7,类的成员组成及访问

1,OC语言的前世今生 , 一, 在20世纪80年代早期,布莱德.麦克(Brad Cox)设计了OC语言,它在C语言的基础上增加了一层,这意味着对C进行了扩展,从而创造出一门新的程序设计语言,支持对象的创建和操作. 二,1985年,被赶出苹果公司的乔帮主成立了Next公司; 三, 1988年,Next计算机公司获得了OC语言的授权,并发展了OC语言库和一个开发环境,1994年,Next计算机公司(同年更名为Next软件公司)和Sun公司针对NEXTSTEP系统联合发布了一个标准规范,名为OPEN

第4次作业类测试代码+105032014138+牟平

类测试代码的具体要求如下: 设计三角形完整程序 已经完成的方法是:  String triangle(int a,int b,int c) 现在要求继续增加新的功能: 建立界面,至少包含以下元素,但不限于此: 完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c) 一.类图 二.功能界面 1 2 3 4 5 6 三.代码: import java.awt.EventQueue;