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 |
|
---|
22 | namespace alglib {
|
---|
23 | public class sdet {
|
---|
24 | /*************************************************************************
|
---|
25 | Determinant calculation of the matrix given by LDLT decomposition.
|
---|
26 |
|
---|
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.
|
---|
35 |
|
---|
36 | Result:
|
---|
37 | matrix determinant.
|
---|
38 |
|
---|
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;
|
---|
48 |
|
---|
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 | }
|
---|
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 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /*************************************************************************
|
---|
78 | Determinant calculation of the symmetric matrix
|
---|
79 |
|
---|
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.
|
---|
87 |
|
---|
88 | Result:
|
---|
89 | determinant of matrix A.
|
---|
90 |
|
---|
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];
|
---|
99 |
|
---|
100 | a = (double[,])a.Clone();
|
---|
101 |
|
---|
102 | ldlt.smatrixldlt(ref a, n, isupper, ref pivots);
|
---|
103 | result = smatrixldltdet(ref a, ref pivots, n, isupper);
|
---|
104 | return result;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
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;
|
---|
114 |
|
---|
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 | }
|
---|
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 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | public static double determinantsymmetric(double[,] a,
|
---|
144 | int n,
|
---|
145 | bool isupper) {
|
---|
146 | double result = 0;
|
---|
147 | int[] pivots = new int[0];
|
---|
148 |
|
---|
149 | a = (double[,])a.Clone();
|
---|
150 |
|
---|
151 | ldlt.ldltdecomposition(ref a, n, isupper, ref pivots);
|
---|
152 | result = determinantldlt(ref a, ref pivots, n, isupper);
|
---|
153 | return result;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|