matrix mult. (source and question)
Gregory R. Simpson @ The North Coast
simpsong at ncoast.UUCP
Thu Nov 14 15:18:16 AEST 1985
*** Line eater poison ***
Below is a kludge version of a matrix multiplier in C. The primary
reason for posting this is in hope that someone out there knows the
proper method of doing it. The problem I encountered was that in order
to pass a two dimensional array (hence a matrix) I needed to specify
the second bound of the array. However, I want this to be a generic
matrix multipler, for matrices of all sizes not just a fixed size. Since
the column increases most rapidly in a two dimensional array, declaring
a large array (like mat[][20]) puts all of a 3x3 into the first row.
In the much dreaded language of Fortran, one may simply pass a variable
to the dimension statement... this doesn't work in C. I believe this
is a job for pointers, but I was unsuccessful in getting them to work.
Please give me pointers on pointers (pun intended) for this application.
(or if you are ambitious send me a better version of code)
My fixes were fairly, crude, and the result is returned in a mxn vector
as opposed to an mxn matrix. Please send any pointers, references, code
help, flames to the address below. I have included my version below.
p.s. Once this issue is resolved, I will post an enhanced matrix
manipulation set, (mult., inversion, determinant, co-factors, etc.)
Thanks in advance,
Gregory R. Simpson
UUCP: decvax!cwruecmp!ncoast!simpsong
CSNET: simpsong at case.CSNET
ARPA: simpsong at purdue-ecn.ARPA
p.s. I am also looking for the manual page to roff.c posted from site
gloria about 2 months back...
======= bend - mutilate - cut - line-eat - here - ok - ================
/* a sample main program to call matmult */
main()
{
float a[3][3],b[3][3];
float c[9];
int i,j;
/* a typical array */
a[0][0]=1.0;
a[0][1]=2.0;
a[0][2]=3.0;
a[1][0]=4.0;
a[1][1]=5.0;
a[1][2]=6.0;
a[2][0]=7.0;
a[2][1]=8.0;
a[2][2]=9.0;
/* another typical array */
for (i=0;i<=2;i++) {
for (j=0;j<=2;j++) {
b[i][j] = a[i][j];
}
}
/* multiply two typical arrays together */
matmlt(a,b,c,3,3,3);
/* print the result, (it is in a 3 times 3 vector... boo hiss) */
for (i=0;i<=8;i++) {
printf("%f\n",c[i]);
}
}
/* the matrix multiplier */
matmlt(a,b,c,n,m,l)
/* this is a lot easier in fortran, cause then I can say dimension a(m,n) */
int n, m, l;
float a[], b[];
float c[];
{
float a2[10][10], b2[10][10], c2[10][10];
int i,j,k,inc;
/* load the arrays I passed into an array - kludgy ehhh? */
inc = 0;
for (i=0;i<=2;i++) {
for (j=0;j<=2;j++) {
a2[i][j] = a[inc];
b2[i][j] = b[inc];
inc++;
}
}
for (i = 0; i <= n-1 ; i++) {
for (k = 0; k <= l-1 ; k++) {
c2[i][k] = 0;
for (j = 0; j <= m-1 ; j++) {
c2[i][k] = (a2[i][j])*(b2[j][k]) + c2[i][k];
}
}
}
/* load the array result back into a vector to pass back */
inc = 0;
for (i=0;i<=2;i++) {
for (j=0;j<=2;j++) {
c[inc] = c2[i][j];
printf("%f %f %f \n",a2[i][j],b2[i][j],c2[i][j]);
inc++;
}
}
}
More information about the Comp.sources.unix
mailing list