as I don't share your minor idea for having to transfer all 1-dim matrices to 2-dim matrices forth and back in order to be able to use them (as I already clarified several times), here are the latest additional 1-dim matrix functions. Now one can simply calculate by all matrices of any size from scalar, 1x1.. ..nx1.. ..2x2.. ..nx2.. ..2xm.. ..n x m :
transposed 1-dim-1-column-n-row-matrix v[n] (which will generate a 1-dim-n-column-matrix V'[1][n] with V'[0][n]=n .
Code: Select all
#define ArrayInit2D(array, tmp, init_val, dimx, dimy) { \
ArrayInit(tmp, init_val, dimy); \
ArrayInit(array, tmp, dimx); \
ArrayInit(tmp,0,0); \
}
// [n , m] x [m , k]
void MatrixMatrixMult(float A[][], float B[][], float &C[][], int N, int M, int K){
int i, j, s;
float tmp[];
ArrayInit2D(C,tmp, 0, N, K);
for (i=0; i<N; ++i) {
for (j=0; j<K; ++j) {
C[i][j]=0;
for (s=0; s<M; ++s) {
C[i][j]=C[i][j] + A[i][s]*B[s][j];
}
}
}
}
// Matrix[n,m] x Vector[m]
void MatrixVectorMult(float A[][], float B[], float &C[], int N, int M){
int i, s;
ArrayInit(C,0, M);
for (i=0; i<M; ++i) {
C[i]=0;
for (s=0; s<N; ++s) {
C[i]=C[i] + A[i][s]*B[s];
}
}
}
// Vector[n]^T x Matrix [n,m]
void VectorMatrixMult(float A[], float B[][], float &C[], int N, int M){ // K=1
int j, s;
ArrayInit(C,0, M);
for (j=0; j<M; ++j) {
C[j]=0;
for (s=0; s<N; ++s) {
C[j]=C[j] + A[s]*B[s][j];
}
}
}
void MatrixRotate2DMath(float A[][], float &C[][], float angleDeg) {
float tmp[], x, B[][];
ArrayInit2D(C, tmp, 0, 2, 2);
x=cosd(angleDeg);
ArrayInit2D(B, tmp, x, 2, 2);
x=sind(angleDeg);
B[0][1]=-x;
B[1][0]= x;
MatrixMatrixMult(B, A, C, 2, 2, 2);
}
void VectorRotate2DMath(float A[], float &C[], float angleDeg) {
float tmp[], x, B[][];
ArrayInit(C, 0, 2);
x=cosd(angleDeg);
ArrayInit2D(B, tmp, x, 2, 2);
x=sind(angleDeg);
B[0][1]=-x;
B[1][0]= x;
MatrixVectorMult(B, A, C, 2, 2);
}
void MatrixRotate2DGeo(float A[][], float &C[][], float angleDeg) {
float tmp[], x, B[][];
angleDeg*=-1;
ArrayInit2D(C, tmp, 0, 2, 2);
x=cosd(angleDeg);
ArrayInit2D(B, tmp, x, 2, 2);
x=sind(angleDeg);
B[0][1]=-x;
B[1][0]= x;
MatrixMatrixMult(B, A, C, 2, 2, 2);
}
void VectorRotate2DGeo(float A[], float &C[], float angleDeg) {
float tmp[], x, B[][];
angleDeg*=-1;
ArrayInit(C, 0, 2);
x=cosd(angleDeg);
ArrayInit2D(B, tmp, x, 2, 2);
x=sind(angleDeg);
B[0][1]=-x;
B[1][0]= x;
MatrixVectorMult(B, A, C, 2, 2);
}
void VectorTransp(float A[], float &C[][], int R) {
int r;
float tmp[];
ArrayInit2D(C,tmp, 0, 2,R);
for (r=0; r<R; r++) {
C[0][r]=A[r];
}
}
task main() {
float A[2][2], B[2][2], C[][], x;
float O[4][4], T[][], L[2], M[];
A[0][0]=1; A[0][1]=3;
A[1][0]=2; A[1][1]=4;
NumOut(0,56,A[0][0]); NumOut(30,56,A[0][1]);
NumOut(0,48,A[1][0]); NumOut(30,48,A[1][1]);
B[0][0]=10; B[0][1]=0;
B[1][0]=20; B[1][1]=0;
L[0]=10;
L[1]=20;
TextOut(0,40,"B Matrix L vect");
NumOut(0,32,B[0][0]); NumOut(30,32,B[0][1]); NumOut(72,32,L[0]);
NumOut(0,24,B[1][0]); NumOut(30,24,B[1][1]); NumOut(72,24,L[1]);
MatrixMatrixMult(A,B,C, 2,2,2);
MatrixVectorMult(A,L,M, 2,2);
TextOut(0,16,"A*B A*L");
NumOut(0, 8,C[0][0]); NumOut(30, 8,C[0][1]); NumOut(72, 8,M[0]);
NumOut(0, 0,C[1][0]); NumOut(30, 0,C[1][1]); NumOut(72, 0,M[1]);
while (!ButtonPressed(BTNCENTER, false));
while ( ButtonPressed(BTNCENTER, false));
ClearScreen();
B[0][0]=1; B[0][1]=3;
B[1][0]=2; B[1][1]=4;
NumOut(0,56,B[0][0]); NumOut(30,56,B[0][1]);
NumOut(0,48,B[1][0]); NumOut(30,48,B[1][1]);
A[0][0]=10; A[0][1]=20;
A[1][0]=0; A[1][1]=0;
L[0]=10; L[1]=20;
TextOut(0,40,"A Matrix L vect");
NumOut(0,32,A[0][0]); NumOut(24,32,A[0][1]); NumOut(56,32,L[0]); NumOut(78,32,L[1]);
NumOut(0,24,A[1][0]); NumOut(24,24,A[1][1]);
MatrixMatrixMult(A,B,C, 2,2,2);
VectorMatrixMult(L,B,M, 2,2);
TextOut(0,16,"A*B L*B");
NumOut(0, 8,C[0][0]); NumOut(24, 8,C[0][1]); NumOut(56, 8,M[0]); NumOut(78, 8,M[1]);
NumOut(0, 0,C[1][0]); NumOut(24, 0,C[1][1]);
while (!ButtonPressed(BTNCENTER, false));
while ( ButtonPressed(BTNCENTER, false));
ClearScreen();
TextOut(0,56,"Rotat 30deg Math");
A[0][0]=1; A[0][1]=0;
A[1][0]=1; A[1][1]=0;
L[0]=1;
L[1]=1;
TextOut(0,40,"A Matrix L vect");
NumOut(0,32,A[0][0]); NumOut(36,32,A[0][1]); NumOut(66,32,L[0]);
NumOut(0,24,A[1][0]); NumOut(36,24,A[1][1]); NumOut(66,24,L[1]);
MatrixRotate2DMath(A,C,30);
VectorRotate2DMath(L,M,30);
TextOut(0,16,"A 30deg L 30deg");
NumOut(0, 8,C[0][0]); NumOut(36, 8,C[0][1]); NumOut(66, 8,M[0]);
NumOut(0, 0,C[1][0]); NumOut(36, 0,C[1][1]); NumOut(66, 0,M[1]);
while (!ButtonPressed(BTNCENTER, false));
while ( ButtonPressed(BTNCENTER, false));
ClearScreen();
TextOut(0,56,"Rotat 90deg Math");
A[0][0]=1; A[0][1]=0;
A[1][0]=1; A[1][1]=0;
L[0]=1;
L[1]=1;
TextOut(0,40,"A Matrix L vect");
NumOut(0,32,A[0][0]); NumOut(36,32,A[0][1]); NumOut(66,32,L[0]);
NumOut(0,24,A[1][0]); NumOut(36,24,A[1][1]); NumOut(66,24,L[1]);
MatrixRotate2DMath(A,C,90);
VectorRotate2DMath(L,M,90);
TextOut(0,16,"A 90deg L 90deg");
NumOut(0, 8,C[0][0]); NumOut(36, 8,C[0][1]); NumOut(66, 8,M[0]);
NumOut(0, 0,C[1][0]); NumOut(36, 0,C[1][1]); NumOut(66, 0,M[1]);
while(1);
}