2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % MagickCore Matrix Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/matrix.h"
44 #include "MagickCore/matrix-private.h"
45 #include "MagickCore/memory_.h"
46 #include "MagickCore/utility.h"
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 % A c q u i r e M a g i c k M a t r i x %
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 % AcquireMagickMatrix() allocates and returns a matrix in the form of an
60 % array of pointers to an array of doubles, with all values pre-set to zero.
62 % This used to generate the two dimensional matrix, and vectors required
63 % for the GaussJordanElimination() method below, solving some system of
64 % simultanious equations.
66 % The format of the AcquireMagickMatrix method is:
68 % double **AcquireMagickMatrix(const size_t number_rows,
71 % A description of each parameter follows:
73 % o number_rows: the number pointers for the array of pointers
76 % o size: the size of the array of doubles each pointer points to
80 MagickExport double **AcquireMagickMatrix(const size_t number_rows,
90 matrix=(double **) AcquireQuantumMemory(number_rows,sizeof(*matrix));
91 if (matrix == (double **) NULL)
92 return((double **) NULL);
93 for (i=0; i < (ssize_t) number_rows; i++)
95 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
96 if (matrix[i] == (double *) NULL)
99 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
100 matrix=(double **) RelinquishMagickMemory(matrix);
101 return((double **) NULL);
103 for (j=0; j < (ssize_t) size; j++)
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % G a u s s J o r d a n E l i m i n a t i o n %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % GaussJordanElimination() returns a matrix in reduced row echelon form,
121 % while simultaneously reducing and thus solving the augumented results
124 % See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
126 % The format of the GaussJordanElimination method is:
128 % MagickBooleanType GaussJordanElimination(double **matrix,
129 % double **vectors,const size_t rank,const size_t number_vectors)
131 % A description of each parameter follows:
133 % o matrix: the matrix to be reduced, as an 'array of row pointers'.
135 % o vectors: the additional matrix argumenting the matrix for row reduction.
136 % Producing an 'array of column vectors'.
138 % o rank: The size of the matrix (both rows and columns).
139 % Also represents the number terms that need to be solved.
141 % o number_vectors: Number of vectors columns, argumenting the above matrix.
142 % Usally 1, but can be more for more complex equation solving.
144 % Note that the 'matrix' is given as a 'array of row pointers' of rank size.
145 % That is values can be assigned as matrix[row][column] where 'row' is
146 % typically the equation, and 'column' is the term of the equation.
147 % That is the matrix is in the form of a 'row first array'.
149 % However 'vectors' is a 'array of column pointers' which can have any number
150 % of columns, with each column array the same 'rank' size as 'matrix'.
152 % This allows for simpler handling of the results, especially is only one
153 % column 'vector' is all that is required to produce the desired solution.
155 % For example, the 'vectors' can consist of a pointer to a simple array of
156 % doubles. when only one set of simultanious equations is to be solved from
157 % the given set of coefficient weighted terms.
159 % double **matrix = AcquireMagickMatrix(8UL,8UL);
160 % double coefficents[8];
162 % GaussJordanElimination(matrix, &coefficents, 8UL, 1UL);
164 % However by specifing more 'columns' (as an 'array of vector columns'),
165 % you can use this function to solve multiple sets of 'separable' equations.
167 % For example a distortion function where u = U(x,y) v = V(x,y)
168 % And the functions U() and V() have separate coefficents, but are being
169 % generated from a common x,y->u,v data set.
171 % Another example is generation of a color gradient from a set of colors
172 % at specific coordients, such as a list x,y -> r,g,b,a
173 % (Reference to be added - Anthony)
175 % You can also use the 'vectors' to generate an inverse of the given 'matrix'
176 % though as a 'column first array' rather than a 'row first array' (matrix
177 % is transposed). For details see
178 % http://en.wikipedia.org/wiki/Gauss-Jordan_elimination.
181 MagickPrivate MagickBooleanType GaussJordanElimination(double **matrix,
182 double **vectors,const size_t rank,const size_t number_vectors)
184 #define GaussJordanSwap(x,y) \
210 columns=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*columns));
211 rows=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*rows));
212 pivots=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*pivots));
213 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
214 (pivots == (ssize_t *) NULL))
216 if (pivots != (ssize_t *) NULL)
217 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
218 if (columns != (ssize_t *) NULL)
219 columns=(ssize_t *) RelinquishMagickMemory(columns);
220 if (rows != (ssize_t *) NULL)
221 rows=(ssize_t *) RelinquishMagickMemory(rows);
224 (void) ResetMagickMemory(columns,0,rank*sizeof(*columns));
225 (void) ResetMagickMemory(rows,0,rank*sizeof(*rows));
226 (void) ResetMagickMemory(pivots,0,rank*sizeof(*pivots));
229 for (i=0; i < (ssize_t) rank; i++)
232 for (j=0; j < (ssize_t) rank; j++)
235 for (k=0; k < (ssize_t) rank; k++)
242 if (fabs(matrix[j][k]) >= max)
244 max=fabs(matrix[j][k]);
252 for (k=0; k < (ssize_t) rank; k++)
253 GaussJordanSwap(matrix[row][k],matrix[column][k]);
254 for (k=0; k < (ssize_t) number_vectors; k++)
255 GaussJordanSwap(vectors[k][row],vectors[k][column]);
259 if (matrix[column][column] == 0.0)
260 return(MagickFalse); /* sigularity */
261 scale=1.0/matrix[column][column];
262 matrix[column][column]=1.0;
263 for (j=0; j < (ssize_t) rank; j++)
264 matrix[column][j]*=scale;
265 for (j=0; j < (ssize_t) number_vectors; j++)
266 vectors[j][column]*=scale;
267 for (j=0; j < (ssize_t) rank; j++)
270 scale=matrix[j][column];
271 matrix[j][column]=0.0;
272 for (k=0; k < (ssize_t) rank; k++)
273 matrix[j][k]-=scale*matrix[column][k];
274 for (k=0; k < (ssize_t) number_vectors; k++)
275 vectors[k][j]-=scale*vectors[k][column];
278 for (j=(ssize_t) rank-1; j >= 0; j--)
279 if (columns[j] != rows[j])
280 for (i=0; i < (ssize_t) rank; i++)
281 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
282 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
283 rows=(ssize_t *) RelinquishMagickMemory(rows);
284 columns=(ssize_t *) RelinquishMagickMemory(columns);
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 % L e a s t S q u a r e s A d d T e r m s %
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 % LeastSquaresAddTerms() adds one set of terms and associate results to the
300 % given matrix and vectors for solving using least-squares function fitting.
302 % The format of the AcquireMagickMatrix method is:
304 % void LeastSquaresAddTerms(double **matrix,double **vectors,
305 % const double *terms,const double *results,const size_t rank,
306 % const size_t number_vectors);
308 % A description of each parameter follows:
310 % o matrix: the square matrix to add given terms/results to.
312 % o vectors: the result vectors to add terms/results to.
314 % o terms: the pre-calculated terms (without the unknown coefficent
315 % weights) that forms the equation being added.
317 % o results: the result(s) that should be generated from the given terms
318 % weighted by the yet-to-be-solved coefficents.
320 % o rank: the rank or size of the dimentions of the square matrix.
321 % Also the length of vectors, and number of terms being added.
323 % o number_vectors: Number of result vectors, and number or results being
324 % added. Also represents the number of separable systems of equations
325 % that is being solved.
329 % 2 dimensional Affine Equations (which are separable)
330 % c0*x + c2*y + c4*1 => u
331 % c1*x + c3*y + c5*1 => v
333 % double **matrix = AcquireMagickMatrix(3UL,3UL);
334 % double **vectors = AcquireMagickMatrix(2UL,3UL);
335 % double terms[3], results[2];
337 % for each given x,y -> u,v
343 % LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
345 % if ( GaussJordanElimination(matrix,vectors,3UL,2UL) ) {
346 % c0 = vectors[0][0];
347 % c2 = vectors[0][1];
348 % c4 = vectors[0][2];
349 % c1 = vectors[1][0];
350 % c3 = vectors[1][1];
351 % c5 = vectors[1][2];
354 % printf("Matrix unsolvable\n);
355 % RelinquishMagickMatrix(matrix,3UL);
356 % RelinquishMagickMatrix(vectors,2UL);
359 MagickPrivate void LeastSquaresAddTerms(double **matrix,double **vectors,
360 const double *terms,const double *results,const size_t rank,
361 const size_t number_vectors)
367 for (j=0; j < (ssize_t) rank; j++)
369 for (i=0; i < (ssize_t) rank; i++)
370 matrix[i][j]+=terms[i]*terms[j];
371 for (i=0; i < (ssize_t) number_vectors; i++)
372 vectors[i][j]+=results[i]*terms[j];
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 % R e l i n q u i s h M a g i c k M a t r i x %
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 % RelinquishMagickMatrix() frees the previously acquired matrix (array of
388 % pointers to arrays of doubles).
390 % The format of the RelinquishMagickMatrix method is:
392 % double **RelinquishMagickMatrix(double **matrix,
393 % const size_t number_rows)
395 % A description of each parameter follows:
397 % o matrix: the matrix to relinquish
399 % o number_rows: the first dimension of the acquired matrix (number of
403 MagickExport double **RelinquishMagickMatrix(double **matrix,
404 const size_t number_rows)
409 if (matrix == (double **) NULL )
411 for (i=0; i < (ssize_t) number_rows; i++)
412 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
413 matrix=(double **) RelinquishMagickMemory(matrix);