Esempio n. 1
0
        public static double PowerMethod(this Matrix <double> matrix)
        {
            var lambda = new DyamicList <double>(double.NaN);

            Console.WriteLine(matrix);

            var eigenVector = (Vector <double>)DenseVector.Create(matrix.ColumnCount, i => (i) + 3);

            var index = -1;

            do
            {
                index++;

                eigenVector = matrix * eigenVector;

                lambda[index] = eigenVector.HighestValue();

                eigenVector /= lambda[index];
            } while(lambda[index].Diff(lambda[index - 1]) > Error);

            Console.WriteLine(lambda.ToString("lambda", true));
            Console.WriteLine(eigenVector);

            string.Format("Result: {0}", Math.Round(lambda[index], 5)).Print();

            return(Math.Round(lambda[index], 5));
        }
Esempio n. 2
0
        public static DyamicList <double> GaussElimination(this Matrix <double> m)
        {
            var equations = DenseMatrix.OfMatrix(m);

            var x = new DyamicList <double>(double.NaN);

            Console.WriteLine(equations);

            var numZeros = 0;

            for (var row = 0; row < equations.RowCount; row++)
            {
                for (var zeros = 0; zeros < numZeros; zeros++)
                {
                    var topVector     = equations.Row(row - (numZeros - zeros));
                    var currentVector = equations.Row(row);

                    var top = topVector.At(zeros);
                    var bot = currentVector.At(zeros);

                    var normal = bot / top;

                    currentVector -= topVector * normal;

                    equations.SetRow(row, currentVector);
                }

                numZeros++;
            }

            equations.Round();
            Console.WriteLine(equations);

            for (var row = equations.RowCount - 1; row >= 0; row--)
            {
                var rowVector = equations.Row(row);
                var value     = rowVector.At(rowVector.Count - 1);

                for (var xNum = rowVector.Count - 2; xNum >= row; xNum--)
                {
                    if (x[xNum].Equals(double.NaN))
                    {
                        value /= rowVector.At(xNum);
                        break;
                    }

                    value -= rowVector.At(xNum) * x[xNum];
                }

                x[row] = value;
            }

            Console.WriteLine("Result: ");
            Console.WriteLine(x.ToString("x", true));

            return(x);
        }
Esempio n. 3
0
        public static Matrix Doolittle(this Matrix a, Matrix b)
        {
            Console.WriteLine("a:");
            Console.WriteLine(a);
            Console.WriteLine("b:");
            Console.WriteLine(b);

            var rows = a.RowCount;
            var cols = a.ColumnCount;

            Matrix <double> l = DenseMatrix.Create(rows, cols, double.NaN);

            l.SetUpperTriangle(0);
            l.SetDiagonal(DenseVector.Create(rows, 1));

            Matrix <double> u = DenseMatrix.Create(rows, cols, double.NaN);

            u.SetLowerTriangle(0);

            // first row is known
            u.SetRow(0, a.Row(0));

            for (var row = 1; row < rows; row++)
            {
                for (var col = 0; col < cols; col++)
                {
                    if (double.IsNaN(l[row, col]))
                    {
                        l[row, col] = DoolittleMultiplcation(row, col, l, u, a);
                    }
                    else if (double.IsNaN(u[row, col]))
                    {
                        u[row, col] = DoolittleMultiplcation(row, col, l, u, a);
                    }
                }
            }

            Console.WriteLine("l:");
            Console.WriteLine(l);
            Console.WriteLine("u:");
            Console.WriteLine(u);


            Matrix <double> y      = DenseMatrix.Create(rows, 1, double.NaN);
            var             yValue = new DyamicList <double>(double.NaN);

            for (var row = 0; row < rows; row++)
            {
                var value = DoolittleMultiplcation(row, 0, l, y, b);
                yValue[row] = value;
                y[row, 0]   = value;
            }

            Console.WriteLine(yValue.ToString("y"));

            Matrix <double> x      = DenseMatrix.Create(rows, 1, double.NaN);
            var             xValue = new DyamicList <double>(double.NaN);

            for (var row = rows - 1; row >= 0; row--)
            {
                var valueType = DoolittleMultiplcation(row, 0, u, x, y);

                x[row, 0]   = valueType;
                xValue[row] = valueType;
            }

            Console.WriteLine(xValue.ToString("x"));

            return(null);
        }
Esempio n. 4
0
        public static double Muller(this Func equation, double a, double b)
        {
            var x = new DyamicList <double>(double.NaN);

            x[0] = a;
            x[1] = (a + b) / 2;
            x[2] = b;

            var f = new DyamicList <double>(double.NaN);

            f[0] = equation.Invoke(x[0]);
            f[1] = equation.Invoke(x[1]);
            f[2] = equation.Invoke(x[2]);

            var d = new DyamicList <double>(double.NaN);

            d[0] = x[1] - x[0];
            d[1] = x[2] - x[1];

            var g = new DyamicList <double>(double.NaN);

            g[0] = (f[1] - f[0]) / d[0];
            g[1] = (f[2] - f[1]) / d[1];

            var i = 0;

            var h = new DyamicList <double>(double.NaN);
            var c = new DyamicList <double>(double.NaN);

            while (true)
            {
                h[i] = (g[i + 1] - g[i]) / (d[i + 1] + d[i]);

                c[i] = g[i + 1] + (d[i + 1] * h[i]);

                var sqr = Math.Pow(c[i], 2) - (4 * f[i + 2] * h[i]);

                if (sqr < 0)
                {
                    Console.WriteLine("Cannot converge with interval.");
                    return(Double.NaN);
                }

                d[i + 2] = (-2 * f[i + 2]) / (c[i] + (Math.Sign(c[i]) * Math.Sqrt(sqr)));

                x[i + 3] = x[i + 2] + d[i + 2];

                f[i + 3] = equation.Invoke(x[i + 3]);

                if (Equals(f[i + 3], 0d))
                {
                    f[i + 3] = 0.0000000000000001;
                }

                g[i + 2] = (f[i + 3] - f[i + 2]) / (d[i + 2]);

                Console.WriteLine(x[i + 1]);

                if (x[i + 1].Diff(x[i]) < Error)
                {
                    break;
                }

                i++;
            }

            Console.WriteLine(x.ToString("x", true));
            Console.WriteLine(d.ToString("d", true));
            Console.WriteLine(c.ToString("c", true));
            Console.WriteLine(g.ToString("g", true));
            Console.WriteLine(h.ToString("h", true));
            Console.WriteLine("Result: " + x[i + 1]);

            return(x[i + 1]);
        }