storin.{tests,debug}-ref: Ancient versions of the test output.
[storin] / matrix.c
CommitLineData
e6e0e332
MW
1/* -*-c-*-
2 *
05dfc037 3 * $Id$
e6e0e332
MW
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
02f1f6b7 33 * NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
e6e0e332
MW
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 $
da124bf5
MW
52 * Revision 1.3 2000/08/05 11:27:56 mdw
53 * (matinv): Fix type error.
54 *
02f1f6b7
MW
55 * Revision 1.2 2000/07/02 15:22:07 mdw
56 * Fix licence text. Change to matinv to mark inputs as constants.
57 *
e6e0e332
MW
58 * Revision 1.1 2000/05/21 11:28:30 mdw
59 * Initial check-in.
60 *
61 */
62
63/*----- Header files ------------------------------------------------------*/
64
65#include <assert.h>
66#include <stdio.h>
67#include <stdlib.h>
68
69#include "arith24.h"
70#include "bits.h"
71#include "matrix.h"
72
73/*----- Main code ---------------------------------------------------------*/
74
75/* --- @matmul@ --- *
76 *
77 * Arguments: @uint24 *d@ = pointer to destination matrix
78 * @uint24 *a, *b@ = pointer to operand matrices
79 * @unsigned x, y, z@ = dimensions of the operand matrices
80 *
81 * Returns: ---
82 *
83 * Use: Performs matrix multiplication mod %$2^{24}$%. The matrix
84 * @d@ may not overlap either operand matrix.
85 */
86
87void matmul(uint24 *d, const uint24 *a, const uint24 *b,
88 unsigned x, unsigned y, unsigned z)
89{
90 unsigned i, j, k;
91
92 for (i = 0; i < x; i++) {
93 const uint24 *bb = b;
94 for (j = 0; j < z; j++) {
95 uint24 n = 0;
96 for (k = 0; k < y; k++)
97 n += a[k] * bb[k * z];
98 *d++ = U24(n);
99 bb++;
100 }
101 a += y;
102 }
103}
104
105/* --- @matinv@ --- *
106 *
107 * Arguments: @uint24 *d@ = pointer to destination matrix
02f1f6b7 108 * @const uint24 *a@ = pointer to operand matrix
e6e0e332
MW
109 * @unsigned x, y@ = dimensions of operand matrix
110 *
111 * Returns: Zero if the matrix was successfully inverted, %$-1$% if the
112 * matrix is singular.
113 *
114 * Use: Computes the mod %$2^{24}$% inverse of a square matrix.
115 */
116
02f1f6b7 117int matinv(uint24 *d, const uint24 *a, unsigned x, unsigned y)
e6e0e332
MW
118{
119 unsigned i, j, k;
120 uint24 *aa;
da124bf5 121 uint24 *p, *q, *r, *s;
e6e0e332
MW
122
123 assert(x == y);
124
125 aa = malloc(sizeof(uint24) * x * y);
126 if (!aa) {
127 fprintf(stderr, "unable to allocate memory\n");
128 exit(EXIT_FAILURE);
129 }
130 memcpy(aa, a, sizeof(uint24) * x * y);
131
132 p = d;
133 for (i = 0; i < x; i++) {
134 for (j = 0; j < y; j++)
135 *p++ = i == j;
136 }
137
138 for (i = 0; i < x; i++) {
139 uint24 c = inv24(aa[(x + 1) * i]);
140 if (!c)
141 goto fail;
142 r = aa + y * i;
143 s = d + y * i;
144 for (j = 0; j < y; j++) {
145 r[j] = U24(r[j] * c);
146 s[j] = U24(s[j] * c);
147 }
148 for (j = 0; j < x; j++) {
149 if (j == i)
150 continue;
151 p = aa + y * j;
152 q = d + y * j;
153 c = p[i];
154 for (k = 0; k < y; k++) {
155 p[k] = U24(p[k] - c * r[k]);
156 q[k] = U24(q[k] - c * s[k]);
157 }
158 }
159 }
160
161 free(aa);
162 return (0);
163
164fail:
165 free(aa);
166 return (-1);
167}
168
169/*----- That's all, folks -------------------------------------------------*/