private void BuildMatrices(Network network)
        {
            // Create the current measurement bus incidence matrix
            ComplexMatrix A = (new CurrentFlowMeasurementBusIncidenceMatrix(network)).Matrix;

            // Create the series admittance matrix
            ComplexMatrix Y = (new SeriesAdmittanceMatrix(network)).Matrix;

            // Create the shunt susceptance matrix
            ComplexMatrix Ys = (new LineShuntSusceptanceMatrix(network)).Matrix;

            // Compute the lower partition of the system matrix
            ComplexMatrix K = Y * A + Ys;

            List <int> rowsToBeRemoved         = RowsToRemove(network);
            int        numberOfRowsToBeRemoved = rowsToBeRemoved.Count();

            for (int i = 0; i < numberOfRowsToBeRemoved; i++)
            {
                int rowToRemove = rowsToBeRemoved.Max();
                K = MatrixCalculationExtensions.RemoveRow(K, rowToRemove);
                rowsToBeRemoved.Remove(rowToRemove);
            }

            List <int> columnsToBeRemoved         = ColumnsToRemove(network);
            int        numberOfColumnsToBeRemoved = columnsToBeRemoved.Count();

            for (int i = 0; i < numberOfColumnsToBeRemoved; i++)
            {
                int columnToRemove = columnsToBeRemoved.Max();
                K = MatrixCalculationExtensions.RemoveColumn(K, columnToRemove);
                columnsToBeRemoved.Remove(columnToRemove);
            }
        }
コード例 #2
0
        public void Main()
        {
            // Set matrix dimensions.
            int numberOfRows    = 3;
            int numberOfColumns = 2;

            // Create the data.
            var data = new Complex[6] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7)
            };

            // Assume the data as RowMajor ordered.
            StorageOrder storageOrder = StorageOrder.RowMajor;

            // Create the matrix.
            var matrix = ComplexMatrix.Dense(
                numberOfRows, numberOfColumns, data, storageOrder);

            Console.WriteLine("Assuming RowMajor ordered data.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Assume the data as ColMajor ordered.
            storageOrder = StorageOrder.ColumnMajor;

            // Create the matrix.
            matrix = ComplexMatrix.Dense(
                numberOfRows, numberOfColumns, data, storageOrder);
            Console.WriteLine("Assuming ColMajor ordered data.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
        }
コード例 #3
0
        public void Main()
        {
            // Set matrix dimensions.
            int numberOfRows    = 3;
            int numberOfColumns = 2;

            // Set the initial capacity of the sparse instance.
            int capacity = 0;

            // Create the matrix. All entries will be equal to zero.
            var matrix = ComplexMatrix.Sparse(
                numberOfRows, numberOfColumns, capacity);

            Console.WriteLine("Initially, each entry is equal to zero.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            Console.WriteLine();

            // Set some entries as non-zero values.
            // If needed, the initial capacity is automatically
            // incremented.
            matrix[0, 0] = new Complex(1, -1);
            matrix[2, 1] = new Complex(2, -2);

            Console.WriteLine("Updated data matrix:");
            Console.WriteLine(matrix);
        }
コード例 #4
0
        ///<inheritdoc cref="SingularValueDecomposition.Decompose(
        ///DoubleMatrix, out DoubleMatrix, out DoubleMatrix)"/>
        public static DoubleMatrix Decompose(
            ComplexMatrix matrix,
            out ComplexMatrix leftSingularVectors,
            out ComplexMatrix conjugateTransposedRightSingularVectors)
        {
            #region Input validation

            if (matrix is null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            #endregion

            int m       = matrix.NumberOfRows;
            int n       = matrix.NumberOfColumns;
            int min_m_n = m < n ? m : n;

            Complex[] a  = matrix.AsColumnMajorDenseArray();
            double[]  s  = new double[min_m_n];
            Complex[] u  = new Complex[m * m];
            Complex[] vt = new Complex[n * n];
            int       lapackInfo;
            double[]  superb = new double[min_m_n - 1];
            lapackInfo = SafeNativeMethods.LAPACK.ZGESVD(
                matrix_layout: SafeNativeMethods.LAPACK.ORDER.ColMajor,
                jobu: 'A',
                jobvt: 'A',
                m,
                n,
                a,
                lda: m,
                s,
                u,
                ldu: m,
                vt,
                ldvt: n,
                superb);

            if (lapackInfo > 0)
            {
                throw new InvalidOperationException(
                          ImplementationServices.GetResourceString(
                              "STR_EXCEPT_ALG_DOES_NOT_CONVERGE"));
            }

            DoubleMatrix values = DoubleMatrix.Dense(m, n);
            for (int i = 0; i < min_m_n; i++)
            {
                values[i, i] = s[i];
            }

            leftSingularVectors =
                ComplexMatrix.Dense(m, m, u, copyData: false);

            conjugateTransposedRightSingularVectors =
                ComplexMatrix.Dense(n, n, vt, copyData: false);

            return(values);
        }
コード例 #5
0
 public void ImTest()
 {
     Complex[] x        = { new Complex(1, 5), new Complex(2, -1), new Complex(-5, 1) };
     double[]  expected = { 5, -1, 1 };
     double[]  actual   = ComplexMatrix.Im(x);
     Assert.IsTrue(expected.IsEqual(actual));
 }
コード例 #6
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Initial data matrix:");
            Console.WriteLine(matrix);

            // Get the vectorization of the data matrix.
            var vectorized = matrix.Vec();

            Console.WriteLine();
            Console.WriteLine("Vectorized data matrix:");
            Console.WriteLine(vectorized);

            // Entries can also be vectorized using a read-only wrapper of the matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            Console.WriteLine();
            Console.WriteLine("Vectorized read-only data matrix :");
            Console.WriteLine(readOnlyMatrix.Vec());
        }
コード例 #7
0
ファイル: Matrix.Complex.cs プロジェクト: haf/Accord.Net
 public void AbsTest()
 {
     Complex[] x        = { new Complex(1, 5), new Complex(2, -1), new Complex(-5, 1) };
     Complex[] expected = { new Complex(Math.Sqrt(26), 0), new Complex(Math.Sqrt(5), 0), new Complex(Math.Sqrt(26), 0) };
     Complex[] actual   = ComplexMatrix.Abs(x);
     Assert.IsTrue(expected.IsEqual(actual, 1e-5));
 }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TestableDoubleMatrixComplexMatrixSubtraction{TExpected}"/> class.
 /// </summary>
 /// <param name="expected">The expected result or exception.</param>
 /// <param name="left">The left operand.</param>
 /// <param name="right">The right operand.</param>
 public TestableDoubleMatrixComplexMatrixSubtraction(
     TExpected expected,
     TestableDoubleMatrix left,
     TestableComplexMatrix right) :
     base(
         expected,
         left,
         right,
         leftWritableRightWritableOps:
         new Func <DoubleMatrix, ComplexMatrix, ComplexMatrix>[2] {
     (l, r) => l - r,
     (l, r) => ComplexMatrix.Subtract(l, r)
 },
         leftReadOnlyRightWritableOps:
         new Func <ReadOnlyDoubleMatrix, ComplexMatrix, ComplexMatrix>[2] {
     (l, r) => l - r,
     (l, r) => ComplexMatrix.Subtract(l, r)
 },
         leftWritableRightReadOnlyOps:
         new Func <DoubleMatrix, ReadOnlyComplexMatrix, ComplexMatrix>[2] {
     (l, r) => l - r,
     (l, r) => ReadOnlyComplexMatrix.Subtract(l, r)
 },
         leftReadOnlyRightReadOnlyOps:
         new Func <ReadOnlyDoubleMatrix, ReadOnlyComplexMatrix, ComplexMatrix>[2] {
     (l, r) => l - r,
     (l, r) => ReadOnlyComplexMatrix.Subtract(l, r)
 }
         )
 {
 }
コード例 #9
0
        //----------------------------------------------------------------------------------------------
        public ComplexMatrix GetInverseFourierTransform2D(ComplexMatrix fourierTransform2D)
        {
            bool isMatrixSizeCorrect =
                MathHelper.IsPowerOfTwo(fourierTransform2D.RowCount) &&
                MathHelper.IsPowerOfTwo(fourierTransform2D.ColumnCount);

            if (!isMatrixSizeCorrect)
            {
                throw new FourierTransformException();
            }

            ComplexMatrix resultMatrix =
                new ComplexMatrix(fourierTransform2D.RowCount, fourierTransform2D.ColumnCount);

            //Обработка строк
            for (int row = 0; row < fourierTransform2D.RowCount; row++)
            {
                Complex[] complexData             = fourierTransform2D.GetRow(row);
                Complex[] inverseFourierTransform = this.GetInverseFourierTransform(complexData);
                resultMatrix.SetRowData(row, inverseFourierTransform);
            }


            //Обработка столбцов
            for (int column = 0; column < fourierTransform2D.ColumnCount; column++)
            {
                Complex[] complexData             = resultMatrix.GetColumn(column);
                Complex[] inverseFourierTransform = this.GetInverseFourierTransform(complexData);
                resultMatrix.SetColumnData(column, inverseFourierTransform);
            }


            return(resultMatrix);
        }
コード例 #10
0
 public void AddQubits(Qubit[] qubits)
 {
     for (var i = 0; i < qubits.Length; i++)
     {
         this.matrix   = ComplexMatrix.TensorProduct(this.matrix, qubits[i].Matrix);
         this.register = new Qubit[this.register.Length + 1];
     }
 }
コード例 #11
0
        /// <summary>
        /// Determines if a row in the given matrix has the
        /// specified name.
        /// </summary>
        /// <param name="target">The matrix to test.</param>
        /// <param name="columnName">The name of the row.</param>
        /// <returns><c>true</c> if a row in the matrix has the
        /// specified name, <c>false</c> otherwise.</returns>
        public static bool RowNameExists(ComplexMatrix obj, string rowName)
        {
            var rowNames = (Dictionary <int, string>)Reflector.GetField(
                obj,
                "rowNames");

            return(rowNames.ContainsValue(rowName));
        }
コード例 #12
0
        /// <summary>
        /// Determines if a column in the given matrix has the
        /// specified name.
        /// </summary>
        /// <param name="target">The matrix to test.</param>
        /// <param name="columnName">The name of the column.</param>
        /// <returns><c>true</c> if a column in the matrix has the
        /// specified name, <c>false</c> otherwise.</returns>
        public static bool ColumnNameExists(ComplexMatrix target, string columnName)
        {
            var columnNames = (Dictionary <int, string>)Reflector.GetField(
                target,
                "columnNames");

            return(columnNames.ContainsValue(columnName));
        }
コード例 #13
0
 public Gate(ComplexMatrix matrix)
 {
     if (!matrix.IsUnitary)
     {
         throw new GateConstraintException(GateConstraintException.UNITARY_MESSAGE);
     }
     this.matrix = matrix;
 }
コード例 #14
0
ファイル: Matrix.Complex.cs プロジェクト: sami1971/framework
        public void ReTest()
        {
            Complex[] x        = { new Complex(1, 5), new Complex(2, -1), new Complex(-5, 1) };
            double[]  expected = { 1, 2, -5 };
            double[]  actual   = ComplexMatrix.Re(x);

            Assert.IsTrue(expected.IsEqual(actual));
        }
コード例 #15
0
ファイル: Matrix.Complex.cs プロジェクト: sami1971/framework
        public void SumTest()
        {
            Complex[] x        = { new Complex(1, 5), new Complex(2, -1), new Complex(-5, 1) };
            Complex   expected = new Complex(-2, 5);
            Complex   actual   = ComplexMatrix.Sum(x);

            Assert.AreEqual(expected, actual);
        }
コード例 #16
0
ファイル: Matrix.Complex.cs プロジェクト: sami1971/framework
        public void MagnitudeTest()
        {
            Complex[] x        = { new Complex(1, 5), new Complex(2, -1), new Complex(-5, 1) };
            double[]  expected = { Math.Sqrt(26), Math.Sqrt(5), Math.Sqrt(26) };
            double[]  actual   = ComplexMatrix.Magnitude(x);

            Assert.IsTrue(expected.IsEqual(actual));
        }
コード例 #17
0
ファイル: ComplexMatrix.cs プロジェクト: JesusFreke/didjimp
 public static ComplexMatrix IdentityMatrix()
 {
     ComplexMatrix identity = new ComplexMatrix();
     identity[0, 0] = (Complex)1;
     identity[1, 0] = (Complex)0;
     identity[0, 1] = (Complex)0;
     identity[1, 1] = (Complex)1;
     return identity;
 }
コード例 #18
0
        public void ApplyGate(Gate gate)
        {
            this.matrix = gate.Matrix * this.matrix;

            for (var i = 0; i < this.register.Length; i++)
            {
                this.register[i] = null;
            }
        }
コード例 #19
0
        public void TestComplexMatrix_Setup()
        {
            /*
            MATLAB:
            ma3x2 = [1 -2;-1 4;5 7]
            mb3x2 = [10 2.5;-3 -1.5;19 -6]
            mc2x2 = [1 2;3 4]
            md2x4 = [1 2 -3 12;3 3.1 4 2]
            ra3x2 = ma3x2 + 2
            rb3x2 = mb3x2 - 1
            rc2x2 = mc2x2 + 5
            rd2x4 = md2x4 * 2
            ia3x2 = (ra3x2 * 2) * j
            ib3x2 = (rb3x2 * 3 + 1) * j
            ic2x2 = (rc2x2 + 2) * j
            id2x4 = (rd2x4 - 5) * j
            ca3x2 = 2*ra3x2 - 2*ia3x2
            cb3x2 = rb3x2 + 3*ib3x2
            cc2x2 = rc2x2 + 2 - 3*ic2x2
            cd2x4 = -2*rd2x4 + id2x4 + 1-j
            v2 = [5 -2]
            cv2 = [5+j, -2+3j]
            */

            ma3x2 = new Matrix(new double[][] {
                new double[] { 1, -2 },
                new double[] { -1, 4 },
                new double[] { 5, 7 }});
            mb3x2 = new Matrix(new double[][] {
                new double[] { 10, 2.5 },
                new double[] { -3, -1.5 },
                new double[] { 19, -6 }});
            mc2x2 = new Matrix(new double[][] {
                new double[] { 1, 2 },
                new double[] { 3, 4 }});
            md2x4 = new Matrix(new double[][] {
                new double[] { 1, 2, -3, 12 },
                new double[] { 3, 3.1, 4, 2 }});

            ra3x2 = ComplexMatrix.Create(ma3x2) + 2;
            rb3x2 = ComplexMatrix.Create(mb3x2) - 1;
            rc2x2 = ComplexMatrix.Create(mc2x2) + 5;
            rd2x4 = ComplexMatrix.Create(md2x4) * 2;

            ia3x2 = (ra3x2 * 2) * j;
            ib3x2 = (rb3x2 * 3 + 1) * j;
            ic2x2 = (rc2x2 + 2) * j;
            id2x4 = (rd2x4 - 5) * j;

            ca3x2 = 2 * ra3x2 - 2 * ia3x2;
            cb3x2 = rb3x2 + 3 * ib3x2;
            cc2x2 = rc2x2 + 2 - 3 * ic2x2;
            cd2x4 = -2 * rd2x4 + id2x4 + (1 - j);

            v2 = new Vector(new double[] { 5, -2 });
            cv2 = new ComplexVector(new Complex[] { 5 + j, -2 + 3 * j });
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: shabegger/quantum-emulate
        static void InvertPhase(Register register, ComplexMatrix fn)
        {
            register.SetQubitValue(3, true);

            var gate = (new IdentityGate(3)).Combine(new HadamardGate())
                       .Compose(Gate.FromFunction(fn));

            register.ApplyGate(gate);
        }
コード例 #21
0
        public void TestSerializeComplexMatrix()
        {
            ComplexMatrix before = ComplexMatrix.Random(3, 5, _random);
            ComplexMatrix after  = SerializeDeserialize(before);

            Assert.That(after, Is.Not.Null, "Not Null");
            Assert.That(after, Is.EqualTo(before), "Equal");
            Assert.That(after, NumericIs.AlmostEqualTo(before), "Almost Equal");
        }
コード例 #22
0
ファイル: Matrix.Complex.cs プロジェクト: sami1971/framework
        public void MultiplyTest()
        {
            Complex[] a        = { new Complex(7, 5), new Complex(2, -3), new Complex(-5, 1) };
            Complex[] b        = { new Complex(1, 5), new Complex(8, -1), new Complex(-4, 8) };
            Complex[] expected = { new Complex(-18, 40), new Complex(13, -26), new Complex(12, -44) };
            Complex[] actual   = ComplexMatrix.Multiply(a, b);

            Assert.IsTrue(expected.IsEqual(actual));
        }
コード例 #23
0
        /// <summary>
        /// Computes eigenvalues and eigenvectors of the
        /// specified Hermitian complex matrix.
        /// </summary>
        /// <param name="matrix">
        /// The matrix containing the lower or upper triangular part of the matrix
        /// whose spectral decomposition must be computed.
        /// </param>
        /// <param name="lowerTriangularPart">
        /// <c>true</c> if <paramref name="matrix"/> contains the lower
        /// triangular part of the matrix to be decomposed;
        /// <c>false</c> if <paramref name="matrix"/> contains
        /// its upper triangular part.
        /// </param>
        /// <param name="eigenvectors">
        /// A matrix whose columns represent the eigenvectors
        /// of the decomposed matrix.
        /// </param>
        /// <returns>
        /// A diagonal matrix containing the eigenvalues
        /// of the decomposed matrix, in ascending order.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="matrix"/> is <b>null</b>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="matrix"/> is not square.
        /// </exception>
        public static DoubleMatrix Decompose(
            ComplexMatrix matrix,
            bool lowerTriangularPart,
            out ComplexMatrix eigenvectors)
        {
            #region Input validation

            if (matrix is null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            if (!matrix.IsSquare)
            {
                throw new ArgumentException(
                          message: ImplementationServices.GetResourceString(
                              "STR_EXCEPT_PAR_MUST_BE_SQUARE"),
                          paramName: nameof(matrix));
            }

            #endregion

            int       m            = matrix.NumberOfRows;
            double[]  valuesArray  = new double[m];
            Complex[] vectorsArray = matrix.AsColumnMajorDenseArray();

            int info = SafeNativeMethods.LAPACK.ZHEEV(
                matrix_layout: SafeNativeMethods.LAPACK.ORDER.ColMajor,
                jobz: 'V',
                uplo: lowerTriangularPart ? 'L' : 'U',
                n: m,
                a: vectorsArray,
                lda: m,
                w: valuesArray);

            if (info > 0)
            {
                throw new InvalidOperationException(
                          message: ImplementationServices.GetResourceString(
                              "STR_EXCEPT_ALG_DOES_NOT_CONVERGE"));
            }

            DoubleMatrix eigenvalues = DoubleMatrix.Sparse(m, m, m);
            for (int i = 0; i < m; i++)
            {
                eigenvalues[i, i] = valuesArray[i];
            }

            eigenvectors =
                ComplexMatrix.Dense(m, m, vectorsArray, copyData: false);

            Debug.Assert(eigenvalues != null);
            Debug.Assert(eigenvectors != null);

            return(eigenvalues);
        }
コード例 #24
0
        public void PhaseTest()
        {
            // TODO: Reenable this test when AForge is updated to version 2.2.4.
            Complex[] x        = { new Complex(0, 5), new Complex(2, 0), new Complex(-5, 1) };
            double[]  expected = { 1, Math.Sqrt(5), Math.Sqrt(26) };
            double[]  actual   = ComplexMatrix.Phase(x);

            //for (int i = 0; i < x.Length; i++)
            //    Assert.AreEqual(x[i].Phase, Math.Atan2(x[i].Im, x[i].Re));
        }
コード例 #25
0
        /// <summary>
        /// Appliy the gate to the one or more qubit
        /// </summary>
        /// <param name="qubits">The qubits.</param>
        public override void Apply(params Qubit[] qubits)
        {
            var reg    = new QuantumRegister(qubits);
            var result = QuantumRegister.GetQubits(ComplexMatrix.Multiply(Matrix, reg.BitRegister));

            for (int i = 0; i < qubits.Length; i++)
            {
                qubits[i].BitRegister = result[i].BitRegister;
            }
        }
コード例 #26
0
 /// <summary>
 /// Sets the column names of the specified matrix.
 /// </summary>
 /// <param name="matrix">The matrix.</param>
 /// <param name="columnNames">The column names.</param>
 static void SetColumnNames(ComplexMatrix matrix, Dictionary <int, string> columnNames)
 {
     if (columnNames != null)
     {
         foreach (var pair in columnNames)
         {
             matrix.SetColumnName(pair.Key, pair.Value);
         }
     }
 }
コード例 #27
0
ファイル: ComplexVector.cs プロジェクト: sergioariza/PFC
        ToColumnMatrix()
        {
            Complex[][] m = ComplexMatrix.CreateMatrixData(_length, 1);
            for (int i = 0; i < _data.Length; i++)
            {
                m[i][0] = _data[i];
            }

            return(new ComplexMatrix(m));
        }
コード例 #28
0
 /// <summary>
 /// Sets the row names of the specified matrix.
 /// </summary>
 /// <param name="matrix">The matrix.</param>
 /// <param name="rowNames">The row names.</param>
 static void SetRowNames(ComplexMatrix matrix, Dictionary <int, string> rowNames)
 {
     if (rowNames != null)
     {
         foreach (var pair in rowNames)
         {
             matrix.SetRowName(pair.Key, pair.Value);
         }
     }
 }
コード例 #29
0
ファイル: ComplexVector.cs プロジェクト: sergioariza/PFC
        ToRowMatrix()
        {
            Complex[][] m    = ComplexMatrix.CreateMatrixData(1, _length);
            Complex[]   mRow = m[0];
            for (int i = 0; i < _data.Length; i++)
            {
                mRow[i] = _data[i];
            }

            return(new ComplexMatrix(m));
        }
コード例 #30
0
        //----------------------------------------------------------------------------------------------
        //Быстрое центрированное двумерное преобразование Фурье
        public ComplexMatrix GetCenteredFourierTransform2D(RealMatrix matrix)
        {
            RealMatrix    newMatrix    = this.GetMatrixForCenteredFourierTransform(matrix);
            ComplexMatrix resultMatrix = this.GetFourierTransform2D(newMatrix);

            //ComplexMatrix resultMatrix = this.GetCudaFourierTransform2D(matrix);

            //ComplexMatrix resultMatrix = this.GetCudaFourierTransform2D(newMatrix);

            return(resultMatrix);
        }
コード例 #31
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Initial data matrix:");
            Console.WriteLine(matrix);

            // Specify all row indexes.
            var rowIndexes = ":";

            Console.WriteLine();
            Console.WriteLine("Row indexes: from 0 to {0}", matrix.NumberOfRows - 1);

            // Specify all column indexes.
            var columnIndexes = ":";

            Console.WriteLine();
            Console.WriteLine("Column indexes: from 0 to {0}", matrix.NumberOfColumns - 1);

            // Specify the value matrix.
            var valueData = new Complex[8] {
                new Complex(10, -10), new Complex(50, -50),
                new Complex(20, -20), new Complex(60, -60),
                new Complex(30, -30), new Complex(70, -70),
                new Complex(40, -40), new Complex(80, -80)
            };
            var value = ComplexMatrix.Dense(4, 2, valueData, StorageOrder.RowMajor);

            Console.WriteLine();
            Console.WriteLine("Value matrix:");
            Console.WriteLine(value);

            // Set the entries having the specified indexes to the value matrix.
            matrix[rowIndexes, columnIndexes] = value;

            Console.WriteLine();
            Console.WriteLine("Updated data matrix:");
            Console.WriteLine(matrix);

            // Entries can also be accessed using a read-only wrapper
            // of the matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            Console.WriteLine();
            Console.WriteLine("Updated matrix entries:");
            Console.WriteLine(readOnlyMatrix[rowIndexes, columnIndexes]);
        }
コード例 #32
0
ファイル: Matrix.Complex.cs プロジェクト: sami1971/framework
        public void PhaseTest()
        {
            Complex[] x        = { new Complex(0, 5), new Complex(2, 0), new Complex(-5, 1) };
            double[]  expected = { 1, Math.Sqrt(5), Math.Sqrt(26) };
            double[]  actual   = ComplexMatrix.Phase(x);

            for (int i = 0; i < x.Length; i++)
            {
                Assert.AreEqual(x[i].Phase, Math.Atan2(x[i].Im, x[i].Re));
            }
        }
コード例 #33
0
ファイル: ComplexMatrix.cs プロジェクト: JesusFreke/didjimp
        public static ComplexMatrix operator *(ComplexMatrix m1, ComplexMatrix m2)
        {
            ComplexMatrix product = new ComplexMatrix();

            product[0, 0] = m1[0, 0] * m2[0, 0] + m1[0, 1] * m2[1, 0];
            product[1, 0] = m1[1, 0] * m2[0, 0] + m1[1, 1] * m2[1, 0];
            product[0, 1] = m1[0, 0] * m2[0, 1] + m1[0, 1] * m2[1, 1];
            product[1, 1] = m1[1, 0] * m2[0, 1] + m1[1, 1] * m2[1, 1];

            return product;
        }
コード例 #34
0
        public void ComplexMatrixAddition()
        {
            /*
            MATLAB:
            sum_cc = ca3x2 + cb3x2
            diff_cc = ca3x2 - cb3x2
            sum_cm = ca3x2 + mb3x2
            diff_cm = ca3x2 - mb3x2
            sum_cs = ca3x2 + s
            diff_cs = ca3x2 - s
            neg_c = -ca3x2
            */

            // ComplexMatrix + ComplexMatrix
            ComplexMatrix sumCmCm = new ComplexMatrix(new Complex[][] {
                new Complex[] { 15+(72*j), 1.5+(16.5*j) },
                new Complex[] { -2-(37*j), 9.5-(43.5*j) },
                new Complex[] { 32+(137*j), 11-(96*j) }
                });

            Assert.That(_ca3X2 + _cb3X2, NumericIs.AlmostEqualTo(sumCmCm), "sum cc 1");
            ComplexMatrix sumCmCmInplace = _ca3X2.Clone();
            Assert.That(sumCmCmInplace.Add(_cb3X2), NumericIs.AlmostEqualTo(sumCmCm), "sum cc 2");
            sumCmCmInplace.AddInplace(_cb3X2);
            Assert.That(sumCmCmInplace, NumericIs.AlmostEqualTo(sumCmCm), "sum cc 3");

            // ComplexMatrix - ComplexMatrix
            ComplexMatrix diffCmCm = new ComplexMatrix(new Complex[][] {
                new Complex[] { -3-(96*j), -1.5-(16.5*j) },
                new Complex[] { 6+(29*j), 14.5-(4.5*j) },
                new Complex[] { -4-(193*j), 25+(24*j) }
                });

            Assert.That(_ca3X2 - _cb3X2, NumericIs.AlmostEqualTo(diffCmCm), "diff cc 1");
            ComplexMatrix diffCmCmInplace = _ca3X2.Clone();
            Assert.That(diffCmCmInplace.Subtract(_cb3X2), NumericIs.AlmostEqualTo(diffCmCm), "diff cc 2");
            diffCmCmInplace.SubtractInplace(_cb3X2);
            Assert.That(diffCmCmInplace, NumericIs.AlmostEqualTo(diffCmCm), "diff cc 3");

            // ComplexMatrix + Matrix
            ComplexMatrix sumCmM = new ComplexMatrix(new Complex[][] {
                new Complex[] { 16-(12*j), 2.5 },
                new Complex[] { -1-(4*j), 10.5-(24*j) },
                new Complex[] { 33-(28*j), 12-(36*j) }
                });

            Assert.That(_ca3X2 + _mb3X2, NumericIs.AlmostEqualTo(sumCmM), "sum cm 1");
            ComplexMatrix sumCmMInplace = _ca3X2.Clone();
            Assert.That(sumCmMInplace.Add(_mb3X2), NumericIs.AlmostEqualTo(sumCmM), "sum cm 2");
            sumCmMInplace.AddInplace(_mb3X2);
            Assert.That(sumCmMInplace, NumericIs.AlmostEqualTo(sumCmM), "sum cm 3");

            // ComplexMatrix - Matrix
            ComplexMatrix diffCmM = new ComplexMatrix(new Complex[][] {
                new Complex[] { -4-(12*j), -2.5 },
                new Complex[] { 5-(4*j), 13.5-(24*j) },
                new Complex[] { -5-(28*j), 24-(36*j) }
                });

            Assert.That(_ca3X2 - _mb3X2, NumericIs.AlmostEqualTo(diffCmM), "diff cm 1");
            ComplexMatrix diffCmMInplace = _ca3X2.Clone();
            Assert.That(diffCmMInplace.Subtract(_mb3X2), NumericIs.AlmostEqualTo(diffCmM), "diff cm 2");
            diffCmMInplace.SubtractInplace(_mb3X2);
            Assert.That(diffCmMInplace, NumericIs.AlmostEqualTo(diffCmM), "diff cm 3");

            // ComplexMatrix + Complex
            ComplexMatrix sumCmC = new ComplexMatrix(new Complex[][] {
                new Complex[] { 7-(11*j), 1+j },
                new Complex[] { 3-(3*j), 13-(23*j) },
                new Complex[] { 15-(27*j), 19-(35*j) }
                });

            Assert.That(_ca3X2 + _s, NumericIs.AlmostEqualTo(sumCmC), "sum cs 1");
            ComplexMatrix sumCmCInplace = _ca3X2.Clone();
            Assert.That(sumCmCInplace.Add(_s), NumericIs.AlmostEqualTo(sumCmC), "sum cs 2");
            sumCmCInplace.AddInplace(_s);
            Assert.That(sumCmCInplace, NumericIs.AlmostEqualTo(sumCmC), "sum cs 3");

            // ComplexMatrix - Complex
            ComplexMatrix diffCmC = new ComplexMatrix(new Complex[][] {
                new Complex[] { 5-(13*j), -1-j },
                new Complex[] { 1-(5*j), 11-(25*j) },
                new Complex[] { 13-(29*j), 17-(37*j) }
                });

            Assert.That(_ca3X2 - _s, NumericIs.AlmostEqualTo(diffCmC), "diff cs 1");
            ComplexMatrix diffCmCInplace = _ca3X2.Clone();
            Assert.That(diffCmCInplace.Subtract(_s), NumericIs.AlmostEqualTo(diffCmC), "diff cs 2");
            diffCmCInplace.SubtractInplace(_s);
            Assert.That(diffCmCInplace, NumericIs.AlmostEqualTo(diffCmC), "diff cs 3");

            // ComplexMatrix Negate
            ComplexMatrix negateCm = new ComplexMatrix(new Complex[][] {
                new Complex[] { -6+(12*j), 0 },
                new Complex[] { -2+(4*j), -12+(24*j) },
                new Complex[] { -14+(28*j), -18+(36*j) }
                });

            Assert.That(-_ca3X2, NumericIs.AlmostEqualTo(negateCm), "neg c 1");
            ComplexMatrix negCmInplace = _ca3X2.Clone();
            Assert.That(negCmInplace.Negate(), NumericIs.AlmostEqualTo(negateCm), "neg c 2");
            negCmInplace.NegateInplace();
            Assert.That(negCmInplace, NumericIs.AlmostEqualTo(negateCm), "neg c 3");
        }
コード例 #35
0
 public ComplexMatrix TensorProduct(ComplexMatrix other)
 {
     var s = this;
     return FromColumns(
         from c1 in s.Columns
         from c2 in other.Columns
         select from r1 in c1
                from r2 in c2
                select r1 * r2);
 }
コード例 #36
0
ファイル: MatrixView.cs プロジェクト: davidsiaw/neuron
        /// <summary>
        /// Sets the matrix to show.
        /// </summary>
        /// <param name="matrix">A real matrix.</param>
        public void Matrix(BaseMatrix matrix)
        {
            this._RealMatrix = matrix;
            this._IsRealMatrix = true;

            this._ComplexMatrix = new ComplexMatrix(1);

            this.ShowMatrix();
        }
コード例 #37
0
        /// <summary>
        /// Converts the specified expression to a ComplexMatrix.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The ComplexMatrix. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static ComplexMatrix AsComplexMatrix(this SymbolicExpression expression)
        {
            if (!expression.IsVector())
            {
                return null;
            }

            int rowCount = 0;
            int columnCount = 0;

            if (expression.IsMatrix())
            {
                if (expression.Type == SymbolicExpressionType.ComplexVector)
                {
                    return new ComplexMatrix(expression.Engine, expression.DangerousGetHandle());
                }
                else
                {
                    rowCount = expression.GetFunction<Rf_nrows>()(expression.DangerousGetHandle());
                    columnCount = expression.GetFunction<Rf_ncols>()(expression.DangerousGetHandle());
                }
            }

            if (columnCount == 0)
            {
                rowCount = expression.GetFunction<Rf_length>()(expression.DangerousGetHandle());
                columnCount = 1;
            }

            IntPtr coerced = expression.GetFunction<Rf_coerceVector>()(expression.DangerousGetHandle(), SymbolicExpressionType.ComplexVector);
            var dim = new IntegerVector(expression.Engine, new[] { rowCount, columnCount });
            SymbolicExpression dimSymbol = expression.Engine.GetPredefinedSymbol("R_DimSymbol");
            var matrix = new ComplexMatrix(expression.Engine, coerced);
            matrix.SetAttribute(dimSymbol, dim);
            return matrix;
        }
コード例 #38
0
        public void TestComplexMatrix_AdditiveTranspose()
        {
            /*
            MATLAB:
            sum_cc = ca3x2 + cb3x2
            diff_cc = ca3x2 - cb3x2
            sum_cm = ca3x2 + mb3x2
            diff_cm = ca3x2 - mb3x2
            sum_cs = ca3x2 + s
            diff_cs = ca3x2 - s
            neg_c = -ca3x2
            conj_c = conj(ca3x2)
            trans_c = ca3x2.'
            htrans_c = ca3x2'
            trans_c2 = cc2x2.'
            htrans_c2 = cc2x2'
            */

            // ComplexMatrix + ComplexMatrix
            ComplexMatrix sum_cc = new ComplexMatrix(new Complex[][] {
                new Complex[] { 15+72*j, 1.5+16.5*j },
                new Complex[] { -2-37*j, 9.5-43.5*j },
                new Complex[] { 32+137*j, 11-96*j }});
            NumericAssert.AreAlmostEqual(sum_cc, ca3x2 + cb3x2, "sum cc 1");
            ComplexMatrix sum_cc_inplace = ca3x2.Clone();
            NumericAssert.AreAlmostEqual(sum_cc, sum_cc_inplace.Add(cb3x2), "sum cc 2");
            sum_cc_inplace.AddInplace(cb3x2);
            NumericAssert.AreAlmostEqual(sum_cc, sum_cc_inplace, "sum cc 3");

            // ComplexMatrix - ComplexMatrix
            ComplexMatrix diff_cc = new ComplexMatrix(new Complex[][] {
                new Complex[] { -3-96*j, -1.5-16.5*j },
                new Complex[] { 6+29*j, 14.5-4.5*j },
                new Complex[] { -4-193*j, 25+24*j }});
            NumericAssert.AreAlmostEqual(diff_cc, ca3x2 - cb3x2, "diff cc 1");
            ComplexMatrix diff_cc_inplace = ca3x2.Clone();
            NumericAssert.AreAlmostEqual(diff_cc, diff_cc_inplace.Subtract(cb3x2), "diff cc 2");
            diff_cc_inplace.SubtractInplace(cb3x2);
            NumericAssert.AreAlmostEqual(diff_cc, diff_cc_inplace, "diff cc 3");

            // ComplexMatrix + Matrix
            ComplexMatrix sum_cm = new ComplexMatrix(new Complex[][] {
                new Complex[] { 16-12*j, 2.5 },
                new Complex[] { -1-4*j, 10.5-24*j },
                new Complex[] { 33-28*j, 12-36*j }});
            NumericAssert.AreAlmostEqual(sum_cm, ca3x2 + mb3x2, "sum cm 1");
            ComplexMatrix sum_cm_inplace = ca3x2.Clone();
            NumericAssert.AreAlmostEqual(sum_cm, sum_cm_inplace.Add(mb3x2), "sum cm 2");
            sum_cm_inplace.AddInplace(mb3x2);
            NumericAssert.AreAlmostEqual(sum_cm, sum_cm_inplace, "sum cm 3");

            // ComplexMatrix - Matrix
            ComplexMatrix diff_cm = new ComplexMatrix(new Complex[][] {
                new Complex[] { -4-12*j, -2.5 },
                new Complex[] { 5-4*j, 13.5-24*j },
                new Complex[] { -5-28*j, 24-36*j }});
            NumericAssert.AreAlmostEqual(diff_cm, ca3x2 - mb3x2, "diff cm 1");
            ComplexMatrix diff_cm_inplace = ca3x2.Clone();
            NumericAssert.AreAlmostEqual(diff_cm, diff_cm_inplace.Subtract(mb3x2), "diff cm 2");
            diff_cm_inplace.SubtractInplace(mb3x2);
            NumericAssert.AreAlmostEqual(diff_cm, diff_cm_inplace, "diff cm 3");

            // ComplexMatrix + Complex
            ComplexMatrix sum_cs = new ComplexMatrix(new Complex[][] {
                new Complex[] { 7-11*j, 1+j },
                new Complex[] { 3-3*j, 13-23*j },
                new Complex[] { 15-27*j, 19-35*j }});
            NumericAssert.AreAlmostEqual(sum_cs, ca3x2 + s, "sum cs 1");
            ComplexMatrix sum_cs_inplace = ca3x2.Clone();
            NumericAssert.AreAlmostEqual(sum_cs, sum_cs_inplace.Add(s), "sum cs 2");
            sum_cs_inplace.AddInplace(s);
            NumericAssert.AreAlmostEqual(sum_cs, sum_cs_inplace, "sum cs 3");

            // ComplexMatrix - Complex
            ComplexMatrix diff_cs = new ComplexMatrix(new Complex[][] {
                new Complex[] { 5-13*j, -1-j },
                new Complex[] { 1-5*j, 11-25*j },
                new Complex[] { 13-29*j, 17-37*j }});
            NumericAssert.AreAlmostEqual(diff_cs, ca3x2 - s, "diff cs 1");
            ComplexMatrix diff_cs_inplace = ca3x2.Clone();
            NumericAssert.AreAlmostEqual(diff_cs, diff_cs_inplace.Subtract(s), "diff cs 2");
            diff_cs_inplace.SubtractInplace(s);
            NumericAssert.AreAlmostEqual(diff_cs, diff_cs_inplace, "diff cs 3");

            // ComplexMatrix Negate
            ComplexMatrix neg_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { -6+12*j, 0 },
                new Complex[] { -2+4*j, -12+24*j },
                new Complex[] { -14+28*j, -18+36*j }});
            NumericAssert.AreAlmostEqual(neg_c, -ca3x2, "neg c 1");
            ComplexMatrix neg_c_inplace = ca3x2.Clone();
            NumericAssert.AreAlmostEqual(neg_c, neg_c_inplace.Negate(), "neg c 2");
            neg_c_inplace.NegateInplace();
            NumericAssert.AreAlmostEqual(neg_c, neg_c_inplace, "neg c 3");

            // ComplexMatrix Conjugate
            ComplexMatrix conj_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6+12*j, 0 },
                new Complex[] { 2+4*j, 12+24*j },
                new Complex[] { 14+28*j, 18+36*j }});
            NumericAssert.AreAlmostEqual(conj_c, ca3x2.Conjugate(), "conj c 1");
            ComplexMatrix conj_c_inplace = ca3x2.Clone();
            conj_c_inplace.ConjugateInplace();
            NumericAssert.AreAlmostEqual(conj_c, conj_c_inplace, "conj c 2");

            // ComplexMatrix Transpose (Non-Conjugated)
            ComplexMatrix trans_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6-12*j, 2-4*j, 14-28*j },
                new Complex[] { 0, 12-24*j, 18-36*j }});
            NumericAssert.AreAlmostEqual(trans_c, ca3x2.Transpose(), "trans c 1");

            // ComplexMatrix Hermitian Transpose (Conjugated)
            ComplexMatrix htrans_c = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6+12*j, 2+4*j, 14+28*j },
                new Complex[] { 0, 12+24*j, 18+36*j }});
            NumericAssert.AreAlmostEqual(htrans_c, ca3x2.HermitianTranspose(), "htrans c 1");

            // ComplexMatrix Transpose (Non-Conjugated) (Square)
            ComplexMatrix trans_c2 = new ComplexMatrix(new Complex[][] {
                new Complex[] { 8-24*j, 10-30*j },
                new Complex[] { 9-27*j, 11-33*j }});
            NumericAssert.AreAlmostEqual(trans_c2, cc2x2.Transpose(), "trans c2 1");
            ComplexMatrix trans_c2_inplace = cc2x2.Clone();
            trans_c2_inplace.TransposeInplace();
            NumericAssert.AreAlmostEqual(trans_c2, trans_c2_inplace, "trans c2 2");

            // ComplexMatrix Hermitian Transpose (Conjugated) (Square)
            ComplexMatrix htrans_c2 = new ComplexMatrix(new Complex[][] {
                new Complex[] { 8+24*j, 10+30*j },
                new Complex[] { 9+27*j, 11+33*j }});
            NumericAssert.AreAlmostEqual(htrans_c2, cc2x2.HermitianTranspose(), "htrans c2 1");
            ComplexMatrix htrans_c2_inplace = cc2x2.Clone();
            htrans_c2_inplace.HermitianTransposeInplace();
            NumericAssert.AreAlmostEqual(htrans_c2, htrans_c2_inplace, "htrans c2 2");

        }
コード例 #39
0
        public void ComplexMatrixConjugate()
        {
            // MATLAB: conj_c = conj(ca3x2)
            ComplexMatrix u = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6+(12*j), 0 },
                new Complex[] { 2+(4*j), 12+(24*j) },
                new Complex[] { 14+(28*j), 18+(36*j) }
                });

            Assert.That(_ca3X2.Conjugate(), NumericIs.AlmostEqualTo(u), "conj c 1");
            Assert.That(_ca3X2.Conjugate(), Is.Not.SameAs(_ca3X2));
            Assert.That(_ca3X2.Conjugate().GetArray(), Is.Not.SameAs(_ca3X2.GetArray()));

            ComplexMatrix uInplace = _ca3X2.Clone();
            Complex[][] internalArray = uInplace.GetArray();
            uInplace.ConjugateInplace();
            Assert.That(uInplace, NumericIs.AlmostEqualTo(u), "conj c 2");
            Assert.That(internalArray, Is.Not.SameAs(_ca3X2.GetArray()));
            Assert.That(internalArray, Is.SameAs(uInplace.GetArray()));
        }
コード例 #40
0
        public void Setup()
        {
            // MATLAB: ma3x2 = [1 -2;-1 4;5 7]
            _ma3X2 = new Matrix(new double[][] {
                new double[] { 1, -2 },
                new double[] { -1, 4 },
                new double[] { 5, 7 }
                });

            // MATLAB: mb3x2 = [10 2.5;-3 -1.5;19 -6]
            _mb3X2 = new Matrix(new double[][] {
                new double[] { 10, 2.5 },
                new double[] { -3, -1.5 },
                new double[] { 19, -6 }
                });

            // MATLAB: mc2x2 = [1 2;3 4]
            _mc2X2 = new Matrix(new double[][] {
                new double[] { 1, 2 },
                new double[] { 3, 4 }
                });

            // MATLAB: md2x4 = [1 2 -3 12;3 3.1 4 2]
            _md2X4 = new Matrix(new double[][] {
                new double[] { 1, 2, -3, 12 },
                new double[] { 3, 3.1, 4, 2 }
                });

            // MATLAB: ra3x2 = ma3x2 + 2
            _ra3X2 = ComplexMatrix.Create(_ma3X2) + 2;

            // MATLAB: rb3x2 = mb3x2 - 1
            _rb3X2 = ComplexMatrix.Create(_mb3X2) - 1;

            // MATLAB: rc2x2 = mc2x2 + 5
            _rc2X2 = ComplexMatrix.Create(_mc2X2) + 5;

            // MATLAB: rd2x4 = md2x4 * 2
            _rd2X4 = ComplexMatrix.Create(_md2X4) * 2;

            // MATLAB:  ia3x2 = (ra3x2 * 2) * j
            _ia3X2 = (_ra3X2 * 2) * j;

            // MATLAB: ib3x2 = (rb3x2 * 3 + 1) * j
            _ib3X2 = ((_rb3X2 * 3) + 1) * j;

            // MATLAB: ic2x2 = (rc2x2 + 2) * j
            _ic2X2 = (_rc2X2 + 2) * j;

            // MATLAB: id2x4 = (rd2x4 - 5) * j
            _id2X4 = (_rd2X4 - 5) * j;

            // MATLAB: ca3x2 = 2*ra3x2 - 2*ia3x2
            _ca3X2 = (2 * _ra3X2) - (2 * _ia3X2);

            // MATLAB: cb3x2 = rb3x2 + 3*ib3x2
            _cb3X2 = _rb3X2 + (3 * _ib3X2);

            // MATLAB: cc2x2 = rc2x2 + 2 - 3*ic2x2
            _cc2X2 = _rc2X2 + 2 - (3 * _ic2X2);

            // MATLAB: cd2x4 = -2*rd2x4 + id2x4 + 1-j
            _cd2X4 = (-2 * _rd2X4) + _id2X4 + (1 - j);

            // MATLAB: v2 = [5 -2]
            _v2 = new Vector(new double[] { 5, -2 });

            // MATLAB: cv2 = [5+j, -2+3j]
            _cv2 = new ComplexVector(new Complex[] { 5 + j, -2 + (3 * j) });
        }
コード例 #41
0
        public void ComplexMatrixTranspose()
        {
            /* 2x3 rectangular case */

            // MATLAB: trans_c = ca3x2.'
            ComplexMatrix u = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6-(12*j), 2-(4*j), 14-(28*j) },
                new Complex[] { 0, 12-(24*j), 18-(36*j) }
                });

            Assert.That(_ca3X2.Transpose(), NumericIs.AlmostEqualTo(u), "trans c 1");
            Assert.That(_ca3X2.Transpose(), Is.Not.SameAs(_ca3X2));
            Assert.That(_ca3X2.Transpose().GetArray(), Is.Not.SameAs(_ca3X2.GetArray()));

            /* 2x2 square case */

            // MATLAB: trans_c2 = cc2x2.'
            ComplexMatrix v = new ComplexMatrix(new Complex[][] {
                new Complex[] { 8-(24*j), 10-(30*j) },
                new Complex[] { 9-(27*j), 11-(33*j) }
                });

            Assert.That(_cc2X2.Transpose(), NumericIs.AlmostEqualTo(v), "trans c2 1");
            Assert.That(_cc2X2.Transpose(), Is.Not.SameAs(_cc2X2));
            Assert.That(_cc2X2.Transpose().GetArray(), Is.Not.SameAs(_cc2X2.GetArray()));

            ComplexMatrix vInplace = _cc2X2.Clone();
            Assert.That(vInplace, Is.Not.SameAs(_cc2X2));
            Assert.That(vInplace.GetArray(), Is.Not.SameAs(_cc2X2.GetArray()));

            Complex[][] internalArray = vInplace.GetArray();
            vInplace.TransposeInplace();
            Assert.That(vInplace, NumericIs.AlmostEqualTo(v), "trans c2 2");
            Assert.That(vInplace.GetArray(), Is.SameAs(internalArray));
        }
コード例 #42
0
        public void ComplexMatrixMultiplication()
        {
            /*
            MATLAB:
            prod_cc = ca3x2 * cd2x4
            prod_cm = ca3x2 * md2x4
            prod_cs = ca3x2 * s
            prod_cc2 = cc2x2 * cc2x2
            prod_cm2 = cc2x2 * mc2x2
            prod_cs2 = cc2x2 * s
            prod_ccv = ca3x2 * cv2.'
            prod_cv = ca3x2 * v2.'
            prod_ccvdl = diag(cv2) * cc2x2
            prod_ccvdr = cc2x2 * diag(cv2)
            prod_cvdl = diag(v2) * cc2x2
            prod_cvdr = cc2x2 * diag(v2)
            */

            // ComplexMatrix * ComplexMatrix
            ComplexMatrix prodCmCm = new ComplexMatrix(new Complex[][] {
                new Complex[] { -66+(12*j), -66+(72*j), -66-(228*j), -66+(672*j) },
                new Complex[] { -154+(268*j), -154+(300*j), -154+(308*j), -154+(368*j) },
                new Complex[] { -352+(424*j), -352+(582*j), -352+(44*j), -352+(1784*j) }
                });

            Assert.That(_ca3X2 * _cd2X4, NumericIs.AlmostEqualTo(prodCmCm), "prod cc 1");
            Assert.That(_ca3X2.Multiply(_cd2X4), NumericIs.AlmostEqualTo(prodCmCm), "prod cc 2");

            // ComplexMatrix * Matrix
            ComplexMatrix prodCmM = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6-(12*j), 12-(24*j), -18+(36*j), 72-(144*j) },
                new Complex[] { 38-(76*j), 41.2-(82.4*j), 42-(84*j), 48-(96*j) },
                new Complex[] { 68-(136*j), 83.8-(167.6*j), 30-(60*j), 204-(408*j) }
                });

            Assert.That(_ca3X2 * _md2X4, NumericIs.AlmostEqualTo(prodCmM), "prod cm 1");
            Assert.That(_ca3X2.Multiply(_md2X4), NumericIs.AlmostEqualTo(prodCmM), "prod cm 2");

            // ComplexMatrix * Complex
            ComplexMatrix prodCmC = new ComplexMatrix(new Complex[][] {
                new Complex[] { 18-(6*j), 0 },
                new Complex[] { 6-(2*j), 36-(12*j) },
                new Complex[] { 42-(14*j), 54-(18*j) }
                });

            Assert.That(_ca3X2 * _s, NumericIs.AlmostEqualTo(prodCmC), "prod cs 1");
            Assert.That(_ca3X2.Multiply(_s), NumericIs.AlmostEqualTo(prodCmC), "prod cs 2");

            // ComplexMatrix * ComplexMatrix (Square)
            ComplexMatrix prodCmCmSquare = new ComplexMatrix(new Complex[][] {
                new Complex[] { -1232-(924*j), -1368-(1026*j) },
                new Complex[] { -1520-(1140*j), -1688-(1266*j) }
                });

            Assert.That(_cc2X2 * _cc2X2, NumericIs.AlmostEqualTo(prodCmCmSquare), "prod cc2 1");
            Assert.That(_cc2X2.Multiply(_cc2X2), NumericIs.AlmostEqualTo(prodCmCmSquare), "prod cc2 2");
            ComplexMatrix prodCmCmSquareInplace = _cc2X2.Clone();
            prodCmCmSquareInplace.MultiplyInplace(_cc2X2);
            Assert.That(prodCmCmSquareInplace, NumericIs.AlmostEqualTo(prodCmCmSquare), "prod cc2 3");

            // ComplexMatrix * Matrix (Square)
            ComplexMatrix prodCmMSquare = new ComplexMatrix(new Complex[][] {
                new Complex[] { 35-(105*j), 52-(156*j) },
                new Complex[] { 43-(129*j), 64-(192*j) }
                });

            Assert.That(_cc2X2 * _mc2X2, NumericIs.AlmostEqualTo(prodCmMSquare), "prod cm2 1");
            Assert.That(_cc2X2.Multiply(_mc2X2), NumericIs.AlmostEqualTo(prodCmMSquare), "prod cm2 2");
            ComplexMatrix prodCmMSquareInplace = _cc2X2.Clone();
            prodCmMSquareInplace.MultiplyInplace(_mc2X2);
            Assert.That(prodCmMSquareInplace, NumericIs.AlmostEqualTo(prodCmMSquare), "prod cm2 3");

            // ComplexMatrix * Complex (Square)
            ComplexMatrix prodCmCSquare = new ComplexMatrix(new Complex[][] {
                new Complex[] { 32-(16*j), 36-(18*j) },
                new Complex[] { 40-(20*j), 44-(22*j) }
                });

            Assert.That(_cc2X2 * _s, NumericIs.AlmostEqualTo(prodCmCSquare), "prod cs2 1");
            Assert.That(_cc2X2.Multiply(_s), NumericIs.AlmostEqualTo(prodCmCSquare), "prod cs2 2");
            ComplexMatrix prodCmCSquareInplace = _cc2X2.Clone();
            prodCmCSquareInplace.MultiplyInplace(_s);
            Assert.That(prodCmCSquareInplace, NumericIs.AlmostEqualTo(prodCmCSquare), "prod cs2 3");

            // ComplexMatrix * ComplexVector (Column)
            ComplexVector prodCmCvc = new ComplexVector(new Complex[] { 42 - (54 * j), 62 + (66 * j), 170 });
            Assert.That(_ca3X2 * _cv2, NumericIs.AlmostEqualTo(prodCmCvc), "prod ccv 1");
            Assert.That(_ca3X2.MultiplyRightColumn(_cv2), NumericIs.AlmostEqualTo(prodCmCvc), "prod ccv 2");

            // ComplexMatrix * Vector (Column)
            ComplexVector prodCmVc = new ComplexVector(new Complex[] { 30 - (60 * j), -14 + (28 * j), 34 - (68 * j) });
            Assert.That(_ca3X2 * _v2, NumericIs.AlmostEqualTo(prodCmVc), "prod cv 1");
            Assert.That(_ca3X2.MultiplyRightColumn(_v2), NumericIs.AlmostEqualTo(prodCmVc), "prod cv 2");

            // ComplexMatrix * ComplexVector (Diagonal, Left)
            ComplexMatrix prodCmCvdl = new ComplexMatrix(new Complex[][] {
                new Complex[] { 64-(112*j), 72-(126*j) },
                new Complex[] { 70+(90*j), 77+(99*j) }
                });

            Assert.That(_cc2X2.MultiplyLeftDiagonal(_cv2), NumericIs.AlmostEqualTo(prodCmCvdl), "prod ccv dl 1");
            ComplexMatrix prodCmCvdlInplace = _cc2X2.Clone();
            prodCmCvdlInplace.MultiplyLeftDiagonalInplace(_cv2);
            Assert.That(prodCmCvdlInplace, NumericIs.AlmostEqualTo(prodCmCvdl), "prod ccv dl 2");
            Assert.That(ComplexMatrix.Diagonal(_cv2) * _cc2X2, NumericIs.AlmostEqualTo(prodCmCvdl), "prod ccv dl 3");

            // ComplexMatrix * Vector (Diagonal, Left)
            ComplexMatrix prodCmVdl = new ComplexMatrix(new Complex[][] {
                new Complex[] { 40-(120*j), 45-(135*j) },
                new Complex[] { -20+(60*j), -22+(66*j) }
                });

            Assert.That(_cc2X2.MultiplyLeftDiagonal(_v2), NumericIs.AlmostEqualTo(prodCmVdl), "prod cv dl 1");
            ComplexMatrix prodCmVdlInplace = _cc2X2.Clone();
            prodCmVdlInplace.MultiplyLeftDiagonalInplace(_v2);
            Assert.That(prodCmVdlInplace, NumericIs.AlmostEqualTo(prodCmVdl), "prod cv dl 2");

            // ComplexMatrix * ComplexVector (Diagonal, Right)
            ComplexMatrix prodCmCvdr = new ComplexMatrix(new Complex[][] {
                new Complex[] { 64-(112*j), 63+(81*j) },
                new Complex[] { 80-(140*j), 77+(99*j) }
                });

            Assert.That(_cc2X2.MultiplyRightDiagonal(_cv2), NumericIs.AlmostEqualTo(prodCmCvdr), "prod ccv dr 1");
            ComplexMatrix prodCmCvdrInplace = _cc2X2.Clone();
            prodCmCvdrInplace.MultiplyRightDiagonalInplace(_cv2);
            Assert.That(prodCmCvdrInplace, NumericIs.AlmostEqualTo(prodCmCvdr), "prod ccv dr 2");
            Assert.That(_cc2X2 * ComplexMatrix.Diagonal(_cv2), NumericIs.AlmostEqualTo(prodCmCvdr), "prod ccv dr 3");

            // ComplexMatrix * Vector (Diagonal, Right)
            ComplexMatrix prodCmVdr = new ComplexMatrix(new Complex[][] {
                new Complex[] { 40-(120*j), -18+(54*j) },
                new Complex[] { 50-(150*j), -22+(66*j) }
                });

            Assert.That(_cc2X2.MultiplyRightDiagonal(_v2), NumericIs.AlmostEqualTo(prodCmVdr), "prod cv dr 1");
            ComplexMatrix prodCmVdrInplace = _cc2X2.Clone();
            prodCmVdrInplace.MultiplyRightDiagonalInplace(_v2);
            Assert.That(prodCmVdrInplace, NumericIs.AlmostEqualTo(prodCmVdr), "prod cv dr 2");
        }
コード例 #43
0
ファイル: MatrixView.cs プロジェクト: davidsiaw/neuron
        /// <summary>
        /// Sets the matrix to show.
        /// </summary>
        /// <param name="matrix">A complex matrix.</param>
        public void Matrix(ComplexMatrix matrix)
        {
            this._ComplexMatrix = matrix;
            this._IsRealMatrix = false;

            this._RealMatrix = new Matrix(1);

            this.ShowMatrix();
        }
コード例 #44
0
ファイル: EigenSystem.cs プロジェクト: davidsiaw/neuron
        /// <summary>
        /// Computes the eigenvalues and eigenvectors for an complex general matrix A.
        /// </summary>
        /// <param name="A">The complex general matrix A.</param>
        /// <param name="EigenVectors">The eigenvectors.</param>
        /// <returns>The eigenvalues.</returns>
        public ComplexMatrix GetEigenvalues(ComplexMatrix A, out ComplexMatrix EigenVectors)
        {
            //Fortran Ejemplo
            //CG(NM,N,AR,AI,WR,WI,1,ZR,ZI,SCALE,ORTR,ORTI,ERROR)
            //C
            //C     THIS DRIVER TESTS  EISPACK  FOR THE CLASS OF COMPLEX GENERAL
            //C     MATRICES SUMMARIZING THE FIGURES OF MERIT FOR ALL PATHS.
            //C
            //C     THIS DRIVER IS CATALOGUED AS  EISPDRV4(CGSUMARY).
            //C
            //C     THE DIMENSION OF  AR,AI,ZR,ZI,ASAVER,ASAVEI,RM1,  AND  RM2 SHOULD
            //C     BE  NM  BY  NM.
            //C     THE DIMENSION OF  WR,WI,WR1,WI1,SELECT,SLHOLD,INT,SCALE,ORTR,ORTI,
            //C     RV1  AND  RV2  SHOULD BE  NM.
            //C     THE DIMENSION OF  ARHOLD  AND  AIHOLD  SHOULD BE  NM  BY  NM.
            //C     HERE NM = 20.

            if (this._cg == null) this._cg = new CG();

            this.CheckDimensions(A);

            Matrix AReal = A.GetReal();
            double[] ARealData = AReal.Data;
            Matrix AImag = A.GetImag();
            double[] AImagData = AImag.Data;

            ComplexMatrix EigenVals = new ComplexMatrix(A.RowCount, 1);
            Matrix RealEigenVals = new Matrix(A.RowCount, 1);
            double[] RealEigenValsData = RealEigenVals.Data;
            Matrix ImagEigenVals = new Matrix(A.RowCount, 1);
            double[] ImagEigenValsData = ImagEigenVals.Data;

            EigenVectors = new ComplexMatrix(A.RowCount, A.ColumnCount);
            Matrix RealEigVect = new Matrix(A.RowCount);
            double[] RealEigVectData = RealEigVect.Data;
            Matrix ImagEigVect = new Matrix(A.RowCount);
            double[] ImagEigVectData = ImagEigVect.Data;

            double[] SCALE = new double[A.RowCount];
            double[] ORTR = new double[A.RowCount];
            double[] ORTI = new double[A.RowCount];

            int Info = 0;
            int matz = 1; //Se calculan los eigenvalores y los eigenvectores
            _cg.Run(A.RowCount, A.RowCount, ref ARealData, 0, ref AImagData, 0, ref RealEigenValsData, 0, ref ImagEigenValsData, 0,
                matz, ref RealEigVectData, 0, ref ImagEigVectData, 0, ref SCALE, 0, ref ORTR, 0, ref ORTI, 0, ref Info);

            #region Error
            /// is set to
            /// zero       for normal return,
            /// j          if the limit of 30*n iterations is exhausted
            /// while the j-th eigenvalue is being sought.

            if (Info != 0)
            {
                throw new ArgumentException("The limit of 30*n iterations is exhausted");
            }

            #endregion

            EigenVals.SetReal(RealEigenVals);
            EigenVals.SetImag(ImagEigenVals);

            EigenVectors.SetReal(RealEigVect);
            EigenVectors.SetImag(ImagEigVect);

            return EigenVals;
        }
コード例 #45
0
ファイル: EigenSystem.cs プロジェクト: davidsiaw/neuron
 private void CheckDimensions(ComplexMatrix matrixA)
 {
     if (matrixA.IsSquare != true)
     {
         throw new System.ArgumentException("Matrix A is not a square matrix.");
     }
 }
コード例 #46
0
ファイル: MatrixView.cs プロジェクト: davidsiaw/neuron
 /// <summary>
 /// Remove the matrix.
 /// </summary>
 public void RemoveMatrix()
 {
     this._ComplexMatrix = new ComplexMatrix(1);
     this._ComplexMatrix = new ComplexMatrix(1);
     this.dataGridView1.DataSource = null;
 }
コード例 #47
0
 public static Animation ShowMatrix(Rect pos,
                               ComplexMatrix u,
                               Ani<Brush> or,
                               Ani<Brush> bla)
 {
     var d = Math.Min(pos.Width / (u.Columns.Count +2), pos.Height / (u.Rows.Count)) / 2;
     pos = new Rect(pos.TopLeft, new Size(d * (u.Columns.Count + 2) * 2, d * u.Rows.Count * 2));
     var ur = d;
     return new Animation {
         new RectDesc(new Rect(pos.X + d*2, pos.Y, pos.Width - d*4, pos.Height),
                      Brushes.Black,
                      strokeThickness: 0.6,
                      dashed: 5),
         new RectDesc(new Rect(pos.X, pos.Y, d*2, pos.Height),
                      Brushes.Black,
                      strokeThickness: 0.6,
                      dashed: 5),
         new RectDesc(new Rect(pos.Right - d*2, pos.Y, d*2, pos.Height),
                      Brushes.Black,
                      strokeThickness: 0.6,
                      dashed: 5),
         // matrix
         u.Rows.Count.Range().SelectMany(
             r => u.Columns.Count.Range().Select(
                 c => ShowComplex(
                     or,
                     bla,
                     u.Rows[r][c],
                     (pos.TopLeft + new Vector(d*3 + c*d*2, d + r*d*2)),
                     ur)))
     };
 }
コード例 #48
0
        public void TestComplexMatrix_Multiplicative()
        {
            /*
            MATLAB:
            prod_cc = ca3x2 * cd2x4
            prod_cm = ca3x2 * md2x4
            prod_cs = ca3x2 * s
            prod_cc2 = cc2x2 * cc2x2
            prod_cm2 = cc2x2 * mc2x2
            prod_cs2 = cc2x2 * s
            prod_ccv = ca3x2 * cv2.'
            prod_cv = ca3x2 * v2.'
            prod_ccvdl = diag(cv2) * cc2x2
            prod_ccvdr = cc2x2 * diag(cv2)
            prod_cvdl = diag(v2) * cc2x2
            prod_cvdr = cc2x2 * diag(v2)
            */

            // ComplexMatrix * ComplexMatrix
            ComplexMatrix prod_cc = new ComplexMatrix(new Complex[][] {
                new Complex[] { -66+12*j, -66+72*j, -66-228*j, -66+672*j },
                new Complex[] { -154+268*j, -154+300*j, -154+308*j, -154+368*j },
                new Complex[] { -352+424*j, -352+582*j, -352+44*j, -352+1784*j }});
            NumericAssert.AreAlmostEqual(prod_cc, ca3x2 * cd2x4, "prod cc 1");
            NumericAssert.AreAlmostEqual(prod_cc, ca3x2.Multiply(cd2x4), "prod cc 2");

            // ComplexMatrix * Matrix
            ComplexMatrix prod_cm = new ComplexMatrix(new Complex[][] {
                new Complex[] { 6-12*j, 12-24*j, -18+36*j, 72-144*j },
                new Complex[] { 38-76*j,41.2-82.4*j, 42-84*j, 48-96*j },
                new Complex[] { 68-136*j, 83.8-167.6*j, 30-60*j, 204-408*j }});
            NumericAssert.AreAlmostEqual(prod_cm, ca3x2 * md2x4, "prod cm 1");
            NumericAssert.AreAlmostEqual(prod_cm, ca3x2.Multiply(md2x4), "prod cm 2");

            // ComplexMatrix * Complex
            ComplexMatrix prod_cs = new ComplexMatrix(new Complex[][] {
                new Complex[] { 18-6*j, 0 },
                new Complex[] { 6-2*j,36-12*j },
                new Complex[] { 42-14*j, 54-18*j }});
            NumericAssert.AreAlmostEqual(prod_cs, ca3x2 * s, "prod cs 1");
            NumericAssert.AreAlmostEqual(prod_cs, ca3x2.Multiply(s), "prod cs 2");

            // ComplexMatrix * ComplexMatrix (Square)
            ComplexMatrix prod_cc2 = new ComplexMatrix(new Complex[][] {
                new Complex[] { -1232-924*j, -1368-1026*j },
                new Complex[] { -1520-1140*j, -1688-1266*j }});
            NumericAssert.AreAlmostEqual(prod_cc2, cc2x2 * cc2x2, "prod cc2 1");
            NumericAssert.AreAlmostEqual(prod_cc2, cc2x2.Multiply(cc2x2), "prod cc2 2");
            ComplexMatrix prod_cc2_inplace = cc2x2.Clone();
            prod_cc2_inplace.MultiplyInplace(cc2x2);
            NumericAssert.AreAlmostEqual(prod_cc2, prod_cc2_inplace, "prod cc2 3");

            // ComplexMatrix * Matrix (Square)
            ComplexMatrix prod_cm2 = new ComplexMatrix(new Complex[][] {
                new Complex[] { 35-105*j, 52-156*j },
                new Complex[] { 43-129*j, 64-192*j }});
            NumericAssert.AreAlmostEqual(prod_cm2, cc2x2 * mc2x2, "prod cm2 1");
            NumericAssert.AreAlmostEqual(prod_cm2, cc2x2.Multiply(mc2x2), "prod cm2 2");
            ComplexMatrix prod_cm2_inplace = cc2x2.Clone();
            prod_cm2_inplace.MultiplyInplace(mc2x2);
            NumericAssert.AreAlmostEqual(prod_cm2, prod_cm2_inplace, "prod cm2 3");

            // ComplexMatrix * Complex (Square)
            ComplexMatrix prod_cs2 = new ComplexMatrix(new Complex[][] {
                new Complex[] { 32-16*j, 36-18*j },
                new Complex[] { 40-20*j, 44-22*j }});
            NumericAssert.AreAlmostEqual(prod_cs2, cc2x2 * s, "prod cs2 1");
            NumericAssert.AreAlmostEqual(prod_cs2, cc2x2.Multiply(s), "prod cs2 2");
            ComplexMatrix prod_cs2_inplace = cc2x2.Clone();
            prod_cs2_inplace.MultiplyInplace(s);
            NumericAssert.AreAlmostEqual(prod_cs2, prod_cs2_inplace, "prod cs2 3");

            // ComplexMatrix * ComplexVector (Column)
            ComplexVector prod_ccv = new ComplexVector(new Complex[] { 42 - 54 * j, 62 + 66 * j, 170 });
            NumericAssert.AreAlmostEqual(prod_ccv, ca3x2 * cv2, "prod ccv 1");
            NumericAssert.AreAlmostEqual(prod_ccv, ca3x2.MultiplyRightColumn(cv2), "prod ccv 2");

            // ComplexMatrix * Vector (Column)
            ComplexVector prod_cv = new ComplexVector(new Complex[] { 30 - 60 * j, -14 + 28 * j, 34 - 68 * j });
            NumericAssert.AreAlmostEqual(prod_cv, ca3x2 * v2, "prod cv 1");
            NumericAssert.AreAlmostEqual(prod_cv, ca3x2.MultiplyRightColumn(v2), "prod cv 2");

            // ComplexMatrix * ComplexVector (Diagonal, Left)
            ComplexMatrix prod_ccvdl = new ComplexMatrix(new Complex[][] {
                new Complex[] { 64-112*j, 72-126*j },
                new Complex[] { 70+90*j, 77+99*j }});
            NumericAssert.AreAlmostEqual(prod_ccvdl, cc2x2.MultiplyLeftDiagonal(cv2), "prod ccv dl 1");
            ComplexMatrix prod_ccvdl_inplace = cc2x2.Clone();
            prod_ccvdl_inplace.MultiplyLeftDiagonalInplace(cv2);
            NumericAssert.AreAlmostEqual(prod_ccvdl, prod_ccvdl_inplace, "prod ccv dl 2");
            NumericAssert.AreAlmostEqual(prod_ccvdl, ComplexMatrix.Diagonal(cv2) * cc2x2, "prod ccv dl 3");

            // ComplexMatrix * Vector (Diagonal, Left)
            ComplexMatrix prod_cvdl = new ComplexMatrix(new Complex[][] {
                new Complex[] { 40-120*j, 45-135*j },
                new Complex[] { -20+60*j, -22+66*j }});
            NumericAssert.AreAlmostEqual(prod_cvdl, cc2x2.MultiplyLeftDiagonal(v2), "prod cv dl 1");
            ComplexMatrix prod_cvdl_inplace = cc2x2.Clone();
            prod_cvdl_inplace.MultiplyLeftDiagonalInplace(v2);
            NumericAssert.AreAlmostEqual(prod_cvdl, prod_cvdl_inplace, "prod cv dl 2");

            // ComplexMatrix * ComplexVector (Diagonal, Right)
            ComplexMatrix prod_ccvdr = new ComplexMatrix(new Complex[][] {
                new Complex[] { 64-112*j, 63+81*j },
                new Complex[] { 80-140*j, 77+99*j }});
            NumericAssert.AreAlmostEqual(prod_ccvdr, cc2x2.MultiplyRightDiagonal(cv2), "prod ccv dr 1");
            ComplexMatrix prod_ccvdr_inplace = cc2x2.Clone();
            prod_ccvdr_inplace.MultiplyRightDiagonalInplace(cv2);
            NumericAssert.AreAlmostEqual(prod_ccvdr, prod_ccvdr_inplace, "prod ccv dr 2");
            NumericAssert.AreAlmostEqual(prod_ccvdr, cc2x2 * ComplexMatrix.Diagonal(cv2), "prod ccv dr 3");

            // ComplexMatrix * Vector (Diagonal, Right)
            ComplexMatrix prod_cvdr = new ComplexMatrix(new Complex[][] {
                new Complex[] { 40-120*j, -18+54*j },
                new Complex[] { 50-150*j, -22+66*j }});
            NumericAssert.AreAlmostEqual(prod_cvdr, cc2x2.MultiplyRightDiagonal(v2), "prod cv dr 1");
            ComplexMatrix prod_cvdr_inplace = cc2x2.Clone();
            prod_cvdr_inplace.MultiplyRightDiagonalInplace(v2);
            NumericAssert.AreAlmostEqual(prod_cvdr, prod_cvdr_inplace, "prod cv dr 2");
        }
コード例 #49
0
ファイル: EigenSystem.cs プロジェクト: davidsiaw/neuron
        /// <summary>
        /// Computes the eigenvalues for an N-by-N real nonsymmetric matrix A.
        /// </summary>
        /// <param name="A">N-by-N real nonsymmetric matrix A.</param>
        /// <returns>The eigenvalues.</returns>
        public ComplexMatrix GetEigenvalues(Matrix A)
        {
            if (this._dgeev == null) this._dgeev = new DGEEV();

            this.CheckDimensions(A);

            Matrix ACopy = A.Clone();
            double[] ACopyData = ACopy.Data;
            Matrix RealEVectors = new Matrix(1, 1);
            double[] EigenVectsData = RealEVectors.Data;
            ComplexMatrix EigenVals = new ComplexMatrix(A.RowCount, 1);

            double[] REigVal = new double[A.RowCount];
            double[] IEigVal = new double[A.RowCount];

            //double[] EigenValsData = EigenVals.Data;
            int Info = 0;

            double[] VL = new double[A.RowCount];

            double[] Work = new double[1];
            int LWork = -1;

            //Calculamos LWORK
            _dgeev.Run("N", "N", A.RowCount, ref ACopyData, 0, ACopy.RowCount, ref REigVal, 0, ref IEigVal, 0, ref VL, 0, 1, ref  EigenVectsData, 0, A.RowCount, ref Work, 0, LWork, ref Info);

            LWork = Convert.ToInt32(Work[0]);
            if (LWork > 0)
            {
                Work = new double[LWork];
                _dgeev.Run("N", "N", A.RowCount, ref ACopyData, 0, ACopy.RowCount, ref REigVal, 0, ref IEigVal, 0, ref VL, 0, 1, ref  EigenVectsData, 0, A.RowCount, ref Work, 0, LWork, ref Info);
            }
            else
            {

                //Error
            }

            #region Error
            //= 0:  successful exit
            //.LT. 0:  if INFO = -i, the i-th argument had an illegal value.
            //.GT. 0:  if INFO = i, the QR algorithm failed to compute all the
            // eigenvalues, and no eigenvectors have been computed;
            // elements i+1:N of WR and WI contain eigenvalues which
            // have converged.

            if (Info < 0)
            {
                string infoSTg = Math.Abs(Info).ToString();
                throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
            }
            else if (Info > 0)
            {
                string infoSTg = Math.Abs(Info).ToString();
                throw new Exception("The QR algorithm failed to compute all the eigenvalues.");
            }

            #endregion

            for (int i = 0; i < EigenVals.RowCount; i++)
            {
                EigenVals[i, 0] = new Complex(REigVal[i], IEigVal[i]);
            }

            return EigenVals;
        }
コード例 #50
0
 public static Animation ShowMatrixMultiplication(TimeSpan period,
                                             Rect pos,
                                             ComplexMatrix u,
                                             ComplexVector v,
                                             Ani<Brush> or,
                                             Ani<Brush> blu,
                                             Ani<Brush> bla)
 {
     var d = Math.Min(pos.Width/(u.Columns.Count + 2), pos.Height/u.Rows.Count)/2;
     pos = new Rect(pos.TopLeft, new Size(d * (u.Columns.Count + 2)*2, d*u.Rows.Count*2));
     var ur = d;
     var animation = new Animation {
         new RectDesc(new Rect(pos.X + d * 2, pos.Y, pos.Width - d * 4, pos.Height),
                      Brushes.Black,
                      strokeThickness: 0.6,
                      dashed: 5),
         new RectDesc(new Rect(pos.X, pos.Y, d * 2, pos.Height),
                      Brushes.Black,
                      strokeThickness: 0.6,
                      dashed: 5),
         new RectDesc(new Rect(pos.Right - d*2, pos.Y, d * 2, pos.Height),
                      Brushes.Black,
                      strokeThickness: 0.6,
                      dashed: 5)
     };
     var per = animation.Periodic(period);
     var s0 = per.LimitedSameTime(0.Seconds(), period.DividedBy(4));
     foreach (var r in u.Rows.Count.Range()) {
         var pp = s0.Proper;
         // input vector
         var p1 = pos.TopLeft + new Vector(d, d + r*d*2);
         var p2 = pos.TopLeft + new Vector(d*3 + r*d*2, 0);
         var p3 = pos.TopLeft + new Vector(d*3 + r*d*2, u.Rows.Count*d*2);
         s0.Add(ShowComplex(pp.Combine(blu, (p, b) => b.LerpToTransparent(p.SmoothTransition(0, 0, 1))),
                            pp.Combine(bla, (p, b) => b.LerpToTransparent(p.SmoothTransition(0, 0, 1))),
                            v.Values[r],
                            pp.Select(p => p.SmoothTransition(p1, p1, p2, p3)),
                            ur,
                            rotation: pp.Select(t => Turn.FromNaturalAngle(t.SmoothTransition(0, Math.PI/2, Math.PI/2)))));
         foreach (var c in u.Columns.Count.Range()) {
             // vector copied into matrix
             s0.Add(ShowComplex(pp.Combine(blu, (p, b) => b.LerpToTransparent(p.SmoothTransition(1, 1, 0))),
                                pp.Combine(bla, (p, b) => b.LerpToTransparent(p.SmoothTransition(1, 1, 0))),
                                v.Values[c],
                                (pos.TopLeft + new Vector(d*3 + c*d*2, d + r*d*2)),
                                ur,
                                Brushes.Transparent,
                                rotation: Turn.FromNaturalAngle(Math.PI/2)));
             // matrix
             s0.Add(ShowComplex(pp.Combine(or, (p, b) => b.LerpToTransparent(p.SmoothTransition(0, 0, 0))),
                                pp.Combine(bla, (p, b) => b.LerpToTransparent(p.SmoothTransition(0, 0, 0))),
                                u.Rows[r][c],
                                (pos.TopLeft + new Vector(d*3 + c*d*2, d + r*d*2)),
                                ur));
         }
     }
     var s1 = per.LimitedSameTime(period.Times(0.25), period.Times(0.5));
     foreach (var r in u.Rows.Count.Range()) {
         foreach (var c in u.Columns.Count.Range()) {
             s1.Add(
                 ShowComplexProduct(
                     blu,
                     or,
                     bla,
                     bla,
                     v.Values[c],
                     u.Rows[r][c],
                     (pos.TopLeft + new Vector(d*3 + c*d*2, d + r*d*2)),
                     ur,
                     s1.Proper));
         }
     }
     var s2 = per.LimitedSameTime(period.Times(0.5), period);
     foreach (var r in u.Rows.Count.Range()) {
         s2.Add(
             ShowComplexSum(
                 blu,
                 blu,
                 bla,
                 u.Rows[r].Count.Range()
                          .Select(e => u.Rows[r][e]*v.Values[e])
                          .Select(e => new ConstantAni<Complex>(e)),
                 (pos.TopLeft + new Vector(d*3, d + r*d*2)),
                 (pos.TopLeft + new Vector(d*3 + u.Columns.Count*d*2, d + r*d*2)),
                 new Vector(d*2, 0),
                 ur,
                 s2.Proper));
     }
     return animation;
 }
コード例 #51
0
 public static void AreAlmostEqual(ComplexMatrix expected, ComplexMatrix actual, double relativeAccuracy, string message)
 {
     Assert.DoAssert(new AlmostEqualAsserter(expected.Norm1(), actual.Norm1(), (expected - actual).Norm1(), relativeAccuracy, message));
 }
コード例 #52
0
 public MatrixComplexDebuggerDisplay(ComplexMatrix matrix)
 {
     this.MeMatrix = matrix;
 }