~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to test/harness/test-runner/ExocortexDSP/src/ComplexArray.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * BSD Licence:
 
3
 * Copyright (c) 2001, 2002 Ben Houston [ ben@exocortex.org ]
 
4
 * Exocortex Technologies [ www.exocortex.org ]
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without 
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 *
 
10
 * 1. Redistributions of source code must retain the above copyright notice, 
 
11
 * this list of conditions and the following disclaimer.
 
12
 * 2. Redistributions in binary form must reproduce the above copyright 
 
13
 * notice, this list of conditions and the following disclaimer in the 
 
14
 * documentation and/or other materials provided with the distribution.
 
15
 * 3. Neither the name of the <ORGANIZATION> nor the names of its contributors
 
16
 * may be used to endorse or promote products derived from this software
 
17
 * without specific prior written permission.
 
18
 *
 
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 
23
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
25
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
26
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 
29
 * DAMAGE.
 
30
 */
 
31
 
 
32
 
 
33
using System;
 
34
using System.Diagnostics;
 
35
using Exocortex.DSP;
 
36
 
 
37
namespace Exocortex.DSP {
 
38
        
 
39
        // Comments? Questions? Bugs? Tell Ben Houston at ben@exocortex.org
 
40
        // Version: May 4, 2002
 
41
        
 
42
        /// <summary>
 
43
        /// <p>A set of array utilities for complex number arrays</p>
 
44
        /// </summary>
 
45
        public class ComplexArray {
 
46
 
 
47
                //---------------------------------------------------------------------------------------------
 
48
 
 
49
                private ComplexArray() {
 
50
                }
 
51
 
 
52
                //---------------------------------------------------------------------------------------------
 
53
 
 
54
                /// <summary>
 
55
                /// Clamp length (modulus) of the elements in the complex array
 
56
                /// </summary>
 
57
                /// <param name="array"></param>
 
58
                /// <param name="fMinimum"></param>
 
59
                /// <param name="fMaximum"></param>
 
60
                static public void ClampLength( Complex[] array, double fMinimum, double fMaximum ) {
 
61
                        for( int i = 0; i < array.Length; i ++ ) {
 
62
                                array[i] = Complex.FromModulusArgument( Math.Max( fMinimum, Math.Min( fMaximum, array[i].GetModulus() ) ), array[i].GetArgument() );
 
63
                        }
 
64
                }
 
65
 
 
66
                /// <summary>
 
67
                /// Clamp elements in the complex array to range [minimum,maximum]
 
68
                /// </summary>
 
69
                /// <param name="array"></param>
 
70
                /// <param name="minimum"></param>
 
71
                /// <param name="maximum"></param>
 
72
                static public void Clamp( Complex[] array, Complex minimum, Complex maximum ) {
 
73
                        for( int i = 0; i < array.Length; i ++ ) {
 
74
                                array[i].Re     = Math.Min( Math.Max( array[ i ].Re, minimum.Re ), maximum.Re );
 
75
                                array[i].Im     = Math.Min( Math.Max( array[ i ].Re, minimum.Im ), maximum.Im );
 
76
                        }
 
77
                }
 
78
 
 
79
                /// <summary>
 
80
                /// Clamp elements in the complex array to real unit range (i.e. [0,1])
 
81
                /// </summary>
 
82
                /// <param name="array"></param>
 
83
                static public void ClampToRealUnit( Complex[] array ) {
 
84
                        for( int i = 0; i < array.Length; i ++ ) {
 
85
                                array[i].Re =  Math.Min( Math.Max( array[i].Re, 0 ), 1 );
 
86
                                array[i].Im = 0;
 
87
                        }
 
88
                }
 
89
                
 
90
                //---------------------------------------------------------------------------------------------
 
91
 
 
92
                static private bool                     _workspaceFLocked       = false;
 
93
                static private ComplexF[]       _workspaceF                     = new ComplexF[ 0 ];
 
94
 
 
95
                static private void             LockWorkspaceF( int length, ref ComplexF[] workspace ) {
 
96
                        Debug.Assert( _workspaceFLocked == false );
 
97
                        _workspaceFLocked = true;
 
98
                        if( length >= _workspaceF.Length ) {
 
99
                                _workspaceF     = new ComplexF[ length ];
 
100
                        }
 
101
                        workspace =     _workspaceF;
 
102
                }
 
103
                static private void             UnlockWorkspaceF( ref ComplexF[] workspace ) {
 
104
                        Debug.Assert( _workspaceF == workspace );
 
105
                        Debug.Assert( _workspaceFLocked == true );
 
106
                        _workspaceFLocked = false;
 
107
                        workspace = null;
 
108
                }
 
109
 
 
110
                //---------------------------------------------------------------------------------------------
 
111
 
 
112
                /// <summary>
 
113
                /// Shift (offset) the elements in the array
 
114
                /// </summary>
 
115
                /// <param name="array"></param>
 
116
                /// <param name="offset"></param>
 
117
                static public void Shift( Complex[] array, int offset ) {
 
118
                        Debug.Assert( array != null );
 
119
                        Debug.Assert( offset >= 0 );
 
120
                        Debug.Assert( offset < array.Length );
 
121
 
 
122
                        if( offset == 0 ) {
 
123
                                return;
 
124
                        }
 
125
 
 
126
                        int                     length  = array.Length;
 
127
                        Complex[]       temp    = new Complex[ length ];
 
128
 
 
129
                        for( int i = 0; i < length; i ++ ) {
 
130
                                temp[ ( i + offset ) % length ] = array[ i ];
 
131
                        }
 
132
                        for( int i = 0; i < length; i ++ ) {
 
133
                                array[ i ] = temp[ i ];
 
134
                        }
 
135
                }
 
136
 
 
137
                /// <summary>
 
138
                /// Shift (offset) the elements in the array
 
139
                /// </summary>
 
140
                /// <param name="array"></param>
 
141
                /// <param name="offset"></param>
 
142
                static public void Shift( ComplexF[] array, int offset ) {
 
143
                        Debug.Assert( array != null );
 
144
                        Debug.Assert( offset >= 0 );
 
145
                        Debug.Assert( offset < array.Length );
 
146
 
 
147
                        if( offset == 0 ) {
 
148
                                return;
 
149
                        }
 
150
 
 
151
                        int                     length          = array.Length;
 
152
                        ComplexF[]      workspace       = null;
 
153
                        ComplexArray.LockWorkspaceF( length, ref workspace );
 
154
 
 
155
                        for( int i = 0; i < length; i ++ ) {
 
156
                                workspace[ ( i + offset ) % length ] = array[ i ];
 
157
                        }
 
158
                        for( int i = 0; i < length; i ++ ) {
 
159
                                array[ i ] = workspace[ i ];
 
160
                        }
 
161
 
 
162
                        ComplexArray.UnlockWorkspaceF( ref workspace );
 
163
                }
 
164
 
 
165
                //---------------------------------------------------------------------------------------------
 
166
 
 
167
                /// <summary>
 
168
                /// Get the range of element lengths
 
169
                /// </summary>
 
170
                /// <param name="array"></param>
 
171
                /// <param name="minimum"></param>
 
172
                /// <param name="maximum"></param>
 
173
                static public void GetLengthRange( Complex[] array, ref double minimum, ref double maximum ) {
 
174
                        minimum = +double.MaxValue;
 
175
                        maximum = -double.MaxValue;
 
176
                        for( int i = 0; i < array.Length; i ++ ) {
 
177
                                double temp = array[i].GetModulus();
 
178
                                minimum = Math.Min( temp, minimum );
 
179
                                maximum = Math.Max( temp, maximum );
 
180
                        }
 
181
                }
 
182
                /// <summary>
 
183
                /// Get the range of element lengths
 
184
                /// </summary>
 
185
                /// <param name="array"></param>
 
186
                /// <param name="minimum"></param>
 
187
                /// <param name="maximum"></param>
 
188
                static public void GetLengthRange( ComplexF[] array, ref float minimum, ref float maximum ) {
 
189
                        minimum = +float.MaxValue;
 
190
                        maximum = -float.MaxValue;
 
191
                        for( int i = 0; i < array.Length; i ++ ) {
 
192
                                float temp = array[i].GetModulus();
 
193
                                minimum = Math.Min( temp, minimum );
 
194
                                maximum = Math.Max( temp, maximum );
 
195
                        }
 
196
                }
 
197
 
 
198
                // // <summary>
 
199
                // // Conver the complex array to a double array
 
200
                // // </summary>
 
201
                // // <param name="array"></param>
 
202
                // // <param name="style"></param>
 
203
                // // <returns></returns>
 
204
                /* static public double[]       ConvertToDoubleArray( Complex[] array, ConversionStyle style ) {
 
205
                        double[] newArray = new double[ array.Length ];
 
206
                        switch( style ) {
 
207
                        case ConversionStyle.Length:
 
208
                                for( int i = 0; i < array.Length; i ++ ) {
 
209
                                        newArray[i] = (double) array[i].GetModulus();
 
210
                                }
 
211
                                break;
 
212
                        case ConversionStyle.Real:
 
213
                                for( int i = 0; i < array.Length; i ++ ) {
 
214
                                        newArray[i] = (double) array[i].Re;
 
215
                                }
 
216
                                break;
 
217
                        case ConversionStyle.Imaginary:
 
218
                                for( int i = 0; i < array.Length; i ++ ) {
 
219
                                        newArray[i] = (double) array[i].Im;
 
220
                                }
 
221
                                break;
 
222
                        default:
 
223
                                Debug.Assert( false );
 
224
                                break;
 
225
                        }
 
226
                        return  newArray;
 
227
                }        */
 
228
 
 
229
                //---------------------------------------------------------------------------------------------
 
230
 
 
231
                /// <summary>
 
232
                /// Determine whether the elements in the two arrays are the same
 
233
                /// </summary>
 
234
                /// <param name="array1"></param>
 
235
                /// <param name="array2"></param>
 
236
                /// <param name="tolerance"></param>
 
237
                /// <returns></returns>
 
238
                static public bool              IsEqual( Complex[] array1, Complex[] array2, double tolerance ) {
 
239
                        if ( array1.Length != array2.Length ) {
 
240
                                return false;
 
241
                        }
 
242
                        for( int i = 0; i < array1.Length; i ++ ) {
 
243
                                if( Complex.IsEqual( array1[i], array2[i], tolerance ) == false ) {
 
244
                                        return false;
 
245
                                }
 
246
                        }
 
247
                        return true;
 
248
                }
 
249
 
 
250
                /// <summary>
 
251
                ///  Determine whether the elements in the two arrays are the same
 
252
                /// </summary>
 
253
                /// <param name="array1"></param>
 
254
                /// <param name="array2"></param>
 
255
                /// <param name="tolerance"></param>
 
256
                /// <returns></returns>
 
257
                static public bool              IsEqual( ComplexF[] array1, ComplexF[] array2, float tolerance ) {
 
258
                        if ( array1.Length != array2.Length ) {
 
259
                                return false;
 
260
                        }
 
261
                        for( int i = 0; i < array1.Length; i ++ ) {
 
262
                                if( ComplexF.IsEqual( array1[i], array2[i], tolerance ) == false ) {
 
263
                                        return false;
 
264
                                }
 
265
                        }
 
266
                        return true;
 
267
                }
 
268
 
 
269
                //---------------------------------------------------------------------------------------------
 
270
                
 
271
                /// <summary>
 
272
                /// Add a specific value to each element in the array
 
273
                /// </summary>
 
274
                /// <param name="array"></param>
 
275
                /// <param name="offset"></param>
 
276
                static public void Offset( Complex[] array, double offset ) {
 
277
                        int length = array.Length;
 
278
                        for( int i = 0; i < length; i ++ ) {
 
279
                                array[i].Re += offset;
 
280
                        }
 
281
                }
 
282
 
 
283
                /// <summary>
 
284
                /// Add a specific value to each element in the array
 
285
                /// </summary>
 
286
                /// <param name="array"></param>
 
287
                /// <param name="offset"></param>
 
288
                static public void Offset( Complex[] array, Complex offset ) {
 
289
                        int length = array.Length;
 
290
                        for( int i = 0; i < length; i ++ ) {
 
291
                                array[i] += offset;
 
292
                        }
 
293
                }
 
294
 
 
295
                /// <summary>
 
296
                /// Add a specific value to each element in the array
 
297
                /// </summary>
 
298
                /// <param name="array"></param>
 
299
                /// <param name="offset"></param>
 
300
                static public void Offset( ComplexF[] array, float offset ) {
 
301
                        int length = array.Length;
 
302
                        for( int i = 0; i < length; i ++ ) {
 
303
                                array[i].Re += offset;
 
304
                        }
 
305
                }
 
306
 
 
307
                /// <summary>
 
308
                /// Add a specific value to each element in the array
 
309
                /// </summary>
 
310
                /// <param name="array"></param>
 
311
                /// <param name="offset"></param>
 
312
                static public void Offset( ComplexF[] array, ComplexF offset ) {
 
313
                        int length = array.Length;
 
314
                        for( int i = 0; i < length; i ++ ) {
 
315
                                array[i] += offset;
 
316
                        }
 
317
                }
 
318
 
 
319
                //---------------------------------------------------------------------------------------------
 
320
                
 
321
                /// <summary>
 
322
                /// Multiply each element in the array by a specific value
 
323
                /// </summary>
 
324
                /// <param name="array"></param>
 
325
                /// <param name="scale"></param>
 
326
                static public void Scale( Complex[] array, double scale ) {
 
327
                        Debug.Assert( array != null );
 
328
 
 
329
                        int length = array.Length;
 
330
                        for( int i = 0; i < length; i ++ ) {
 
331
                                array[i] *= scale;
 
332
                        }
 
333
                }
 
334
                /// <summary>
 
335
                ///  Multiply each element in the array by a specific value
 
336
                /// </summary>
 
337
                /// <param name="array"></param>
 
338
                /// <param name="scale"></param>
 
339
                /// <param name="start"></param>
 
340
                /// <param name="length"></param>
 
341
                static public void Scale( Complex[] array, double scale, int start, int length ) {
 
342
                        Debug.Assert( array != null );
 
343
                        Debug.Assert( start >= 0 );
 
344
                        Debug.Assert( length >= 0 );
 
345
                        Debug.Assert( ( start + length ) < array.Length );
 
346
 
 
347
                        for( int i = 0; i < length; i ++ ) {
 
348
                                array[i + start] *= scale;
 
349
                        }
 
350
                }
 
351
 
 
352
                /// <summary>
 
353
                /// Multiply each element in the array by a specific value
 
354
                /// </summary>
 
355
                /// <param name="array"></param>
 
356
                /// <param name="scale"></param>
 
357
                static public void Scale( Complex[] array, Complex scale ) {
 
358
                        Debug.Assert( array != null );
 
359
 
 
360
                        int length = array.Length;
 
361
                        for( int i = 0; i < length; i ++ ) {
 
362
                                array[i] *= scale;
 
363
                        }
 
364
                }
 
365
                /// <summary>
 
366
                /// Multiply each element in the array by a specific value 
 
367
                /// </summary>
 
368
                /// <param name="array"></param>
 
369
                /// <param name="scale"></param>
 
370
                /// <param name="start"></param>
 
371
                /// <param name="length"></param>
 
372
                static public void Scale( Complex[] array, Complex scale, int start, int length ) {
 
373
                        Debug.Assert( array != null );
 
374
                        Debug.Assert( start >= 0 );
 
375
                        Debug.Assert( length >= 0 );
 
376
                        Debug.Assert( ( start + length ) < array.Length );
 
377
 
 
378
                        for( int i = 0; i < length; i ++ ) {
 
379
                                array[i + start] *= scale;
 
380
                        }
 
381
                }
 
382
 
 
383
                /// <summary>
 
384
                /// Multiply each element in the array by a specific value
 
385
                /// </summary>
 
386
                /// <param name="array"></param>
 
387
                /// <param name="scale"></param>
 
388
                static public void Scale( ComplexF[] array, float scale ) {
 
389
                        Debug.Assert( array != null );
 
390
 
 
391
                        int length = array.Length;
 
392
                        for( int i = 0; i < length; i ++ ) {
 
393
                                array[i] *= scale;
 
394
                        }
 
395
                }
 
396
                /// <summary>
 
397
                /// Multiply each element in the array by a specific value 
 
398
                /// </summary>
 
399
                /// <param name="array"></param>
 
400
                /// <param name="scale"></param>
 
401
                /// <param name="start"></param>
 
402
                /// <param name="length"></param>
 
403
                static public void Scale( ComplexF[] array, float scale, int start, int length ) {
 
404
                        Debug.Assert( array != null );
 
405
                        Debug.Assert( start >= 0 );
 
406
                        Debug.Assert( length >= 0 );
 
407
                        Debug.Assert( ( start + length ) < array.Length );
 
408
 
 
409
                        for( int i = 0; i < length; i ++ ) {
 
410
                                array[i + start] *= scale;
 
411
                        }
 
412
                }
 
413
 
 
414
                /// <summary>
 
415
                /// Multiply each element in the array by a specific value
 
416
                /// </summary>
 
417
                /// <param name="array"></param>
 
418
                /// <param name="scale"></param>
 
419
                static public void Scale( ComplexF[] array, ComplexF scale ) {
 
420
                        Debug.Assert( array != null );
 
421
 
 
422
                        int length = array.Length;
 
423
                        for( int i = 0; i < length; i ++ ) {
 
424
                                array[i] *= scale;
 
425
                        }
 
426
                }
 
427
                /// <summary>
 
428
                /// Multiply each element in the array by a specific value 
 
429
                /// </summary>
 
430
                /// <param name="array"></param>
 
431
                /// <param name="scale"></param>
 
432
                /// <param name="start"></param>
 
433
                /// <param name="length"></param>
 
434
                static public void Scale( ComplexF[] array, ComplexF scale, int start, int length ) {
 
435
                        Debug.Assert( array != null );
 
436
                        Debug.Assert( start >= 0 );
 
437
                        Debug.Assert( length >= 0 );
 
438
                        Debug.Assert( ( start + length ) < array.Length );
 
439
 
 
440
                        for( int i = 0; i < length; i ++ ) {
 
441
                                array[i + start] *= scale;
 
442
                        }
 
443
                }
 
444
 
 
445
                //---------------------------------------------------------------------------------------------
 
446
 
 
447
                /// <summary>
 
448
                /// Multiply each element in target array with corresponding element in rhs array
 
449
                /// </summary>
 
450
                /// <param name="target"></param>
 
451
                /// <param name="rhs"></param>
 
452
                static public void Multiply( Complex[] target, Complex[] rhs ) {
 
453
                        ComplexArray.Multiply( target, rhs, target );
 
454
                }
 
455
                /// <summary>
 
456
                /// Multiply each element in lhs array with corresponding element in rhs array and
 
457
                /// put product in result array
 
458
                /// </summary>
 
459
                /// <param name="lhs"></param>
 
460
                /// <param name="rhs"></param>
 
461
                /// <param name="result"></param>
 
462
                static public void Multiply( Complex[] lhs, Complex[] rhs, Complex[] result ) {
 
463
                        Debug.Assert( lhs != null );
 
464
                        Debug.Assert( rhs != null );
 
465
                        Debug.Assert( result != null );
 
466
                        Debug.Assert( lhs.Length == rhs.Length );
 
467
                        Debug.Assert( lhs.Length == result.Length );
 
468
 
 
469
                        int length = lhs.Length;
 
470
                        for( int i = 0; i < length; i ++ ) {
 
471
                                result[i] = lhs[i] * rhs[i];
 
472
                        }
 
473
                }
 
474
 
 
475
                /// <summary>
 
476
                /// Multiply each element in target array with corresponding element in rhs array
 
477
                /// </summary>
 
478
                /// <param name="target"></param>
 
479
                /// <param name="rhs"></param>
 
480
                static public void Multiply( ComplexF[] target, ComplexF[] rhs ) {
 
481
                        ComplexArray.Multiply( target, rhs, target );
 
482
                }
 
483
                /// <summary>
 
484
                /// Multiply each element in lhs array with corresponding element in rhs array and
 
485
                /// put product in result array
 
486
                /// </summary>
 
487
                /// <param name="lhs"></param>
 
488
                /// <param name="rhs"></param>
 
489
                /// <param name="result"></param>
 
490
                static public void Multiply( ComplexF[] lhs, ComplexF[] rhs, ComplexF[] result ) {
 
491
                        Debug.Assert( lhs != null );
 
492
                        Debug.Assert( rhs != null );
 
493
                        Debug.Assert( result != null );
 
494
                        Debug.Assert( lhs.Length == rhs.Length );
 
495
                        Debug.Assert( lhs.Length == result.Length );
 
496
 
 
497
                        int length = lhs.Length;
 
498
                        for( int i = 0; i < length; i ++ ) {
 
499
                                result[i] = lhs[i] * rhs[i];
 
500
                        }
 
501
                }
 
502
 
 
503
                //---------------------------------------------------------------------------------------------
 
504
 
 
505
                /// <summary>
 
506
                /// Divide each element in target array with corresponding element in rhs array
 
507
                /// </summary>
 
508
                /// <param name="target"></param>
 
509
                /// <param name="rhs"></param>
 
510
                static public void Divide( Complex[] target, Complex[] rhs ) {
 
511
                        ComplexArray.Divide( target, rhs, target );
 
512
                }
 
513
                /// <summary>
 
514
                /// Divide each element in lhs array with corresponding element in rhs array and
 
515
                /// put product in result array
 
516
                /// </summary>
 
517
                /// <param name="lhs"></param>
 
518
                /// <param name="rhs"></param>
 
519
                /// <param name="result"></param>
 
520
                static public void Divide( Complex[] lhs, Complex[] rhs, Complex[] result ) {
 
521
                        Debug.Assert( lhs != null );
 
522
                        Debug.Assert( rhs != null );
 
523
                        Debug.Assert( result != null );
 
524
                        Debug.Assert( lhs.Length == rhs.Length );
 
525
                        Debug.Assert( lhs.Length == result.Length );
 
526
 
 
527
                        int length = lhs.Length;
 
528
                        for( int i = 0; i < length; i ++ ) {
 
529
                                result[i] = lhs[i] / rhs[i];
 
530
                        }
 
531
                }
 
532
 
 
533
                /// <summary>
 
534
                /// Divide each element in target array with corresponding element in rhs array
 
535
                /// </summary>
 
536
                /// <param name="target"></param>
 
537
                /// <param name="rhs"></param>
 
538
                static public void Divide( ComplexF[] target, ComplexF[] rhs ) {
 
539
                        ComplexArray.Divide( target, rhs, target );
 
540
                }
 
541
                /// <summary>
 
542
                /// Divide each element in lhs array with corresponding element in rhs array and
 
543
                /// put product in result array
 
544
                /// </summary>
 
545
                /// <param name="lhs"></param>
 
546
                /// <param name="rhs"></param>
 
547
                /// <param name="result"></param>
 
548
                static public void Divide( ComplexF[] lhs, ComplexF[] rhs, ComplexF[] result ) {
 
549
                        Debug.Assert( lhs != null );
 
550
                        Debug.Assert( rhs != null );
 
551
                        Debug.Assert( result != null );
 
552
                        Debug.Assert( lhs.Length == rhs.Length );
 
553
                        Debug.Assert( lhs.Length == result.Length );
 
554
 
 
555
                        ComplexF zero = ComplexF.Zero;
 
556
                        int length = lhs.Length;
 
557
                        for( int i = 0; i < length; i ++ ) {
 
558
                                if( rhs[i] != zero ) {
 
559
                                        result[i] = lhs[i] / rhs[i];
 
560
                                }
 
561
                                else {
 
562
                                        result[i] = zero;
 
563
                                }
 
564
                        }
 
565
                }
 
566
 
 
567
                //---------------------------------------------------------------------------------------------
 
568
 
 
569
                /*static public void Flip( ComplexF[] array, Size3 size ) {
 
570
                        Debug.Assert( array != null );
 
571
 
 
572
                        ComplexF[]      workspace       = null;
 
573
                        ComplexArray.LockWorkspaceF( size.GetTotalLength(), ref workspace );
 
574
                        
 
575
                        for( int z = 0; z < size.Depth; z ++ ) {
 
576
                                for( int y = 0; y < size.Height; y ++ ) {
 
577
                                        int xyzOffset = 0 + y * size.Width + z * size.Width * size.Height;
 
578
                                        int abcOffset = size.Width - 1 + ( size.Height - y - 1 ) * size.Width + ( size.Depth - z - 1 ) * size.Width * size.Height;
 
579
                                        for( int x = 0; x < size.Width; x ++ ) {
 
580
                                                workspace[ xyzOffset ++ ] = array[ abcOffset -- ];
 
581
                                        }
 
582
                                }
 
583
                        }
 
584
 
 
585
                        for( int i = 0; i < size.GetTotalLength(); i ++ ) {
 
586
                                array[ i ] = workspace[ i ];
 
587
                        }
 
588
 
 
589
                        ComplexArray.UnlockWorkspaceF( ref workspace );
 
590
                }  */
 
591
                
 
592
 
 
593
                /// <summary>
 
594
                /// Copy an array
 
595
                /// </summary>
 
596
                /// <param name="dest"></param>
 
597
                /// <param name="source"></param>
 
598
                static public void Copy( Complex[] dest, Complex[] source ) {
 
599
                        Debug.Assert( dest != null );
 
600
                        Debug.Assert( source != null );
 
601
                        Debug.Assert( dest.Length == source.Length );
 
602
                        for( int i = 0; i < dest.Length; i ++ ) {
 
603
                                dest[i] = source[i];
 
604
                        }
 
605
                }
 
606
 
 
607
                /// <summary>
 
608
                /// Copy an array
 
609
                /// </summary>
 
610
                /// <param name="dest"></param>
 
611
                /// <param name="source"></param>
 
612
                static public void Copy( ComplexF[] dest, ComplexF[] source ) {
 
613
                        Debug.Assert( dest != null );
 
614
                        Debug.Assert( source != null );
 
615
                        Debug.Assert( dest.Length == source.Length );
 
616
                        for( int i = 0; i < dest.Length; i ++ ) {
 
617
                                dest[i] = source[i];
 
618
                        }
 
619
                }
 
620
 
 
621
                /// <summary>
 
622
                /// Reverse the elements in the array
 
623
                /// </summary>
 
624
                /// <param name="array"></param>
 
625
                static public void Reverse( Complex[] array ) {
 
626
                        Complex temp;
 
627
                        int length = array.Length;
 
628
                        for( int i = 0; i < length/2; i ++ ) {
 
629
                                temp = array[i];
 
630
                                array[i] = array[length-1-i];
 
631
                                array[length-1-i] = temp;
 
632
                        }
 
633
                }
 
634
 
 
635
                /// <summary>
 
636
                /// Scale and offset the elements in the array so that the
 
637
                /// overall range is [0, 1]
 
638
                /// </summary>
 
639
                /// <param name="array"></param>
 
640
                static public void Normalize( Complex[] array ) {
 
641
                        double min = 0, max = 0;
 
642
                        GetLengthRange( array, ref min, ref max );
 
643
                        Scale( array, ( 1 / ( max - min ) ) );
 
644
                        Offset( array, ( - min / ( max - min ) ) );
 
645
                }
 
646
 
 
647
                /// <summary>
 
648
                /// Scale and offset the elements in the array so that the
 
649
                /// overall range is [0, 1]
 
650
                /// </summary>
 
651
                /// <param name="array"></param>
 
652
                static public void Normalize( ComplexF[] array ) {
 
653
                        float min = 0, max = 0;
 
654
                        GetLengthRange( array, ref min, ref max );
 
655
                        Scale( array, ( 1 / ( max - min ) ) );
 
656
                        Offset( array, ( - min / ( max - min ) ) );
 
657
                }
 
658
 
 
659
                /// <summary>
 
660
                /// Invert each element in the array
 
661
                /// </summary>
 
662
                /// <param name="array"></param>
 
663
                static public void Invert( Complex[] array ) {
 
664
                        for( int i = 0; i < array.Length; i ++ ) {
 
665
                                array[i] = ((Complex) 1 ) / array[i];
 
666
                        }
 
667
                }
 
668
 
 
669
                /// <summary>
 
670
                /// Invert each element in the array
 
671
                /// </summary>
 
672
                /// <param name="array"></param>
 
673
                static public void Invert( ComplexF[] array ) {
 
674
                        for( int i = 0; i < array.Length; i ++ ) {
 
675
                                array[i] = ((ComplexF) 1 ) / array[i];
 
676
                        }
 
677
                }
 
678
 
 
679
                //----------------------------------------------------------------------------------------
 
680
 
 
681
        }
 
682
}