示例#1
0
        public AMatrix <MatrixDataType> Solve(AMatrix <MatrixDataType> A, AMatrix <MatrixDataType> b, AMatrix <MatrixDataType> x0, int max_iter)
        {
            // [solution, solutions, errors, norms] = gmres_simple(A, b, x0, kmax, chosen_solution)
            AMatrix <MatrixDataType> x = x0;
            AMatrix <MatrixDataType> r = b - (A * x);
            double rho0 = r.L2Norm();

            if (rho0 == 0)
            {
                return(x0);
            }

            List <int> REPEATED = new List <int>();
            List <AMatrix <MatrixDataType> > solutions = new List <AMatrix <MatrixDataType> >();

            solutions.Add(x.Transpose());
            List <double> errors = new List <double>();

            errors.Add(rho0);
            List <double> norms = new List <double>();

            norms.Add(x.L2Norm());


            // scale tol for relative residual reduction

            AMatrix <MatrixDataType> H     = algebra.CreateZeros(1, 0);
            AMatrix <MatrixDataType> V     = r / rho0;
            AMatrix <MatrixDataType> gamma = this.algebra.CreateZeros(1, 1);

            gamma.SetElement(0, 0, 1);

            double nu = 1;

            for (int iteration = 0; iteration < max_iter; iteration++)
            {
                Tuple <AMatrix <MatrixDataType>, AMatrix <MatrixDataType> > tuple = ArnoldiStep(A, V, H, REPEATED, -1);
                V = tuple.Item1;
                H = tuple.Item2;
                // compute the nul vector of H'


                AMatrix <MatrixDataType> gk = ((gamma * H.GetColumnSection(0, iteration + 1, iteration)) / H.GetElement(iteration + 1, iteration));
                gamma = gamma.AppendColumns(gk * -1.0);

                // Compute the residual norm
                nu = nu + (gk.Transpose() * gk).GetElement();
                errors.Add(rho0 / Math.Sqrt(nu));

                // compute explicit residual every step
                int k1 = H.ColumnCount;
                AMatrix <MatrixDataType> e = this.algebra.CreateZeros(iteration + 2, 1);
                e.SetElement(0, 0, 1);
                AMatrix <MatrixDataType> y  = simple_solver.Solve(H, e);
                AMatrix <MatrixDataType> x1 = V.GetColumns(0, iteration + 1) * (y * rho0);
                solutions.Add(x1);
                norms.Add(x1.L2Norm());
            }
            return(solutions[solutions.Count - 1]);
            // compute the approximate solution
            //solution  = solutions(chosen_solution,:);
        }
        public IFunction <double, double> GetIRF(double[] signal)
        {
            AMatrix <MatrixType> weights = solver.Solve(forward_matrix, algebra.Create(signal, true));

            return(new FunctionWeigthed <double, double>(new AlgebraRealFloat64(), basis_function_list, weights.ToArray1DFloat64()));
        }