示例#1
0
 // ********************************************************************
 // Multiply matrix 'm1' by 'm2' to give result in this
 //
 // Usage:  A.mult (A1, A2); multiply A1 by A2 giving A
 //
 // ********************************************************************
 public Matrix mult(Matrix m1, Matrix m2)
 {
     if (m1.c == m2.r)
     {
         resize(m1.r, m2.c);
         int m1_col = m1.c;
         for (int i = 0; i < r; i++)
         {
             for (int j = 0; j < m2.c; j++)
             {
                 var sum = new SimpleComplex(0.0, 0.0);
                 for (int k = 0; k < m1_col; k++)
                 {
                     sum = SimpleComplex.complexAdd(sum, SimpleComplex.complexMult(m1[i, k], m2[k, j]));
                 }
                 this[i, j] = sum;
             }
         }
         return(this);
     }
     else
     {
         throw new EMatrixSizeError("Incompatible matrix operands to multiply");
     }
 }
示例#2
0
        // ---------------------------------------------------------------------
        // Create an array from an array argument using the labels to label the matrix
        // ---------------------------------------------------------------------
        public Matrix(double[,] M, string[] rowNames, string[] columnNames)
        {
            r = M.GetLength(0);
            c = M.GetLength(1);

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
            {
                mxx[i] = new SimpleComplex[c];
            }
            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    mxx[i][j] = new SimpleComplex(M[i, j], 0.0);
                }
            }
            for (int i = 0; i < r; i++)
            {
                this.rowNames.Add(rowNames[i]);
            }
            for (int i = 0; i < c; i++)
            {
                this.columnNames.Add(columnNames[i]);
            }
        }
示例#3
0
        // ---------------------------------------------------------------------
        // Create a matrix from a jagged array argument
        // ---------------------------------------------------------------------
        public Matrix(double[][] M)
        {
            r = M.GetLength(0);
            if (r > 0)
            {
                c = M[0].GetLength(0);
            }
            else
            {
                c = 0;
            }

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
            {
                mxx[i] = new SimpleComplex[c];
            }
            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    mxx[i][j] = new SimpleComplex(M[i][j], 0.0);
                }
            }
            setDefaultLabels();
        }
示例#4
0
        public Matrix(Matrix cpy) : base()
        {
            r = cpy.r;
            c = cpy.c;

            mxx = new SimpleComplex[cpy.r][];
            for (int i = 0; i < cpy.r; i++)
            {
                mxx[i] = new SimpleComplex[cpy.c];
            }

            for (int i = 0; i < cpy.r; i++)
            {
                for (int j = 0; j < cpy.c; j++)
                {
                    mxx[i][j] = cpy.mxx[i][j];
                }
            }
            for (int i = 0; i < cpy.r; i++)
            {
                rowNames.Add(cpy.rowNames[i]);
            }
            for (int i = 0; i < cpy.c; i++)
            {
                columnNames.Add(cpy.columnNames[i]);
            }
        }
示例#5
0
 // ********************************************************************
 // Multiply the diagonal of the matrix 'm1' by complex number z to give
 // result in this
 //
 // Usage:  A.multDiag (A1, A2); multiply A1 by A2 giving A
 //
 // ********************************************************************
 public Matrix multDiag(Matrix m, SimpleComplex z)
 {
     for (int i = 0; i < m.r; i++)
     {
         this[i, i] = SimpleComplex.complexMult(m[i, i], z);
     }
     return(this);
 }
示例#6
0
        // ---------------------------------------------------------------------
        // Subtract a scalar value from a matrix
        // Usage:
        //   m.sub (3.1415);
        // ---------------------------------------------------------------------

        public void sub(double k)
        {
            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    this[i, j] = SimpleComplex.complexSub(this[i, j], new SimpleComplex(k, 0.0));
                }
            }
        }
示例#7
0
 // ********************************************************************
 // Multiply matrix 'm1' by complex number z to give result in this
 //
 // Usage:  A.mult (A1, z); multiply A1 by z giving A
 //
 // ********************************************************************
 public Matrix mult(Matrix m, SimpleComplex z)
 {
     for (int i = 0; i < m.r; i++)
     {
         for (int j = 0; j < m.c; j++)
         {
             this[i, j] = SimpleComplex.complexMult(m[i, j], z);
         }
     }
     return(this);
 }
示例#8
0
 // ********************************************************************
 // Multiply matrix 'm1' by scalar k to give result in Self
 //
 // Usage:  A.mult (A1, A2); multiply A1 by A2 giving A
 //
 // ********************************************************************
 public Matrix mult(Matrix m, double k)
 {
     for (int i = 0; i < m.r; i++)
     {
         for (int j = 0; j < m.c; j++)
         {
             this[i, j] = SimpleComplex.complexMult(m[i, j], new SimpleComplex(k, 0.0));
         }
     }
     return(this);
 }
示例#9
0
        // ---------------------------------------------------------------------
        // Statics for generating predefined matrix types
        // Usage:
        //   m = Matrix.Identity();
        // ---------------------------------------------------------------------

        public static Matrix Identity(int n)
        {
            var m = new Matrix(n, n);

            for (int i = 0; i < n; i++)
            {
                m[i, i] = new SimpleComplex(1.0, 0.0);
            }
            m.setDefaultLabels();
            return(m);
        }
示例#10
0
        // ---------------------------------------------------------------------
        // Subtract a scalar value from a matrix and return a new matrix
        // Usage:
        //   Matrix m1 = Matrix.sub (m2, 2.718);
        // ---------------------------------------------------------------------

        public static Matrix sub(Matrix x, double k)
        {
            var result = new Matrix(x.r, x.c);

            for (int i = 0; i < x.r; i++)
            {
                for (int j = 0; j < x.c; j++)
                {
                    result[i, j] = SimpleComplex.complexSub(x[i, j], new SimpleComplex(k, 0.0));
                }
            }
            return(result);
        }
示例#11
0
        // ---------------------------------------------------------------------
        // Swap rows, i and j
        // ---------------------------------------------------------------------
        public void swapRows(int r1, int r2)
        {
            var tr = new SimpleComplex[c];

            tr      = mxx[r1];
            mxx[r1] = mxx[r2];
            mxx[r2] = tr;

            var tmp = (string)rowNames[r1];

            rowNames[r1] = rowNames[r2];
            rowNames[r2] = tmp;
        }
示例#12
0
        // ---------------------------------------------------------------------
        // One of the main constructors. Creates an empty matrix of size r by c
        // Usage:
        //   Matrix m = new Matrix(4,4);
        // ---------------------------------------------------------------------
        // Worth checking for negative values?
        public Matrix(int r, int c)
            : base()
        {
            this.r = r;
            this.c = c;

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
            {
                mxx[i] = new SimpleComplex[c];
                for (int j = 0; j < c; j++)
                    mxx[i][j] = new SimpleComplex();
            }
            setDefaultLabels();
        }
示例#13
0
        // ---------------------------------------------------------------------
        // One of the main constructors. Creates an empty matrix of size r by c
        // Usage:
        //   Matrix m = new Matrix(4,4);
        // ---------------------------------------------------------------------

        // Worth checking for negative values?
        public Matrix(int r, int c) : base()
        {
            this.r = r;
            this.c = c;

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
            {
                mxx[i] = new SimpleComplex[c];
                for (int j = 0; j < c; j++)
                {
                    mxx[i][j] = new SimpleComplex();
                }
            }
            setDefaultLabels();
        }
示例#14
0
        // ---------------------------------------------------------------------
        // Subtract a matrix from this. this is modified
        // Usage:
        //   m.sub (m1);
        // ---------------------------------------------------------------------

        public void sub(Matrix x)
        {
            if (sameDimensions(this, x))
            {
                for (int i = 0; i < x.r; i++)
                {
                    for (int j = 0; j < x.c; j++)
                    {
                        this[i, j] = SimpleComplex.complexSub(this[i, j], x[i, j]);
                    }
                }
            }
            else
            {
                throw new EMatrixException("Matrices must be the same dimension to perform subtraction");
            }
        }
示例#15
0
        public static Matrix Random(int r, int c)
        {
            var m = new Matrix(r, c);

            m.name = "rm";
            var rnd = new Random();

            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    m[i, j] = new SimpleComplex(rnd.NextDouble(), 0.0);
                }
            }
            m.setDefaultLabels();
            return(m);
        }
示例#16
0
        // Sub Methods:

        // Methods that return a new matrix
        // m = Matrix.sub (m1, m2);
        // m = Matrix.sub (m1, 2.3);

        // Methods that modify the matrix object
        // m.sub (m1);
        // m.sub (2.3);


        // ---------------------------------------------------------------------
        // Subtract two matrices together and return the result as a new matrix
        // Usage:
        //   Matrix m3 = Matrix.sub (m1, m2);
        // ---------------------------------------------------------------------

        public static Matrix sub(Matrix x, Matrix y)
        {
            if (sameDimensions(x, y))
            {
                var result = new Matrix(x.r, x.c);
                for (int i = 0; i < x.r; i++)
                {
                    for (int j = 0; j < x.c; j++)
                    {
                        result[i, j] = SimpleComplex.complexSub(x[i, j], y[i, j]);
                    }
                }
                return(result);
            }
            else
            {
                throw new EMatrixException("Matrices must be the same dimension to perform addition");
            }
        }
示例#17
0
        // ---------------------------------------------------------------------
        // Create a matrix from an array argument
        // ---------------------------------------------------------------------
        public Matrix(double[,] M)
        {
            r = M.GetLength(0);
            c = M.GetLength(1);

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
            {
                mxx[i] = new SimpleComplex[c];
            }
            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    mxx[i][j] = new SimpleComplex(M[i, j], 0.0);
                }
            }
            setDefaultLabels();
        }
示例#18
0
        // ---------------------------------------------------------------------
        // Creates a matrix of size r by c filled with scalar value d
        // Usage:
        //   Matrix m = new Matrix (2,2,Math.Pi);
        // ---------------------------------------------------------------------

        public Matrix(int r, int c, double d) : base()
        {
            this.r = r;
            this.c = c;


            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
            {
                mxx[i] = new SimpleComplex[c];
            }
            setDefaultLabels();

            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    this[i, j] = new SimpleComplex(d, 0.0);
                }
            }
        }
示例#19
0
        public Matrix(Matrix cpy)
            : base()
        {
            r = cpy.r;
            c = cpy.c;

            mxx = new SimpleComplex[cpy.r][];
            for (int i = 0; i < cpy.r; i++)
                mxx[i] = new SimpleComplex[cpy.c];

            for (int i = 0; i < cpy.r; i++)
                for (int j = 0; j < cpy.c; j++)
                    mxx[i][j] = cpy.mxx[i][j];
            for (int i = 0; i < cpy.r; i++)
                rowNames.Add(cpy.rowNames[i]);
            for (int i = 0; i < cpy.c; i++)
                columnNames.Add(cpy.columnNames[i]);
        }
示例#20
0
        // ---------------------------------------------------------------------
        // Swap rows, i and j
        // ---------------------------------------------------------------------
        public void swapRows(int r1, int r2)
        {
            var tr = new SimpleComplex[c];

            tr = mxx[r1];
            mxx[r1] = mxx[r2];
            mxx[r2] = tr;

            var tmp = (string) rowNames[r1];
            rowNames[r1] = rowNames[r2];
            rowNames[r2] = tmp;
        }
示例#21
0
 // ********************************************************************
 // Multiply matrix 'm1' by 'm2' to give result in this
 //                                                                        
 // Usage:  A.mult (A1, A2); multiply A1 by A2 giving A
 //                                                                    
 // ********************************************************************
 public Matrix mult(Matrix m1, Matrix m2)
 {
     if (m1.c == m2.r)
     {
         resize(m1.r, m2.c);
         int m1_col = m1.c;
         for (int i = 0; i < r; i++)
             for (int j = 0; j < m2.c; j++)
             {
                 var sum = new SimpleComplex(0.0, 0.0);
                 for (int k = 0; k < m1_col; k++)
                     sum = SimpleComplex.complexAdd(sum, SimpleComplex.complexMult(m1[i, k], m2[k, j]));
                 this[i, j] = sum;
             }
         return this;
     }
     else
         throw new EMatrixSizeError("Incompatible matrix operands to multiply");
 }
示例#22
0
 // ********************************************************************
 // Multiply the diagonal of the matrix 'm1' by complex number z to give
 // result in this
 //                                                                        
 // Usage:  A.multDiag (A1, A2); multiply A1 by A2 giving A
 //                                                                    
 // ********************************************************************
 public Matrix multDiag(Matrix m, SimpleComplex z)
 {
     for (int i = 0; i < m.r; i++)
         this[i, i] = SimpleComplex.complexMult(m[i, i], z);
     return this;
 }
示例#23
0
 public static Matrix Random(int r, int c)
 {
     var m = new Matrix(r, c);
     m.name = "rm";
     var rnd = new Random();
     for (int i = 0; i < r; i++)
         for (int j = 0; j < c; j++)
             m[i, j] = new SimpleComplex(rnd.NextDouble(), 0.0);
     m.setDefaultLabels();
     return m;
 }
示例#24
0
 // ********************************************************************
 // Multiply matrix 'm1' by complex number z to give result in this
 //                                                                        
 // Usage:  A.mult (A1, z); multiply A1 by z giving A
 //                                                                    
 // ********************************************************************
 public Matrix mult(Matrix m, SimpleComplex z)
 {
     for (int i = 0; i < m.r; i++)
         for (int j = 0; j < m.c; j++)
             this[i, j] = SimpleComplex.complexMult(m[i, j], z);
     return this;
 }
示例#25
0
 // ---------------------------------------------------------------------
 // Statics for generating predefined matrix types
 // Usage:
 //   m = Matrix.Identity();
 // ---------------------------------------------------------------------
 public static Matrix Identity(int n)
 {
     var m = new Matrix(n, n);
     for (int i = 0; i < n; i++)
         m[i, i] = new SimpleComplex(1.0, 0.0);
     m.setDefaultLabels();
     return m;
 }
示例#26
0
        // ---------------------------------------------------------------------
        // Creates a matrix of size r by c filled with scalar value d
        // Usage:
        //   Matrix m = new Matrix (2,2,Math.Pi);
        // ---------------------------------------------------------------------
        public Matrix(int r, int c, double d)
            : base()
        {
            this.r = r;
            this.c = c;

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
                mxx[i] = new SimpleComplex[c];
            setDefaultLabels();

            for (int i = 0; i < r; i++)
                for (int j = 0; j < c; j++)
                    this[i, j] = new SimpleComplex(d, 0.0);
        }
示例#27
0
        // ---------------------------------------------------------------------
        // Create an array from an array argument using the labels to label the matrix
        // ---------------------------------------------------------------------
        public Matrix(double[,] M, string[] rowNames, string[] columnNames)
        {
            r = M.GetLength(0);
            c = M.GetLength(1);

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
                mxx[i] = new SimpleComplex[c];
            for (int i = 0; i < r; i++)
                for (int j = 0; j < c; j++)
                    mxx[i][j] = new SimpleComplex(M[i, j], 0.0);
            for (int i = 0; i < r; i++)
                this.rowNames.Add(rowNames[i]);
            for (int i = 0; i < c; i++)
                this.columnNames.Add(columnNames[i]);
        }
示例#28
0
        // ---------------------------------------------------------------------
        // Create a matrix from a jagged array argument
        // ---------------------------------------------------------------------
        public Matrix(double[][] M)
        {
            r = M.GetLength(0);
            if (r > 0)
                c = M[0].GetLength(0);
            else c = 0;

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
                mxx[i] = new SimpleComplex[c];
            for (int i = 0; i < r; i++)
                for (int j = 0; j < c; j++)
                    mxx[i][j] = new SimpleComplex(M[i][j], 0.0);
            setDefaultLabels();
        }
示例#29
0
        // ---------------------------------------------------------------------
        // Create a matrix from an array argument
        // ---------------------------------------------------------------------
        public Matrix(double[,] M)
        {
            r = M.GetLength(0);
            c = M.GetLength(1);

            mxx = new SimpleComplex[r][];
            for (int i = 0; i < r; i++)
                mxx[i] = new SimpleComplex[c];
            for (int i = 0; i < r; i++)
                for (int j = 0; j < c; j++)
                    mxx[i][j] = new SimpleComplex(M[i, j], 0.0);
            setDefaultLabels();
        }