File size: 25,734 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#region Copyright � 2009, De Santiago-Castillo JA. All rights reserved.

//Copyright � 2009 Jose Antonio De Santiago-Castillo 
//E-mail:JAntonioDeSantiago@gmail.com
//Web: www.DotNumerics.com
//
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using DotNumerics.FortranLibrary;

namespace DotNumerics.LinearAlgebra
{
    //[DebuggerDisplay("[{RowCount},{ColumnCount}]", Name = "MatrixComplex")]
    /// <summary>
    /// Represents a Complex Matrix.
    /// </summary>
    [DebuggerDisplay("[{RowCount},{ColumnCount}]")]
    [DebuggerTypeProxy(typeof(MatrixComplexDebuggerDisplay))]
    public class ComplexMatrix : IMatrix<Complex>
    {
        #region Fields
        /// <summary>
        /// Los datos de la matriz, los datos se almacenan en un un array unidimensional,
        /// Los elementos se almacenan por columnas, esto para que sean compatible con los Arrays de Fortran
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        protected Complex[] _Data;
        /// <summary>
        /// El numero de renglones
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        protected int _RowCount;

        /// <summary>
        /// El numero de columnas
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        protected int _ColumnCount;

        #endregion


        #region  Public Constructors

        /// <summary>
        /// Initializes a new instance of the MatrixComplex class of the given size.
        /// </summary>
        /// <param name="rows">Number of rows.</param>
        /// <param name="columns">Number of columns.</param>
        public ComplexMatrix(int rows, int columns)
        {
            if (rows < 1) throw new System.ArgumentException("rows < 1");
            if (columns < 1) throw new System.ArgumentException("columns < 1");

            this._Data = new Complex[rows * columns];
            this._RowCount = rows;
            this._ColumnCount = columns;
        }

        /// <summary>
        /// Initializes a new instance of the MatrixComplex class of the given size using a array
        /// </summary>
        /// <param name="rows">Number of rows.</param>
        /// <param name="columns">Number of columns.</param>
        /// <param name="Data">The data</param>
        internal ComplexMatrix(int rows, int columns, Complex[] Data)
        {
            if (rows < 1) throw new System.ArgumentException("rows < 1");
            if (columns < 1) throw new System.ArgumentException("columns < 1");
            this._Data = new Complex[rows * columns];
            this._RowCount = rows;
            this._ColumnCount = columns;
            //Si incluye la posibilidad de que los datos tengan menos valores que la matriz a crear 
            for (int i = 0; i < Math.Min(this._Data.Length, Data.Length); i++)
            {
                this._Data[i] = Data[i];
            }
        }

        /// <summary>
        /// Initializes a new instance of the MatrixComplex class of the given size.
        /// </summary>
        /// <param name="size">Size</param>
        public ComplexMatrix(int size)
        {
            if (size < 1) throw new System.ArgumentException("size < 1");

            this._Data = new Complex[size * size];
            this._RowCount = size;
            this._ColumnCount = size;
        }

        /// <summary>
        /// Initializes a new instance of the MatrixComplex class of the given size using a array
        /// </summary>
        /// <param name="size">Size</param>
        /// <param name="Data">The data</param>
        internal ComplexMatrix(int size, Complex[] Data)
        {
            if (size < 1) throw new System.ArgumentException("size < 1");

            this._Data = new Complex[size * size];
            this._RowCount = size;
            this._ColumnCount = size;
            //Si incluye la posibilidad de que los datos tengan menos valores que la matriz a crear 
            for (int i = 0; i < Math.Min(this._Data.Length, Data.Length); i++)
            {
                this._Data[i] = Data[i];
            }
        }

        #endregion


        #region Public Properties

        /// <summary>
        /// Los datos de la matriz
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        internal Complex[] Data
        {
            get { return this._Data; }
        }

        /// <summary>
        /// Returns the number of rows.
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public int RowCount
        {
            get { return _RowCount; }
            set { _RowCount = value; }
        }
        /// <summary>
        /// Returns the number of columns.
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public int ColumnCount
        {
            get { return _ColumnCount; }
            set { _ColumnCount = value; }
        }

        /// <summary>
        /// Gets a value indicating if the matrix is square.
        /// </summary>
        public bool IsSquare
        {
            get
            {
                bool isSquare = false;
                if (this._ColumnCount == this.RowCount) isSquare = true;
                return isSquare;
            }
        }

        /// <summary>
        /// Returns the value of a element of the matrix.
        /// </summary>
        /// <param name="row">The row value (zero-based).</param>
        /// <param name="column">The column value (zero-based).</param>
        /// <returns>The matrix value at (row, column).</returns>
        public virtual Complex this[int row, int column]
        {
            get
            {
                if (column >= this._ColumnCount)
                {
                    throw new ArgumentException("Index was outside the bounds of the matrix.");
                }
                return this._Data[row + column * this._RowCount];
            }
            set
            {
                if (column >= this._ColumnCount)
                {
                    throw new ArgumentException("Index was outside the bounds of the matrix.");
                }
                this._Data[row + column * this._RowCount] = value;
            }
        }

        #endregion

        #region	 Private Methods

        /// <summary>Check if size(this) == size(B) </summary>
        private void CheckMatrixDimensions(ComplexMatrix B)
        {
            if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions must agree.");
            }
        }

        /// <summary>Check if size(this) == size(B) </summary>
        private void CheckMatrixDimensions(Matrix B)
        {
            if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions must agree.");
            }
        }
        #endregion //  Private Methods

        #region Elementary linear operations

        ///// <summary>
        ///// aij=Math.Abs(aij)
        ///// </summary>
        //public virtual void ElementsAbs()
        //{
        //    for (int i = 0; i < this.MeData.Length; i++)
        //    {
        //        this.MeData[i] =Complex.  Math.Abs(this.MeData[i]);
        //    }
        //}

        ///// <summary>
        ///// Element-by-element division: aij = aij/bij
        ///// </summary>
        ///// <param name="B"></param>
        //public virtual void ElemntsDiv(MatrixComplex B)
        //{
        //    CheckMatrixDimensions(B);
        //    Complex[] BData = B.Data;
        //    for (int i = 0; i < this.MeData.Length; i++)
        //    {
        //        this.MeData[i] /= BData[i];
        //    }
        //}

        /// <summary>
        /// Element-by-element multiplication: aij = aij*bij
        /// </summary>
        /// <param name="B">The B MatrixComplex</param>
        public virtual void ElemntsMult(ComplexMatrix B)
        {
            CheckMatrixDimensions(B);
            Complex[] BData = B.Data;
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i] = this._Data[i] * BData[i];
            }
        }

        /// <summary>
        /// In place addition A=A+B
        /// </summary>
        /// <param name="B">The B MatrixComplex</param>
        public virtual void Add(ComplexMatrix B)
        {
            CheckMatrixDimensions(B);
            Complex[] BData = B.Data;
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i] = this._Data[i] + BData[i];
            }
        }

        /// <summary>
        /// In place scalar-matrix multiplication, A=s*A
        /// </summary>
        /// <param name="s">The scalar s.</param>
        public virtual void Multiply(double s)
        {
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i].Real = s * this._Data[i].Real;
                this._Data[i].Imaginary = s * this._Data[i].Imaginary;
            }
        }
        /// <summary>
        /// In place scalar-matrix multiplication, A=c*A
        /// </summary>
        /// <param name="s">The scalar s.</param>
        public virtual void MultiplyC(Complex c)
        {
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i] = c * this._Data[i];
            }
        }


        /// <summary>
        /// In place matrix subtraction, A=A-B.
        /// </summary>
        /// <param name="B">The B MatrixComplex.</param>
        public virtual void Subtract(ComplexMatrix B)
        {
            CheckMatrixDimensions(B);
            Complex[] BData = B.Data;
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i].Real = this._Data[i].Real - BData[i].Real;
                this._Data[i].Imaginary = this._Data[i].Imaginary - BData[i].Imaginary;
            }
        }

        /// <summary>
        /// In place unary minus -A.
        /// </summary>
        public virtual void UnaryMinus()
        {
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i].Real = -this._Data[i].Real;
                this._Data[i].Imaginary = -this._Data[i].Imaginary;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the column vectors of this matrix.
        /// </summary>
        /// <returns>The columns vectors.</returns>
        public ComplexVector[] GetColumnVectors()
        {
            ComplexVector[] columnVects = new ComplexVector[this._ColumnCount];

            Complex[] VectData;
            for (int j = 0; j < this._ColumnCount; j++)
            {
                columnVects[j] = new ComplexVector(VectorType.Column, this._RowCount);
                VectData = columnVects[j].Data;
                for (int i = 0; i < VectData.Length; i++)
                {
                    VectData[i] = this._Data[i + j * this._RowCount];
                }
            }

            return columnVects;
        }

        /// <summary>
        /// Gets the row vectors of this matrix.
        /// </summary>
        /// <returns>The row vectors.</returns>
        public ComplexVector[] GetRowVectors()
        {
            ComplexVector[] rowVects = new ComplexVector[this.RowCount];

            Complex[] VectData;
            for (int i = 0; i < this._RowCount; i++)
            {
                rowVects[i] = new ComplexVector(VectorType.Row, this._ColumnCount);
                VectData = rowVects[i].Data;
                for (int j = 0; j < VectData.Length; j++)
                {
                    VectData[j] = this._Data[i + j * this._RowCount];
                }
            }

            return rowVects;
        }

        /// <summary>
        /// Gets a matrix that contains the real part of this matrix.
        /// </summary>
        /// <returns>A matrix that contains the real part of this matrix. </returns>
        public Matrix GetReal()
        {
            Matrix RealMatrix = new Matrix(this.RowCount, this.ColumnCount);
            double[] RealData = RealMatrix.Data; 
            for (int i = 0; i < this._Data.Length; i++)
            {
                RealData[i] = this._Data[i].Real;
            }
            return RealMatrix;
        }

        /// <summary>
        /// Gets a matrix that contains the imaginary part of this matrix.
        /// </summary>
        /// <returns>A matrix that contains the imaginary part of this matrix. </returns>
        public Matrix GetImag()
        {
            Matrix ImagMatrix = new Matrix(this.RowCount, this.ColumnCount);
            double[] ImagData = ImagMatrix.Data;
            for (int i = 0; i < this._Data.Length; i++)
            {
                ImagData[i] = this._Data[i].Imaginary;
            }
            return ImagMatrix;
        }


        /// <summary>
        /// Sets the real part of the elements of this matrix equal to the elemnets of a real matrix.
        /// </summary>
        /// <param name="RM">A matrix that contains the values of the real part.</param>
        public void SetReal(Matrix RM)
        {
            this.CheckMatrixDimensions(RM);
            double[] RealData = RM.Data;
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i].Real = RealData[i];
            }
        }

        /// <summary>
        /// Sets the imaginary part of the elements of this matrix equal to the elemnets of a real matrix.
        /// </summary>
        /// <param name="IM">A matrix that contains the values of the imaginary part.</param>
        public void SetImag(Matrix IM)
        {
            this.CheckMatrixDimensions(IM);
            double[] ImagData = IM.Data;
            for (int i = 0; i < this._Data.Length; i++)
            {
                this._Data[i].Imaginary = ImagData[i];
            }
        }


        /// <summary>
        /// Returns the string of the  matrix.
        /// </summary>
        /// <returns>The string of the  matrix.</returns>
        public string MatrixToString()
        {
            using (StringWriter writer = new StringWriter())
            {
                for (int i = 0; i < this._RowCount; i++)
                {
                    for (int j = 0; j < this._ColumnCount; j++)
                        writer.Write(this[i, j] + ", ");
                    writer.WriteLine();
                }
                return writer.ToString();
            }
        }
        /// <summary>
        /// Returns the string of the  matrix.
        /// </summary>
        /// <param name="format">A numeric format string.</param>
        /// <returns>The string of the  matrix.</returns>
        public string MatrixToString(string format)
        {
            using (StringWriter writer = new StringWriter())
            {
                for (int i = 0; i < this._RowCount; i++)
                {
                    for (int j = 0; j < this._ColumnCount; j++)
                        writer.Write(this[i, j].ToString(format) + ", ");
                    writer.WriteLine();
                }
                return writer.ToString();
            }
        }


        ///// <summary>
        ///// maximum column sum.
        ///// </summary>
        ///// <returns>maximum column sum.</returns>
        //public double Norm1()
        //{
        //    double n = 0.0;
        //    double ColSum = 0.0;
        //    int NRows = this.MeRowCount;

        //    for (int j = 0; j < this.MeColumnCount; j++)
        //    {
        //        ColSum = 0.0;
        //        for (int i = 0; i < this.MeRowCount; i++)
        //        {
        //            ColSum += Math.Abs(this.MeData[i + j * NRows]);

        //        }
        //        n = Math.Max(n, ColSum);
        //    }
        //    return n;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public double InfinityNorm()
        //{
        //    double n = 0.0;
        //    double RowSum = 0.0;
        //    int NRows = this.MeRowCount;
        //    for (int i = 0; i < this.MeRowCount; i++)
        //    {
        //        RowSum = 0.0;
        //        for (int j = 0; j < this.MeColumnCount; j++)
        //        {
        //            RowSum += Math.Abs(this.MeData[i + j * NRows]);
        //        }
        //        n = Math.Max(n, RowSum);
        //    }
        //    return n;
        //}

        ///// <summary>Frobenius norm</summary>
        ///// <returns>Sqrt of sum of squares of all elements.</returns>
        //public double FrobeniusNorm()
        //{
        //    double n=0;
        //    for(int i=0; i<this.MeData.Length; i++)
        //    {
        //        n=this.Hypot(n,this.MeData[i]);
        //    }
        //    return n;
        //}

        ///// <summary>sqrt(a^2 + b^2) without under/overflow.</summary>
        //private  double Hypot(double a, double b) 
        //{
        //    double r;
        //    if (Math.Abs(a) > Math.Abs(b)) 
        //    {
        //        r = b/a;
        //        r = Math.Abs(a) * Math.Sqrt(1 + r * r);
        //    } 
        //    else if (b != 0) 
        //    {
        //        r = a/b;
        //        r = Math.Abs(b) * Math.Sqrt(1 + r * r);
        //    } 
        //    else 
        //    {
        //        r = 0.0;
        //    }
        //    return r;
        //}


        #endregion

        #region Matrix-Matrix Multiplication

        /// <summary>
        /// Matrix multiplication.
        /// </summary>
        /// <param name="A"> The left side matrix of the multiplication operator.</param>
        /// <param name="B">The right side matrix of the multiplication operator.</param>
        /// <returns>A matrix that represents the result of the matrix multiplication.</returns>
        public static ComplexMatrix operator *(ComplexMatrix A, ComplexMatrix B)
        {
            if (B.RowCount != A.ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions are not valid.");
            }

            ComplexMatrix C = new ComplexMatrix(A.RowCount, B.ColumnCount);

            Complex[] AData = A.Data;
            Complex[] BData = B.Data;
            Complex[] CData = C.Data;

            int ARows = A.RowCount;
            int AColumns = A.ColumnCount;

            int BRows = B.RowCount;
            int BColumns = B.ColumnCount;

            Complex Sum = new Complex(0.0, 0.0);
            for (int j = 0; j < BColumns; j++)
            {
                for (int i = 0; i < ARows; i++)
                {
                    Sum.Imaginary = 0.0;
                    Sum.Real = 0.0;
                    for (int k = 0; k < AColumns; k++)
                    {
                        Sum += AData[i + k * ARows] * BData[k + j * BRows];
                    }
                    CData[i + j * ARows] = Sum;
                }
            }
            return C;
        }

        /// <summary>
        /// Matrix multiplication.
        /// </summary>
        /// <param name="A"> The left side matrix of the multiplication operator.</param>
        /// <param name="B">The right side matrix of the multiplication operator.</param>
        /// <returns>A matrix that represents the result of the matrix multiplication.</returns>
        public static ComplexMatrix operator *(BaseMatrix A, ComplexMatrix B)
        {
            if (B.RowCount != A.ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions are not valid.");
            }

            ComplexMatrix C = new ComplexMatrix(A.RowCount, B.ColumnCount);

            double[] AData = A.Data;
            Complex[] BData = B.Data;
            Complex[] CData = C.Data;

            int ARows = A.RowCount;
            int AColumns = A.ColumnCount;

            int BRows = B.RowCount;
            int BColumns = B.ColumnCount;

            Complex Sum = new Complex(0.0, 0.0);
            for (int j = 0; j < BColumns; j++)
            {
                for (int i = 0; i < ARows; i++)
                {
                    Sum.Imaginary = 0.0;
                    Sum.Real = 0.0;
                    for (int k = 0; k < AColumns; k++)
                    {
                        Sum += AData[i + k * ARows] * BData[k + j * BRows];
                    }
                    CData[i + j * ARows] = Sum;
                }
            }
            return C;
        }

        /// <summary>complex-Matrix multiplication.</summary>
        /// <param name="c"> The left side scalar of the multiplication operator.</param>
        /// <param name="B">The right side matrix of the multiplication operator.</param>
        /// <returns>A matrix that represents the result of the multiplication.</returns>
        public static ComplexMatrix operator *(Complex c, ComplexMatrix B)
        {

            ComplexMatrix C = new ComplexMatrix(B.RowCount, B.ColumnCount);

            Complex[] BData = B.Data;
            Complex[] CData = C.Data;


            for (int i = 0; i < BData.Length; i++)
            {
                CData[i] = c * BData[i];
            }
            return C;
        }


        #endregion

        #region Matrix-Matrix Addition
        /// <summary>
        /// Matrix addition.
        /// </summary>
        /// <param name="A">The left side matrix of the addition operator.</param>
        /// <param name="B">The right side matrix of the addition operator.</param>
        /// <returns>A matrix that represents the result of the matrix addition.</returns>
        public static ComplexMatrix operator +(ComplexMatrix A, ComplexMatrix B)
        {
            if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions are not valid.");
            }

            ComplexMatrix C = new ComplexMatrix(A.RowCount, A.ColumnCount);

            Complex[] AData = A.Data;
            Complex[] BData = B.Data;
            Complex[] CData = C.Data;

            for (int i = 0; i < AData.Length; i++)
            {
                CData[i] = AData[i] + BData[i];
            }

            return C;
        }


        #endregion

        #region Matrix-Matrix Subtraction

        ///// <summary>Matrix Subtraction</summary>
        /// <summary>
        /// Matrix subtraction.
        /// </summary>
        /// <param name="A"> The left side matrix of the subtraction operator.</param>
        /// <param name="B">The right side matrix of the subtraction operator.</param>
        /// <returns>A matrix that represents the result of the matrix subtraction.</returns>
        public static ComplexMatrix operator -(ComplexMatrix A, ComplexMatrix B)
        {
            if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount)
            {
                throw new System.ArgumentException("Matrix dimensions are not valid.");
            }

            ComplexMatrix C = new ComplexMatrix(A.RowCount, A.ColumnCount);

            Complex[] AData = A.Data;
            Complex[] BData = B.Data;
            Complex[] CData = C.Data;

            for (int i = 0; i < AData.Length; i++)
            {
                CData[i] = AData[i] - BData[i];
            }

            return C;
        }

        #endregion


        #region IMatrix<Complex> Members

        /// <summary>
        /// Copy all elements of this matrix to a rectangular 2D array.
        /// </summary>
        /// <returns>A rectangular 2D array.</</returns>
        public Complex[,] CopyToArray()
        {
            Complex[,] matrixData = new Complex[this._RowCount, this._ColumnCount];

            for (int j = 0; j < this._ColumnCount; j++)
            {
                for (int i = 0; i < this._RowCount; i++)
                {
                    matrixData[i, j] = this._Data[i + j * this._RowCount];
                }
            }

            return matrixData;
        }
        /// <summary>
        /// Copy all elements of this matrix to a jagged array.
        /// </summary>
        /// <returns>A jagged array.</returns>
        public Complex[][] CopyToJaggedArray()
        {
            Complex[][] newData = new Complex[this._RowCount][];
            for (int i = 0; i < this._RowCount; i++)
            {
                Complex[] row = new Complex[this._ColumnCount];
                for (int j = 0; j < this._ColumnCount; j++)
                {
                    row[j] = this._Data[i + j * this._RowCount];
                }

                newData[i] = row;
            }

            return newData;
        }

        #endregion
    }
}