矩阵的计算 C/CPP代码
作者:互联网
矩阵的计算
C实现
#include <stdio.h>
#define N 100
typedef struct matrix
{
int h, w, aa[N][N];
//h:长 , w:宽, aa 矩阵
} jz;
void initjz(jz *a, int x, int y) //初始化,行x列y,全部0
{
a->h = x, a->w = y;
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
a->aa[i][j] = 0;
}
jz jz_cheng(jz x, jz y) //返回x*y
{
jz s;
initjz(&s, x.h, y.w);
for (int i = 0; i < x.h; i++)
for (int j = 0; j < y.w; j++)
for (int k = 0; k < x.w; k++)
s.aa[i][j] += x.aa[i][k] * y.aa[k][j];
return s;
}
jz jz_jia(jz x, jz y) //返回x+y
{
jz s;
initjz(&s, x.h, x.w);
for (int i = 0; i < s.h; i++)
for (int j = 0; j < s.w; j++)
s.aa[i][j] = x.aa[i][j] + y.aa[i][j];
return s;
}
jz jz_jian(jz x, jz y) //返回x-y
{
jz s;
initjz(&s, x.h, x.w);
for (int i = 0; i < s.h; i++)
for (int j = 0; j < s.w; j++)
s.aa[i][j] = x.aa[i][j] - y.aa[i][j];
return s;
}
jz jz_chengf(jz a, int k) //返回a的k次
{
if (k == 1)
return a;
jz aa = jz_chengf(a, k / 2);
aa = jz_cheng(aa, aa);
if (k % 2 != 0)
return jz_cheng(aa, a);
else
return aa;
}
jz jz_mod(jz x, int m) //返回整体mod
{
jz s;
initjz(&s, x.h, x.w);
for (int i = 0; i < s.h; i++)
for (int j = 0; j < s.w; j++)
s.aa[i][j] = x.aa[i][j] % m;
return s;
}
void jz_cpy(jz *a, jz b)//复制到
{
a->h = b.h;
a->w = b.w;
for (int i = 0; i < a->h; i++)
for (int j = 0; j < a->w; j++)
a->aa[i][j] = b.aa[i][j];
return;
}
int main()
{
jz a, b;
initjz(&a, 23, 31);
b = a; //或jz_cpy(&b,a);
jz c = jz_jia(a, b);
return 0;
}
CPP实现
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 100;
typedef struct matrix
{
int h, w, aa[N][N];
matrix() {}
matrix(int x, int y) : h(x), w(y)
{
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
aa[i][j] = 0;
}
friend matrix operator*(matrix x, matrix y)
{
matrix s(x.h, y.w);
for (int i = 0; i < x.h; i++)
for (int j = 0; j < y.w; j++)
for (int k = 0; k < x.w; k++)
s.aa[i][j] += x.aa[i][k] * y.aa[k][j];
return s;
}
friend matrix operator+(matrix x, matrix y)
{
matrix s(x.h, x.w);
for (int i = 0; i < s.h; i++)
for (int j = 0; j < s.w; j++)
s.aa[i][j] = x.aa[i][j] + y.aa[i][j];
return s;
}
friend matrix operator-(matrix x, matrix y)
{
matrix s(x.h, x.w);
for (int i = 0; i < s.h; i++)
for (int j = 0; j < s.w; j++)
s.aa[i][j] = x.aa[i][j] - y.aa[i][j];
return s;
}
friend matrix operator^(matrix a, int k) //bushi xor
{//是乘方,不是XOR
if (k == 1)
return a;
matrix aa = a ^ (k / 2);
aa = aa * aa;
if (k % 2 != 0)
return aa * a;
else
return aa;
}
friend matrix operator%(matrix a, int k)
{
for (int i = 0; i < a.h; i++)
for (int j = 0; j < a.w; j++)
a.aa[i][j] %= k;
return a;
}
} jz;
jz a;
int main()
{
return 0;
}
标签:aa,return,matrix,jz,int,代码,矩阵,++,CPP 来源: https://www.cnblogs.com/ssj233-aaa/p/16366234.html