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 | /// Take n-th derivative
|
---|
56 | /// </summary>
|
---|
57 | /// <param name="A">Input array</param>
|
---|
58 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
59 | /// <param name="N">[Optional] Degree of derivates. If not specified N=1 is assumed.</param>
|
---|
60 | /// <returns>Array with first derivative of A along dimension <c>dim</c> of first non singleton dimension</returns>
|
---|
61 | /// <remarks>N must be a number in range 1..L, where L is the length of A.Dimensions[dim].
|
---|
62 | /// Otherwise an empty array will be returned.
|
---|
63 | /// <para>If A is empty or scalar, or if N exceeds the length the specified dimension of A,
|
---|
64 | /// an empty array will be returned.</para></remarks>
|
---|
65 | public static ILRetArray< double > diff(ILInArray< double > A, int N = 1, int dim = -1) {
|
---|
66 | using (ILScope.Enter(A)) {
|
---|
67 | if (Object.Equals(A,null))
|
---|
68 | throw new ILArgumentException ("diff: input array A must not be null");
|
---|
69 | if (dim < 0) {
|
---|
70 | dim = A.Size.WorkingDimension();
|
---|
71 | }
|
---|
72 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
73 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
74 | outDims[dim] = 0;
|
---|
75 | return empty< double >(new ILSize(outDims));
|
---|
76 | }
|
---|
77 | if (A.IsScalar) return empty< double >(ILSize.Empty00);
|
---|
78 | if (A.IsEmpty) {
|
---|
79 | int [] retDim = A.Size.ToIntArray();
|
---|
80 | retDim[dim]--;
|
---|
81 | return empty< double >(new ILSize(retDim));
|
---|
82 | }
|
---|
83 | if (N == 0)
|
---|
84 | return A.C;
|
---|
85 | if (N < 1 || N > A.Size[dim]) {
|
---|
86 | return empty< double >(ILSize.Empty00);
|
---|
87 | }
|
---|
88 | ILArray< double > ret = A.C;
|
---|
89 | for (int n = 0; n < N; n++) {
|
---|
90 | ret.a = diff(dim,ret);
|
---|
91 | }
|
---|
92 | return ret;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | /// <summary>
|
---|
96 | /// First derivative along specific dimension
|
---|
97 | /// </summary>
|
---|
98 | /// <param name="A">input array</param>
|
---|
99 | /// <param name="dim">dimensions to create derivative along</param>
|
---|
100 | /// <returns>array with first derivative of A along dimension <c>dim</c></returns>
|
---|
101 | private static ILRetArray< double > diff(int dim, ILInArray< double > A) {
|
---|
102 | using (ILScope.Enter(A)) {
|
---|
103 | if (A.IsEmpty) return empty< double >(A.Size);
|
---|
104 | if (A.IsScalar) return empty< double >(ILSize.Empty00);
|
---|
105 | if (dim < 0)
|
---|
106 | throw new ILArgumentException("diff: leading dimension out of range!");
|
---|
107 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
108 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
109 | outDims[dim] = 0;
|
---|
110 | return empty< double >(new ILSize(outDims));
|
---|
111 | }
|
---|
112 | ILSize inDim = A.Size;
|
---|
113 | int[] newDims = inDim.ToIntArray();
|
---|
114 |
|
---|
115 | if (inDim[dim] == 1) return empty< double >(ILSize.Empty00);
|
---|
116 | int newLength;
|
---|
117 | double [] retArr;
|
---|
118 | // build ILSize
|
---|
119 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
120 | newDims[dim] --;
|
---|
121 | newLength = newLength * newDims[dim];
|
---|
122 | retArr = ILMemoryPool.Pool.New< double >(newLength);
|
---|
123 | ILSize newDimension = new ILSize(newDims);
|
---|
124 | int leadDimLen = inDim[dim];
|
---|
125 | int nrHigherDims = inDim.NumberOfElements / leadDimLen;
|
---|
126 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
127 | double firstVal, secVal;
|
---|
128 | if (A.IsVector)
|
---|
129 | return A["1:end"] - A[vec(0,A.Length-2)];
|
---|
130 | if (dim == 0) {
|
---|
131 | #region physical along 1st leading dimension
|
---|
132 | unsafe {
|
---|
133 | fixed ( double * pOutArr = retArr)
|
---|
134 | fixed ( double * pInArr = A.GetArrayForRead()) {
|
---|
135 | double * lastElement;
|
---|
136 | double * tmpOut = pOutArr;
|
---|
137 | double * tmpIn = pInArr;
|
---|
138 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
139 | lastElement = tmpIn + leadDimLen;
|
---|
140 |
|
---|
141 | firstVal = *tmpIn++;
|
---|
142 | while (tmpIn < lastElement) {
|
---|
143 | secVal = *tmpIn++;
|
---|
144 | *(tmpOut++) = ( double )(secVal-firstVal);
|
---|
145 | firstVal = secVal;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | #endregion
|
---|
151 | } else {
|
---|
152 | #region physical along abitrary dimension
|
---|
153 | // sum along abitrary dimension
|
---|
154 | unsafe {
|
---|
155 | fixed ( double * pOutArr = retArr)
|
---|
156 | fixed ( double * pInArr = A.GetArrayForRead()) {
|
---|
157 | double * lastElementOut = newLength + pOutArr -1;
|
---|
158 | int inLength = inDim.NumberOfElements -1;
|
---|
159 | double * lastElementIn = pInArr + inLength;
|
---|
160 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
161 | double * tmpOut = pOutArr;
|
---|
162 | int outLength = newLength - 1;
|
---|
163 | double * leadEnd;
|
---|
164 | double * tmpIn = pInArr;
|
---|
165 | for (int h = nrHigherDims; h--> 0; ) {
|
---|
166 | leadEnd = tmpIn + leadDimLen * inc;
|
---|
167 | firstVal = *tmpIn;
|
---|
168 | tmpIn += inc;
|
---|
169 | while (tmpIn < leadEnd) {
|
---|
170 | secVal = *tmpIn;
|
---|
171 | *tmpOut = ( double )(secVal - firstVal);
|
---|
172 | tmpIn += inc;
|
---|
173 | tmpOut += incOut;
|
---|
174 | firstVal = secVal;
|
---|
175 | }
|
---|
176 | if (tmpOut > lastElementOut)
|
---|
177 | tmpOut -= outLength;
|
---|
178 | if (tmpIn > lastElementIn)
|
---|
179 | tmpIn -= inLength;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 | }
|
---|
185 | return new ILRetArray< double >(retArr, newDimension);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | |
---|
189 | #region HYCALPER AUTO GENERATED CODE
|
---|
190 | |
---|
191 | /// <summary>
|
---|
192 | /// Take n-th derivative
|
---|
193 | /// </summary>
|
---|
194 | /// <param name="A">Input array</param>
|
---|
195 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
196 | /// <param name="N">[Optional] Degree of derivates. If not specified N=1 is assumed.</param>
|
---|
197 | /// <returns>Array with first derivative of A along dimension <c>dim</c> of first non singleton dimension</returns>
|
---|
198 | /// <remarks>N must be a number in range 1..L, where L is the length of A.Dimensions[dim].
|
---|
199 | /// Otherwise an empty array will be returned.
|
---|
200 | /// <para>If A is empty or scalar, or if N exceeds the length the specified dimension of A,
|
---|
201 | /// an empty array will be returned.</para></remarks>
|
---|
202 | public static ILRetArray< Int64 > diff(ILInArray< Int64 > A, int N = 1, int dim = -1) {
|
---|
203 | using (ILScope.Enter(A)) {
|
---|
204 | if (Object.Equals(A,null))
|
---|
205 | throw new ILArgumentException ("diff: input array A must not be null");
|
---|
206 | if (dim < 0) {
|
---|
207 | dim = A.Size.WorkingDimension();
|
---|
208 | }
|
---|
209 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
210 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
211 | outDims[dim] = 0;
|
---|
212 | return empty< Int64 >(new ILSize(outDims));
|
---|
213 | }
|
---|
214 | if (A.IsScalar) return empty< Int64 >(ILSize.Empty00);
|
---|
215 | if (A.IsEmpty) {
|
---|
216 | int [] retDim = A.Size.ToIntArray();
|
---|
217 | retDim[dim]--;
|
---|
218 | return empty< Int64 >(new ILSize(retDim));
|
---|
219 | }
|
---|
220 | if (N == 0)
|
---|
221 | return A.C;
|
---|
222 | if (N < 1 || N > A.Size[dim]) {
|
---|
223 | return empty< Int64 >(ILSize.Empty00);
|
---|
224 | }
|
---|
225 | ILArray< Int64 > ret = A.C;
|
---|
226 | for (int n = 0; n < N; n++) {
|
---|
227 | ret.a = diff(dim,ret);
|
---|
228 | }
|
---|
229 | return ret;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | /// <summary>
|
---|
233 | /// First derivative along specific dimension
|
---|
234 | /// </summary>
|
---|
235 | /// <param name="A">input array</param>
|
---|
236 | /// <param name="dim">dimensions to create derivative along</param>
|
---|
237 | /// <returns>array with first derivative of A along dimension <c>dim</c></returns>
|
---|
238 | private static ILRetArray< Int64 > diff(int dim, ILInArray< Int64 > A) {
|
---|
239 | using (ILScope.Enter(A)) {
|
---|
240 | if (A.IsEmpty) return empty< Int64 >(A.Size);
|
---|
241 | if (A.IsScalar) return empty< Int64 >(ILSize.Empty00);
|
---|
242 | if (dim < 0)
|
---|
243 | throw new ILArgumentException("diff: leading dimension out of range!");
|
---|
244 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
245 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
246 | outDims[dim] = 0;
|
---|
247 | return empty< Int64 >(new ILSize(outDims));
|
---|
248 | }
|
---|
249 | ILSize inDim = A.Size;
|
---|
250 | int[] newDims = inDim.ToIntArray();
|
---|
251 |
|
---|
252 | if (inDim[dim] == 1) return empty< Int64 >(ILSize.Empty00);
|
---|
253 | int newLength;
|
---|
254 | Int64 [] retArr;
|
---|
255 | // build ILSize
|
---|
256 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
257 | newDims[dim] --;
|
---|
258 | newLength = newLength * newDims[dim];
|
---|
259 | retArr = ILMemoryPool.Pool.New< Int64 >(newLength);
|
---|
260 | ILSize newDimension = new ILSize(newDims);
|
---|
261 | int leadDimLen = inDim[dim];
|
---|
262 | int nrHigherDims = inDim.NumberOfElements / leadDimLen;
|
---|
263 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
264 | Int64 firstVal, secVal;
|
---|
265 | if (A.IsVector)
|
---|
266 | return A["1:end"] - A[vec(0,A.Length-2)];
|
---|
267 | if (dim == 0) {
|
---|
268 | #region physical along 1st leading dimension
|
---|
269 | unsafe {
|
---|
270 | fixed ( Int64 * pOutArr = retArr)
|
---|
271 | fixed ( Int64 * pInArr = A.GetArrayForRead()) {
|
---|
272 | Int64 * lastElement;
|
---|
273 | Int64 * tmpOut = pOutArr;
|
---|
274 | Int64 * tmpIn = pInArr;
|
---|
275 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
276 | lastElement = tmpIn + leadDimLen;
|
---|
277 |
|
---|
278 | firstVal = *tmpIn++;
|
---|
279 | while (tmpIn < lastElement) {
|
---|
280 | secVal = *tmpIn++;
|
---|
281 | *(tmpOut++) = ( Int64 )(secVal-firstVal);
|
---|
282 | firstVal = secVal;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 | #endregion
|
---|
288 | } else {
|
---|
289 | #region physical along abitrary dimension
|
---|
290 | // sum along abitrary dimension
|
---|
291 | unsafe {
|
---|
292 | fixed ( Int64 * pOutArr = retArr)
|
---|
293 | fixed ( Int64 * pInArr = A.GetArrayForRead()) {
|
---|
294 | Int64 * lastElementOut = newLength + pOutArr -1;
|
---|
295 | int inLength = inDim.NumberOfElements -1;
|
---|
296 | Int64 * lastElementIn = pInArr + inLength;
|
---|
297 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
298 | Int64 * tmpOut = pOutArr;
|
---|
299 | int outLength = newLength - 1;
|
---|
300 | Int64 * leadEnd;
|
---|
301 | Int64 * tmpIn = pInArr;
|
---|
302 | for (int h = nrHigherDims; h--> 0; ) {
|
---|
303 | leadEnd = tmpIn + leadDimLen * inc;
|
---|
304 | firstVal = *tmpIn;
|
---|
305 | tmpIn += inc;
|
---|
306 | while (tmpIn < leadEnd) {
|
---|
307 | secVal = *tmpIn;
|
---|
308 | *tmpOut = ( Int64 )(secVal - firstVal);
|
---|
309 | tmpIn += inc;
|
---|
310 | tmpOut += incOut;
|
---|
311 | firstVal = secVal;
|
---|
312 | }
|
---|
313 | if (tmpOut > lastElementOut)
|
---|
314 | tmpOut -= outLength;
|
---|
315 | if (tmpIn > lastElementIn)
|
---|
316 | tmpIn -= inLength;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 | #endregion
|
---|
321 | }
|
---|
322 | return new ILRetArray< Int64 >(retArr, newDimension);
|
---|
323 | }
|
---|
324 | }
|
---|
325 | /// <summary>
|
---|
326 | /// Take n-th derivative
|
---|
327 | /// </summary>
|
---|
328 | /// <param name="A">Input array</param>
|
---|
329 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
330 | /// <param name="N">[Optional] Degree of derivates. If not specified N=1 is assumed.</param>
|
---|
331 | /// <returns>Array with first derivative of A along dimension <c>dim</c> of first non singleton dimension</returns>
|
---|
332 | /// <remarks>N must be a number in range 1..L, where L is the length of A.Dimensions[dim].
|
---|
333 | /// Otherwise an empty array will be returned.
|
---|
334 | /// <para>If A is empty or scalar, or if N exceeds the length the specified dimension of A,
|
---|
335 | /// an empty array will be returned.</para></remarks>
|
---|
336 | public static ILRetArray< Int32 > diff(ILInArray< Int32 > A, int N = 1, int dim = -1) {
|
---|
337 | using (ILScope.Enter(A)) {
|
---|
338 | if (Object.Equals(A,null))
|
---|
339 | throw new ILArgumentException ("diff: input array A must not be null");
|
---|
340 | if (dim < 0) {
|
---|
341 | dim = A.Size.WorkingDimension();
|
---|
342 | }
|
---|
343 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
344 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
345 | outDims[dim] = 0;
|
---|
346 | return empty< Int32 >(new ILSize(outDims));
|
---|
347 | }
|
---|
348 | if (A.IsScalar) return empty< Int32 >(ILSize.Empty00);
|
---|
349 | if (A.IsEmpty) {
|
---|
350 | int [] retDim = A.Size.ToIntArray();
|
---|
351 | retDim[dim]--;
|
---|
352 | return empty< Int32 >(new ILSize(retDim));
|
---|
353 | }
|
---|
354 | if (N == 0)
|
---|
355 | return A.C;
|
---|
356 | if (N < 1 || N > A.Size[dim]) {
|
---|
357 | return empty< Int32 >(ILSize.Empty00);
|
---|
358 | }
|
---|
359 | ILArray< Int32 > ret = A.C;
|
---|
360 | for (int n = 0; n < N; n++) {
|
---|
361 | ret.a = diff(dim,ret);
|
---|
362 | }
|
---|
363 | return ret;
|
---|
364 | }
|
---|
365 | }
|
---|
366 | /// <summary>
|
---|
367 | /// First derivative along specific dimension
|
---|
368 | /// </summary>
|
---|
369 | /// <param name="A">input array</param>
|
---|
370 | /// <param name="dim">dimensions to create derivative along</param>
|
---|
371 | /// <returns>array with first derivative of A along dimension <c>dim</c></returns>
|
---|
372 | private static ILRetArray< Int32 > diff(int dim, ILInArray< Int32 > A) {
|
---|
373 | using (ILScope.Enter(A)) {
|
---|
374 | if (A.IsEmpty) return empty< Int32 >(A.Size);
|
---|
375 | if (A.IsScalar) return empty< Int32 >(ILSize.Empty00);
|
---|
376 | if (dim < 0)
|
---|
377 | throw new ILArgumentException("diff: leading dimension out of range!");
|
---|
378 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
379 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
380 | outDims[dim] = 0;
|
---|
381 | return empty< Int32 >(new ILSize(outDims));
|
---|
382 | }
|
---|
383 | ILSize inDim = A.Size;
|
---|
384 | int[] newDims = inDim.ToIntArray();
|
---|
385 |
|
---|
386 | if (inDim[dim] == 1) return empty< Int32 >(ILSize.Empty00);
|
---|
387 | int newLength;
|
---|
388 | Int32 [] retArr;
|
---|
389 | // build ILSize
|
---|
390 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
391 | newDims[dim] --;
|
---|
392 | newLength = newLength * newDims[dim];
|
---|
393 | retArr = ILMemoryPool.Pool.New< Int32 >(newLength);
|
---|
394 | ILSize newDimension = new ILSize(newDims);
|
---|
395 | int leadDimLen = inDim[dim];
|
---|
396 | int nrHigherDims = inDim.NumberOfElements / leadDimLen;
|
---|
397 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
398 | Int32 firstVal, secVal;
|
---|
399 | if (A.IsVector)
|
---|
400 | return A["1:end"] - A[vec(0,A.Length-2)];
|
---|
401 | if (dim == 0) {
|
---|
402 | #region physical along 1st leading dimension
|
---|
403 | unsafe {
|
---|
404 | fixed ( Int32 * pOutArr = retArr)
|
---|
405 | fixed ( Int32 * pInArr = A.GetArrayForRead()) {
|
---|
406 | Int32 * lastElement;
|
---|
407 | Int32 * tmpOut = pOutArr;
|
---|
408 | Int32 * tmpIn = pInArr;
|
---|
409 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
410 | lastElement = tmpIn + leadDimLen;
|
---|
411 |
|
---|
412 | firstVal = *tmpIn++;
|
---|
413 | while (tmpIn < lastElement) {
|
---|
414 | secVal = *tmpIn++;
|
---|
415 | *(tmpOut++) = ( Int32 )(secVal-firstVal);
|
---|
416 | firstVal = secVal;
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 | }
|
---|
421 | #endregion
|
---|
422 | } else {
|
---|
423 | #region physical along abitrary dimension
|
---|
424 | // sum along abitrary dimension
|
---|
425 | unsafe {
|
---|
426 | fixed ( Int32 * pOutArr = retArr)
|
---|
427 | fixed ( Int32 * pInArr = A.GetArrayForRead()) {
|
---|
428 | Int32 * lastElementOut = newLength + pOutArr -1;
|
---|
429 | int inLength = inDim.NumberOfElements -1;
|
---|
430 | Int32 * lastElementIn = pInArr + inLength;
|
---|
431 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
432 | Int32 * tmpOut = pOutArr;
|
---|
433 | int outLength = newLength - 1;
|
---|
434 | Int32 * leadEnd;
|
---|
435 | Int32 * tmpIn = pInArr;
|
---|
436 | for (int h = nrHigherDims; h--> 0; ) {
|
---|
437 | leadEnd = tmpIn + leadDimLen * inc;
|
---|
438 | firstVal = *tmpIn;
|
---|
439 | tmpIn += inc;
|
---|
440 | while (tmpIn < leadEnd) {
|
---|
441 | secVal = *tmpIn;
|
---|
442 | *tmpOut = ( Int32 )(secVal - firstVal);
|
---|
443 | tmpIn += inc;
|
---|
444 | tmpOut += incOut;
|
---|
445 | firstVal = secVal;
|
---|
446 | }
|
---|
447 | if (tmpOut > lastElementOut)
|
---|
448 | tmpOut -= outLength;
|
---|
449 | if (tmpIn > lastElementIn)
|
---|
450 | tmpIn -= inLength;
|
---|
451 | }
|
---|
452 | }
|
---|
453 | }
|
---|
454 | #endregion
|
---|
455 | }
|
---|
456 | return new ILRetArray< Int32 >(retArr, newDimension);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | /// <summary>
|
---|
460 | /// Take n-th derivative
|
---|
461 | /// </summary>
|
---|
462 | /// <param name="A">Input array</param>
|
---|
463 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
464 | /// <param name="N">[Optional] Degree of derivates. If not specified N=1 is assumed.</param>
|
---|
465 | /// <returns>Array with first derivative of A along dimension <c>dim</c> of first non singleton dimension</returns>
|
---|
466 | /// <remarks>N must be a number in range 1..L, where L is the length of A.Dimensions[dim].
|
---|
467 | /// Otherwise an empty array will be returned.
|
---|
468 | /// <para>If A is empty or scalar, or if N exceeds the length the specified dimension of A,
|
---|
469 | /// an empty array will be returned.</para></remarks>
|
---|
470 | public static ILRetArray< byte > diff(ILInArray< byte > A, int N = 1, int dim = -1) {
|
---|
471 | using (ILScope.Enter(A)) {
|
---|
472 | if (Object.Equals(A,null))
|
---|
473 | throw new ILArgumentException ("diff: input array A must not be null");
|
---|
474 | if (dim < 0) {
|
---|
475 | dim = A.Size.WorkingDimension();
|
---|
476 | }
|
---|
477 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
478 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
479 | outDims[dim] = 0;
|
---|
480 | return empty< byte >(new ILSize(outDims));
|
---|
481 | }
|
---|
482 | if (A.IsScalar) return empty< byte >(ILSize.Empty00);
|
---|
483 | if (A.IsEmpty) {
|
---|
484 | int [] retDim = A.Size.ToIntArray();
|
---|
485 | retDim[dim]--;
|
---|
486 | return empty< byte >(new ILSize(retDim));
|
---|
487 | }
|
---|
488 | if (N == 0)
|
---|
489 | return A.C;
|
---|
490 | if (N < 1 || N > A.Size[dim]) {
|
---|
491 | return empty< byte >(ILSize.Empty00);
|
---|
492 | }
|
---|
493 | ILArray< byte > ret = A.C;
|
---|
494 | for (int n = 0; n < N; n++) {
|
---|
495 | ret.a = diff(dim,ret);
|
---|
496 | }
|
---|
497 | return ret;
|
---|
498 | }
|
---|
499 | }
|
---|
500 | /// <summary>
|
---|
501 | /// First derivative along specific dimension
|
---|
502 | /// </summary>
|
---|
503 | /// <param name="A">input array</param>
|
---|
504 | /// <param name="dim">dimensions to create derivative along</param>
|
---|
505 | /// <returns>array with first derivative of A along dimension <c>dim</c></returns>
|
---|
506 | private static ILRetArray< byte > diff(int dim, ILInArray< byte > A) {
|
---|
507 | using (ILScope.Enter(A)) {
|
---|
508 | if (A.IsEmpty) return empty< byte >(A.Size);
|
---|
509 | if (A.IsScalar) return empty< byte >(ILSize.Empty00);
|
---|
510 | if (dim < 0)
|
---|
511 | throw new ILArgumentException("diff: leading dimension out of range!");
|
---|
512 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
513 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
514 | outDims[dim] = 0;
|
---|
515 | return empty< byte >(new ILSize(outDims));
|
---|
516 | }
|
---|
517 | ILSize inDim = A.Size;
|
---|
518 | int[] newDims = inDim.ToIntArray();
|
---|
519 |
|
---|
520 | if (inDim[dim] == 1) return empty< byte >(ILSize.Empty00);
|
---|
521 | int newLength;
|
---|
522 | byte [] retArr;
|
---|
523 | // build ILSize
|
---|
524 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
525 | newDims[dim] --;
|
---|
526 | newLength = newLength * newDims[dim];
|
---|
527 | retArr = ILMemoryPool.Pool.New< byte >(newLength);
|
---|
528 | ILSize newDimension = new ILSize(newDims);
|
---|
529 | int leadDimLen = inDim[dim];
|
---|
530 | int nrHigherDims = inDim.NumberOfElements / leadDimLen;
|
---|
531 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
532 | byte firstVal, secVal;
|
---|
533 | if (A.IsVector)
|
---|
534 | return A["1:end"] - A[vec(0,A.Length-2)];
|
---|
535 | if (dim == 0) {
|
---|
536 | #region physical along 1st leading dimension
|
---|
537 | unsafe {
|
---|
538 | fixed ( byte * pOutArr = retArr)
|
---|
539 | fixed ( byte * pInArr = A.GetArrayForRead()) {
|
---|
540 | byte * lastElement;
|
---|
541 | byte * tmpOut = pOutArr;
|
---|
542 | byte * tmpIn = pInArr;
|
---|
543 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
544 | lastElement = tmpIn + leadDimLen;
|
---|
545 |
|
---|
546 | firstVal = *tmpIn++;
|
---|
547 | while (tmpIn < lastElement) {
|
---|
548 | secVal = *tmpIn++;
|
---|
549 | *(tmpOut++) = ( byte )(secVal-firstVal);
|
---|
550 | firstVal = secVal;
|
---|
551 | }
|
---|
552 | }
|
---|
553 | }
|
---|
554 | }
|
---|
555 | #endregion
|
---|
556 | } else {
|
---|
557 | #region physical along abitrary dimension
|
---|
558 | // sum along abitrary dimension
|
---|
559 | unsafe {
|
---|
560 | fixed ( byte * pOutArr = retArr)
|
---|
561 | fixed ( byte * pInArr = A.GetArrayForRead()) {
|
---|
562 | byte * lastElementOut = newLength + pOutArr -1;
|
---|
563 | int inLength = inDim.NumberOfElements -1;
|
---|
564 | byte * lastElementIn = pInArr + inLength;
|
---|
565 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
566 | byte * tmpOut = pOutArr;
|
---|
567 | int outLength = newLength - 1;
|
---|
568 | byte * leadEnd;
|
---|
569 | byte * tmpIn = pInArr;
|
---|
570 | for (int h = nrHigherDims; h--> 0; ) {
|
---|
571 | leadEnd = tmpIn + leadDimLen * inc;
|
---|
572 | firstVal = *tmpIn;
|
---|
573 | tmpIn += inc;
|
---|
574 | while (tmpIn < leadEnd) {
|
---|
575 | secVal = *tmpIn;
|
---|
576 | *tmpOut = ( byte )(secVal - firstVal);
|
---|
577 | tmpIn += inc;
|
---|
578 | tmpOut += incOut;
|
---|
579 | firstVal = secVal;
|
---|
580 | }
|
---|
581 | if (tmpOut > lastElementOut)
|
---|
582 | tmpOut -= outLength;
|
---|
583 | if (tmpIn > lastElementIn)
|
---|
584 | tmpIn -= inLength;
|
---|
585 | }
|
---|
586 | }
|
---|
587 | }
|
---|
588 | #endregion
|
---|
589 | }
|
---|
590 | return new ILRetArray< byte >(retArr, newDimension);
|
---|
591 | }
|
---|
592 | }
|
---|
593 | /// <summary>
|
---|
594 | /// Take n-th derivative
|
---|
595 | /// </summary>
|
---|
596 | /// <param name="A">Input array</param>
|
---|
597 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
598 | /// <param name="N">[Optional] Degree of derivates. If not specified N=1 is assumed.</param>
|
---|
599 | /// <returns>Array with first derivative of A along dimension <c>dim</c> of first non singleton dimension</returns>
|
---|
600 | /// <remarks>N must be a number in range 1..L, where L is the length of A.Dimensions[dim].
|
---|
601 | /// Otherwise an empty array will be returned.
|
---|
602 | /// <para>If A is empty or scalar, or if N exceeds the length the specified dimension of A,
|
---|
603 | /// an empty array will be returned.</para></remarks>
|
---|
604 | public static ILRetArray< fcomplex > diff(ILInArray< fcomplex > A, int N = 1, int dim = -1) {
|
---|
605 | using (ILScope.Enter(A)) {
|
---|
606 | if (Object.Equals(A,null))
|
---|
607 | throw new ILArgumentException ("diff: input array A must not be null");
|
---|
608 | if (dim < 0) {
|
---|
609 | dim = A.Size.WorkingDimension();
|
---|
610 | }
|
---|
611 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
612 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
613 | outDims[dim] = 0;
|
---|
614 | return empty< fcomplex >(new ILSize(outDims));
|
---|
615 | }
|
---|
616 | if (A.IsScalar) return empty< fcomplex >(ILSize.Empty00);
|
---|
617 | if (A.IsEmpty) {
|
---|
618 | int [] retDim = A.Size.ToIntArray();
|
---|
619 | retDim[dim]--;
|
---|
620 | return empty< fcomplex >(new ILSize(retDim));
|
---|
621 | }
|
---|
622 | if (N == 0)
|
---|
623 | return A.C;
|
---|
624 | if (N < 1 || N > A.Size[dim]) {
|
---|
625 | return empty< fcomplex >(ILSize.Empty00);
|
---|
626 | }
|
---|
627 | ILArray< fcomplex > ret = A.C;
|
---|
628 | for (int n = 0; n < N; n++) {
|
---|
629 | ret.a = diff(dim,ret);
|
---|
630 | }
|
---|
631 | return ret;
|
---|
632 | }
|
---|
633 | }
|
---|
634 | /// <summary>
|
---|
635 | /// First derivative along specific dimension
|
---|
636 | /// </summary>
|
---|
637 | /// <param name="A">input array</param>
|
---|
638 | /// <param name="dim">dimensions to create derivative along</param>
|
---|
639 | /// <returns>array with first derivative of A along dimension <c>dim</c></returns>
|
---|
640 | private static ILRetArray< fcomplex > diff(int dim, ILInArray< fcomplex > A) {
|
---|
641 | using (ILScope.Enter(A)) {
|
---|
642 | if (A.IsEmpty) return empty< fcomplex >(A.Size);
|
---|
643 | if (A.IsScalar) return empty< fcomplex >(ILSize.Empty00);
|
---|
644 | if (dim < 0)
|
---|
645 | throw new ILArgumentException("diff: leading dimension out of range!");
|
---|
646 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
647 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
648 | outDims[dim] = 0;
|
---|
649 | return empty< fcomplex >(new ILSize(outDims));
|
---|
650 | }
|
---|
651 | ILSize inDim = A.Size;
|
---|
652 | int[] newDims = inDim.ToIntArray();
|
---|
653 |
|
---|
654 | if (inDim[dim] == 1) return empty< fcomplex >(ILSize.Empty00);
|
---|
655 | int newLength;
|
---|
656 | fcomplex [] retArr;
|
---|
657 | // build ILSize
|
---|
658 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
659 | newDims[dim] --;
|
---|
660 | newLength = newLength * newDims[dim];
|
---|
661 | retArr = ILMemoryPool.Pool.New< fcomplex >(newLength);
|
---|
662 | ILSize newDimension = new ILSize(newDims);
|
---|
663 | int leadDimLen = inDim[dim];
|
---|
664 | int nrHigherDims = inDim.NumberOfElements / leadDimLen;
|
---|
665 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
666 | fcomplex firstVal, secVal;
|
---|
667 | if (A.IsVector)
|
---|
668 | return A["1:end"] - A[vec(0,A.Length-2)];
|
---|
669 | if (dim == 0) {
|
---|
670 | #region physical along 1st leading dimension
|
---|
671 | unsafe {
|
---|
672 | fixed ( fcomplex * pOutArr = retArr)
|
---|
673 | fixed ( fcomplex * pInArr = A.GetArrayForRead()) {
|
---|
674 | fcomplex * lastElement;
|
---|
675 | fcomplex * tmpOut = pOutArr;
|
---|
676 | fcomplex * tmpIn = pInArr;
|
---|
677 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
678 | lastElement = tmpIn + leadDimLen;
|
---|
679 |
|
---|
680 | firstVal = *tmpIn++;
|
---|
681 | while (tmpIn < lastElement) {
|
---|
682 | secVal = *tmpIn++;
|
---|
683 | *(tmpOut++) = ( fcomplex )(secVal-firstVal);
|
---|
684 | firstVal = secVal;
|
---|
685 | }
|
---|
686 | }
|
---|
687 | }
|
---|
688 | }
|
---|
689 | #endregion
|
---|
690 | } else {
|
---|
691 | #region physical along abitrary dimension
|
---|
692 | // sum along abitrary dimension
|
---|
693 | unsafe {
|
---|
694 | fixed ( fcomplex * pOutArr = retArr)
|
---|
695 | fixed ( fcomplex * pInArr = A.GetArrayForRead()) {
|
---|
696 | fcomplex * lastElementOut = newLength + pOutArr -1;
|
---|
697 | int inLength = inDim.NumberOfElements -1;
|
---|
698 | fcomplex * lastElementIn = pInArr + inLength;
|
---|
699 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
700 | fcomplex * tmpOut = pOutArr;
|
---|
701 | int outLength = newLength - 1;
|
---|
702 | fcomplex * leadEnd;
|
---|
703 | fcomplex * tmpIn = pInArr;
|
---|
704 | for (int h = nrHigherDims; h--> 0; ) {
|
---|
705 | leadEnd = tmpIn + leadDimLen * inc;
|
---|
706 | firstVal = *tmpIn;
|
---|
707 | tmpIn += inc;
|
---|
708 | while (tmpIn < leadEnd) {
|
---|
709 | secVal = *tmpIn;
|
---|
710 | *tmpOut = ( fcomplex )(secVal - firstVal);
|
---|
711 | tmpIn += inc;
|
---|
712 | tmpOut += incOut;
|
---|
713 | firstVal = secVal;
|
---|
714 | }
|
---|
715 | if (tmpOut > lastElementOut)
|
---|
716 | tmpOut -= outLength;
|
---|
717 | if (tmpIn > lastElementIn)
|
---|
718 | tmpIn -= inLength;
|
---|
719 | }
|
---|
720 | }
|
---|
721 | }
|
---|
722 | #endregion
|
---|
723 | }
|
---|
724 | return new ILRetArray< fcomplex >(retArr, newDimension);
|
---|
725 | }
|
---|
726 | }
|
---|
727 | /// <summary>
|
---|
728 | /// Take n-th derivative
|
---|
729 | /// </summary>
|
---|
730 | /// <param name="A">Input array</param>
|
---|
731 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
732 | /// <param name="N">[Optional] Degree of derivates. If not specified N=1 is assumed.</param>
|
---|
733 | /// <returns>Array with first derivative of A along dimension <c>dim</c> of first non singleton dimension</returns>
|
---|
734 | /// <remarks>N must be a number in range 1..L, where L is the length of A.Dimensions[dim].
|
---|
735 | /// Otherwise an empty array will be returned.
|
---|
736 | /// <para>If A is empty or scalar, or if N exceeds the length the specified dimension of A,
|
---|
737 | /// an empty array will be returned.</para></remarks>
|
---|
738 | public static ILRetArray< float > diff(ILInArray< float > A, int N = 1, int dim = -1) {
|
---|
739 | using (ILScope.Enter(A)) {
|
---|
740 | if (Object.Equals(A,null))
|
---|
741 | throw new ILArgumentException ("diff: input array A must not be null");
|
---|
742 | if (dim < 0) {
|
---|
743 | dim = A.Size.WorkingDimension();
|
---|
744 | }
|
---|
745 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
746 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
747 | outDims[dim] = 0;
|
---|
748 | return empty< float >(new ILSize(outDims));
|
---|
749 | }
|
---|
750 | if (A.IsScalar) return empty< float >(ILSize.Empty00);
|
---|
751 | if (A.IsEmpty) {
|
---|
752 | int [] retDim = A.Size.ToIntArray();
|
---|
753 | retDim[dim]--;
|
---|
754 | return empty< float >(new ILSize(retDim));
|
---|
755 | }
|
---|
756 | if (N == 0)
|
---|
757 | return A.C;
|
---|
758 | if (N < 1 || N > A.Size[dim]) {
|
---|
759 | return empty< float >(ILSize.Empty00);
|
---|
760 | }
|
---|
761 | ILArray< float > ret = A.C;
|
---|
762 | for (int n = 0; n < N; n++) {
|
---|
763 | ret.a = diff(dim,ret);
|
---|
764 | }
|
---|
765 | return ret;
|
---|
766 | }
|
---|
767 | }
|
---|
768 | /// <summary>
|
---|
769 | /// First derivative along specific dimension
|
---|
770 | /// </summary>
|
---|
771 | /// <param name="A">input array</param>
|
---|
772 | /// <param name="dim">dimensions to create derivative along</param>
|
---|
773 | /// <returns>array with first derivative of A along dimension <c>dim</c></returns>
|
---|
774 | private static ILRetArray< float > diff(int dim, ILInArray< float > A) {
|
---|
775 | using (ILScope.Enter(A)) {
|
---|
776 | if (A.IsEmpty) return empty< float >(A.Size);
|
---|
777 | if (A.IsScalar) return empty< float >(ILSize.Empty00);
|
---|
778 | if (dim < 0)
|
---|
779 | throw new ILArgumentException("diff: leading dimension out of range!");
|
---|
780 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
781 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
782 | outDims[dim] = 0;
|
---|
783 | return empty< float >(new ILSize(outDims));
|
---|
784 | }
|
---|
785 | ILSize inDim = A.Size;
|
---|
786 | int[] newDims = inDim.ToIntArray();
|
---|
787 |
|
---|
788 | if (inDim[dim] == 1) return empty< float >(ILSize.Empty00);
|
---|
789 | int newLength;
|
---|
790 | float [] retArr;
|
---|
791 | // build ILSize
|
---|
792 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
793 | newDims[dim] --;
|
---|
794 | newLength = newLength * newDims[dim];
|
---|
795 | retArr = ILMemoryPool.Pool.New< float >(newLength);
|
---|
796 | ILSize newDimension = new ILSize(newDims);
|
---|
797 | int leadDimLen = inDim[dim];
|
---|
798 | int nrHigherDims = inDim.NumberOfElements / leadDimLen;
|
---|
799 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
800 | float firstVal, secVal;
|
---|
801 | if (A.IsVector)
|
---|
802 | return A["1:end"] - A[vec(0,A.Length-2)];
|
---|
803 | if (dim == 0) {
|
---|
804 | #region physical along 1st leading dimension
|
---|
805 | unsafe {
|
---|
806 | fixed ( float * pOutArr = retArr)
|
---|
807 | fixed ( float * pInArr = A.GetArrayForRead()) {
|
---|
808 | float * lastElement;
|
---|
809 | float * tmpOut = pOutArr;
|
---|
810 | float * tmpIn = pInArr;
|
---|
811 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
812 | lastElement = tmpIn + leadDimLen;
|
---|
813 |
|
---|
814 | firstVal = *tmpIn++;
|
---|
815 | while (tmpIn < lastElement) {
|
---|
816 | secVal = *tmpIn++;
|
---|
817 | *(tmpOut++) = ( float )(secVal-firstVal);
|
---|
818 | firstVal = secVal;
|
---|
819 | }
|
---|
820 | }
|
---|
821 | }
|
---|
822 | }
|
---|
823 | #endregion
|
---|
824 | } else {
|
---|
825 | #region physical along abitrary dimension
|
---|
826 | // sum along abitrary dimension
|
---|
827 | unsafe {
|
---|
828 | fixed ( float * pOutArr = retArr)
|
---|
829 | fixed ( float * pInArr = A.GetArrayForRead()) {
|
---|
830 | float * lastElementOut = newLength + pOutArr -1;
|
---|
831 | int inLength = inDim.NumberOfElements -1;
|
---|
832 | float * lastElementIn = pInArr + inLength;
|
---|
833 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
834 | float * tmpOut = pOutArr;
|
---|
835 | int outLength = newLength - 1;
|
---|
836 | float * leadEnd;
|
---|
837 | float * tmpIn = pInArr;
|
---|
838 | for (int h = nrHigherDims; h--> 0; ) {
|
---|
839 | leadEnd = tmpIn + leadDimLen * inc;
|
---|
840 | firstVal = *tmpIn;
|
---|
841 | tmpIn += inc;
|
---|
842 | while (tmpIn < leadEnd) {
|
---|
843 | secVal = *tmpIn;
|
---|
844 | *tmpOut = ( float )(secVal - firstVal);
|
---|
845 | tmpIn += inc;
|
---|
846 | tmpOut += incOut;
|
---|
847 | firstVal = secVal;
|
---|
848 | }
|
---|
849 | if (tmpOut > lastElementOut)
|
---|
850 | tmpOut -= outLength;
|
---|
851 | if (tmpIn > lastElementIn)
|
---|
852 | tmpIn -= inLength;
|
---|
853 | }
|
---|
854 | }
|
---|
855 | }
|
---|
856 | #endregion
|
---|
857 | }
|
---|
858 | return new ILRetArray< float >(retArr, newDimension);
|
---|
859 | }
|
---|
860 | }
|
---|
861 | /// <summary>
|
---|
862 | /// Take n-th derivative
|
---|
863 | /// </summary>
|
---|
864 | /// <param name="A">Input array</param>
|
---|
865 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
866 | /// <param name="N">[Optional] Degree of derivates. If not specified N=1 is assumed.</param>
|
---|
867 | /// <returns>Array with first derivative of A along dimension <c>dim</c> of first non singleton dimension</returns>
|
---|
868 | /// <remarks>N must be a number in range 1..L, where L is the length of A.Dimensions[dim].
|
---|
869 | /// Otherwise an empty array will be returned.
|
---|
870 | /// <para>If A is empty or scalar, or if N exceeds the length the specified dimension of A,
|
---|
871 | /// an empty array will be returned.</para></remarks>
|
---|
872 | public static ILRetArray< complex > diff(ILInArray< complex > A, int N = 1, int dim = -1) {
|
---|
873 | using (ILScope.Enter(A)) {
|
---|
874 | if (Object.Equals(A,null))
|
---|
875 | throw new ILArgumentException ("diff: input array A must not be null");
|
---|
876 | if (dim < 0) {
|
---|
877 | dim = A.Size.WorkingDimension();
|
---|
878 | }
|
---|
879 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
880 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
881 | outDims[dim] = 0;
|
---|
882 | return empty< complex >(new ILSize(outDims));
|
---|
883 | }
|
---|
884 | if (A.IsScalar) return empty< complex >(ILSize.Empty00);
|
---|
885 | if (A.IsEmpty) {
|
---|
886 | int [] retDim = A.Size.ToIntArray();
|
---|
887 | retDim[dim]--;
|
---|
888 | return empty< complex >(new ILSize(retDim));
|
---|
889 | }
|
---|
890 | if (N == 0)
|
---|
891 | return A.C;
|
---|
892 | if (N < 1 || N > A.Size[dim]) {
|
---|
893 | return empty< complex >(ILSize.Empty00);
|
---|
894 | }
|
---|
895 | ILArray< complex > ret = A.C;
|
---|
896 | for (int n = 0; n < N; n++) {
|
---|
897 | ret.a = diff(dim,ret);
|
---|
898 | }
|
---|
899 | return ret;
|
---|
900 | }
|
---|
901 | }
|
---|
902 | /// <summary>
|
---|
903 | /// First derivative along specific dimension
|
---|
904 | /// </summary>
|
---|
905 | /// <param name="A">input array</param>
|
---|
906 | /// <param name="dim">dimensions to create derivative along</param>
|
---|
907 | /// <returns>array with first derivative of A along dimension <c>dim</c></returns>
|
---|
908 | private static ILRetArray< complex > diff(int dim, ILInArray< complex > A) {
|
---|
909 | using (ILScope.Enter(A)) {
|
---|
910 | if (A.IsEmpty) return empty< complex >(A.Size);
|
---|
911 | if (A.IsScalar) return empty< complex >(ILSize.Empty00);
|
---|
912 | if (dim < 0)
|
---|
913 | throw new ILArgumentException("diff: leading dimension out of range!");
|
---|
914 | if (dim >= A.Size.NumberOfDimensions) {
|
---|
915 | int[] outDims = A.Size.ToIntArray(dim+1);
|
---|
916 | outDims[dim] = 0;
|
---|
917 | return empty< complex >(new ILSize(outDims));
|
---|
918 | }
|
---|
919 | ILSize inDim = A.Size;
|
---|
920 | int[] newDims = inDim.ToIntArray();
|
---|
921 |
|
---|
922 | if (inDim[dim] == 1) return empty< complex >(ILSize.Empty00);
|
---|
923 | int newLength;
|
---|
924 | complex [] retArr;
|
---|
925 | // build ILSize
|
---|
926 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
927 | newDims[dim] --;
|
---|
928 | newLength = newLength * newDims[dim];
|
---|
929 | retArr = ILMemoryPool.Pool.New< complex >(newLength);
|
---|
930 | ILSize newDimension = new ILSize(newDims);
|
---|
931 | int leadDimLen = inDim[dim];
|
---|
932 | int nrHigherDims = inDim.NumberOfElements / leadDimLen;
|
---|
933 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
934 | complex firstVal, secVal;
|
---|
935 | if (A.IsVector)
|
---|
936 | return A["1:end"] - A[vec(0,A.Length-2)];
|
---|
937 | if (dim == 0) {
|
---|
938 | #region physical along 1st leading dimension
|
---|
939 | unsafe {
|
---|
940 | fixed ( complex * pOutArr = retArr)
|
---|
941 | fixed ( complex * pInArr = A.GetArrayForRead()) {
|
---|
942 | complex * lastElement;
|
---|
943 | complex * tmpOut = pOutArr;
|
---|
944 | complex * tmpIn = pInArr;
|
---|
945 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
946 | lastElement = tmpIn + leadDimLen;
|
---|
947 |
|
---|
948 | firstVal = *tmpIn++;
|
---|
949 | while (tmpIn < lastElement) {
|
---|
950 | secVal = *tmpIn++;
|
---|
951 | *(tmpOut++) = ( complex )(secVal-firstVal);
|
---|
952 | firstVal = secVal;
|
---|
953 | }
|
---|
954 | }
|
---|
955 | }
|
---|
956 | }
|
---|
957 | #endregion
|
---|
958 | } else {
|
---|
959 | #region physical along abitrary dimension
|
---|
960 | // sum along abitrary dimension
|
---|
961 | unsafe {
|
---|
962 | fixed ( complex * pOutArr = retArr)
|
---|
963 | fixed ( complex * pInArr = A.GetArrayForRead()) {
|
---|
964 | complex * lastElementOut = newLength + pOutArr -1;
|
---|
965 | int inLength = inDim.NumberOfElements -1;
|
---|
966 | complex * lastElementIn = pInArr + inLength;
|
---|
967 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
968 | complex * tmpOut = pOutArr;
|
---|
969 | int outLength = newLength - 1;
|
---|
970 | complex * leadEnd;
|
---|
971 | complex * tmpIn = pInArr;
|
---|
972 | for (int h = nrHigherDims; h--> 0; ) {
|
---|
973 | leadEnd = tmpIn + leadDimLen * inc;
|
---|
974 | firstVal = *tmpIn;
|
---|
975 | tmpIn += inc;
|
---|
976 | while (tmpIn < leadEnd) {
|
---|
977 | secVal = *tmpIn;
|
---|
978 | *tmpOut = ( complex )(secVal - firstVal);
|
---|
979 | tmpIn += inc;
|
---|
980 | tmpOut += incOut;
|
---|
981 | firstVal = secVal;
|
---|
982 | }
|
---|
983 | if (tmpOut > lastElementOut)
|
---|
984 | tmpOut -= outLength;
|
---|
985 | if (tmpIn > lastElementIn)
|
---|
986 | tmpIn -= inLength;
|
---|
987 | }
|
---|
988 | }
|
---|
989 | }
|
---|
990 | #endregion
|
---|
991 | }
|
---|
992 | return new ILRetArray< complex >(retArr, newDimension);
|
---|
993 | }
|
---|
994 | }
|
---|
995 |
|
---|
996 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
997 |
|
---|
998 | }
|
---|
999 | }
|
---|