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 det
|
---|
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 | lu.rmatrixlu(ref a, n, n, ref pivots);
|
---|
87 | result = rmatrixludet(ref a, ref pivots, n);
|
---|
88 | return result;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | public static double determinantlu(ref double[,] a,
|
---|
93 | ref int[] pivots,
|
---|
94 | int n)
|
---|
95 | {
|
---|
96 | double result = 0;
|
---|
97 | int i = 0;
|
---|
98 | int s = 0;
|
---|
99 |
|
---|
100 | result = 1;
|
---|
101 | s = 1;
|
---|
102 | for(i=1; i<=n; i++)
|
---|
103 | {
|
---|
104 | result = result*a[i,i];
|
---|
105 | if( pivots[i]!=i )
|
---|
106 | {
|
---|
107 | s = -s;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | result = result*s;
|
---|
111 | return result;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | public static double determinant(double[,] a,
|
---|
116 | int n)
|
---|
117 | {
|
---|
118 | double result = 0;
|
---|
119 | int[] pivots = new int[0];
|
---|
120 |
|
---|
121 | a = (double[,])a.Clone();
|
---|
122 |
|
---|
123 | lu.ludecomposition(ref a, n, n, ref pivots);
|
---|
124 | result = determinantlu(ref a, ref pivots, n);
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|