Skip to content

Commit 76fb4a8

Browse files
author
Cristy
committed
correct Gauss-Jorfan elimination algorithm
1 parent b47b292 commit 76fb4a8

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

magick/matrix.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ MagickExport MagickBooleanType GaussJordanElimination(double **matrix,
480480
double **vectors,const size_t rank,const size_t number_vectors)
481481
{
482482
#define GaussJordanSwap(x,y) \
483+
{ \
484+
double temp = (x); \
485+
(x)=(y); \
486+
(y)=temp; \
487+
}
488+
#define GaussJordanSwapLD(x,y) \
483489
{ \
484490
long double temp = (x); \
485491
(x)=(y); \
@@ -559,21 +565,19 @@ MagickExport MagickBooleanType GaussJordanElimination(double **matrix,
559565
row=j;
560566
column=k;
561567
}
562-
if ((column == -1) || (row == -1) ||
563-
((fabsl(max) > 0.0L) && (fabsl(max) <= LDBL_MIN)))
568+
if ((column == -1) || (row == -1) || (fabsl(max) < LDBL_MIN))
564569
ThrowGaussJordanException(); /* Singular or nearly singular matrix */
565570
pivots[column]++;
566571
if (row != column)
567572
{
568573
for (k=0; k < (ssize_t) rank; k++)
569-
GaussJordanSwap(hp_matrix[row][k],hp_matrix[column][k]);
574+
GaussJordanSwapLD(hp_matrix[row][k],hp_matrix[column][k]);
570575
for (k=0; k < (ssize_t) number_vectors; k++)
571576
GaussJordanSwap(vectors[k][row],vectors[k][column]);
572577
}
573578
rows[i]=row;
574579
columns[i]=column;
575-
if ((fabsl(hp_matrix[column][column]) > 0.0L) &&
576-
(fabsl(hp_matrix[column][column]) <= LDBL_MIN))
580+
if (fabsl(hp_matrix[column][column]) < LDBL_MIN)
577581
ThrowGaussJordanException(); /* Singular matrix */
578582
scale=1.0L/hp_matrix[column][column];
579583
hp_matrix[column][column]=1.0;
@@ -595,7 +599,7 @@ MagickExport MagickBooleanType GaussJordanElimination(double **matrix,
595599
for (j=(ssize_t) rank-1; j >= 0; j--)
596600
if (columns[j] != rows[j])
597601
for (i=0; i < (ssize_t) rank; i++)
598-
GaussJordanSwap(hp_matrix[i][columns[j]],hp_matrix[i][rows[j]]);
602+
GaussJordanSwapLD(hp_matrix[i][columns[j]],hp_matrix[i][rows[j]]);
599603
/*
600604
Copy back the result to the original matrix.
601605
*/

0 commit comments

Comments
 (0)