1 | /*************************************************************************
|
---|
2 | Copyright (c) 2005-2007, Sergey Bochkanov (ALGLIB project).
|
---|
3 |
|
---|
4 | >>> SOURCE LICENSE >>>
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
8 | License, or (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | A copy of the GNU General Public License is available at
|
---|
16 | http://www.fsf.org/licensing/licenses
|
---|
17 |
|
---|
18 | >>> END OF LICENSE >>>
|
---|
19 | *************************************************************************/
|
---|
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;
|
---|
63 | }
|
---|
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;
|
---|
89 | }
|
---|
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 | }
|
---|
227 | }
|
---|