Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/2.5.0/ALGLIB-2.5.0/matdet.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/2.5.0/ALGLIB-2.5.0/matdet.cs
r3839 r4068 19 19 *************************************************************************/ 20 20 21 using System; 22 23 namespace alglib 24 { 25 public class matdet 26 { 27 /************************************************************************* 28 Determinant calculation of the matrix given by its LU decomposition. 29 30 Input parameters: 31 A - LU decomposition of the matrix (output of 32 RMatrixLU subroutine). 33 Pivots - table of permutations which were made during 34 the LU decomposition. 35 Output of RMatrixLU subroutine. 36 N - size of matrix A. 37 38 Result: matrix determinant. 39 40 -- ALGLIB -- 41 Copyright 2005 by Bochkanov Sergey 42 *************************************************************************/ 43 public static double rmatrixludet(ref double[,] a, 44 ref int[] pivots, 45 int n) 46 { 47 double result = 0; 48 int i = 0; 49 int s = 0; 50 51 result = 1; 52 s = 1; 53 for(i=0; i<=n-1; i++) 54 { 55 result = result*a[i,i]; 56 if( pivots[i]!=i ) 57 { 58 s = -s; 59 } 60 } 61 result = result*s; 62 return result; 21 22 namespace alglib { 23 public class matdet { 24 /************************************************************************* 25 Determinant calculation of the matrix given by its LU decomposition. 26 27 Input parameters: 28 A - LU decomposition of the matrix (output of 29 RMatrixLU subroutine). 30 Pivots - table of permutations which were made during 31 the LU decomposition. 32 Output of RMatrixLU subroutine. 33 N - size of matrix A. 34 35 Result: matrix determinant. 36 37 -- ALGLIB -- 38 Copyright 2005 by Bochkanov Sergey 39 *************************************************************************/ 40 public static double rmatrixludet(ref double[,] a, 41 ref int[] pivots, 42 int n) { 43 double result = 0; 44 int i = 0; 45 int s = 0; 46 47 result = 1; 48 s = 1; 49 for (i = 0; i <= n - 1; i++) { 50 result = result * a[i, i]; 51 if (pivots[i] != i) { 52 s = -s; 63 53 } 64 65 66 /************************************************************************* 67 Calculation of the determinant of a general matrix 68 69 Input parameters: 70 A - matrix, array[0..N-1, 0..N-1] 71 N - size of matrix A. 72 73 Result: determinant of matrix A. 74 75 -- ALGLIB -- 76 Copyright 2005 by Bochkanov Sergey 77 *************************************************************************/ 78 public static double rmatrixdet(double[,] a, 79 int n) 80 { 81 double result = 0; 82 int[] pivots = new int[0]; 83 84 a = (double[,])a.Clone(); 85 86 trfac.rmatrixlu(ref a, n, n, ref pivots); 87 result = rmatrixludet(ref a, ref pivots, n); 88 return result; 54 } 55 result = result * s; 56 return result; 57 } 58 59 60 /************************************************************************* 61 Calculation of the determinant of a general matrix 62 63 Input parameters: 64 A - matrix, array[0..N-1, 0..N-1] 65 N - size of matrix A. 66 67 Result: determinant of matrix A. 68 69 -- ALGLIB -- 70 Copyright 2005 by Bochkanov Sergey 71 *************************************************************************/ 72 public static double rmatrixdet(double[,] a, 73 int n) { 74 double result = 0; 75 int[] pivots = new int[0]; 76 77 a = (double[,])a.Clone(); 78 79 trfac.rmatrixlu(ref a, n, n, ref pivots); 80 result = rmatrixludet(ref a, ref pivots, n); 81 return result; 82 } 83 84 85 /************************************************************************* 86 Determinant calculation of the matrix given by its LU decomposition. 87 88 Input parameters: 89 A - LU decomposition of the matrix (output of 90 RMatrixLU subroutine). 91 Pivots - table of permutations which were made during 92 the LU decomposition. 93 Output of RMatrixLU subroutine. 94 N - size of matrix A. 95 96 Result: matrix determinant. 97 98 -- ALGLIB -- 99 Copyright 2005 by Bochkanov Sergey 100 *************************************************************************/ 101 public static AP.Complex cmatrixludet(ref AP.Complex[,] a, 102 ref int[] pivots, 103 int n) { 104 AP.Complex result = 0; 105 int i = 0; 106 int s = 0; 107 108 result = 1; 109 s = 1; 110 for (i = 0; i <= n - 1; i++) { 111 result = result * a[i, i]; 112 if (pivots[i] != i) { 113 s = -s; 89 114 } 90 91 92 /************************************************************************* 93 Determinant calculation of the matrix given by its LU decomposition. 94 95 Input parameters: 96 A - LU decomposition of the matrix (output of 97 RMatrixLU subroutine). 98 Pivots - table of permutations which were made during 99 the LU decomposition. 100 Output of RMatrixLU subroutine. 101 N - size of matrix A. 102 103 Result: matrix determinant. 104 105 -- ALGLIB -- 106 Copyright 2005 by Bochkanov Sergey 107 *************************************************************************/ 108 public static AP.Complex cmatrixludet(ref AP.Complex[,] a, 109 ref int[] pivots, 110 int n) 111 { 112 AP.Complex result = 0; 113 int i = 0; 114 int s = 0; 115 116 result = 1; 117 s = 1; 118 for(i=0; i<=n-1; i++) 119 { 120 result = result*a[i,i]; 121 if( pivots[i]!=i ) 122 { 123 s = -s; 124 } 125 } 126 result = result*s; 127 return result; 128 } 129 130 131 /************************************************************************* 132 Calculation of the determinant of a general matrix 133 134 Input parameters: 135 A - matrix, array[0..N-1, 0..N-1] 136 N - size of matrix A. 137 138 Result: determinant of matrix A. 139 140 -- ALGLIB -- 141 Copyright 2005 by Bochkanov Sergey 142 *************************************************************************/ 143 public static AP.Complex cmatrixdet(AP.Complex[,] a, 144 int n) 145 { 146 AP.Complex result = 0; 147 int[] pivots = new int[0]; 148 149 a = (AP.Complex[,])a.Clone(); 150 151 trfac.cmatrixlu(ref a, n, n, ref pivots); 152 result = cmatrixludet(ref a, ref pivots, n); 153 return result; 154 } 155 156 157 /************************************************************************* 158 Determinant calculation of the matrix given by the Cholesky decomposition. 159 160 Input parameters: 161 A - Cholesky decomposition, 162 output of SMatrixCholesky subroutine. 163 N - size of matrix A. 164 165 As the determinant is equal to the product of squares of diagonal elements, 166 its not necessary to specify which triangle - lower or upper - the matrix 167 is stored in. 168 169 Result: 170 matrix determinant. 171 172 -- ALGLIB -- 173 Copyright 2005-2008 by Bochkanov Sergey 174 *************************************************************************/ 175 public static double spdmatrixcholeskydet(ref double[,] a, 176 int n) 177 { 178 double result = 0; 179 int i = 0; 180 181 result = 1; 182 for(i=0; i<=n-1; i++) 183 { 184 result = result*AP.Math.Sqr(a[i,i]); 185 } 186 return result; 187 } 188 189 190 /************************************************************************* 191 Determinant calculation of the symmetric positive definite matrix. 192 193 Input parameters: 194 A - matrix. Array with elements [0..N-1, 0..N-1]. 195 N - size of matrix A. 196 IsUpper - if IsUpper = True, then the symmetric matrix A is given by 197 its upper triangle, and the lower triangle isnt used by 198 subroutine. Similarly, if IsUpper = False, then A is given 199 by its lower triangle. 200 201 Result: 202 determinant of matrix A. 203 If matrix A is not positive definite, then subroutine returns -1. 204 205 -- ALGLIB -- 206 Copyright 2005-2008 by Bochkanov Sergey 207 *************************************************************************/ 208 public static double spdmatrixdet(double[,] a, 209 int n, 210 bool isupper) 211 { 212 double result = 0; 213 214 a = (double[,])a.Clone(); 215 216 if( !trfac.spdmatrixcholesky(ref a, n, isupper) ) 217 { 218 result = -1; 219 } 220 else 221 { 222 result = spdmatrixcholeskydet(ref a, n); 223 } 224 return result; 225 } 226 } 115 } 116 result = result * s; 117 return result; 118 } 119 120 121 /************************************************************************* 122 Calculation of the determinant of a general matrix 123 124 Input parameters: 125 A - matrix, array[0..N-1, 0..N-1] 126 N - size of matrix A. 127 128 Result: determinant of matrix A. 129 130 -- ALGLIB -- 131 Copyright 2005 by Bochkanov Sergey 132 *************************************************************************/ 133 public static AP.Complex cmatrixdet(AP.Complex[,] a, 134 int n) { 135 AP.Complex result = 0; 136 int[] pivots = new int[0]; 137 138 a = (AP.Complex[,])a.Clone(); 139 140 trfac.cmatrixlu(ref a, n, n, ref pivots); 141 result = cmatrixludet(ref a, ref pivots, n); 142 return result; 143 } 144 145 146 /************************************************************************* 147 Determinant calculation of the matrix given by the Cholesky decomposition. 148 149 Input parameters: 150 A - Cholesky decomposition, 151 output of SMatrixCholesky subroutine. 152 N - size of matrix A. 153 154 As the determinant is equal to the product of squares of diagonal elements, 155 its not necessary to specify which triangle - lower or upper - the matrix 156 is stored in. 157 158 Result: 159 matrix determinant. 160 161 -- ALGLIB -- 162 Copyright 2005-2008 by Bochkanov Sergey 163 *************************************************************************/ 164 public static double spdmatrixcholeskydet(ref double[,] a, 165 int n) { 166 double result = 0; 167 int i = 0; 168 169 result = 1; 170 for (i = 0; i <= n - 1; i++) { 171 result = result * AP.Math.Sqr(a[i, i]); 172 } 173 return result; 174 } 175 176 177 /************************************************************************* 178 Determinant calculation of the symmetric positive definite matrix. 179 180 Input parameters: 181 A - matrix. Array with elements [0..N-1, 0..N-1]. 182 N - size of matrix A. 183 IsUpper - if IsUpper = True, then the symmetric matrix A is given by 184 its upper triangle, and the lower triangle isnt used by 185 subroutine. Similarly, if IsUpper = False, then A is given 186 by its lower triangle. 187 188 Result: 189 determinant of matrix A. 190 If matrix A is not positive definite, then subroutine returns -1. 191 192 -- ALGLIB -- 193 Copyright 2005-2008 by Bochkanov Sergey 194 *************************************************************************/ 195 public static double spdmatrixdet(double[,] a, 196 int n, 197 bool isupper) { 198 double result = 0; 199 200 a = (double[,])a.Clone(); 201 202 if (!trfac.spdmatrixcholesky(ref a, n, isupper)) { 203 result = -1; 204 } else { 205 result = spdmatrixcholeskydet(ref a, n); 206 } 207 return result; 208 } 209 } 227 210 }
Note: See TracChangeset
for help on using the changeset viewer.