Beispiel #1
0
        public void HasPatternTest()
        {
            int returnVal;

            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 2, 6, 12, 20, 30, 42, 56, 72 }, out returnVal), true);
            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 2, 6, 12, 20 }, out returnVal), true);
            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023 }, out returnVal), false); // dangerous case that will not work because of 2^x
            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 0, 2, 8, 26, 80, 242, 728, 2186, 6560 }, out returnVal), false);
        }
Beispiel #2
0
        public void HasPatternForceCheck()
        {
            int index;

            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 1, 2, 3, 4, 5, 6 }, out index), true);
            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 1, 2, 3, 4, 5, 7 }, out index, true), false);

            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 0, 1, 4, 9, 16, 25 }, out index, true), true);
            Assert.AreEqual(FiniteCalculus.HasPattern(new double[] { 0, 1, 4, 9, 16, 26 }, out index, true), false);
        }
Beispiel #3
0
        public void GetCoefficientsForNthTermTest()
        {
            int index;

            double[] sequence = { 1, 2, 3, 4, 5 };

            FiniteCalculus.HasPattern(sequence, out index);

            var a = FiniteCalculus.GetCoefficientsForNthTerm(sequence, index);

            Assert.AreEqual(a[0], 1.0);
            Assert.AreEqual(a[1], 0.0);
        }
Beispiel #4
0
        public void GetExpressionForNthTermTest()
        {
            int index;

            double[] sequence = { 1, 2, 3, 4, 5 };

            FiniteCalculus.HasPattern(sequence, out index);

            var expression = FiniteCalculus.GetExpressionForNthTerm(FiniteCalculus.GetCoefficientsForNthTerm(sequence, index));

            Assert.AreEqual(expression, "1x");

            double[] sequence2 = { 1, 4, 8, 13 };

            FiniteCalculus.HasPattern(sequence2, out index);

            var expression2 = FiniteCalculus.GetExpressionForNthTerm(FiniteCalculus.GetCoefficientsForNthTerm(sequence2, index));

            Assert.AreEqual(expression2, "0.8888888889x^2+0.4444444444x+0.1111111111");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text != "")
            {
                var tokens = TextBox1.Text.Split(',');

                double[] seq;
                try
                {
                    seq = Array.ConvertAll(tokens, double.Parse);
                }
                catch
                {
                    Label1.Text = "Could not convert the sequence into an array.";

                    return;
                }

                int degree;

                if (FiniteCalculus.HasPattern(seq, out degree))
                {
                    var coeff  = FiniteCalculus.GetCoefficientsForNthSum(seq, degree);
                    var output = FiniteCalculus.GetExpressionForNthSum(coeff);

                    Label1.Text = output;
                }
                else
                {
                    Label1.Text = "Not enough terms to spot a pattern or not a polynomial sequence.";
                }
            }
            else
            {
                Label1.Text = "No sequence was entered.";
            }
        }