Exemplo n.º 1
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);
        }
Exemplo n.º 2
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");
        }
Exemplo n.º 3
0
        protected void FindNthTerm_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.GetCoefficientsForNthTerm(seq, degree);
                    var output = FiniteCalculus.GetExpressionForNthTerm(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.";
            }
        }