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( !trfac.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 | }
|
---|