Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site turtlevax.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!oliveb!Glacier!decwrl!turtlevax!ken From: ken@turtlevax.UUCP (Ken Turkowski) Newsgroups: net.sources Subject: Re: matrix mult. Message-ID: <960@turtlevax.UUCP> Date: Mon, 18-Nov-85 02:54:37 EST Article-I.D.: turtleva.960 Posted: Mon Nov 18 02:54:37 1985 Date-Received: Thu, 21-Nov-85 03:37:51 EST References: <890@ncoast.UUCP> Reply-To: ken@turtlevax.UUCP (Ken Turkowski) Distribution: net Organization: CIMLINC, Inc. @ Menlo Park, CA Lines: 72 In article <890@ncoast.UUCP> simpsong@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@DECWRL.DEC.COM