matrix mult.
Ken Turkowski
ken at turtlevax.UUCP
Mon Nov 18 17:54:37 AEST 1985
In article <890 at ncoast.UUCP> simpsong at ncoast.UUCP (Gregory R. Simpson @ The North Coast) writes:
>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.
Anyone who's diddled with two dimensional arrays, and has tried to think
about how a compiler would do it has come up with something like the
following:
-----------------------------------------------------------------
# This is a shell archive. Remove anything before this line, then
# unpack it by saving it in a file and typing "sh file". (Files
# unpacked will be owned by you and have default permissions.)
#
# This archive contains:
# matcat.c
echo x - matcat.c
cat > "matcat.c" << '//E*O*F matcat.c//'
/* Matcat multiplies two (n x n) matrices together. The source matrices
* are given in A and B, and the result is returned in C.
*/
# ifndef ARRAYS
matcat(C, A, B, n)
register double *C;
double *A, *B;
register int n;
{
register double *ap, *bp;
register int k, j, i;
for (i = n; i-- > 0; A += n) { /* Each row in A */
for (j = 0; j < n; j++) { /* Each column in B */
ap = A; /* Left of ith row of A */
bp = B + j; /* Top of jth column of B */
*C = 0.0;
for (k = n; --k >= 0; bp += n)
*C += *ap++ * (*bp); /* *C += A[i'][k'] * B[k'][j]; */
}
}
}
# else
matcat(C, A, B, n)
register double *A, *B, *C;
register int n;
{
register int i, j, k;
double sum;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
sum = 0.0;
for (k = 0; k < n; k++)
sum += A[n*i+k] * B[n*k+j];
C[n*i+j] = sum;
}
}
}
# endif
//E*O*F matcat.c//
exit 0
--
Ken Turkowski @ CIMLINC (formerly CADLINC), Menlo Park, CA
UUCP: {amd,decwrl,hplabs,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken at DECWRL.DEC.COM
More information about the Comp.sources.unix
mailing list