Example #1
0
        public void TestFactorialNegative()
        {
            int  k      = -1;
            long result = MathExtention.Factorial(k);

            Assert.AreEqual(result, 0);
        }
Example #2
0
        public void TestFactorialZero()
        {
            int  k      = 0;
            long result = MathExtention.Factorial(k);

            Assert.AreEqual(result, 1);
        }
Example #3
0
        public void TestBinomialCooeficientZero()
        {
            int  n      = 0;
            int  k      = 0;
            long result = MathExtention.BinomialCoefficient(k, n);

            Assert.AreEqual(result, 1);
        }
Example #4
0
        public void TestBinomialCooeficientNegative()
        {
            int  n      = -1;
            int  k      = 2;
            long result = MathExtention.BinomialCoefficient(k, n);

            Assert.AreEqual(result, 0);
        }
        private void tql2()
        {
            //  This is derived from the Algol procedures tql2, by
            //  Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
            //  Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
            //  Fortran subroutine in EISPACK.

            for (int i = 1; i < _cols; i++)
            {
                _workingEigenVector2[i - 1] = _workingEigenVector2[i];
            }
            _workingEigenVector2[_cols - 1] = 0.0;

            double f    = 0.0;
            double tst1 = 0.0;
            double eps  = Math.Pow(2.0, -52.0);

            for (int l = 0; l < _cols; l++)
            {
                // Find small subdiagonal element

                tst1 = Math.Max(tst1, Math.Abs(_workingEigenVector1[l]) + Math.Abs(_workingEigenVector2[l]));
                int m = l;
                while (m < _cols)
                {
                    if (Math.Abs(_workingEigenVector2[m]) <= eps * tst1)
                    {
                        break;
                    }
                    m++;
                }

                // If m == l, d[l] is an eigenvalue,
                // otherwise, iterate.

                if (m > l)
                {
                    int iter = 0;
                    do
                    {
                        iter = iter + 1; // (Could check iteration count here.)

                        // Compute implicit shift

                        double g = _workingEigenVector1[l];
                        double p = (_workingEigenVector1[l + 1] - g) / (2.0 * _workingEigenVector2[l]);
                        double r = MathExtention.Hypot(p, 1.0);
                        if (p < 0)
                        {
                            r = -r;
                        }
                        _workingEigenVector1[l]     = _workingEigenVector2[l] / (p + r);
                        _workingEigenVector1[l + 1] = _workingEigenVector2[l] * (p + r);
                        double dl1 = _workingEigenVector1[l + 1];
                        double h   = g - _workingEigenVector1[l];
                        for (int i = l + 2; i < _cols; i++)
                        {
                            _workingEigenVector1[i] -= h;
                        }
                        f = f + h;

                        // Implicit QL transformation.

                        p = _workingEigenVector1[m];
                        double c   = 1.0;
                        double c2  = c;
                        double c3  = c;
                        double el1 = _workingEigenVector2[l + 1];
                        double s   = 0.0;
                        double s2  = 0.0;
                        for (int i = m - 1; i >= l; i--)
                        {
                            c3 = c2;
                            c2 = c;
                            s2 = s;
                            g  = c * _workingEigenVector2[i];
                            h  = c * p;
                            r  = MathExtention.Hypot(p, _workingEigenVector2[i]);
                            _workingEigenVector2[i + 1] = s * r;
                            s = _workingEigenVector2[i] / r;
                            c = p / r;
                            p = c * _workingEigenVector1[i] - s * g;
                            _workingEigenVector1[i + 1] = h + s * (c * g + s * _workingEigenVector1[i]);

                            // Accumulate transformation.

                            for (int k = 0; k < _cols; k++)
                            {
                                h = _eigenVectors[k][i + 1];
                                _eigenVectors[k][i + 1] = s * _eigenVectors[k][i] + c * h;
                                _eigenVectors[k][i]     = c * _eigenVectors[k][i] - s * h;
                            }
                        }
                        p = (-s) * s2 * c3 * el1 * _workingEigenVector2[l] / dl1;
                        _workingEigenVector2[l] = s * p;
                        _workingEigenVector1[l] = c * p;

                        // Check for convergence.
                    }while (Math.Abs(_workingEigenVector2[l]) > eps * tst1);
                }
                _workingEigenVector1[l] = _workingEigenVector1[l] + f;
                _workingEigenVector2[l] = 0.0;
            }

            // Sort eigenvalues and corresponding vectors.

            for (int i = 0; i < _cols - 1; i++)
            {
                int    k = i;
                double p = _workingEigenVector1[i];
                for (int j = i + 1; j < _cols; j++)
                {
                    if (_workingEigenVector1[j] < p)
                    {
                        k = j;
                        p = _workingEigenVector1[j];
                    }
                }
                if (k != i)
                {
                    _workingEigenVector1[k] = _workingEigenVector1[i];
                    _workingEigenVector1[i] = p;
                    for (int j = 0; j < _cols; j++)
                    {
                        p = _eigenVectors[j][i];
                        _eigenVectors[j][i] = _eigenVectors[j][k];
                        _eigenVectors[j][k] = p;
                    }
                }
            }
        }