Esempio n. 1
0
        /// <summary>
        /// MATLAB 'backslash' solver for the system
        /// <paramref name="M"/>*<paramref name="X"/> = <paramref name="RHS"/>.
        /// </summary>
        /// <param name="M">
        /// Matrix of the linear system.
        /// </param>
        /// <param name="RHS">
        /// Input, the right hand side of the linear system.
        /// </param>
        /// <param name="X">
        /// Output, the solution.
        /// </param>
        /// <param name="__WorkingPath"></param>
        public static void SolveMATLAB <T1, T2>(this IMutableMatrixEx M, T1 X, T2 RHS, string __WorkingPath = null)
            where T1 : IList <double>
            where T2 : IList <double> //
        {
            if (M.RowPartitioning.LocalLength != RHS.Count)
            {
                throw new ArgumentException("Mismatch between number of rows and length of right-hand-side.");
            }
            if (M.ColPartition.LocalLength != X.Count)
            {
                throw new ArgumentException("Mismatch between number of columns and length of unknown vector.");
            }

            MultidimensionalArray Xwrapper = MultidimensionalArray.Create(X.Count, 1);

            using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) {
                MultidimensionalArray output = MultidimensionalArray.Create(1, 1);
                connector.PutSparseMatrix(M, "Matrix");
                connector.PutVector(RHS, "RHS");

                connector.Cmd("X = Matrix \\ RHS ;");
                connector.GetMatrix(Xwrapper, "X");

                connector.Execute(false);

                Xwrapper.GetColumn(0, X);
            }
        }