Exemplo n.º 1
0
        public static IEnumerable <Movement> LINQ(string[] lines)
        {
            IEnumerable <Movement> enumerable()
            {
                foreach (var Lu in lines.Skip(1))
                {
                    var split = Lu.Split(';');
                    yield return(new Movement
                    {
                        Date = split[0],
                        Object_A = split[1],
                        Type_A = split[2],
                        Object_B = split[3],
                        Type_B = split[4],
                        Direction = split[5],
                        Color = split[6],
                        Intensity = int.Parse(split[7]),
                        LatitudeA = double.Parse(split[8], CultureInfo.InvariantCulture),
                        LongitudeA = double.Parse(split[9], CultureInfo.InvariantCulture),
                        LatitudeB = double.Parse(split[10], CultureInfo.InvariantCulture),
                        LongitudeB = double.Parse(split[11], CultureInfo.InvariantCulture)
                    });

                    if (checkPressed == true)
                    {
                        break;
                    }
                    lineValue++;
                }
            }

            var data = enumerable();

            return(data.ToList());;
        }
Exemplo n.º 2
0
        public static double[,] SolveWith(double[,] matrix, double[] v, Lu lu)
        {
            int nrows = matrix.GetLength(0);
            int ncols = matrix.GetLength(1);

            if (!IsSquare(matrix))
            {
                throw new Exception("SolveWith requires a square matrix.");
            }
            if (nrows != v.Length)
            {
                throw new Exception("Wrong number of results in solution vector.");
            }
            double[,] b = new double[nrows, ncols];
            for (int i = 0; i < nrows; i++)
            {
                b[lu.pi[i], 0] = v[i];
            }
            if (lu.llu == null)
            {
                lu.llu = LuDecomposition(lu.l);
            }
            double[,] z = ForwardSubstitution(lu.l, lu.llu, b);
            if (lu.ulu == null)
            {
                lu.ulu = LuDecomposition(lu.u);
            }
            double[,] x = BackwardSubstitution(lu.u, lu.ulu, z);
            return(x);
        }
Exemplo n.º 3
0
        public void FailOnDecomposeNonSquareMatrix()
        {
            // Arrange
            var nonSquareMatrix = new double[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0, 0, 0 }
            };

            // Act
            void Act(double[,] source) => Lu.Decompose(source);

            // Assert
            Assert.Throws <ArgumentException>(() => Act(nonSquareMatrix));
        }
Exemplo n.º 4
0
        public void EliminateIdentityEquation()
        {
            // Arrange
            var identityMatrix = new double[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
            };
            var coefficients = new double[] { 1, 2, 3 };

            // Act
            var solution = Lu.Eliminate(identityMatrix, coefficients);

            // Assert
            Assert.AreEqual(coefficients, solution);
        }
Exemplo n.º 5
0
        public void FailOnEliminateEquationWithNonSquareMatrix()
        {
            // Arrange
            var nonSquareMatrix = new double[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0, 0, 0 }
            };
            var coefficients = new double[] { 1, 2, 3, 4 };

            // Act
            void Act(double[,] source, double[] c) => Lu.Eliminate(source, c);

            // Assert
            Assert.Throws <ArgumentException>(() => Act(nonSquareMatrix, coefficients));
        }
Exemplo n.º 6
0
        public virtual double Determinant()
        {
            if (Lu.RowCount() != Lu.ColumnCount())
            {
                throw new ArgumentException("Matrix must be square.");
            }
            double d = PivotSign;

            for (int j = 0; j < Lu.ColumnCount(); j++)
            {
                d *= Lu[j, j];
            }
            return(d);
        }
Exemplo n.º 7
0
        public static double[,] Invert(double[,] matrix, Lu lu)
        {
            int nrows = matrix.GetLength(0);
            int ncols = matrix.GetLength(1);

            double[,] inv = new double[nrows, ncols];
            for (int i = 0; i < nrows; i++)
            {
                double[] ei = new double[nrows];
                ei[lu.pi[lu.pi[i]]] = 1;
                double[,] col       = SolveWith(matrix, ei, lu);
                SetColumn(inv, ExtractColumn(col, 0), i);
            }
            return(inv);
        }
Exemplo n.º 8
0
        public void EliminateEquation_Case3X3()
        {
            // Arrange
            var source = new double[, ] {
                { 2, 1, -1 }, { -3, -1, 2 }, { -2, 1, 2 }
            };
            var coefficients     = new double[] { 8, -11, -3 };
            var expectedSolution = new double[] { 2, 3, -1 };

            // Act
            var solution = Lu.Eliminate(source, coefficients);

            // Assert
            Assert.IsTrue(VectorMembersAreEqual(expectedSolution, solution));
        }
Exemplo n.º 9
0
        public static double[,] BackwardSubstitution(double[,] a, Lu alu, double[,] b)
        {
            int n = a.GetLength(0);

            double[,] x = new double[n, 1];
            for (int i = n - 1; i > -1; i--)
            {
                x[i, 0] = b[i, 0];
                for (int j = n - 1; j > i; j--)
                {
                    x[i, 0] -= a[i, j] * x[j, 0];
                }
                x[i, 0] = x[i, 0] / a[i, i];
            }
            return(x);
        }
Exemplo n.º 10
0
        public static double[,] ForwardSubstitution(double[,] a, Lu alu, double[,] b)
        {
            int n = a.GetLength(0);

            double[,] x = new double[n, 1];
            for (int i = 0; i < n; i++)
            {
                x[i, 0] = b[i, 0];
                for (int j = 0; j < i; j++)
                {
                    x[i, 0] -= a[i, j] * x[j, 0];
                }
                x[i, 0] = x[i, 0] / a[i, i];
            }
            return(x);
        }
Exemplo n.º 11
0
        /// <summary>Solve A*X = B</summary>
        /// <param name="B">
        ///     A Matrix with as many rows as A and any number of columns.
        /// </param>
        /// <returns>
        ///     X so that L*U*X = B(piv,:)
        /// </returns>
        /// <exception cref="System.ArgumentException">
        ///     Matrix row dimensions must agree.
        /// </exception>
        /// <exception cref="System.SystemException">
        ///     Matrix is singular.
        /// </exception>
        public virtual double[,] Solve(double[,] B)
        {
            if (B.RowCount() != Lu.RowCount())
            {
                throw new ArgumentException("Matrix row dimensions must agree.");
            }
            if (!IsNonSingular)
            {
                throw new SystemException("Matrix is singular.");
            }

            int n = Lu.ColumnCount();
            // Copy right hand side with pivoting
            int nx = B.ColumnCount();

            double[,] X = B.Submatrix(Pivot, 0, nx - 1);

            // Solve L*Y = B(piv,:)
            for (int k = 0; k < n; k++)
            {
                for (int i = k + 1; i < n; i++)
                {
                    for (int j = 0; j < nx; j++)
                    {
                        X[i, j] -= X[k, j] * Lu[i, k];
                    }
                }
            }
            // Solve U*X = Y;
            for (int k = n - 1; k >= 0; k--)
            {
                for (int j = 0; j < nx; j++)
                {
                    X[k, j] /= Lu[k, k];
                }
                for (int i = 0; i < k; i++)
                {
                    for (int j = 0; j < nx; j++)
                    {
                        X[i, j] -= X[k, j] * Lu[i, k];
                    }
                }
            }
            return(X);
        }
Exemplo n.º 12
0
        public void EliminateEquation_Case4X4()
        {
            // Arrange
            var source = new[, ]
            {
                { 1.0, 2.0, -3.0, -1.0 },
                { 0.0, -3.0, 2.0, 6.0 },
                { 0.0, 5.0, -6.0, -2.0 },
                { 0.0, -1.0, 8.0, 1.0 },
            };
            var coefficients     = new[] { 0.0, -8.0, 0.0, -8.0 };
            var expectedSolution = new[] { -1.0, -2.0, -1.0, -2.0 };

            // Act
            var solution = Lu.Eliminate(source, coefficients);

            // Assert
            Assert.IsTrue(VectorMembersAreEqual(expectedSolution, solution));
        }
        private void radButton1_Click(object sender, EventArgs e)
        {
            double Hus, Bwu, Hds, Bds, QU, Lu;

            //up stream retaing wall
            Hus = h + He + (double)0.5;
            Bwu = (double)2 / 3 * Hus;

            //down stream retaing wall
            Hds = d2 + 0.5;
            Bds = (double)2 / 3 * Hds;

            QU = X * Qp;
            Lu = QU / ((double)1.71 * (Math.Pow(Hd + h, (double)3 / 2)));

            undersluiceradTextBox1.Text = Lu.ToString();
            HeightradTextBox1.Text      = Hus.ToString();
            BottomwidthradTextBox3.Text = Bwu.ToString();
            downheightradTextBox4.Text  = Hds.ToString();
            downbottomradTextBox2.Text  = Bds.ToString();
        }
Exemplo n.º 14
0
        public void DecomposeMatrix_Case3X3()
        {
            // Arrange
            var source = new double[, ] {
                { 2, 1, 4 }, { 7, 1, 1 }, { 4, 2, 9 }
            };
            var expectedLower = new[, ] {
                { 1, 0, 0 }, { 3.5, 1, 0 }, { 2, 0, 1 }
            };
            var expectedUpper = new[, ] {
                { 2, 1, 4 }, { 0, -2.5, -13 }, { 0, 0, 1 }
            };

            // Act
            (double[,] lower, double[,] upper) = Lu.Decompose(source);

            // Assert
            Assert.AreEqual(expectedLower, lower);
            Assert.AreEqual(expectedUpper, upper);
            Assert.AreEqual(lower.Multiply(upper), source);
        }
Exemplo n.º 15
0
        public void DecomposeIdentityMatrix()
        {
            // Arrange
            var identityMatrix = new double[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
            };
            var expectedLower = new double[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
            };
            var expectedUpper = new double[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
            };

            // Act
            (double[,] lower, double[,] upper) = Lu.Decompose(identityMatrix);

            // Assert
            Assert.AreEqual(expectedLower, lower);
            Assert.AreEqual(expectedUpper, upper);
            Assert.AreEqual(lower.Multiply(upper), identityMatrix);
        }
Exemplo n.º 16
0
        public void DecomposeMatrix_Case4X4()
        {
            // Arrange
            var source = new[, ] {
                { 1, 2, 4.5, 7 }, { 3, 8, 0.5, 2 }, { 2, 6, 4, 1.5 }, { 4, 14, 2, 10.5 }
            };
            var expectedLower = new[, ] {
                { 1, 0, 0, 0 }, { 3, 1, 0, 0 }, { 2, 1, 1, 0 }, { 4, 3, 2.875, 1 }
            };
            var expectedUpper = new[, ] {
                { 1, 2, 4.5, 7 }, { 0, 2, -13, -19 }, { 0, 0, 8, 6.5 }, { 0, 0, 0, 20.8125 }
            };

            // Act
            (double[,] lower, double[,] upper) = Lu.Decompose(source);

            // Assert
            Assert.AreEqual(expectedLower, lower);
            Assert.AreEqual(expectedUpper, upper);
            Assert.AreEqual(lower.Multiply(upper), source);
        }
Exemplo n.º 17
0
        public static Lu LuDecomposition(double[,] matrix)
        {
            if (!IsSquare(matrix))
            {
                throw new Exception("LU decomposition requires a square matrix");
            }
            int nrows = matrix.GetLength(0);
            int ncols = matrix.GetLength(1);
            Lu  lu    = new Lu {
                l = Identity(nrows, ncols), u = (double[, ])matrix.Clone(), pi = new int[nrows]
            };

            for (int i = 0; i < nrows; i++)
            {
                lu.pi[i] = i;
            }
            int k0 = 0;

            lu.determinantOfPi = 1;
            for (int k = 0; k < ncols - 1; k++)
            {
                double p = 0;
                // find the row with the biggest pivot
                for (int i = k; i < nrows; i++)
                {
                    if (Math.Abs(lu.u[i, k]) > p)
                    {
                        p  = Math.Abs(lu.u[i, k]);
                        k0 = i;
                    }
                }
                if (p == 0)
                {
                    throw new Exception("The matrix is singular!");
                }
                // switch two rows in permutation matrix
                int pom1 = lu.pi[k];
                lu.pi[k]  = lu.pi[k0];
                lu.pi[k0] = pom1;
                for (int i = 0; i < k; i++)
                {
                    double pom2 = lu.l[k, i];
                    lu.l[k, i]  = lu.l[k0, i];
                    lu.l[k0, i] = pom2;
                }
                if (k != k0)
                {
                    lu.determinantOfPi *= -1;
                }
                // Switch rows in U
                for (int i = 0; i < ncols; i++)
                {
                    double pom2 = lu.u[k, i];
                    lu.u[k, i]  = lu.u[k0, i];
                    lu.u[k0, i] = pom2;
                }
                for (int i = k + 1; i < nrows; i++)
                {
                    lu.l[i, k] = lu.u[i, k] / lu.u[k, k];
                    for (int j = k; j < ncols; j++)
                    {
                        lu.u[i, j] = lu.u[i, j] - lu.l[i, k] * lu.u[k, j];
                    }
                }
            }
            return(lu);
        }