3 * $Id: matrix.c,v 1.1 2000/05/21 11:28:30 mdw Exp $
5 * Matrix arithmetic mod %$2^{24}$%
7 * (c) 2000 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * Copyright (c) 2000 Mark Wooding
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * 2, Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the authors may not be used to endorse or promote
27 * products derived from this software without specific prior written
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
31 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
33 * NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
42 * Instead of accepting the above terms, you may redistribute and/or modify
43 * this software under the terms of either the GNU General Public License,
44 * or the GNU Library General Public License, published by the Free
45 * Software Foundation; either version 2 of the License, or (at your
46 * option) any later version.
49 /*----- Revision history --------------------------------------------------*
52 * Revision 1.1 2000/05/21 11:28:30 mdw
57 /*----- Header files ------------------------------------------------------*/
67 /*----- Main code ---------------------------------------------------------*/
71 * Arguments: @uint24 *d@ = pointer to destination matrix
72 * @uint24 *a, *b@ = pointer to operand matrices
73 * @unsigned x, y, z@ = dimensions of the operand matrices
77 * Use: Performs matrix multiplication mod %$2^{24}$%. The matrix
78 * @d@ may not overlap either operand matrix.
81 void matmul(uint24 *d, const uint24 *a, const uint24 *b,
82 unsigned x, unsigned y, unsigned z)
86 for (i = 0; i < x; i++) {
88 for (j = 0; j < z; j++) {
90 for (k = 0; k < y; k++)
91 n += a[k] * bb[k * z];
101 * Arguments: @uint24 *d@ = pointer to destination matrix
102 * @uint24 *a@ = pointer to operand matrix
103 * @unsigned x, y@ = dimensions of operand matrix
105 * Returns: Zero if the matrix was successfully inverted, %$-1$% if the
106 * matrix is singular.
108 * Use: Computes the mod %$2^{24}$% inverse of a square matrix.
111 int matinv(uint24 *d, uint24 *a, unsigned x, unsigned y)
115 uint32 *p, *q, *r, *s;
119 aa = malloc(sizeof(uint24) * x * y);
121 fprintf(stderr, "unable to allocate memory\n");
124 memcpy(aa, a, sizeof(uint24) * x * y);
127 for (i = 0; i < x; i++) {
128 for (j = 0; j < y; j++)
132 for (i = 0; i < x; i++) {
133 uint24 c = inv24(aa[(x + 1) * i]);
138 for (j = 0; j < y; j++) {
139 r[j] = U24(r[j] * c);
140 s[j] = U24(s[j] * c);
142 for (j = 0; j < x; j++) {
148 for (k = 0; k < y; k++) {
149 p[k] = U24(p[k] - c * r[k]);
150 q[k] = U24(q[k] - c * s[k]);
163 /*----- That's all, folks -------------------------------------------------*/