Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/2.5.0/ALGLIB-2.5.0/sdet.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/sdet.cs
r3839 r4068 19 19 *************************************************************************/ 20 20 21 using System;22 21 23 namespace alglib 24 { 25 public class sdet 26 { 27 /************************************************************************* 28 Determinant calculation of the matrix given by LDLT decomposition. 22 namespace alglib { 23 public class sdet { 24 /************************************************************************* 25 Determinant calculation of the matrix given by LDLT decomposition. 29 26 30 31 32 33 34 35 36 37 27 Input parameters: 28 A - LDLT-decomposition of the matrix, 29 output of subroutine SMatrixLDLT. 30 Pivots - table of permutations which were made during 31 LDLT decomposition, output of subroutine SMatrixLDLT. 32 N - size of matrix A. 33 IsUpper - matrix storage format. The value is equal to the input 34 parameter of subroutine SMatrixLDLT. 38 35 39 40 36 Result: 37 matrix determinant. 41 38 42 -- ALGLIB -- 43 Copyright 2005-2008 by Bochkanov Sergey 44 *************************************************************************/ 45 public static double smatrixldltdet(ref double[,] a, 46 ref int[] pivots, 47 int n, 48 bool isupper) 49 { 50 double result = 0; 51 int k = 0; 39 -- ALGLIB -- 40 Copyright 2005-2008 by Bochkanov Sergey 41 *************************************************************************/ 42 public static double smatrixldltdet(ref double[,] a, 43 ref int[] pivots, 44 int n, 45 bool isupper) { 46 double result = 0; 47 int k = 0; 52 48 53 result = 1; 54 if( isupper ) 55 { 56 k = 0; 57 while( k<n ) 58 { 59 if( pivots[k]>=0 ) 60 { 61 result = result*a[k,k]; 62 k = k+1; 63 } 64 else 65 { 66 result = result*(a[k,k]*a[k+1,k+1]-a[k,k+1]*a[k,k+1]); 67 k = k+2; 68 } 69 } 70 } 71 else 72 { 73 k = n-1; 74 while( k>=0 ) 75 { 76 if( pivots[k]>=0 ) 77 { 78 result = result*a[k,k]; 79 k = k-1; 80 } 81 else 82 { 83 result = result*(a[k-1,k-1]*a[k,k]-a[k,k-1]*a[k,k-1]); 84 k = k-2; 85 } 86 } 87 } 88 return result; 49 result = 1; 50 if (isupper) { 51 k = 0; 52 while (k < n) { 53 if (pivots[k] >= 0) { 54 result = result * a[k, k]; 55 k = k + 1; 56 } else { 57 result = result * (a[k, k] * a[k + 1, k + 1] - a[k, k + 1] * a[k, k + 1]); 58 k = k + 2; 59 } 89 60 } 61 } else { 62 k = n - 1; 63 while (k >= 0) { 64 if (pivots[k] >= 0) { 65 result = result * a[k, k]; 66 k = k - 1; 67 } else { 68 result = result * (a[k - 1, k - 1] * a[k, k] - a[k, k - 1] * a[k, k - 1]); 69 k = k - 2; 70 } 71 } 72 } 73 return result; 74 } 90 75 91 76 92 93 77 /************************************************************************* 78 Determinant calculation of the symmetric matrix 94 79 95 96 97 98 99 100 101 80 Input parameters: 81 A - matrix. Array with elements [0..N-1, 0..N-1]. 82 N - size of matrix A. 83 IsUpper - if IsUpper = True, then symmetric matrix A is given by its 84 upper triangle, and the lower triangle isnt used by 85 subroutine. Similarly, if IsUpper = False, then A is given 86 by its lower triangle. 102 87 103 104 88 Result: 89 determinant of matrix A. 105 90 106 -- ALGLIB -- 107 Copyright 2005-2008 by Bochkanov Sergey 108 *************************************************************************/ 109 public static double smatrixdet(double[,] a, 110 int n, 111 bool isupper) 112 { 113 double result = 0; 114 int[] pivots = new int[0]; 91 -- ALGLIB -- 92 Copyright 2005-2008 by Bochkanov Sergey 93 *************************************************************************/ 94 public static double smatrixdet(double[,] a, 95 int n, 96 bool isupper) { 97 double result = 0; 98 int[] pivots = new int[0]; 115 99 116 100 a = (double[,])a.Clone(); 117 101 118 119 120 121 102 ldlt.smatrixldlt(ref a, n, isupper, ref pivots); 103 result = smatrixldltdet(ref a, ref pivots, n, isupper); 104 return result; 105 } 122 106 123 107 124 public static double determinantldlt(ref double[,] a, 125 ref int[] pivots, 126 int n, 127 bool isupper) 128 { 129 double result = 0; 130 int k = 0; 108 public static double determinantldlt(ref double[,] a, 109 ref int[] pivots, 110 int n, 111 bool isupper) { 112 double result = 0; 113 int k = 0; 131 114 132 result = 1; 133 if( isupper ) 134 { 135 k = 1; 136 while( k<=n ) 137 { 138 if( pivots[k]>0 ) 139 { 140 result = result*a[k,k]; 141 k = k+1; 142 } 143 else 144 { 145 result = result*(a[k,k]*a[k+1,k+1]-a[k,k+1]*a[k,k+1]); 146 k = k+2; 147 } 148 } 149 } 150 else 151 { 152 k = n; 153 while( k>=1 ) 154 { 155 if( pivots[k]>0 ) 156 { 157 result = result*a[k,k]; 158 k = k-1; 159 } 160 else 161 { 162 result = result*(a[k-1,k-1]*a[k,k]-a[k,k-1]*a[k,k-1]); 163 k = k-2; 164 } 165 } 166 } 167 return result; 115 result = 1; 116 if (isupper) { 117 k = 1; 118 while (k <= n) { 119 if (pivots[k] > 0) { 120 result = result * a[k, k]; 121 k = k + 1; 122 } else { 123 result = result * (a[k, k] * a[k + 1, k + 1] - a[k, k + 1] * a[k, k + 1]); 124 k = k + 2; 125 } 168 126 } 127 } else { 128 k = n; 129 while (k >= 1) { 130 if (pivots[k] > 0) { 131 result = result * a[k, k]; 132 k = k - 1; 133 } else { 134 result = result * (a[k - 1, k - 1] * a[k, k] - a[k, k - 1] * a[k, k - 1]); 135 k = k - 2; 136 } 137 } 138 } 139 return result; 140 } 169 141 170 142 171 public static double determinantsymmetric(double[,] a, 172 int n, 173 bool isupper) 174 { 175 double result = 0; 176 int[] pivots = new int[0]; 143 public static double determinantsymmetric(double[,] a, 144 int n, 145 bool isupper) { 146 double result = 0; 147 int[] pivots = new int[0]; 177 148 178 149 a = (double[,])a.Clone(); 179 150 180 ldlt.ldltdecomposition(ref a, n, isupper, ref pivots); 181 result = determinantldlt(ref a, ref pivots, n, isupper); 182 return result; 183 } 151 ldlt.ldltdecomposition(ref a, n, isupper, ref pivots); 152 result = determinantldlt(ref a, ref pivots, n, isupper); 153 return result; 184 154 } 155 } 185 156 }
Note: See TracChangeset
for help on using the changeset viewer.