Fix licence text. Change to matinv to mark inputs as constants.
[storin] / matrix.c
1 /* -*-c-*-
2 *
3 * $Id: matrix.c,v 1.2 2000/07/02 15:22:07 mdw Exp $
4 *
5 * Matrix arithmetic mod %$2^{24}$%
6 *
7 * (c) 2000 Mark Wooding
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * Copyright (c) 2000 Mark Wooding
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met:
18 *
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
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.
25 *
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
28 * permission.
29 *
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 AUTHORS 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.
41 *
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.
47 */
48
49 /*----- Revision history --------------------------------------------------*
50 *
51 * $Log: matrix.c,v $
52 * Revision 1.2 2000/07/02 15:22:07 mdw
53 * Fix licence text. Change to matinv to mark inputs as constants.
54 *
55 * Revision 1.1 2000/05/21 11:28:30 mdw
56 * Initial check-in.
57 *
58 */
59
60 /*----- Header files ------------------------------------------------------*/
61
62 #include <assert.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65
66 #include "arith24.h"
67 #include "bits.h"
68 #include "matrix.h"
69
70 /*----- Main code ---------------------------------------------------------*/
71
72 /* --- @matmul@ --- *
73 *
74 * Arguments: @uint24 *d@ = pointer to destination matrix
75 * @uint24 *a, *b@ = pointer to operand matrices
76 * @unsigned x, y, z@ = dimensions of the operand matrices
77 *
78 * Returns: ---
79 *
80 * Use: Performs matrix multiplication mod %$2^{24}$%. The matrix
81 * @d@ may not overlap either operand matrix.
82 */
83
84 void matmul(uint24 *d, const uint24 *a, const uint24 *b,
85 unsigned x, unsigned y, unsigned z)
86 {
87 unsigned i, j, k;
88
89 for (i = 0; i < x; i++) {
90 const uint24 *bb = b;
91 for (j = 0; j < z; j++) {
92 uint24 n = 0;
93 for (k = 0; k < y; k++)
94 n += a[k] * bb[k * z];
95 *d++ = U24(n);
96 bb++;
97 }
98 a += y;
99 }
100 }
101
102 /* --- @matinv@ --- *
103 *
104 * Arguments: @uint24 *d@ = pointer to destination matrix
105 * @const uint24 *a@ = pointer to operand matrix
106 * @unsigned x, y@ = dimensions of operand matrix
107 *
108 * Returns: Zero if the matrix was successfully inverted, %$-1$% if the
109 * matrix is singular.
110 *
111 * Use: Computes the mod %$2^{24}$% inverse of a square matrix.
112 */
113
114 int matinv(uint24 *d, const uint24 *a, unsigned x, unsigned y)
115 {
116 unsigned i, j, k;
117 uint24 *aa;
118 uint32 *p, *q, *r, *s;
119
120 assert(x == y);
121
122 aa = malloc(sizeof(uint24) * x * y);
123 if (!aa) {
124 fprintf(stderr, "unable to allocate memory\n");
125 exit(EXIT_FAILURE);
126 }
127 memcpy(aa, a, sizeof(uint24) * x * y);
128
129 p = d;
130 for (i = 0; i < x; i++) {
131 for (j = 0; j < y; j++)
132 *p++ = i == j;
133 }
134
135 for (i = 0; i < x; i++) {
136 uint24 c = inv24(aa[(x + 1) * i]);
137 if (!c)
138 goto fail;
139 r = aa + y * i;
140 s = d + y * i;
141 for (j = 0; j < y; j++) {
142 r[j] = U24(r[j] * c);
143 s[j] = U24(s[j] * c);
144 }
145 for (j = 0; j < x; j++) {
146 if (j == i)
147 continue;
148 p = aa + y * j;
149 q = d + y * j;
150 c = p[i];
151 for (k = 0; k < y; k++) {
152 p[k] = U24(p[k] - c * r[k]);
153 q[k] = U24(q[k] - c * s[k]);
154 }
155 }
156 }
157
158 free(aa);
159 return (0);
160
161 fail:
162 free(aa);
163 return (-1);
164 }
165
166 /*----- That's all, folks -------------------------------------------------*/