Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Functions/builtin/chol.cs @ 10355

Last change on this file since 10355 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 15.2 KB
RevLine 
[9102]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
40using System;
41using System.Collections.Generic;
42using System.Text;
43using ILNumerics.Storage;
44using ILNumerics.Misc;
45using ILNumerics.Exceptions;
46
47
48
49namespace ILNumerics {
50
51    public partial class ILMath {
52
53
54        /// <summary>Cholesky factorization</summary>
55        /// <param name="A">Input array A. A must be a symmetric/hermitian matrix.
56        /// Therefore the upper triangular part of A must be the transpose (complex conjugate for complex input) of the lower triangular part. No check
57        /// is made for that! <br/>
58        /// The elements of A will not be altered.</param>
59        /// <param name="throwException">Throw an ILArgumentException if A
60        /// is found not to be positive definite.</param>
61        /// <returns>Cholesky factorization</returns>
62        /// <remarks><para>If <paramref name="throwException"/> is true and
63        /// A is found not to be positive definite, an ILArgumentException
64        /// will be thrown and the operation will be canceled.</para>
65        /// <para>If <paramref name="throwException"/> is false, check the
66        /// return value's dimension to determine the success of the
67        /// operation (unless you are sure, A was positive definite).
68        /// If A was found not to be positive definite the matrix returned
69        /// will be of dimension [k x k] and the result of the cholesky
70        /// factorization of A[0:k-1;0:k-1]. Here k is the first leading
71        /// minor of A at which A was found to be not positive definite.</para>
72        /// The factorization is carried out by use of the LAPACK functions
73        /// DPOTRF, ZPOTRF, SPOTRF or CPOTRF respectively. </remarks>
74        public static ILRetArray<double> chol(ILInArray<double> A, bool throwException = true) {
75            using (ILScope.Enter(A)) {
76                if (!A.IsMatrix)
77                    throw new ILArgumentSizeException("chol is defined for matrices only!");
78                int n = A.Size[0], info = 0;
79                if (A.Size[1] != n)
80                    throw new ILArgumentException("chol: input matrix must be square!");
81                ILDenseStorage<double> ret = A.Storage.copyUpperTriangle(n);
82                /*!HC:lapack_*potrf*/ Lapack.dpotrf ('U', n, ret.GetArrayForRead(), n, ref info);
83                if (info < 0 ) {
84                    throw new ILArgumentException("chol: illegal parameter error.");
85                } else if (info > 0) {
86                    // not pos.definite
87                    if (! throwException) {
88                        if (info > 1) {
89                            int newDim = info - 1;
90                            ret = A.Storage.copyUpperTriangle(newDim);
91                            /*!HC:lapack_*potrf*/  Lapack.dpotrf ('U',newDim,ret.GetArrayForRead(), newDim, ref info);
92                            if (info != 0)
93                                throw new ILArgumentException("chol: the original matrix given was not pos. definit. An attempt to decompose the submatrix of order " + (newDim + 1).ToString() + " failed.");
94                            return new ILRetArray<double>(ret);
95                        } else {
96                            return empty<double>(ILSize.Empty00);
97                        }
98                    } else {
99                        throw new ILArgumentException("chol: the matrix given is not positive definite!");
100                    }
101                }
102                return new ILRetArray<double>(ret);
103            }
104        }
105
106#region HYCALPER AUTO GENERATED CODE
107
108        /// <summary>Cholesky factorization</summary>
109        /// <param name="A">Input array A. A must be a symmetric/hermitian matrix.
110        /// Therefore the upper triangular part of A must be the transpose (complex conjugate for complex input) of the lower triangular part. No check
111        /// is made for that! <br/>
112        /// The elements of A will not be altered.</param>
113        /// <param name="throwException">Throw an ILArgumentException if A
114        /// is found not to be positive definite.</param>
115        /// <returns>Cholesky factorization</returns>
116        /// <remarks><para>If <paramref name="throwException"/> is true and
117        /// A is found not to be positive definite, an ILArgumentException
118        /// will be thrown and the operation will be canceled.</para>
119        /// <para>If <paramref name="throwException"/> is false, check the
120        /// return value's dimension to determine the success of the
121        /// operation (unless you are sure, A was positive definite).
122        /// If A was found not to be positive definite the matrix returned
123        /// will be of dimension [k x k] and the result of the cholesky
124        /// factorization of A[0:k-1;0:k-1]. Here k is the first leading
125        /// minor of A at which A was found to be not positive definite.</para>
126        /// The factorization is carried out by use of the LAPACK functions
127        /// DPOTRF, ZPOTRF, SPOTRF or CPOTRF respectively. </remarks>
128        public static ILRetArray<float> chol(ILInArray<float> A, bool throwException = true) {
129            using (ILScope.Enter(A)) {
130                if (!A.IsMatrix)
131                    throw new ILArgumentSizeException("chol is defined for matrices only!");
132                int n = A.Size[0], info = 0;
133                if (A.Size[1] != n)
134                    throw new ILArgumentException("chol: input matrix must be square!");
135                ILDenseStorage<float> ret = A.Storage.copyUpperTriangle(n);
136                Lapack.spotrf ('U', n, ret.GetArrayForRead(), n, ref info);
137                if (info < 0 ) {
138                    throw new ILArgumentException("chol: illegal parameter error.");
139                } else if (info > 0) {
140                    // not pos.definite
141                    if (! throwException) {
142                        if (info > 1) {
143                            int newDim = info - 1;
144                            ret = A.Storage.copyUpperTriangle(newDim);
145                             Lapack.spotrf ('U',newDim,ret.GetArrayForRead(), newDim, ref info);
146                            if (info != 0)
147                                throw new ILArgumentException("chol: the original matrix given was not pos. definit. An attempt to decompose the submatrix of order " + (newDim + 1).ToString() + " failed.");
148                            return new ILRetArray<float>(ret);
149                        } else {
150                            return empty<float>(ILSize.Empty00);
151                        }
152                    } else {
153                        throw new ILArgumentException("chol: the matrix given is not positive definite!");
154                    }
155                }
156                return new ILRetArray<float>(ret);
157            }
158        }
159        /// <summary>Cholesky factorization</summary>
160        /// <param name="A">Input array A. A must be a symmetric/hermitian matrix.
161        /// Therefore the upper triangular part of A must be the transpose (complex conjugate for complex input) of the lower triangular part. No check
162        /// is made for that! <br/>
163        /// The elements of A will not be altered.</param>
164        /// <param name="throwException">Throw an ILArgumentException if A
165        /// is found not to be positive definite.</param>
166        /// <returns>Cholesky factorization</returns>
167        /// <remarks><para>If <paramref name="throwException"/> is true and
168        /// A is found not to be positive definite, an ILArgumentException
169        /// will be thrown and the operation will be canceled.</para>
170        /// <para>If <paramref name="throwException"/> is false, check the
171        /// return value's dimension to determine the success of the
172        /// operation (unless you are sure, A was positive definite).
173        /// If A was found not to be positive definite the matrix returned
174        /// will be of dimension [k x k] and the result of the cholesky
175        /// factorization of A[0:k-1;0:k-1]. Here k is the first leading
176        /// minor of A at which A was found to be not positive definite.</para>
177        /// The factorization is carried out by use of the LAPACK functions
178        /// DPOTRF, ZPOTRF, SPOTRF or CPOTRF respectively. </remarks>
179        public static ILRetArray<fcomplex> chol(ILInArray<fcomplex> A, bool throwException = true) {
180            using (ILScope.Enter(A)) {
181                if (!A.IsMatrix)
182                    throw new ILArgumentSizeException("chol is defined for matrices only!");
183                int n = A.Size[0], info = 0;
184                if (A.Size[1] != n)
185                    throw new ILArgumentException("chol: input matrix must be square!");
186                ILDenseStorage<fcomplex> ret = A.Storage.copyUpperTriangle(n);
187                Lapack.cpotrf ('U', n, ret.GetArrayForRead(), n, ref info);
188                if (info < 0 ) {
189                    throw new ILArgumentException("chol: illegal parameter error.");
190                } else if (info > 0) {
191                    // not pos.definite
192                    if (! throwException) {
193                        if (info > 1) {
194                            int newDim = info - 1;
195                            ret = A.Storage.copyUpperTriangle(newDim);
196                             Lapack.cpotrf ('U',newDim,ret.GetArrayForRead(), newDim, ref info);
197                            if (info != 0)
198                                throw new ILArgumentException("chol: the original matrix given was not pos. definit. An attempt to decompose the submatrix of order " + (newDim + 1).ToString() + " failed.");
199                            return new ILRetArray<fcomplex>(ret);
200                        } else {
201                            return empty<fcomplex>(ILSize.Empty00);
202                        }
203                    } else {
204                        throw new ILArgumentException("chol: the matrix given is not positive definite!");
205                    }
206                }
207                return new ILRetArray<fcomplex>(ret);
208            }
209        }
210        /// <summary>Cholesky factorization</summary>
211        /// <param name="A">Input array A. A must be a symmetric/hermitian matrix.
212        /// Therefore the upper triangular part of A must be the transpose (complex conjugate for complex input) of the lower triangular part. No check
213        /// is made for that! <br/>
214        /// The elements of A will not be altered.</param>
215        /// <param name="throwException">Throw an ILArgumentException if A
216        /// is found not to be positive definite.</param>
217        /// <returns>Cholesky factorization</returns>
218        /// <remarks><para>If <paramref name="throwException"/> is true and
219        /// A is found not to be positive definite, an ILArgumentException
220        /// will be thrown and the operation will be canceled.</para>
221        /// <para>If <paramref name="throwException"/> is false, check the
222        /// return value's dimension to determine the success of the
223        /// operation (unless you are sure, A was positive definite).
224        /// If A was found not to be positive definite the matrix returned
225        /// will be of dimension [k x k] and the result of the cholesky
226        /// factorization of A[0:k-1;0:k-1]. Here k is the first leading
227        /// minor of A at which A was found to be not positive definite.</para>
228        /// The factorization is carried out by use of the LAPACK functions
229        /// DPOTRF, ZPOTRF, SPOTRF or CPOTRF respectively. </remarks>
230        public static ILRetArray<complex> chol(ILInArray<complex> A, bool throwException = true) {
231            using (ILScope.Enter(A)) {
232                if (!A.IsMatrix)
233                    throw new ILArgumentSizeException("chol is defined for matrices only!");
234                int n = A.Size[0], info = 0;
235                if (A.Size[1] != n)
236                    throw new ILArgumentException("chol: input matrix must be square!");
237                ILDenseStorage<complex> ret = A.Storage.copyUpperTriangle(n);
238                Lapack.zpotrf ('U', n, ret.GetArrayForRead(), n, ref info);
239                if (info < 0 ) {
240                    throw new ILArgumentException("chol: illegal parameter error.");
241                } else if (info > 0) {
242                    // not pos.definite
243                    if (! throwException) {
244                        if (info > 1) {
245                            int newDim = info - 1;
246                            ret = A.Storage.copyUpperTriangle(newDim);
247                             Lapack.zpotrf ('U',newDim,ret.GetArrayForRead(), newDim, ref info);
248                            if (info != 0)
249                                throw new ILArgumentException("chol: the original matrix given was not pos. definit. An attempt to decompose the submatrix of order " + (newDim + 1).ToString() + " failed.");
250                            return new ILRetArray<complex>(ret);
251                        } else {
252                            return empty<complex>(ILSize.Empty00);
253                        }
254                    } else {
255                        throw new ILArgumentException("chol: the matrix given is not positive definite!");
256                    }
257                }
258                return new ILRetArray<complex>(ret);
259            }
260        }
261
262#endregion HYCALPER AUTO GENERATED CODE
263
264    }
265}
Note: See TracBrowser for help on using the repository browser.