1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | using System;
|
---|
41 | using System.Collections.Generic;
|
---|
42 | using System.Text;
|
---|
43 | using ILNumerics.Storage;
|
---|
44 | using ILNumerics.Misc;
|
---|
45 | using ILNumerics.Exceptions;
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | namespace ILNumerics {
|
---|
50 |
|
---|
51 | public partial class ILMath {
|
---|
52 |
|
---|
53 | |
---|
54 | /// <summary>
|
---|
55 | /// Determinant of square matrix
|
---|
56 | /// </summary>
|
---|
57 | /// <param name="A">Input matrix (square)</param>
|
---|
58 | /// <returns>Determinant of A</returns>
|
---|
59 | /// <remarks><para>The determinant is computed by decomposing A into upper and lower triangular part (using the LAPACK function ?getrf).<br />
|
---|
60 | /// Due to the properties of determinants, det(a) is the same as det(L) * det(U),where det(L) can easily be extracted from the permutation indices returned from LU decomposition. det(U) - with U being an upper triangular matrix - equals the product of the diagonal elements.</para>
|
---|
61 | /// <para>For scalar A, a plain copy of A is returned.</para></remarks>
|
---|
62 | /// <example>Creating a nonsingular 4x4 (double) matrix and it's determinant
|
---|
63 | /// <code>ILArray<double> A = ILMath.counter(1.0,1.0,4,4);
|
---|
64 | ///A[1] = 0.0; // make A nonsingular
|
---|
65 | ///A[14] = 0.0; //(same as: A[2,3] = 0.0;)
|
---|
66 | /// // A is now:
|
---|
67 | /// //<Double> [4,4]
|
---|
68 | /// //(:,:) 1e+001 *
|
---|
69 | /// // 0,10000 0,50000 0,90000 1,30000
|
---|
70 | /// // 0,00000 0,60000 1,00000 1,40000
|
---|
71 | /// // 0,30000 0,70000 1,10000 0,00000
|
---|
72 | /// // 0,40000 0,80000 1,20000 1,60000
|
---|
73 | ///
|
---|
74 | ///ILMath.det(A) gives:
|
---|
75 | /// //<Double> -360
|
---|
76 | ///</code></example>
|
---|
77 | ///<exception cref="ILNumerics.Exceptions.ILArgumentException">if A is empty or not a square matrix</exception>
|
---|
78 | public static ILRetArray< double > det(ILInArray< double > A) {
|
---|
79 | using (ILScope.Enter(A)) {
|
---|
80 | if (A.IsScalar)
|
---|
81 | return A.C;
|
---|
82 | if (A.IsEmpty)
|
---|
83 | throw new ILArgumentException("det: A must be a matrix");
|
---|
84 | int m = A.Size[0];
|
---|
85 | if (m != A.Size[1]) {
|
---|
86 | throw new ILArgumentException("det: matrix A must be square");
|
---|
87 | }
|
---|
88 |
|
---|
89 | ILArray< double > L = A.C;
|
---|
90 | double [] lArr = L.GetArrayForWrite();
|
---|
91 | int [] pivInd = new int[m];
|
---|
92 | int info = 0;
|
---|
93 | /*!HC:lapack_*getrf*/ Lapack.dgetrf (m, m, lArr, m, pivInd ,ref info);
|
---|
94 | if (info < 0 ) {
|
---|
95 | throw new ILArgumentException("det: illegal parameter error");
|
---|
96 | }
|
---|
97 | // determine pivoting: number of exchanges
|
---|
98 | double retA = 1.0 ;
|
---|
99 | for (int i = 0; i < m;) {
|
---|
100 | retA *= lArr[i * m + i];
|
---|
101 | if (pivInd[i] != ++i) retA *= -1.0 ;
|
---|
102 | }
|
---|
103 | return retA;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | |
---|
107 | #region HYCALPER AUTO GENERATED CODE
|
---|
108 | |
---|
109 | /// <summary>
|
---|
110 | /// Determinant of square matrix
|
---|
111 | /// </summary>
|
---|
112 | /// <param name="A">Input matrix (square)</param>
|
---|
113 | /// <returns>Determinant of A</returns>
|
---|
114 | /// <remarks><para>The determinant is computed by decomposing A into upper and lower triangular part (using the LAPACK function ?getrf).<br />
|
---|
115 | /// Due to the properties of determinants, det(a) is the same as det(L) * det(U),where det(L) can easily be extracted from the permutation indices returned from LU decomposition. det(U) - with U being an upper triangular matrix - equals the product of the diagonal elements.</para>
|
---|
116 | /// <para>For scalar A, a plain copy of A is returned.</para></remarks>
|
---|
117 | /// <example>Creating a nonsingular 4x4 (double) matrix and it's determinant
|
---|
118 | /// <code>ILArray<double> A = ILMath.counter(1.0,1.0,4,4);
|
---|
119 | ///A[1] = 0.0; // make A nonsingular
|
---|
120 | ///A[14] = 0.0; //(same as: A[2,3] = 0.0;)
|
---|
121 | /// // A is now:
|
---|
122 | /// //<Double> [4,4]
|
---|
123 | /// //(:,:) 1e+001 *
|
---|
124 | /// // 0,10000 0,50000 0,90000 1,30000
|
---|
125 | /// // 0,00000 0,60000 1,00000 1,40000
|
---|
126 | /// // 0,30000 0,70000 1,10000 0,00000
|
---|
127 | /// // 0,40000 0,80000 1,20000 1,60000
|
---|
128 | ///
|
---|
129 | ///ILMath.det(A) gives:
|
---|
130 | /// //<Double> -360
|
---|
131 | ///</code></example>
|
---|
132 | ///<exception cref="ILNumerics.Exceptions.ILArgumentException">if A is empty or not a square matrix</exception>
|
---|
133 | public static ILRetArray< float > det(ILInArray< float > A) {
|
---|
134 | using (ILScope.Enter(A)) {
|
---|
135 | if (A.IsScalar)
|
---|
136 | return A.C;
|
---|
137 | if (A.IsEmpty)
|
---|
138 | throw new ILArgumentException("det: A must be a matrix");
|
---|
139 | int m = A.Size[0];
|
---|
140 | if (m != A.Size[1]) {
|
---|
141 | throw new ILArgumentException("det: matrix A must be square");
|
---|
142 | }
|
---|
143 |
|
---|
144 | ILArray< float > L = A.C;
|
---|
145 | float [] lArr = L.GetArrayForWrite();
|
---|
146 | int [] pivInd = new int[m];
|
---|
147 | int info = 0;
|
---|
148 | Lapack.sgetrf (m, m, lArr, m, pivInd ,ref info);
|
---|
149 | if (info < 0 ) {
|
---|
150 | throw new ILArgumentException("det: illegal parameter error");
|
---|
151 | }
|
---|
152 | // determine pivoting: number of exchanges
|
---|
153 | float retA = 1.0f ;
|
---|
154 | for (int i = 0; i < m;) {
|
---|
155 | retA *= lArr[i * m + i];
|
---|
156 | if (pivInd[i] != ++i) retA *= -1.0f ;
|
---|
157 | }
|
---|
158 | return retA;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | /// <summary>
|
---|
162 | /// Determinant of square matrix
|
---|
163 | /// </summary>
|
---|
164 | /// <param name="A">Input matrix (square)</param>
|
---|
165 | /// <returns>Determinant of A</returns>
|
---|
166 | /// <remarks><para>The determinant is computed by decomposing A into upper and lower triangular part (using the LAPACK function ?getrf).<br />
|
---|
167 | /// Due to the properties of determinants, det(a) is the same as det(L) * det(U),where det(L) can easily be extracted from the permutation indices returned from LU decomposition. det(U) - with U being an upper triangular matrix - equals the product of the diagonal elements.</para>
|
---|
168 | /// <para>For scalar A, a plain copy of A is returned.</para></remarks>
|
---|
169 | /// <example>Creating a nonsingular 4x4 (double) matrix and it's determinant
|
---|
170 | /// <code>ILArray<double> A = ILMath.counter(1.0,1.0,4,4);
|
---|
171 | ///A[1] = 0.0; // make A nonsingular
|
---|
172 | ///A[14] = 0.0; //(same as: A[2,3] = 0.0;)
|
---|
173 | /// // A is now:
|
---|
174 | /// //<Double> [4,4]
|
---|
175 | /// //(:,:) 1e+001 *
|
---|
176 | /// // 0,10000 0,50000 0,90000 1,30000
|
---|
177 | /// // 0,00000 0,60000 1,00000 1,40000
|
---|
178 | /// // 0,30000 0,70000 1,10000 0,00000
|
---|
179 | /// // 0,40000 0,80000 1,20000 1,60000
|
---|
180 | ///
|
---|
181 | ///ILMath.det(A) gives:
|
---|
182 | /// //<Double> -360
|
---|
183 | ///</code></example>
|
---|
184 | ///<exception cref="ILNumerics.Exceptions.ILArgumentException">if A is empty or not a square matrix</exception>
|
---|
185 | public static ILRetArray< fcomplex > det(ILInArray< fcomplex > A) {
|
---|
186 | using (ILScope.Enter(A)) {
|
---|
187 | if (A.IsScalar)
|
---|
188 | return A.C;
|
---|
189 | if (A.IsEmpty)
|
---|
190 | throw new ILArgumentException("det: A must be a matrix");
|
---|
191 | int m = A.Size[0];
|
---|
192 | if (m != A.Size[1]) {
|
---|
193 | throw new ILArgumentException("det: matrix A must be square");
|
---|
194 | }
|
---|
195 |
|
---|
196 | ILArray< fcomplex > L = A.C;
|
---|
197 | fcomplex [] lArr = L.GetArrayForWrite();
|
---|
198 | int [] pivInd = new int[m];
|
---|
199 | int info = 0;
|
---|
200 | Lapack.cgetrf (m, m, lArr, m, pivInd ,ref info);
|
---|
201 | if (info < 0 ) {
|
---|
202 | throw new ILArgumentException("det: illegal parameter error");
|
---|
203 | }
|
---|
204 | // determine pivoting: number of exchanges
|
---|
205 | fcomplex retA = new fcomplex(1.0f,0.0f) ;
|
---|
206 | for (int i = 0; i < m;) {
|
---|
207 | retA *= lArr[i * m + i];
|
---|
208 | if (pivInd[i] != ++i) retA *= -1.0f ;
|
---|
209 | }
|
---|
210 | return retA;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | /// <summary>
|
---|
214 | /// Determinant of square matrix
|
---|
215 | /// </summary>
|
---|
216 | /// <param name="A">Input matrix (square)</param>
|
---|
217 | /// <returns>Determinant of A</returns>
|
---|
218 | /// <remarks><para>The determinant is computed by decomposing A into upper and lower triangular part (using the LAPACK function ?getrf).<br />
|
---|
219 | /// Due to the properties of determinants, det(a) is the same as det(L) * det(U),where det(L) can easily be extracted from the permutation indices returned from LU decomposition. det(U) - with U being an upper triangular matrix - equals the product of the diagonal elements.</para>
|
---|
220 | /// <para>For scalar A, a plain copy of A is returned.</para></remarks>
|
---|
221 | /// <example>Creating a nonsingular 4x4 (double) matrix and it's determinant
|
---|
222 | /// <code>ILArray<double> A = ILMath.counter(1.0,1.0,4,4);
|
---|
223 | ///A[1] = 0.0; // make A nonsingular
|
---|
224 | ///A[14] = 0.0; //(same as: A[2,3] = 0.0;)
|
---|
225 | /// // A is now:
|
---|
226 | /// //<Double> [4,4]
|
---|
227 | /// //(:,:) 1e+001 *
|
---|
228 | /// // 0,10000 0,50000 0,90000 1,30000
|
---|
229 | /// // 0,00000 0,60000 1,00000 1,40000
|
---|
230 | /// // 0,30000 0,70000 1,10000 0,00000
|
---|
231 | /// // 0,40000 0,80000 1,20000 1,60000
|
---|
232 | ///
|
---|
233 | ///ILMath.det(A) gives:
|
---|
234 | /// //<Double> -360
|
---|
235 | ///</code></example>
|
---|
236 | ///<exception cref="ILNumerics.Exceptions.ILArgumentException">if A is empty or not a square matrix</exception>
|
---|
237 | public static ILRetArray< complex > det(ILInArray< complex > A) {
|
---|
238 | using (ILScope.Enter(A)) {
|
---|
239 | if (A.IsScalar)
|
---|
240 | return A.C;
|
---|
241 | if (A.IsEmpty)
|
---|
242 | throw new ILArgumentException("det: A must be a matrix");
|
---|
243 | int m = A.Size[0];
|
---|
244 | if (m != A.Size[1]) {
|
---|
245 | throw new ILArgumentException("det: matrix A must be square");
|
---|
246 | }
|
---|
247 |
|
---|
248 | ILArray< complex > L = A.C;
|
---|
249 | complex [] lArr = L.GetArrayForWrite();
|
---|
250 | int [] pivInd = new int[m];
|
---|
251 | int info = 0;
|
---|
252 | Lapack.zgetrf (m, m, lArr, m, pivInd ,ref info);
|
---|
253 | if (info < 0 ) {
|
---|
254 | throw new ILArgumentException("det: illegal parameter error");
|
---|
255 | }
|
---|
256 | // determine pivoting: number of exchanges
|
---|
257 | complex retA = new complex(1.0,0.0) ;
|
---|
258 | for (int i = 0; i < m;) {
|
---|
259 | retA *= lArr[i * m + i];
|
---|
260 | if (pivInd[i] != ++i) retA *= -1.0 ;
|
---|
261 | }
|
---|
262 | return retA;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
267 | }
|
---|
268 | }
|
---|