Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/2.5.0/ALGLIB-2.5.0/matdet.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.6 KB
Line 
1/*************************************************************************
2Copyright (c) 2005-2007, Sergey Bochkanov (ALGLIB project).
3
4>>> SOURCE LICENSE >>>
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation (www.fsf.org); either version 2 of the
8License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15A copy of the GNU General Public License is available at
16http://www.fsf.org/licensing/licenses
17
18>>> END OF LICENSE >>>
19*************************************************************************/
20
21
22namespace alglib {
23  public class matdet {
24    /*************************************************************************
25    Determinant calculation of the matrix given by its LU decomposition.
26
27    Input parameters:
28        A       -   LU decomposition of the matrix (output of
29                    RMatrixLU subroutine).
30        Pivots  -   table of permutations which were made during
31                    the LU decomposition.
32                    Output of RMatrixLU subroutine.
33        N       -   size of matrix A.
34
35    Result: matrix determinant.
36
37      -- ALGLIB --
38         Copyright 2005 by Bochkanov Sergey
39    *************************************************************************/
40    public static double rmatrixludet(ref double[,] a,
41        ref int[] pivots,
42        int n) {
43      double result = 0;
44      int i = 0;
45      int s = 0;
46
47      result = 1;
48      s = 1;
49      for (i = 0; i <= n - 1; i++) {
50        result = result * a[i, i];
51        if (pivots[i] != i) {
52          s = -s;
53        }
54      }
55      result = result * s;
56      return result;
57    }
58
59
60    /*************************************************************************
61    Calculation of the determinant of a general matrix
62
63    Input parameters:
64        A       -   matrix, array[0..N-1, 0..N-1]
65        N       -   size of matrix A.
66
67    Result: determinant of matrix A.
68
69      -- ALGLIB --
70         Copyright 2005 by Bochkanov Sergey
71    *************************************************************************/
72    public static double rmatrixdet(double[,] a,
73        int n) {
74      double result = 0;
75      int[] pivots = new int[0];
76
77      a = (double[,])a.Clone();
78
79      trfac.rmatrixlu(ref a, n, n, ref pivots);
80      result = rmatrixludet(ref a, ref pivots, n);
81      return result;
82    }
83
84
85    /*************************************************************************
86    Determinant calculation of the matrix given by its LU decomposition.
87
88    Input parameters:
89        A       -   LU decomposition of the matrix (output of
90                    RMatrixLU subroutine).
91        Pivots  -   table of permutations which were made during
92                    the LU decomposition.
93                    Output of RMatrixLU subroutine.
94        N       -   size of matrix A.
95
96    Result: matrix determinant.
97
98      -- ALGLIB --
99         Copyright 2005 by Bochkanov Sergey
100    *************************************************************************/
101    public static AP.Complex cmatrixludet(ref AP.Complex[,] a,
102        ref int[] pivots,
103        int n) {
104      AP.Complex result = 0;
105      int i = 0;
106      int s = 0;
107
108      result = 1;
109      s = 1;
110      for (i = 0; i <= n - 1; i++) {
111        result = result * a[i, i];
112        if (pivots[i] != i) {
113          s = -s;
114        }
115      }
116      result = result * s;
117      return result;
118    }
119
120
121    /*************************************************************************
122    Calculation of the determinant of a general matrix
123
124    Input parameters:
125        A       -   matrix, array[0..N-1, 0..N-1]
126        N       -   size of matrix A.
127
128    Result: determinant of matrix A.
129
130      -- ALGLIB --
131         Copyright 2005 by Bochkanov Sergey
132    *************************************************************************/
133    public static AP.Complex cmatrixdet(AP.Complex[,] a,
134        int n) {
135      AP.Complex result = 0;
136      int[] pivots = new int[0];
137
138      a = (AP.Complex[,])a.Clone();
139
140      trfac.cmatrixlu(ref a, n, n, ref pivots);
141      result = cmatrixludet(ref a, ref pivots, n);
142      return result;
143    }
144
145
146    /*************************************************************************
147    Determinant calculation of the matrix given by the Cholesky decomposition.
148
149    Input parameters:
150        A   -   Cholesky decomposition,
151                output of SMatrixCholesky subroutine.
152        N   -   size of matrix A.
153
154    As the determinant is equal to the product of squares of diagonal elements,
155    it’s not necessary to specify which triangle - lower or upper - the matrix
156    is stored in.
157
158    Result:
159        matrix determinant.
160
161      -- ALGLIB --
162         Copyright 2005-2008 by Bochkanov Sergey
163    *************************************************************************/
164    public static double spdmatrixcholeskydet(ref double[,] a,
165        int n) {
166      double result = 0;
167      int i = 0;
168
169      result = 1;
170      for (i = 0; i <= n - 1; i++) {
171        result = result * AP.Math.Sqr(a[i, i]);
172      }
173      return result;
174    }
175
176
177    /*************************************************************************
178    Determinant calculation of the symmetric positive definite matrix.
179
180    Input parameters:
181        A       -   matrix. Array with elements [0..N-1, 0..N-1].
182        N       -   size of matrix A.
183        IsUpper -   if IsUpper = True, then the symmetric matrix A is given by
184                    its upper triangle, and the lower triangle isn’t used by
185                    subroutine. Similarly, if IsUpper = False, then A is given
186                    by its lower triangle.
187
188    Result:
189        determinant of matrix A.
190        If matrix A is not positive definite, then subroutine returns -1.
191
192      -- ALGLIB --
193         Copyright 2005-2008 by Bochkanov Sergey
194    *************************************************************************/
195    public static double spdmatrixdet(double[,] a,
196        int n,
197        bool isupper) {
198      double result = 0;
199
200      a = (double[,])a.Clone();
201
202      if (!trfac.spdmatrixcholesky(ref a, n, isupper)) {
203        result = -1;
204      } else {
205        result = spdmatrixcholeskydet(ref a, n);
206      }
207      return result;
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.