Example #1
0
        static private void TestPiecewise()
        {
            var x      = new double[] { -2.0, 0, 2 };
            var p1     = new Polynomial(new double[] { -4 });
            var p2     = new Polynomial(new double[] { 4 });
            var myPoly = new Piecewise(x, new Polynomial[] { p1, p2 });

            Debug.Assert(myPoly.Evaluate(-2) == -4);
            Debug.Assert(myPoly.Evaluate(-1) == -4);
            Debug.Assert(myPoly.Evaluate(0) == -4);
            Debug.Assert(myPoly.Evaluate(1) == 4);
            Debug.Assert(myPoly.Evaluate(2) == 4);
            myPoly = myPoly * 2;
            Debug.Assert(myPoly.Evaluate(-2) == -8);
            Debug.Assert(myPoly.Evaluate(-1) == -8);
            Debug.Assert(myPoly.Evaluate(0) == -8);
            Debug.Assert(myPoly.Evaluate(1) == 8);
            Debug.Assert(myPoly.Evaluate(2) == 8);
            myPoly = myPoly * myPoly;
            Debug.Assert(myPoly.Evaluate(-2) == 64);
            Debug.Assert(myPoly.Evaluate(-1) == 64);
            Debug.Assert(myPoly.Evaluate(0) == 64);
            Debug.Assert(myPoly.Evaluate(1) == 64);
            Debug.Assert(myPoly.Evaluate(2) == 64);

            var myPoly2 = new Piecewise(-4, 2, p1);
            var myPoly3 = new Piecewise(-2, 4, p2);
            var myPoly4 = myPoly2 + myPoly3;

            Debug.Assert(myPoly4.Evaluate(-100) == 0);
            Debug.Assert(myPoly4.Evaluate(100) == 0);
            Debug.Assert(myPoly4.Evaluate(0) == 0);
            Debug.Assert(myPoly4.Evaluate(-3) == -4);
            Debug.Assert(myPoly4.Evaluate(3) == 4);
        }
        // using Cox-de Boor formula to calculate basis functions
        // This forms a triangular dependence, see pp 51 An Introduction to NURBS by Rogers
        // This is tricky to implement since adjacent elements of the knot vector can be equal to each other
        public BSpline_Basis_Function(double[] x, int K)
        {
            Polys = new Piecewise[x.Length - 1];
            for (int i = 0; i < Polys.Length; i++)
            {
                Polys[i] = new Piecewise(x[i], x[i + 1], new Polynomial(new double[] { 1 }));
            }
            int Length = Polys.Length - 1;

            for (int iLevel = 2; iLevel < K + 1; iLevel++)
            {
                for (int i = 0; i < Length; i++)
                {
                    var mpoly1 = new Polynomial(new double[] { -x[i], 1 });
                    var mult1  = x[i + iLevel - 1] - x[i];
                    if (mult1 != 0)
                    {
                        mult1 = 1.0 / mult1;
                    }
                    mpoly1 = mpoly1 * mult1;
                    var mpoly2 = new Polynomial(new double[] { x[i + iLevel], -1 });
                    var mult2  = x[i + iLevel] - x[i + 1];
                    if (mult2 != 0)
                    {
                        mult2 = 1.0 / mult2;
                    }
                    mpoly2   = mpoly2 * mult2;
                    Polys[i] = Polys[i] * mpoly1 + Polys[i + 1] * mpoly2;
                }
                Length--;
            }

            Array.Resize(ref Polys, Polys.Length - K + 1);
        }
Example #3
0
        private void FormEditEquation_Load(object sender, EventArgs e)
        {
            richTextBoxEquation.SelectionTabs = new int[] { 25, 50, 75, 100 };
            Global.Attributes.Clear();
            Global.LoadAttributes();
            List <String> listAttribute = new List <String>();

            if (m_bArea)
            {
                listBoxAttribute.Items.Add("AREA");
                listBoxAttribute.Items.Add("LENGTH");
            }

            richTextBoxEquation.Text = m_strEquation;

            if (m_isPiecewise)
            {
                Piecewise pw = new Piecewise(richTextBoxEquation.Text);
                foreach (AgeValue ageValue in pw.AgeValueList)
                {
                    dgvPerformance.Rows.Add(ageValue.Age, ageValue.Value);
                }
            }
            checkBoxAsFunction.Checked = m_isFunction;
            ReloadAttributes();

            m_hashFunction.Add("E", "Represents the natural logarithmic base, specified by the constant, e.");
            m_hashFunction.Add("PI", "Represents the ratio of the circumference of a circle to its diameter, specified by the constant, pi.");

            m_hashFunction.Add("Abs(x)", "Returns the absolute value of a specified number.");
            m_hashFunction.Add("Acos(x)", "Returns the angle whose cosine is the specified number.");
            m_hashFunction.Add("Asin(x)", "Returns the angle whose sine is the specified number.");
            m_hashFunction.Add("Atan(x)", "Returns the angle whose tangent is the specified number.");
            m_hashFunction.Add("Atan(x,y)", "Returns the angle whose tangent is the quotient of two specified numbers.");
            m_hashFunction.Add("Ceiling(x)", "Returns the smallest whole number greater than or equal to the specified number.");
            m_hashFunction.Add("Cos(x)", "Returns the cosine of the specified angle.");
            m_hashFunction.Add("Cosh(x)", "Returns the hyperbolic cosine of the specified angle.");
            m_hashFunction.Add("Exp(x)", "Returns e raised to the specified power.");
            m_hashFunction.Add("Floor(x)", "Returns the largest whole number less than or equal to the specified number.");
            m_hashFunction.Add("IEEERemainder(x,y)", "Returns the remainder resulting from the division of a specified number by another specified number.");
            m_hashFunction.Add("Log(x)", "Returns the logarithm of a specified number.");
            m_hashFunction.Add("Log10(x)", "Returns the base 10 logarithm of a specified number.");
            m_hashFunction.Add("Max(x,y)", "Returns the larger of two specified numbers.");
            m_hashFunction.Add("Min(x,y)", "Returns the smaller of two numbers.");
            m_hashFunction.Add("Pow(x,y)", "Returns a specified number raised to the specified power.");
            m_hashFunction.Add("Round(x)", "Returns the number nearest the specified value.");
            m_hashFunction.Add("Sign(x)", "Returns a value indicating the sign of a number.");
            m_hashFunction.Add("Sin(x)", "Returns the sine of the specified angle.");
            m_hashFunction.Add("Sinh(x)", "Returns the hyperbolic sine of the specified angle.");
            m_hashFunction.Add("Sqrt(x)", "Returns the square root of a specified number.");
            m_hashFunction.Add("Tan(x)", "Returns the tangent of the specified angle.");
            m_hashFunction.Add("Tanh(x)", "Returns the hyperbolic tangent of the specified angle.");

            FillDefault();
        }
Example #4
0
    private static double u_x1_01(double t)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    U_X1_01 evaluates U at the boundary X1.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    24 January 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double T, the time.
    //
    //    Output, double U, the value of U(T,X1).
    //
    {
        int nd = 6;

        double[] td =
        {
            0.0, 0.10, 0.20, 0.30, 0.40, 0.50
        }

        ;
        double[] tv = new double[1];
        double[] ud =
        {
            0.0, 2.0, 10.0, 8.0, 5.0, 0.0
        }

        ;

        tv[0] = t;

        double[] uv = Piecewise.piecewise_linear(nd, td, ud, 1, tv);

        double u = uv[0];

        return(u);
    }