#include "Stdio.h"
#include "memory.h"
template
<
typename
T>
void
TypePrint(T v);
template
<
typename
T,
int
M,
int
N>
class
Matrix
{
public
:
Matrix(
void
){
data =
new
T[M*N];
};
~Matrix(
void
){};
int
getVIndex()
{
return
M;
}
int
getHIndex()
{
return
N;
}
T getxy(
int
x,
int
y)
{
return
data[x*N+y];
}
void
setxy(
int
x,
int
y,T f)
{
data[x*N + y] = f;
}
void
setdata(T*datap,
int
size)
{
memcpy
(data,datap,size);
}
Matrix<T,N,M> transpote()
{
Matrix<T,N,M> m;
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < N; j++)
{
m.setxy(j,i,getxy(i,j));
}
}
return
m;
}
Matrix<T,M,N> operator+(Matrix<T,M,N> &adv)
{
Matrix<T,N,M> m;
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < N; j++)
{
m.setxy(getxy(i,j)+adv.getxy(i,j));
}
}
return
m;
}
Matrix<T,M,N> operator-(Matrix<T,M,N> &adv)
{
Matrix<T,N,M> m;
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < N; j++)
{
m.setxy(getxy(i,j)-adv.getxy(i,j));
}
}
return
m;
}
bool
operator==(Matrix<T,M,N> &adv)
{
Matrix<T,N,M> m;
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < N; j++)
{
if
(getxy(i,j)!=adv.getxy(i,j))
return
false
;
}
}
return
true
;
}
bool
operator!=(Matrix<T,M,N> &adv)
{
Matrix<T,N,M> m;
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < N; j++)
{
if
(getxy(i,j)!=adv.getxy(i,j))
return
true
;
}
}
return
false
;
}
void
print()
{
printf
(
"\n"
);
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < N; j++)
{
TypePrint(getxy(i,j));
printf
(
",\t"
);
}
printf
(
"\n"
);
}
}
private
:
T *data;
};
template
<
typename
T,
int
M,
int
N,
int
P>
Matrix<T,M,P> operator*(Matrix<T,M,N> &x, Matrix<T,N,P> &y)
{
Matrix<T,M,P> m;
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < P; j++)
{
T v = 0;
for
(
int
k = 0; k < N; k++)
{
v += (x.getxy(i,k)*y.getxy(k,j));
}
m.setxy(i,j,v);
}
}
return
m;
}
template
<
typename
T,
int
M,
int
N>
Matrix<T,M,N> operator*(Matrix<T,M,N> &x, T y)
{
Matrix<T,M,N> m;
for
(
int
i = 0; i < M; i++)
{
for
(
int
j = 0; j < N; j++)
{
m.setxy(i,j,m.getxy(i,j)*y);
}
}
return
m;
}
template
<
typename
T,
int
M,
int
N>
Matrix<T,M,N> operator*(T y,Matrix<T,M,N> &x)
{
return
x*y;
}
template
<>
void
TypePrint(
float
v)
{
printf
(
"%f"
,v);
}
template
<>
void
TypePrint(
int
v)
{
printf
(
"%d"
,v);
}
#define type float
int
d1[] =
{
1,2,
2,3,
3,0
};
int
d2[] =
{
2,-3,0,
0,1,-2,
-4,5,10
};
int
main()
{
Matrix<
int
,3,2> s;
s.setdata(d1,
sizeof
(d1));
Matrix<
int
,3,3> s1;
s1.setdata(d2,
sizeof
(d2));
Matrix<
int
,3,2> s2 = s1*s;
Matrix<
int
,2,3> s3 = s2.transpote();
s.print();
s1.print();
s2.print();
s3.print();
return
0;
}