blob: c61e673150426d1362a2fc05cb795169de107565 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M AAA TTTTT RRRR IIIII X X %
7% MM MM A A T R R I X X %
8% M M M AAAAA T RRRR I X %
9% M M A A T R R I X X %
10% M M A A T R R IIIII X X %
11% %
12% %
13% MagickCore Matrix Methods %
14% %
15% Software Design %
16% John Cristy %
17% August 2007 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/matrix.h"
cristyd1dd6e42011-09-04 01:46:08 +000044#include "MagickCore/matrix-private.h"
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/memory_.h"
cristy3ed852e2009-09-05 21:47:34 +000046
47/*
48%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49% %
50% %
51% %
52% A c q u i r e M a g i c k M a t r i x %
53% %
54% %
55% %
56%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57%
58% AcquireMagickMatrix() allocates and returns a matrix in the form of an
59% array of pointers to an array of doubles, with all values pre-set to zero.
60%
glennrp2489f532011-06-25 03:02:43 +000061% This used to generate the two dimensional matrix, and vectors required
cristy3ed852e2009-09-05 21:47:34 +000062% for the GaussJordanElimination() method below, solving some system of
63% simultanious equations.
64%
65% The format of the AcquireMagickMatrix method is:
66%
cristybb503372010-05-27 20:51:26 +000067% double **AcquireMagickMatrix(const size_t number_rows,
68% const size_t size)
cristy3ed852e2009-09-05 21:47:34 +000069%
70% A description of each parameter follows:
71%
cristy74908cf2010-05-17 19:43:54 +000072% o number_rows: the number pointers for the array of pointers
cristy1ad491d2010-05-17 19:45:27 +000073% (first dimension).
cristy3ed852e2009-09-05 21:47:34 +000074%
cristy1ad491d2010-05-17 19:45:27 +000075% o size: the size of the array of doubles each pointer points to
76% (second dimension).
cristy3ed852e2009-09-05 21:47:34 +000077%
78*/
cristybb503372010-05-27 20:51:26 +000079MagickExport double **AcquireMagickMatrix(const size_t number_rows,
80 const size_t size)
cristy3ed852e2009-09-05 21:47:34 +000081{
82 double
cristy74908cf2010-05-17 19:43:54 +000083 **matrix;
cristy3ed852e2009-09-05 21:47:34 +000084
cristybb503372010-05-27 20:51:26 +000085 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +000086 i,
87 j;
88
cristy74908cf2010-05-17 19:43:54 +000089 matrix=(double **) AcquireQuantumMemory(number_rows,sizeof(*matrix));
cristy3ed852e2009-09-05 21:47:34 +000090 if (matrix == (double **) NULL)
cristy26295322011-09-22 00:50:36 +000091 return((double **) NULL);
cristybb503372010-05-27 20:51:26 +000092 for (i=0; i < (ssize_t) number_rows; i++)
cristy3ed852e2009-09-05 21:47:34 +000093 {
94 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
95 if (matrix[i] == (double *) NULL)
96 {
97 for (j=0; j < i; j++)
98 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
99 matrix=(double **) RelinquishMagickMemory(matrix);
100 return((double **) NULL);
101 }
cristybb503372010-05-27 20:51:26 +0000102 for (j=0; j < (ssize_t) size; j++)
cristy74908cf2010-05-17 19:43:54 +0000103 matrix[i][j]=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000104 }
105 return(matrix);
106}
107
108/*
109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110% %
111% %
112% %
113% G a u s s J o r d a n E l i m i n a t i o n %
114% %
115% %
116% %
117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118%
119% GaussJordanElimination() returns a matrix in reduced row echelon form,
120% while simultaneously reducing and thus solving the augumented results
121% matrix.
122%
123% See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
124%
125% The format of the GaussJordanElimination method is:
126%
cristy26295322011-09-22 00:50:36 +0000127% MagickBooleanType GaussJordanElimination(double **matrix,
128% double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000129%
130% A description of each parameter follows:
131%
132% o matrix: the matrix to be reduced, as an 'array of row pointers'.
133%
134% o vectors: the additional matrix argumenting the matrix for row reduction.
135% Producing an 'array of column vectors'.
136%
137% o rank: The size of the matrix (both rows and columns).
138% Also represents the number terms that need to be solved.
139%
cristy74908cf2010-05-17 19:43:54 +0000140% o number_vectors: Number of vectors columns, argumenting the above matrix.
cristy3ed852e2009-09-05 21:47:34 +0000141% Usally 1, but can be more for more complex equation solving.
142%
143% Note that the 'matrix' is given as a 'array of row pointers' of rank size.
144% That is values can be assigned as matrix[row][column] where 'row' is
145% typically the equation, and 'column' is the term of the equation.
146% That is the matrix is in the form of a 'row first array'.
147%
148% However 'vectors' is a 'array of column pointers' which can have any number
149% of columns, with each column array the same 'rank' size as 'matrix'.
150%
151% This allows for simpler handling of the results, especially is only one
152% column 'vector' is all that is required to produce the desired solution.
153%
154% For example, the 'vectors' can consist of a pointer to a simple array of
155% doubles. when only one set of simultanious equations is to be solved from
156% the given set of coefficient weighted terms.
157%
158% double **matrix = AcquireMagickMatrix(8UL,8UL);
159% double coefficents[8];
160% ...
161% GaussJordanElimination(matrix, &coefficents, 8UL, 1UL);
anthony34364f42011-10-21 05:31:53 +0000162%
anthonyb16e1432011-10-21 05:06:56 +0000163% However by specifing more 'columns' (as an 'array of vector columns'),
164% you can use this function to solve multiple sets of 'separable' equations.
cristy3ed852e2009-09-05 21:47:34 +0000165%
166% For example a distortion function where u = U(x,y) v = V(x,y)
167% And the functions U() and V() have separate coefficents, but are being
168% generated from a common x,y->u,v data set.
169%
170% Another example is generation of a color gradient from a set of colors
171% at specific coordients, such as a list x,y -> r,g,b,a
172% (Reference to be added - Anthony)
173%
174% You can also use the 'vectors' to generate an inverse of the given 'matrix'
anthonyb16e1432011-10-21 05:06:56 +0000175% though as a 'column first array' rather than a 'row first array' (matrix
anthony34364f42011-10-21 05:31:53 +0000176% is transposed). For details see
177% http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
cristy3ed852e2009-09-05 21:47:34 +0000178%
179*/
cristyd1dd6e42011-09-04 01:46:08 +0000180MagickPrivate MagickBooleanType GaussJordanElimination(double **matrix,
cristybb503372010-05-27 20:51:26 +0000181 double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000182{
183#define GaussJordanSwap(x,y) \
184{ \
185 if ((x) != (y)) \
186 { \
187 (x)+=(y); \
188 (y)=(x)-(y); \
189 (x)=(x)-(y); \
190 } \
191}
192
193 double
194 max,
195 scale;
196
cristy9d314ff2011-03-09 01:30:28 +0000197 register ssize_t
198 i,
199 j,
200 k;
201
cristybb503372010-05-27 20:51:26 +0000202 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000203 column,
204 *columns,
205 *pivots,
206 row,
207 *rows;
208
cristybb503372010-05-27 20:51:26 +0000209 columns=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*columns));
210 rows=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*rows));
211 pivots=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*pivots));
212 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
213 (pivots == (ssize_t *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000214 {
cristybb503372010-05-27 20:51:26 +0000215 if (pivots != (ssize_t *) NULL)
216 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
217 if (columns != (ssize_t *) NULL)
218 columns=(ssize_t *) RelinquishMagickMemory(columns);
219 if (rows != (ssize_t *) NULL)
220 rows=(ssize_t *) RelinquishMagickMemory(rows);
cristy3ed852e2009-09-05 21:47:34 +0000221 return(MagickFalse);
222 }
223 (void) ResetMagickMemory(columns,0,rank*sizeof(*columns));
224 (void) ResetMagickMemory(rows,0,rank*sizeof(*rows));
225 (void) ResetMagickMemory(pivots,0,rank*sizeof(*pivots));
226 column=0;
227 row=0;
cristybb503372010-05-27 20:51:26 +0000228 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000229 {
230 max=0.0;
cristybb503372010-05-27 20:51:26 +0000231 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000232 if (pivots[j] != 1)
233 {
cristybb503372010-05-27 20:51:26 +0000234 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000235 if (pivots[k] != 0)
236 {
237 if (pivots[k] > 1)
238 return(MagickFalse);
239 }
240 else
241 if (fabs(matrix[j][k]) >= max)
242 {
243 max=fabs(matrix[j][k]);
244 row=j;
245 column=k;
246 }
247 }
248 pivots[column]++;
249 if (row != column)
250 {
cristybb503372010-05-27 20:51:26 +0000251 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000252 GaussJordanSwap(matrix[row][k],matrix[column][k]);
cristybb503372010-05-27 20:51:26 +0000253 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000254 GaussJordanSwap(vectors[k][row],vectors[k][column]);
255 }
256 rows[i]=row;
257 columns[i]=column;
258 if (matrix[column][column] == 0.0)
anthony34364f42011-10-21 05:31:53 +0000259 return(MagickFalse); /* singularity */
cristy3ed852e2009-09-05 21:47:34 +0000260 scale=1.0/matrix[column][column];
261 matrix[column][column]=1.0;
cristybb503372010-05-27 20:51:26 +0000262 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000263 matrix[column][j]*=scale;
cristybb503372010-05-27 20:51:26 +0000264 for (j=0; j < (ssize_t) number_vectors; j++)
cristy3ed852e2009-09-05 21:47:34 +0000265 vectors[j][column]*=scale;
cristybb503372010-05-27 20:51:26 +0000266 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000267 if (j != column)
268 {
269 scale=matrix[j][column];
270 matrix[j][column]=0.0;
cristybb503372010-05-27 20:51:26 +0000271 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000272 matrix[j][k]-=scale*matrix[column][k];
cristybb503372010-05-27 20:51:26 +0000273 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000274 vectors[k][j]-=scale*vectors[k][column];
275 }
276 }
cristybb503372010-05-27 20:51:26 +0000277 for (j=(ssize_t) rank-1; j >= 0; j--)
cristy3ed852e2009-09-05 21:47:34 +0000278 if (columns[j] != rows[j])
cristybb503372010-05-27 20:51:26 +0000279 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000280 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
cristybb503372010-05-27 20:51:26 +0000281 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
282 rows=(ssize_t *) RelinquishMagickMemory(rows);
283 columns=(ssize_t *) RelinquishMagickMemory(columns);
cristy3ed852e2009-09-05 21:47:34 +0000284 return(MagickTrue);
285}
286
287/*
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289% %
290% %
291% %
292% L e a s t S q u a r e s A d d T e r m s %
293% %
294% %
295% %
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297%
298% LeastSquaresAddTerms() adds one set of terms and associate results to the
299% given matrix and vectors for solving using least-squares function fitting.
300%
301% The format of the AcquireMagickMatrix method is:
302%
303% void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000304% const double *terms,const double *results,const size_t rank,
305% const size_t number_vectors);
cristy3ed852e2009-09-05 21:47:34 +0000306%
307% A description of each parameter follows:
308%
309% o matrix: the square matrix to add given terms/results to.
310%
311% o vectors: the result vectors to add terms/results to.
312%
313% o terms: the pre-calculated terms (without the unknown coefficent
cristy26295322011-09-22 00:50:36 +0000314% weights) that forms the equation being added.
cristy3ed852e2009-09-05 21:47:34 +0000315%
316% o results: the result(s) that should be generated from the given terms
cristy26295322011-09-22 00:50:36 +0000317% weighted by the yet-to-be-solved coefficents.
cristy3ed852e2009-09-05 21:47:34 +0000318%
cristy4c08aed2011-07-01 19:47:50 +0000319% o rank: the rank or size of the dimentions of the square matrix.
cristy26295322011-09-22 00:50:36 +0000320% Also the length of vectors, and number of terms being added.
cristy3ed852e2009-09-05 21:47:34 +0000321%
cristy1ad491d2010-05-17 19:45:27 +0000322% o number_vectors: Number of result vectors, and number or results being
323% added. Also represents the number of separable systems of equations
324% that is being solved.
cristy3ed852e2009-09-05 21:47:34 +0000325%
326% Example of use...
327%
glennrp2489f532011-06-25 03:02:43 +0000328% 2 dimensional Affine Equations (which are separable)
cristy3ed852e2009-09-05 21:47:34 +0000329% c0*x + c2*y + c4*1 => u
330% c1*x + c3*y + c5*1 => v
331%
332% double **matrix = AcquireMagickMatrix(3UL,3UL);
333% double **vectors = AcquireMagickMatrix(2UL,3UL);
334% double terms[3], results[2];
335% ...
336% for each given x,y -> u,v
337% terms[0] = x;
338% terms[1] = y;
339% terms[2] = 1;
340% results[0] = u;
341% results[1] = v;
342% LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
343% ...
344% if ( GaussJordanElimination(matrix,vectors,3UL,2UL) ) {
345% c0 = vectors[0][0];
346% c2 = vectors[0][1];
347% c4 = vectors[0][2];
348% c1 = vectors[1][0];
349% c3 = vectors[1][1];
350% c5 = vectors[1][2];
351% }
352% else
353% printf("Matrix unsolvable\n);
354% RelinquishMagickMatrix(matrix,3UL);
355% RelinquishMagickMatrix(vectors,2UL);
356%
357*/
cristyd1dd6e42011-09-04 01:46:08 +0000358MagickPrivate void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000359 const double *terms,const double *results,const size_t rank,
360 const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000361{
cristybb503372010-05-27 20:51:26 +0000362 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000363 i,
364 j;
365
cristybb503372010-05-27 20:51:26 +0000366 for (j=0; j < (ssize_t) rank; j++)
cristy74908cf2010-05-17 19:43:54 +0000367 {
cristybb503372010-05-27 20:51:26 +0000368 for (i=0; i < (ssize_t) rank; i++)
cristy74908cf2010-05-17 19:43:54 +0000369 matrix[i][j]+=terms[i]*terms[j];
cristybb503372010-05-27 20:51:26 +0000370 for (i=0; i < (ssize_t) number_vectors; i++)
cristy74908cf2010-05-17 19:43:54 +0000371 vectors[i][j]+=results[i]*terms[j];
cristy3ed852e2009-09-05 21:47:34 +0000372 }
cristy3ed852e2009-09-05 21:47:34 +0000373}
374
375/*
376%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377% %
378% %
379% %
380% R e l i n q u i s h M a g i c k M a t r i x %
381% %
382% %
383% %
384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385%
386% RelinquishMagickMatrix() frees the previously acquired matrix (array of
387% pointers to arrays of doubles).
388%
389% The format of the RelinquishMagickMatrix method is:
390%
391% double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +0000392% const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +0000393%
394% A description of each parameter follows:
395%
396% o matrix: the matrix to relinquish
397%
cristy1ad491d2010-05-17 19:45:27 +0000398% o number_rows: the first dimension of the acquired matrix (number of
399% pointers)
cristy3ed852e2009-09-05 21:47:34 +0000400%
401*/
402MagickExport double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +0000403 const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +0000404{
cristybb503372010-05-27 20:51:26 +0000405 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000406 i;
407
408 if (matrix == (double **) NULL )
409 return(matrix);
cristybb503372010-05-27 20:51:26 +0000410 for (i=0; i < (ssize_t) number_rows; i++)
cristy3ed852e2009-09-05 21:47:34 +0000411 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
412 matrix=(double **) RelinquishMagickMemory(matrix);
cristy3ed852e2009-09-05 21:47:34 +0000413 return(matrix);
414}