矩阵的基本变换 | Matrix

#include<bits/stdc++.h>

using namespace std;

#define max(x,y) Max(x,y)
#define min(x,y) Min(x,y)
#define REP(i,j,k) for (int i=(j),_end_=(k);i<=_end_;++i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define pb push_back
#define ALL(x) ((x).begin()+1),(x).end()
#define SZ(x) (int((x).size()-1))

template <typename T> inline T Max(T x,T y){ return x > y ? x : y; }
template <typename T> inline T Min(T x,T y){ return x < y ? x : y; }

typedef long long LL;

const int dmax=1010,INF=0x3f3f3f3f,MOD=1e6+7;

int n;

struct matrix{
	int n,m;
	int mat[dmax][dmax];
	matrix(){ memset(mat,0,sizeof(mat)); }
	void powset(int n){
		REP(i,1,n)
			REP(j,1,n)
				mat[i][j]=i==j;
	}
	void fibset(){ n=m=2; mat[1][1]=mat[1][2]=mat[2][1]=1; }
	int fib(){ return mat[2][1]; }
	void sizeset(int x,int y){ n=x,m=y; }
	void clear(){ n=m=0; memset(mat,0,sizeof(mat)); }
	void inversion(){
		matrix tmp;
		REP(i,1,n)
			REP(j,1,m)
				tmp.mat[j][i]=mat[i][j];
		REP(i,1,n)
			REP(j,1,m)
				tmp.mat[j][i]=mat[i][j];
		REP(i,1,m)
			REP(j,1,n)
				mat[i][j]=tmp.mat[i][j];
		swap(n,m);
	}
};

inline bool cmp(const matrix &x,const matrix &y){ return x.n==y.n && x.m==y.m; }
inline matrix operator +(matrix x,matrix y){
	matrix tmp;
	if (!cmp(x,y)){
		debug("Matrix calc failed ( error = + )\n");
		return tmp;
	}
	int n=x.n,m=x.m;
	tmp.sizeset(n,m);
	REP(i,1,n)
		REP(j,1,m)
			tmp.mat[i][j]=x.mat[i][j]+y.mat[i][j];
	return tmp;
}
inline void operator +=(matrix &x,matrix y){ x=x+y; }
inline matrix operator *(matrix x,matrix y){
	matrix tmp;
	if (x.m!=y.n){
		debug("Matrix calc failed ( error = * )\n");
		return tmp;
	}
	int n=x.n,p=x.m,m=y.m;
	tmp.sizeset(n,m);
	REP(i,1,n)
		REP(j,1,m)
			REP(k,1,p)
				tmp.mat[i][j]+=x.mat[i][k]*y.mat[k][j];
	return tmp;
}
inline void operator *=(matrix &x,matrix y){ x=x*y; }
inline matrix operator ^(matrix x,int y){
	matrix tmp;
	tmp.powset(n);
	tmp.sizeset(2,2);
	while (y>0){
		if (y&1)
			tmp*=x;
		x*=x;
		y>>=1;
	}
	return tmp;
}
inline void fib(matrix &x,int y){ x=x^y; }
void init(matrix &x){
	int n,m;
	scanf("%d%d",&n,&m);
	x.sizeset(n,m);
	REP(i,1,x.n)
		REP(j,1,x.m)
			scanf("%d",&x.mat[i][j]);
}
void outit(matrix x){
	REP(i,1,x.n)
		REP(j,1,x.m)
			printf("%d%c",x.mat[i][j],j==x.m?'\n':' ');
} 
int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	matrix x,y;
	init(x);
	init(y);
	x+=y;
	outit(x);
	x.clear();
	y.clear();
	init(x);
	init(y);
	x*=y;
	outit(x);
	x.clear();
	init(x);
	x.inversion();
	outit(x);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值