Esempio n. 1
0
        public static void TestMultiplyScalar <MatrixType>(IAlgebraLinear <MatrixType> algebra)
        {
            AMatrix <MatrixType> A = algebra.Create(new double[, ] {
                { 1, 2 }, { 3, 4 }
            });
            AMatrix <MatrixType> C = A * 2;

            Assert.AreEqual(2, C.GetElement(0, 0));
            Assert.AreEqual(4, C.GetElement(0, 1));
            Assert.AreEqual(6, C.GetElement(1, 0));
            Assert.AreEqual(8, C.GetElement(1, 1));
        }
Esempio n. 2
0
        public static void TestAddOne <MatrixType>(IAlgebraLinear <MatrixType> algebra)
        {
            AMatrix <MatrixType> A = algebra.Create(new double[, ] {
                { 1, 2 }, { 3, 4 }
            });
            AMatrix <MatrixType> B = A + 1;

            Assert.AreEqual(2, B.GetElement(0, 0));
            Assert.AreEqual(3, B.GetElement(0, 1));
            Assert.AreEqual(4, B.GetElement(1, 0));
            Assert.AreEqual(5, B.GetElement(1, 1));
        }
Esempio n. 3
0
        public static void TestMatrixMatrixProduct0 <MatrixType>(IAlgebraLinear <MatrixType> algebra)
        {
            AMatrix <MatrixType> A = algebra.Create(new double[, ] {
                { 1, 0 }, { 0, 1 }
            });
            AMatrix <MatrixType> B = algebra.Create(new double[, ] {
                { 1, 2 }, { 3, 4 }
            });
            AMatrix <MatrixType> C = A * B;

            Assert.AreEqual(1, C.GetElement(0, 0));
            Assert.AreEqual(2, C.GetElement(0, 1));
            Assert.AreEqual(3, C.GetElement(1, 0));
            Assert.AreEqual(4, C.GetElement(1, 1));
        }
Esempio n. 4
0
        ///**
        // * Calculates {@code P(D_n < d)} using method described in [1] and doubles
        // * (see above).
        // *
        // * @param d statistic
        // * @return the two-sided probability of {@code P(D_n < d)}
        // * @throws MathArithmeticException if algorithm fails to convert {@code h}
        // * to a {@link org.apache.commons.math3.fraction.BigFraction} in expressing
        // * {@code d} as {@code (k - h) / m} for integer {@code k, m} and
        // * {@code 0 <= h < 1}.
        // */
        private double RoundedK(double d)
        {
            int k = (int)Math.Ceiling(n * d);

            BigDecimal[,] HBigFraction = this.createH(d);//In row, column dimension order
            int m = HBigFraction.GetLength(0);

            ///*
            // * Here the rounding part comes into play: use
            // * RealMatrix instead of FieldMatrix<BigFraction>
            // */ new
            MatrixMathNet H = new MatrixMathNet(m, m);

            for (int i = 0; i < m; ++i)
            {
                for (int j = 0; j < m; ++j)
                {
                    H.SetElement(i, j, (double)HBigFraction[i, j]);
                }
            }
            AMatrix <Matrix <double> > Hpower = H.Power(n);
            double pFrac = Hpower.GetElement(k - 1, k - 1);

            for (int i = 1; i <= n; ++i)
            {
                pFrac *= i / (double)n;
            }
            return(pFrac);
        }
Esempio n. 5
0
        /*
         * printing
         */
        public override String ToString()
        {
            String ret = "layer" + (layerNumber + 1) + "Dim=[ " + d_output_size + " " + d_input_size + " ];\n";

            ret += "net.W{" + (layerNumber + 1) + "}=[ ";           //idx+1 for matlab....
            for (int o = 0; o < d_output_size; ++o)
            {
                for (int i = 0; i < d_input_size; ++i)
                {
                    ret += d_weights.GetElement(o, i).ToString();
                    ret += i + 1 == d_input_size ?
                           (o + 1 == d_output_size ? " ];\n" : " ;\n            ") : " ";
                }
            }
            return(ret);
        }
Esempio n. 6
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,:);
        }