Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Functions/builtin/counter.cs @ 11316

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

#1967: ILNumerics source for experimentation

File size: 14.0 KB
Line 
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 System.Runtime.InteropServices;
44using ILNumerics.Storage;
45using ILNumerics.Misc;
46using ILNumerics.Native;
47using ILNumerics.Exceptions;
48
49namespace ILNumerics {
50
51    public partial class ILMath {
52
53        #region counter
54        /// <summary>
55        /// Create n-dimensional array with elements counting from 1, double precision
56        /// </summary>
57        /// <param name="dimensions">Variable number of numeric scalar arrays with dimension specification</param>
58        /// <returns>Double array with elements counting from 1 to dimensions.NumberOfElements</returns>
59        /// <remarks>This function may be used for the convenient creation of arrays for testing purposes.</remarks>
60        public static ILRetArray<double> counter(params ILBaseArray[] dimensions) {
61            return counter<double>(1.0, 1.0, dimensions);
62        }
63        /// <summary>
64        /// Create n-dimensional array with elements counting from 1, double precision
65        /// </summary>
66        /// <param name="dimensions">Variable number of numeric scalar arrays with dimension specification</param>
67        /// <returns>Double array with elements counting from 1 ... dimensions.NumberOfElements</returns>
68        /// <remarks>This function may be used for the convenient creation of arrays for testing purposes.</remarks>
69        public static ILRetArray<double> counter(params int[] dimensions) {
70            return counter<double>(1.0, 1.0, new ILSize(dimensions));
71        }
72        /// <summary>
73        /// Create n-dimensional array with elements counting from 1, double precision
74        /// </summary>
75        /// <param name="dimensions">Variable number of numeric scalar arrays with dimension specification</param>
76        /// <param name="start">Starting number</param>
77        /// <param name="inc">Incrementing step</param>
78        /// <returns>Double array with elements counting from start to start + (dimensions.NumberOfElements * inc)</returns>
79        /// <remarks>This function may be used for the convenient creation of arrays for testing purposes.</remarks>
80        public static ILRetArray<double> counter(double start, double inc, params ILBaseArray[] dimensions) {
81            return counter<double>(start, inc, dimensions);
82        }
83        /// <summary>
84        /// Create n-dimensional array with elements counting from 1, double precision
85        /// </summary>
86        /// <param name="dimensions">Variable number of numeric scalar arrays with dimension specification</param>
87        /// <param name="start">Starting number</param>
88        /// <param name="inc">Incrementing step</param>
89        /// <returns>Double array with elements counting from start to start + (dimensions.NumberOfElements * inc)</returns>
90        /// <remarks>This function may be used for the convenient creation of arrays for testing purposes.</remarks>
91        public static ILRetArray<double> counter(double start, double inc, ILSize dimensions) {
92            return counter<double>(start, inc, dimensions);
93        }
94        /// <summary>
95        /// Create n-dimensional array with elements counting from 1, double precision
96        /// </summary>
97        /// <param name="dimensions">Variable number of numeric scalar arrays with dimension specification</param>
98        /// <returns>Double array with elements counting from 1 ... dimensions.NumberOfElements</returns>
99        /// <remarks>This function may be used for the convenient creation of arrays for testing purposes.</remarks>
100        public static ILRetArray<double> counter(ILSize dimensions) {
101            return counter<double>(1.0, 1.0, dimensions);
102        }
103        /// <summary>
104        /// Create n-dimensional array with counting elements
105        /// </summary>
106        /// <param name="start">Initial value</param>
107        /// <param name="increment">Increment for each element</param>
108        /// <param name="dimensions">Variable number of numeric, scalar arrays with dimension specification</param>
109        /// <returns>Array with elements counting from <paramref name="start"/> along the first dimension with steps of <paramref name="increment"/>.</returns>
110        /// <remarks>
111        /// <example><code>
112        /// // This will create elements counting from 1...24:
113        /// <![CDATA[ILArray<double>]]> A = ILMath.counter(4,3,2);
114        /// // This will create elements counting from 1...48 with intervals of 2.0:
115        /// <![CDATA[ILArray<double>]]> A = ILMath.counter(1.0,2.0,4,3,2);
116        /// // This will create an array with all elements having contant value of -4f:
117        /// // (note: start, increment and dimension specifier do not need to be of the same type)
118        /// <![CDATA[ILArray<float> A = ILMath.counter<float>(4.0,0,4,3,2);]]>
119        /// </code></example>  </remarks>
120        public static ILRetArray<T> counter<T>(ILBaseArray start, ILBaseArray increment, params ILBaseArray[] dimensions) {
121            using (ILScope.Enter(dimensions))
122                return counter<T>(start, increment, new ILSize(dimensions));
123        }
124        /// <summary>
125        /// Create n-dimensional array with counting elements
126        /// </summary>
127        /// <param name="start">Initial value</param>
128        /// <param name="increment">Increment for each element</param>
129        /// <param name="dimensions">Variable int array with dimension specification</param>
130        /// <returns>Array with elements counting from <paramref name="start"/> to dimensions.NumberOfElements - <paramref name="start"/>.</returns>
131        /// <remarks>
132        /// <example><code>
133        /// // This will create elements counting from 1...24:
134        /// <![CDATA[ILArray<double>]]> A = ILMath.counter(4,3,2);
135        /// // This will create elements counting from 1...48 with intervals of 2.0:
136        /// <![CDATA[ILArray<double>]]> A = ILMath.counter(1.0,2.0,4,3,2);
137        /// // This will create an array with all elements having contant value of -4f:
138        /// // (note: start, increment and dimension specifier do not need to be of the same type)
139        /// <![CDATA[ILArray<float> A = ILMath.counter<float>(4.0,0,4,3,2);]]>
140        /// </code></example>  </remarks>
141        public static ILRetArray<T> counter<T>(ILBaseArray start, ILBaseArray increment, ILSize dimensions) {
142            using (ILScope.Enter(start, increment)) {
143                if (object.Equals(start, null) || !start.IsNumeric || !start.IsScalar) throw new ILArgumentException("start must be a numeric scalar");
144                if (object.Equals(increment, null) || !increment.IsNumeric || !increment.IsScalar) throw new ILArgumentException("increment must be a numeric scalar");
145                double dStart = todouble(start).GetValue(0);
146                double dInc = todouble(increment).GetValue(0);
147
148                T[] retArr = ILMemoryPool.Pool.New<T>(dimensions.NumberOfElements);
149                if (false) {
150
151                   
152                } else if (retArr is  double[]) {
153                   
154                    double val = ( double)dStart;
155                    unsafe {
156                        fixed ( double* pRetArr = (retArr as  double[])) {
157                           
158                            double* pRetArray = pRetArr;
159                           
160                            double* pEnd = pRetArr + dimensions.NumberOfElements;
161                            while (pRetArray < pEnd) {
162                                *pRetArray++ = val;
163                                val = ( double)(val + dInc);
164                            }
165                        }
166                    }
167
168#region HYCALPER AUTO GENERATED CODE
169
170                   
171                } else if (retArr is  byte[]) {
172                   
173                    byte val = ( byte)dStart;
174                    unsafe {
175                        fixed ( byte* pRetArr = (retArr as  byte[])) {
176                           
177                            byte* pRetArray = pRetArr;
178                           
179                            byte* pEnd = pRetArr + dimensions.NumberOfElements;
180                            while (pRetArray < pEnd) {
181                                *pRetArray++ = val;
182                                val = ( byte)(val + dInc);
183                            }
184                        }
185                    }
186                   
187                } else if (retArr is  Int64[]) {
188                   
189                    Int64 val = ( Int64)dStart;
190                    unsafe {
191                        fixed ( Int64* pRetArr = (retArr as  Int64[])) {
192                           
193                            Int64* pRetArray = pRetArr;
194                           
195                            Int64* pEnd = pRetArr + dimensions.NumberOfElements;
196                            while (pRetArray < pEnd) {
197                                *pRetArray++ = val;
198                                val = ( Int64)(val + dInc);
199                            }
200                        }
201                    }
202                   
203                } else if (retArr is  complex[]) {
204                   
205                    complex val = ( complex)dStart;
206                    unsafe {
207                        fixed ( complex* pRetArr = (retArr as  complex[])) {
208                           
209                            complex* pRetArray = pRetArr;
210                           
211                            complex* pEnd = pRetArr + dimensions.NumberOfElements;
212                            while (pRetArray < pEnd) {
213                                *pRetArray++ = val;
214                                val = ( complex)(val + dInc);
215                            }
216                        }
217                    }
218                   
219                } else if (retArr is  fcomplex[]) {
220                   
221                    fcomplex val = ( fcomplex)dStart;
222                    unsafe {
223                        fixed ( fcomplex* pRetArr = (retArr as  fcomplex[])) {
224                           
225                            fcomplex* pRetArray = pRetArr;
226                           
227                            fcomplex* pEnd = pRetArr + dimensions.NumberOfElements;
228                            while (pRetArray < pEnd) {
229                                *pRetArray++ = val;
230                                val = ( fcomplex)(val + dInc);
231                            }
232                        }
233                    }
234                   
235                } else if (retArr is  Int32[]) {
236                   
237                    Int32 val = ( Int32)dStart;
238                    unsafe {
239                        fixed ( Int32* pRetArr = (retArr as  Int32[])) {
240                           
241                            Int32* pRetArray = pRetArr;
242                           
243                            Int32* pEnd = pRetArr + dimensions.NumberOfElements;
244                            while (pRetArray < pEnd) {
245                                *pRetArray++ = val;
246                                val = ( Int32)(val + dInc);
247                            }
248                        }
249                    }
250                   
251                } else if (retArr is  float[]) {
252                   
253                    float val = ( float)dStart;
254                    unsafe {
255                        fixed ( float* pRetArr = (retArr as  float[])) {
256                           
257                            float* pRetArray = pRetArr;
258                           
259                            float* pEnd = pRetArr + dimensions.NumberOfElements;
260                            while (pRetArray < pEnd) {
261                                *pRetArray++ = val;
262                                val = ( float)(val + dInc);
263                            }
264                        }
265                    }
266
267#endregion HYCALPER AUTO GENERATED CODE
268               } else {
269                    throw new ILArgumentException(String.Format("counter is not supported for arrays of inner type '{0}'", typeof(T).Name));
270                }
271                return new ILRetArray<T>(retArr, dimensions);
272            }
273        }
274        #endregion
275
276    }
277}
Note: See TracBrowser for help on using the repository browser.