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 spddet
|
---|
26 | {
|
---|
27 | /*************************************************************************
|
---|
28 | Determinant calculation of the matrix given by the Cholesky decomposition.
|
---|
29 |
|
---|
30 | Input parameters:
|
---|
31 | A - Cholesky decomposition,
|
---|
32 | output of SMatrixCholesky subroutine.
|
---|
33 | N - size of matrix A.
|
---|
34 |
|
---|
35 | As the determinant is equal to the product of squares of diagonal elements,
|
---|
36 | its not necessary to specify which triangle - lower or upper - the matrix
|
---|
37 | is stored in.
|
---|
38 |
|
---|
39 | Result:
|
---|
40 | matrix determinant.
|
---|
41 |
|
---|
42 | -- ALGLIB --
|
---|
43 | Copyright 2005-2008 by Bochkanov Sergey
|
---|
44 | *************************************************************************/
|
---|
45 | public static double spdmatrixcholeskydet(ref double[,] a,
|
---|
46 | int n)
|
---|
47 | {
|
---|
48 | double result = 0;
|
---|
49 | int i = 0;
|
---|
50 |
|
---|
51 | result = 1;
|
---|
52 | for(i=0; i<=n-1; i++)
|
---|
53 | {
|
---|
54 | result = result*AP.Math.Sqr(a[i,i]);
|
---|
55 | }
|
---|
56 | return result;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /*************************************************************************
|
---|
61 | Determinant calculation of the symmetric positive definite matrix.
|
---|
62 |
|
---|
63 | Input parameters:
|
---|
64 | A - matrix. Array with elements [0..N-1, 0..N-1].
|
---|
65 | N - size of matrix A.
|
---|
66 | IsUpper - if IsUpper = True, then the symmetric matrix A is given by
|
---|
67 | its upper triangle, and the lower triangle isnt used by
|
---|
68 | subroutine. Similarly, if IsUpper = False, then A is given
|
---|
69 | by its lower triangle.
|
---|
70 |
|
---|
71 | Result:
|
---|
72 | determinant of matrix A.
|
---|
73 | If matrix A is not positive definite, then subroutine returns -1.
|
---|
74 |
|
---|
75 | -- ALGLIB --
|
---|
76 | Copyright 2005-2008 by Bochkanov Sergey
|
---|
77 | *************************************************************************/
|
---|
78 | public static double spdmatrixdet(double[,] a,
|
---|
79 | int n,
|
---|
80 | bool isupper)
|
---|
81 | {
|
---|
82 | double result = 0;
|
---|
83 |
|
---|
84 | a = (double[,])a.Clone();
|
---|
85 |
|
---|
86 | if( !cholesky.spdmatrixcholesky(ref a, n, isupper) )
|
---|
87 | {
|
---|
88 | result = -1;
|
---|
89 | }
|
---|
90 | else
|
---|
91 | {
|
---|
92 | result = spdmatrixcholeskydet(ref a, n);
|
---|
93 | }
|
---|
94 | return result;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | public static double determinantcholesky(ref double[,] a,
|
---|
99 | int n)
|
---|
100 | {
|
---|
101 | double result = 0;
|
---|
102 | int i = 0;
|
---|
103 |
|
---|
104 | result = 1;
|
---|
105 | for(i=1; i<=n; i++)
|
---|
106 | {
|
---|
107 | result = result*AP.Math.Sqr(a[i,i]);
|
---|
108 | }
|
---|
109 | return result;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | public static double determinantspd(double[,] a,
|
---|
114 | int n,
|
---|
115 | bool isupper)
|
---|
116 | {
|
---|
117 | double result = 0;
|
---|
118 |
|
---|
119 | a = (double[,])a.Clone();
|
---|
120 |
|
---|
121 | if( !cholesky.choleskydecomposition(ref a, n, isupper) )
|
---|
122 | {
|
---|
123 | result = -1;
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | result = determinantcholesky(ref a, n);
|
---|
128 | }
|
---|
129 | return result;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|