Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Functions/builtin/diag.cs @ 9407

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

#1967: ILNumerics source for experimentation

File size: 12.7 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
49
50namespace ILNumerics  {
51    public partial class ILMath {
52
53
54        /// <summary>
55        /// Diagonal matrix or diagonal of matrix
56        /// </summary>
57        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
58        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
59        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
60        /// the elements of X on the diagPosition's diagonal.</param>
61        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
62        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
63        /// below the main diagonal.</param>
64        /// <returns>Depending on X a matrix or a vector with the elements on a diagonal.</returns>
65        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
66        public static ILRetArray<double> diag(ILInArray<double> X, int diagPosition = 0) {
67            return diag<double>(X, diagPosition);
68        }
69
70#region HYCALPER AUTO GENERATED CODE
71
72        /// <summary>
73        /// Diagonal matrix or diagonal of matrix
74        /// </summary>
75        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
76        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
77        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
78        /// the elements of X on the diagPosition's diagonal.</param>
79        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
80        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
81        /// below the main diagonal.</param>
82        /// <returns>Depending on X a matrix or a vector with the elements on a diagonal.</returns>
83        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
84        public static ILRetArray<fcomplex> diag(ILInArray<fcomplex> X, int diagPosition = 0) {
85            return diag<fcomplex>(X, diagPosition);
86        }
87        /// <summary>
88        /// Diagonal matrix or diagonal of matrix
89        /// </summary>
90        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
91        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
92        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
93        /// the elements of X on the diagPosition's diagonal.</param>
94        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
95        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
96        /// below the main diagonal.</param>
97        /// <returns>Depending on X a matrix or a vector with the elements on a diagonal.</returns>
98        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
99        public static ILRetArray<complex> diag(ILInArray<complex> X, int diagPosition = 0) {
100            return diag<complex>(X, diagPosition);
101        }
102        /// <summary>
103        /// Diagonal matrix or diagonal of matrix
104        /// </summary>
105        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
106        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
107        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
108        /// the elements of X on the diagPosition's diagonal.</param>
109        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
110        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
111        /// below the main diagonal.</param>
112        /// <returns>Depending on X a matrix or a vector with the elements on a diagonal.</returns>
113        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
114        public static ILRetArray<byte> diag(ILInArray<byte> X, int diagPosition = 0) {
115            return diag<byte>(X, diagPosition);
116        }
117        /// <summary>
118        /// Diagonal matrix or diagonal of matrix
119        /// </summary>
120        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
121        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
122        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
123        /// the elements of X on the diagPosition's diagonal.</param>
124        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
125        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
126        /// below the main diagonal.</param>
127        /// <returns>Depending on X a matrix or a vector with the elements on a diagonal.</returns>
128        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
129        public static ILRetArray<Int64> diag(ILInArray<Int64> X, int diagPosition = 0) {
130            return diag<Int64>(X, diagPosition);
131        }
132        /// <summary>
133        /// Diagonal matrix or diagonal of matrix
134        /// </summary>
135        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
136        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
137        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
138        /// the elements of X on the diagPosition's diagonal.</param>
139        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
140        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
141        /// below the main diagonal.</param>
142        /// <returns>Depending on X a matrix or a vector with the elements on a diagonal.</returns>
143        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
144        public static ILRetArray<Int32> diag(ILInArray<Int32> X, int diagPosition = 0) {
145            return diag<Int32>(X, diagPosition);
146        }
147        /// <summary>
148        /// Diagonal matrix or diagonal of matrix
149        /// </summary>
150        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
151        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
152        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
153        /// the elements of X on the diagPosition's diagonal.</param>
154        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
155        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
156        /// below the main diagonal.</param>
157        /// <returns>Depending on X a matrix or a vector with the elements on a diagonal.</returns>
158        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
159        public static ILRetArray<float> diag(ILInArray<float> X, int diagPosition = 0) {
160            return diag<float>(X, diagPosition);
161        }
162
163#endregion HYCALPER AUTO GENERATED CODE
164
165        /// <summary>
166        /// Diagonal matrix or diagonal of matrix
167        /// </summary>
168        /// <param name="X">Input matrix or vector. If X is matrix, diag(X) returns the
169        /// elements on the diagPosition's diagonal as column vector. If X is vector, a square matrix of size
170        /// [length(X) + abs(diagPosition), length(X) + abs(diagPosition)] will be created, having
171        /// the elements of X on the diagPosition's diagonal.</param>
172        /// <param name="diagPosition">[Optional] Index of diagonal to extract/create. Here 0 means the
173        /// main diagonal, diagPosition > 0 is above the main diagonal, diagPosition smaller 0 means
174        /// below the main diagonal.</param>
175        /// <returns>Depending on 'X' a matrix or a vector with the elements on a diagonal.</returns>
176        /// <remarks>The type of the ILArray returned will be the same as the type of X.</remarks>
177        public static ILRetArray<T> diag<T>(ILInArray<T> X, int diagPosition = 0) {
178            using (ILScope.Enter(X)) {
179                if (Object.Equals(X, null) || X.Size.NumberOfDimensions > 2) {
180                    throw new ILArgumentException("diag: input array must be matrix, vector or scalar");
181                }
182                if (X.IsEmpty)
183                    return empty<T>(X.Size);
184                ILArray<T> ret = empty<T>(ILSize.Empty00);
185                if  (X.IsScalar) {
186                    ret.a = X.GetValue(0,0);
187                } else if (X.IsVector) {
188                    int retLen = X.Length + Math.Abs(diagPosition);
189                    ret.a = zeros<T> (new ILSize(retLen, retLen));
190                    T [] dIn = X.GetArrayForRead();
191                    if (diagPosition > 0) {
192                        for (int i = 0; i < X.S.NumberOfElements; i++) {
193                            ret.SetValue(dIn[i], i, i + diagPosition);
194                        }
195                    } else {
196                        for (int i = 0; i < X.S.NumberOfElements; i++) {
197                            ret.SetValue(dIn[i],i - diagPosition, i);
198                        }
199                    }
200                } else  { // X is matrix
201                    int retLen = Math.Min(X.Size[0], X.Size[1]) - Math.Abs(diagPosition);
202                    ret = zeros<T> (new ILSize(retLen, 1));
203                    T [] dRet = ret.GetArrayForWrite();
204                    if (diagPosition > 0) {
205                        if (diagPosition >= X.Size[1])
206                            return empty<T>(ILSize.Empty00);
207                        for (int i = 0; i < retLen; i++) {
208                            dRet[i] = X.GetValue(i, diagPosition + i);
209                        }
210                    } else {
211                        if (diagPosition <= -X.Size[0])
212                            return empty<T>(ILSize.Empty00);
213                        for (int i = 0; i < retLen; i++) {
214                            dRet[i] = X.GetValue(diagPosition + i, i);
215                        }
216                    }
217                }
218                return ret;
219            }
220        }
221    }
222}
Note: See TracBrowser for help on using the repository browser.