1.矩阵定义
由m×n个数排列而成的,称为m×n矩阵
2.矩阵的基本运算
矩阵的加减法运算
减法同样满足
矩阵单个数相乘
矩阵乘法
[x行y列]×[y行z列]=[x行z列]
必须要求第一个矩阵的列数等于第二个矩阵的行数
例子如下,
我们不难发现,如果用一个c数组表示,那么可以表示为
代码表示如下
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn=1001;
int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int m1,n1,m2,n2;
cin>>m1>>n1>>m2>>n2;
for(int i=1;i<=n1;i++){
for(int j=1;j<=m1;j++){
cin>>a[i][j];
}
}
for(int i=1;i<=n2;i++){
for(int j=1;j<=m2;j++){
cin>>b[i][j];
}
}
for(int i=1;i<=n1;i++){
for(int j=1;j<=m2;j++){
for(int k=1;k<=m1;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=1;i<=n1;i++){
for(int j=1;j<=m2;j++){
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
3.单位元
单位元是集合中的一个特殊元素,它在与其他元素的运算过程中保持不变性。具体来说:
- 当单位元与自身相乘时,得到的结果等于原单位元本身。
- 如果单位元与另一个元素相乘,得到的也是这个元素本身。
4.单位矩阵
主对角线为1,其他元素为0,成为单位矩阵
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
a[i][i]=1;
5.结构体封装矩阵算法
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
struct Mat{
long long a[105][105];
long long r,c;
Mat(int _r=0,int _c=0){
r=_r,c=_c;
memset(a,0,sizeof(a));
if(c==0) c=r;
}
void unit(){
memset(a,0,sizeof(a));
for(int i=1;i<=r;i++) a[i][i]=1;
}
Mat operator*(const Mat &t) const{
Mat ans(r,t,c);
for(int i=1;i<=r;i++){
for(int j=1;j<=t.c;j++){
for(int k=1;k<=c;k++){
ans.a[i][j]+=a[i][k]*t.a[k][j];
}
}
}
return ans;
}
Mat operator%(const long long &t) const{
Mat ans=*this;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
ans.a[i][j]=ans.a[i][j]%t;
}
}
return ans;
}
Mat pow(long long b,long long p){
Mat ans(r,c);
Mat a=*this;
ans.unit();
while(b){
if(b&1) ans=(ans*a)%p;
a=(a*a)%p;
b>>=1;
}
return ans%p;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}