矩阵
时间:2022-05-30
本文章向大家介绍矩阵,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
在说矩阵前,先说一小小点关于数组的知识: 数组分为两种:
- 行主映射 从第一行开始,依次对没一行的索引从左至右连续编号。
- 列主映射 对索引的编号从最左列开始,依次对每一列的索引从上到下连续编号。
- 不规则的二维数组 但一个二维数组有两行或者更多的行,每一行的元素个数不等时,这个数组就被称为不规则数组。对应的,所谓规则数组,即为每行元素个数相同的二维数组。
一个m×n的矩阵,是一个m行、n列的表,m和n是矩阵的维数。 矩阵主要完成的操作有三种:
- 矩阵相加
- 矩阵转置
- 矩阵相乘 这三个概念,大学线性代数的课程里都讲过,这里就不赘述。 下面直接上代码: matrix.cpp
/*
* 矩阵测试代码
* matrix.cpp
*/
#include<iostream>
#include"matrix.h"
using namespace std;
int main(void)
{
//测试矩阵类
matrix<int> x(3,2),y,z;
int i,j;
for(i = 1;i<=3;i++)
for(j = 1;j<=2;j++)
x(i,j) = 2*i+j;
cout << "Initalized x(x,j) = 2*i + j"<<endl;
cout <<"x(3,1) = "<<x(3,1)<<endl;
cout<<"The metrix x is:"<<endl;
cout <<x;
//矩阵赋值
y = x;
cout<<"The matrix y is:"<<endl;
cout <<y;
//两个矩阵相加结果
z = y + x;
cout <<"y + x is"<<endl;
cout<<z;
//矩阵求负
cout<<"-(y+x) is "<<endl;
cout<<-z;
//矩阵相乘
matrix<int> w(2,3);
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
w(i,j) = i+j;
}
}
cout<<"Initialized w(i,j) = i+j"<<endl;
cout<<"w is "<<endl;
cout<< w <<endl;
z = y*w;
cout<<"y * w is"<<endl;
cout<< z<<endl;
cout<<"Hello World!"<<endl;
return 0;
}
matrix.h
/*
* 矩阵类,实现了矩阵的一些基础性质:矩阵相加,相乘,矩阵转置
* matrix.h
*/
#ifndef MATRIX_H
#define MATRIX_H
#include"myexceptions.h"
using namespace std;
template<class T>
class matrix
{
friend ostream& operator<<(ostream&,const matrix<T>&);
public:
//构造函数
matrix(int theRows = 0,int theColumns = 0);
//复制构造函数
matrix(const matrix<T>&);
//析构函数
~matrix(){delete [] element;}
int rows() const {return theRows;}//返回数组行数
int columns() const {return theColumns;}//返回数组列数
T& operator()(int i,int j) const;//对"()"进行运算符重载
matrix<T>& operator=(const matrix<T>&);//对"="进行运算符重载
matrix<T> operator+()const;//非数组“+”
matrix<T> operator+(const matrix<T>&) const;//数组相加
matrix<T> operator-()const;//非数组减
matrix<T> operator-(const matrix<T>&) const;//数组减
matrix<T> operator*(const matrix<T>&) const;//数组乘
matrix<T>& operator+=(const T&);
private:
int theRows;//数组的行数
int theColumns;//数组的列数
T *element;//元素数组
};
/*
* 以下是对类内各种函数的具体实现
*/
//构造函数
template<class T>
matrix<T>::matrix(int theRows, int theColumns)
{
if(theColumns < 0 || theRows < 0)
throw illegalParameterValue("Rows and Columns must be >= 0");
if((theRows == 0 || theColumns == 0)
&& (theRows != 0 || theColumns != 0))
throw illegalParameterValue("Either both or neither rows and columns should be zero");
///创建矩阵
this->theColumns = theColumns;
this->theRows = theRows;
element = new T[theRows*theColumns];
}
//复制构造函数
template<class T>
matrix<T>::matrix(const matrix<T> &m)
{
theRows = m.theRows;
theColumns = m.theColumns;
element = new T [theRows*theColumns];
copy(m.element,
m.element+theColumns*theRows,
element);
}
//“=”重载
template<class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& m)
{
if(this != &m)
{
delete [] element;
theRows = m.theRows;
theColumns = m.theColumns;
element = new T[theColumns*theRows];
copy(m.element,m.element+theRows*theColumns,element);
}
return *this;
}
//"()"重载
template<class T>
T& matrix<T>::operator ()(int i,int j) const
{
if(i<1 || i > theRows
|| j < 1 || j > theColumns)
throw matrixIndexOutOfBounds();
return element[(i-1)*theColumns+j-1];
}
//"+"重载
template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m)const
{
if(theRows != m.theRows
|| theColumns != m.theColumns)
throw matrixSizeMismatch();
matrix<T> w(theRows,theColumns);//创建结果矩阵w
for(int i = 0;i< theRows*theColumns;i++)
w.element[i] = element[i] + m.element[i];
return w;
}
//"-"重载
template<class T>
matrix<T> matrix<T>::operator - (const matrix<T>& m)const
{
if(theRows != m.theRows
|| theColumns != m.theColumns)
throw matrixSizeMismatch();
matrix<T> w(theRows,theColumns);//创建结果数组
for(int i = 0;i< theColumns*theRows;i++)
w.element[i] = element[i]-m.element[i];
return w;
}
template<class T>
matrix<T> matrix<T>::operator - () const
{
matrix<T> w(theRows,theColumns);
for(int i = 0;i<theColumns*theRows;i++)
w.element[i] = -element[i];
return w;
}
//"*"重载,实现数组乘法
template<class T>
matrix<T> matrix<T>::operator * (const matrix<T>& m) const
{
//计算(*this)*m的结果
if(theColumns != m.theRows)
throw matrixSizeMismatch();
matrix<T> w(theRows,m.theColumns);
int ct = 0,cm = 0,cw = 0;
for(int i = 1;i <= theRows;i++)
{
for(int j = 1;j <= m.theColumns;j++)
{
T sum = element[ct] * m.element[cm];
for(int k = 2;k<=theColumns;k++)
{
ct++;
cm += m.theColumns;
sum += element[ct]*m.element[cm];
}
w.element[cw++] = sum;
ct -= theColumns - 1;
cm = j;
}
ct += theColumns;
cm = 0;
}
return w;
}
template<class T>
ostream& operator<<(ostream& out,const matrix<T>& m)
{
int k = 0;
for(int i = 0;i < m.theRows;i++)
{
for(int j = 0;j < m.theRows;j++)
out << m.element[k++] << " ";
out <<endl;
}
return out;
}
ostream& operator <<(ostream& out,const matrix<int>& m)
{
int k = 0;
for(int i = 0;i < m.theRows;i++)
{
for(int j = 0;j < m.theColumns;j++)
out << m.element[k++] <<" ";
out <<endl;
}
return out;
}
#endif // MATRIX_H
myExceptions.h
/*
*异常类
* myExceptions.h
*/
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H
#include<string>
using namespace std;
//参数值不合法
class illegalParameterValue
{
public:
illegalParameterValue(string theMessage = "Illegal parameter value")
{
message = theMessage;
}
void outputMessage()
{
cout << message <<endl;
}
private:
string message;
};
//矩阵索引值越界
class matrixIndexOutOfBounds
{
public:
matrixIndexOutOfBounds
(string theMessage = "Matrix index out of bounds")
{
message = theMessage;
}
void outputMessage()
{
cout <<message <<endl;
}
private:
string message;
};
class matrixSizeMismatch
{
public:
matrixSizeMismatch
(string theMessage = "The size of the two matrics doesn't match")
{
message = theMessage;
}
void outputMessage()
{
cout << message <<endl;
}
private:
string message;
};
#endif // MYEXCEPTIONS_H
- 十条有用的 Golang语言 技术
- Android 开发者必知必会的权限管理知识
- 你可能需要为你的 APP 适配 iOS 11
- Golang语言 redis 使用
- 页面结构化在 Android 上的尝试
- iOS 11: CORE ML—浅析
- 高性能网络编程7--tcp连接的内存使用
- Android 平台 Native 代码的崩溃捕获机制及实现
- go语言变参,匿名函数的多种用法
- 问题帖子--Concurrent Read/Write Map
- Android 混淆那些事儿
- H5 直播避坑指南
- H5 和移动端 WebView 缓存机制解析与实战
- 根据IE版本加载不同CSS样式的方法小结,解决低版本IE兼容问题
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法