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-2012 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"
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 % A c q u i r e M a g i c k M a t r i x %
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
61 % This used to generate the two dimensional matrix, and vectors required
62 % for the GaussJordanElimination() method below, solving some system of
63 % simultanious equations.
65 % The format of the AcquireMagickMatrix method is:
67 % double **AcquireMagickMatrix(const size_t number_rows,
70 % A description of each parameter follows:
72 % o number_rows: the number pointers for the array of pointers
75 % o size: the size of the array of doubles each pointer points to
79 MagickExport double **AcquireMagickMatrix(const size_t number_rows,
89 matrix=(double **) AcquireQuantumMemory(number_rows,sizeof(*matrix));
90 if (matrix == (double **) NULL)
91 return((double **) NULL);
92 for (i=0; i < (ssize_t) number_rows; i++)
94 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
95 if (matrix[i] == (double *) NULL)
98 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
99 matrix=(double **) RelinquishMagickMemory(matrix);
100 return((double **) NULL);
102 for (j=0; j < (ssize_t) size; j++)
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 % G a u s s J o r d a n E l i m i n a t i o n %
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % GaussJordanElimination() returns a matrix in reduced row echelon form,
120 % while simultaneously reducing and thus solving the augumented results
123 % See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
125 % The format of the GaussJordanElimination method is:
127 % MagickBooleanType GaussJordanElimination(double **matrix,
128 % double **vectors,const size_t rank,const size_t number_vectors)
130 % A description of each parameter follows:
132 % o matrix: the matrix to be reduced, as an 'array of row pointers'.
134 % o vectors: the additional matrix argumenting the matrix for row reduction.
135 % Producing an 'array of column vectors'.
137 % o rank: The size of the matrix (both rows and columns).
138 % Also represents the number terms that need to be solved.
140 % o number_vectors: Number of vectors columns, argumenting the above matrix.
141 % Usally 1, but can be more for more complex equation solving.
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'.
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'.
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.
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.
158 % double **matrix = AcquireMagickMatrix(8UL,8UL);
159 % double coefficents[8];
161 % GaussJordanElimination(matrix, &coefficents, 8UL, 1UL);
163 % However by specifing more 'columns' (as an 'array of vector columns'),
164 % you can use this function to solve multiple sets of 'separable' equations.
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.
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)
174 % You can also use the 'vectors' to generate an inverse of the given 'matrix'
175 % though as a 'column first array' rather than a 'row first array' (matrix
176 % is transposed). For details see
177 % http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
180 MagickPrivate MagickBooleanType GaussJordanElimination(double **matrix,
181 double **vectors,const size_t rank,const size_t number_vectors)
183 #define GaussJordanSwap(x,y) \
209 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))
215 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);
223 (void) ResetMagickMemory(columns,0,rank*sizeof(*columns));
224 (void) ResetMagickMemory(rows,0,rank*sizeof(*rows));
225 (void) ResetMagickMemory(pivots,0,rank*sizeof(*pivots));
228 for (i=0; i < (ssize_t) rank; i++)
231 for (j=0; j < (ssize_t) rank; j++)
234 for (k=0; k < (ssize_t) rank; k++)
241 if (fabs(matrix[j][k]) >= max)
243 max=fabs(matrix[j][k]);
251 for (k=0; k < (ssize_t) rank; k++)
252 GaussJordanSwap(matrix[row][k],matrix[column][k]);
253 for (k=0; k < (ssize_t) number_vectors; k++)
254 GaussJordanSwap(vectors[k][row],vectors[k][column]);
258 if (matrix[column][column] == 0.0)
259 return(MagickFalse); /* singularity */
260 scale=1.0/matrix[column][column];
261 matrix[column][column]=1.0;
262 for (j=0; j < (ssize_t) rank; j++)
263 matrix[column][j]*=scale;
264 for (j=0; j < (ssize_t) number_vectors; j++)
265 vectors[j][column]*=scale;
266 for (j=0; j < (ssize_t) rank; j++)
269 scale=matrix[j][column];
270 matrix[j][column]=0.0;
271 for (k=0; k < (ssize_t) rank; k++)
272 matrix[j][k]-=scale*matrix[column][k];
273 for (k=0; k < (ssize_t) number_vectors; k++)
274 vectors[k][j]-=scale*vectors[k][column];
277 for (j=(ssize_t) rank-1; j >= 0; j--)
278 if (columns[j] != rows[j])
279 for (i=0; i < (ssize_t) rank; i++)
280 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
281 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
282 rows=(ssize_t *) RelinquishMagickMemory(rows);
283 columns=(ssize_t *) RelinquishMagickMemory(columns);
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % L e a s t S q u a r e s A d d T e r m s %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % LeastSquaresAddTerms() adds one set of terms and associate results to the
299 % given matrix and vectors for solving using least-squares function fitting.
301 % The format of the AcquireMagickMatrix method is:
303 % void LeastSquaresAddTerms(double **matrix,double **vectors,
304 % const double *terms,const double *results,const size_t rank,
305 % const size_t number_vectors);
307 % A description of each parameter follows:
309 % o matrix: the square matrix to add given terms/results to.
311 % o vectors: the result vectors to add terms/results to.
313 % o terms: the pre-calculated terms (without the unknown coefficent
314 % weights) that forms the equation being added.
316 % o results: the result(s) that should be generated from the given terms
317 % weighted by the yet-to-be-solved coefficents.
319 % o rank: the rank or size of the dimentions of the square matrix.
320 % Also the length of vectors, and number of terms being added.
322 % 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.
328 % 2 dimensional Affine Equations (which are separable)
329 % c0*x + c2*y + c4*1 => u
330 % c1*x + c3*y + c5*1 => v
332 % double **matrix = AcquireMagickMatrix(3UL,3UL);
333 % double **vectors = AcquireMagickMatrix(2UL,3UL);
334 % double terms[3], results[2];
336 % for each given x,y -> u,v
342 % LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
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];
353 % printf("Matrix unsolvable\n);
354 % RelinquishMagickMatrix(matrix,3UL);
355 % RelinquishMagickMatrix(vectors,2UL);
358 MagickPrivate void LeastSquaresAddTerms(double **matrix,double **vectors,
359 const double *terms,const double *results,const size_t rank,
360 const size_t number_vectors)
366 for (j=0; j < (ssize_t) rank; j++)
368 for (i=0; i < (ssize_t) rank; i++)
369 matrix[i][j]+=terms[i]*terms[j];
370 for (i=0; i < (ssize_t) number_vectors; i++)
371 vectors[i][j]+=results[i]*terms[j];
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 % R e l i n q u i s h M a g i c k M a t r i x %
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 % RelinquishMagickMatrix() frees the previously acquired matrix (array of
387 % pointers to arrays of doubles).
389 % The format of the RelinquishMagickMatrix method is:
391 % double **RelinquishMagickMatrix(double **matrix,
392 % const size_t number_rows)
394 % A description of each parameter follows:
396 % o matrix: the matrix to relinquish
398 % o number_rows: the first dimension of the acquired matrix (number of
402 MagickExport double **RelinquishMagickMatrix(double **matrix,
403 const size_t number_rows)
408 if (matrix == (double **) NULL )
410 for (i=0; i < (ssize_t) number_rows; i++)
411 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
412 matrix=(double **) RelinquishMagickMemory(matrix);