/// <summary>
        /// Handles the graphical plotting of the 2D function.
        /// </summary>
        protected override void PlotFunction()
        {
            // Create a temporary PFunction2D which will be used
            // in order to preview current data.
            PFunction2D tempFunction = new PFunction2D();

            DataGridToPointFunctionData(tempFunction);

            if (tempFunction.Xcoordinates.Length == 0 || tempFunction.Ycoordinates.Length == 0)
            {
                MessageBox.Show(this, "Cannot draw underlying function, it contains inconsistencies!");
                return;
            }
            // Get the defined coordinates for the function in order
            // to define its edges and set them for the plot.
            // The values are in this order.
            // 0 => X1 start
            // 1 => X1 end
            // 2 => X2 start
            // 3 => X2 end.
            Vector startingValues = new Vector(4);

            startingValues[0] = tempFunction.Xcoordinates[0].V();
            startingValues[1] = tempFunction.Xcoordinates[tempFunction.Xcoordinates.Length - 1].V();
            startingValues[2] = tempFunction.Ycoordinates[0].V();
            startingValues[3] = tempFunction.Ycoordinates[tempFunction.Ycoordinates.Length - 1].V();

            // The coordinates are always 2 here as it's a 2D function.
            base.OnPlotGenericFunction(2, tempFunction, startingValues);
        }
Esempio n. 2
0
        public void TestZeroOrderInterpolation1()
        {
            PFunction2D func = SetTestData1();

            func.Interpolation = EInterpolationType.ZERO_ORDER;
            func.Parse(null);
            Assert.AreEqual(100, func.Evaluate(50, 50));
        }
Esempio n. 3
0
        public void TestLinearInterpolation1()
        {
            PFunction2D func = SetTestData1();

            func.Interpolation = EInterpolationType.LINEAR;
            func.Parse(null);
            Assert.AreEqual(50, func.Evaluate(50, 50));
        }
 /// <summary>
 /// Copies the data stored in this object in another object.
 /// </summary>
 /// <remarks>
 /// Anything stored in the other object is overwritten.
 /// </remarks>
 /// <param name="other">The object where to copy this object to.</param>
 internal void CopyTo(PFunction2D other)
 {
     other.SetSizes(this.coordinatesX.Length, this.coordinatesY.Length);
     other.coordinatesX             = (IRightValue[])this.coordinatesX.Clone();
     other.coordinatesY             = (IRightValue[])this.coordinatesY.Clone();
     other.values                   = (IRightValue[, ]) this.values.Clone();
     other.extrapolationType        = this.extrapolationType;
     other.interpolationType        = this.interpolationType;
     other.leastSquaresCoefficients = this.leastSquaresCoefficients;
 }
        /// <summary>
        /// Loads the data from the function on the grid
        /// so it's possible to view and edit it.
        /// </summary>
        private void PointFunctionDataToDataGrid()
        {
            // Take a reference to the function casted to PFunction2D
            // so we can access its members, it's assured to be one
            // from checks in Fairmat (due to the ProvidesTo implementation).
            PFunction2D function = (PFunction2D)base.m_Function;

            // Clear all the columns so it's possible to populate them from clean.
            base.fairmatDataGridViewPointData.Columns.Clear();

            // As we use our numbers disable the functionality to
            // number the rows from the fairmatDataGridView control.
            base.fairmatDataGridViewPointData.ShowRowNumber = false;

            // Creates the columns for each given x coordinate point stored in the function.
            for (int i = 0; i < function.Xcoordinates.Length; i++)
            {
                // The column name will have the coordinate associated to the specific x entry.
                base.fairmatDataGridViewPointData.ColumnAdd(function.Xcoordinates[i].Expression);
            }

            // Populates all the rows of the matrix which defines the function.
            for (int y = 0; y < function.Ycoordinates.Length; y++)
            {
                // Creates a row for each y coordinate present in the function definition.
                DataGridViewRow row = new DataGridViewRow();

                // Sets the header cell for the row to contain the value of the coordinate.
                row.HeaderCell.Value = function.Ycoordinates[y].Expression;

                // Finally fill each cell of the current row with the respective value.
                for (int x = 0; x < function.Xcoordinates.Length; x++)
                {
                    DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                    cell.Value = function[x, y].Expression;
                    row.Cells.Add(cell);
                }

                // Finally add the row which was just constructed to the grind.
                base.fairmatDataGridViewPointData.Rows.Add(row);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the data from the Data Grid and puts it back in the function.
        /// </summary>
        /// <param name="destination">
        /// The <see cref="PFunction2D"/> object where
        /// the data will be stored if it succeeds validation.
        /// </param>
        /// <returns>True if the operation was successful, False otherwise.</returns>
        private bool DataGridToPointFunctionData(PFunction2D destination)
        {
            PFunction2D tempFunction = new PFunction2D(null);

            // Check if bind was executed. Is it still needed?
            if (m_Project != null)
            {
                m_Project.ResetExpressionParser();
                m_Project.CreateSymbols();

            #if MONO
                // If it's running on Mono the event EditingControlShowing doesn't work, so
                // convert the decimal separators in the data grid before calculating the points.
                if (Engine.RunningOnMono)
                    fairmatDataGridViewPointData.ConvertDecimalSeparators();
            #endif

                DataGridViewRowCollection rows = fairmatDataGridViewPointData.Rows;
                DataGridViewColumnCollection columns = fairmatDataGridViewPointData.Columns;

                // Sets the sizes of the function. Additionally check if the row.Count is zero,
                // in that case it means there are no columns and rows and so must be handled
                // separately
                tempFunction.SetSizes(columns.Count, rows.Count > 0 ? rows.Count - 1 : 0);

                // First load the column headers values
                for (int x = columns.Count - 1; x >= 0; x--)
                {
                    // DataGridView in case the header cell is edited to an empty string replaces
                    // its value with DBNull (non existent value).
                    // So, substitute the cell value explicitly with the empty string.
                    if (columns[x].HeaderCell.Value is DBNull ||
                        columns[x].HeaderCell.Value == null)
                    {
                        columns[x].HeaderCell.Value = string.Empty;
                    }

                    try
                    {
                        tempFunction[x, -1] = RightValue.ConvertFrom(columns[x].HeaderText, true);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("The string " + columns[x].HeaderText + " (position " +
                                        (x + 1) + ") is invalid due to: " + e.Message,
                                        DataExchange.ApplicationName);
                        return false;
                    }
                }

                // Then the rows and the cells.
                for (int y = rows.Count - 2; y >= 0; y--)
                {
                    // DataGridView in case the header cell is edited to an empty string replaces
                    // its value with DBNull (non existent value).
                    // So, substitute the cell value explicitly with the empty string.
                    if (rows[y].HeaderCell.Value is DBNull || rows[y].HeaderCell.Value == null)
                    {
                        rows[y].HeaderCell.Value = string.Empty;
                    }

                    try
                    {
                        tempFunction[-1, y] = RightValue.ConvertFrom(rows[y].HeaderCell.Value, true);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("The string " + rows[y].HeaderCell.Value + " (position " +
                                        (y + 1) + ") is invalid due to: " + e.Message,
                                        DataExchange.ApplicationName);
                        return false;
                    }

                    for (int x = 0; x < columns.Count; x++)
                    {
                        // DataGridView in case the cell is edited to an empty string replaces
                        // its value with DBNull (non existent value).
                        // So, substitute the cell value explicitly with the empty string.
                        if (rows[y].Cells[x].Value is DBNull)
                        {
                            rows[y].Cells[x].Value = string.Empty;
                        }

                        try
                        {
                            tempFunction[x, y] = RightValue.ConvertFrom(rows[y].Cells[x].Value, true);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("The string " + rows[y].Cells[x].Value +
                                            " is invalid due to: " + e.Message,
                                            DataExchange.ApplicationName);
                            return false;
                        }
                    }
                }
            }

            try
            {
                // Save the information about the interpolation/extrapolation
                tempFunction.Interpolation = this.interpolationExtrapolationControlPF.GetInterpolationType();
                tempFunction.LeastSquaresCoefficients = this.interpolationExtrapolationControlPF.LeastSquareCoefficients;
                tempFunction.Extrapolation = this.interpolationExtrapolationControlPF.GetExtrapolationType();
            }
            catch (Exception e)
            {
                MessageBox.Show("The selected functionality is currently not usable: " + e.Message,
                                DataExchange.ApplicationName);
                return false;
            }

            tempFunction.CopyTo(destination);
            return true;
        }
Esempio n. 7
0
        /// <summary>
        /// Handles the graphical plotting of the 2D function.
        /// </summary>
        protected override void PlotFunction()
        {
            // Create a temporary PFunction2D which will be used
            // in order to preview current data.
            PFunction2D tempFunction = new PFunction2D(null);
            DataGridToPointFunctionData(tempFunction);

            // The coordinates are always 2 here as it's a 2D function.
            base.OnPlotGenericFunction(2, tempFunction);
        }
        /// <summary>
        /// Handles the graphical plotting of the 2D function.
        /// </summary>
        protected override void PlotFunction()
        {
            // Create a temporary PFunction2D which will be used
            // in order to preview current data.
            PFunction2D tempFunction = new PFunction2D();
            DataGridToPointFunctionData(tempFunction);

            if (tempFunction.Xcoordinates.Length == 0 || tempFunction.Ycoordinates.Length == 0)
            {
                MessageBox.Show(this, "Cannot draw underlying function, it contains inconsistencies!");
                return;
            }
            // Get the defined coordinates for the function in order
            // to define its edges and set them for the plot.
            // The values are in this order.
            // 0 => X1 start
            // 1 => X1 end
            // 2 => X2 start
            // 3 => X2 end.
            Vector startingValues = new Vector(4);
            startingValues[0] = tempFunction.Xcoordinates[0].V();
            startingValues[1] = tempFunction.Xcoordinates[tempFunction.Xcoordinates.Length - 1].V();
            startingValues[2] = tempFunction.Ycoordinates[0].V();
            startingValues[3] = tempFunction.Ycoordinates[tempFunction.Ycoordinates.Length - 1].V();

            // The coordinates are always 2 here as it's a 2D function.
            base.OnPlotGenericFunction(2, tempFunction, startingValues);
        }
Esempio n. 9
0
 /// <summary>
 /// Copies the data stored in this object in another object.
 /// </summary>
 /// <remarks>
 /// Anything stored in the other object is overwritten.
 /// </remarks>
 /// <param name="other">The object where to copy this object to.</param>
 internal void CopyTo(PFunction2D other)
 {
     other.SetSizes(this.coordinatesX.Length, this.coordinatesY.Length);
     other.coordinatesX = (IRightValue[])this.coordinatesX.Clone();
     other.coordinatesY = (IRightValue[])this.coordinatesY.Clone();
     other.values = (IRightValue[,])this.values.Clone();
     other.extrapolationType = this.extrapolationType;
     other.interpolationType = this.interpolationType;
     other.leastSquaresCoefficients = this.leastSquaresCoefficients;
 }
        /// <summary>
        /// Gets the data from the Data Grid and puts it back in the function.
        /// </summary>
        /// <param name="destination">
        /// The <see cref="PFunction2D"/> object where
        /// the data will be stored if it succeeds validation.
        /// </param>
        /// <returns>True if the operation was successful, False otherwise.</returns>
        private bool DataGridToPointFunctionData(PFunction2D destination)
        {
            PFunction2D tempFunction = new PFunction2D();

            // Check if bind was executed. Is it still needed?
            if (base.m_Project != null)
            {
                base.m_Project.ResetExpressionParser();
                base.m_Project.CreateSymbols();

#if MONO
                // If it's running on Mono the event EditingControlShowing doesn't work, so
                // convert the decimal separators in the data grid before calculating the points.
                if (Engine.RunningOnMono)
                {
                    fairmatDataGridViewPointData.ConvertDecimalSeparators();
                }
#endif

                DataGridViewRowCollection    rows    = fairmatDataGridViewPointData.Rows;
                DataGridViewColumnCollection columns = fairmatDataGridViewPointData.Columns;

                // Sets the sizes of the function. Additionally check if the row.Count is zero,
                // in that case it means there are no columns and rows and so must be handled
                // separately
                tempFunction.SetSizes(columns.Count, rows.Count > 0 ? rows.Count - 1 : 0);

                // First load the column headers values
                for (int x = columns.Count - 1; x >= 0; x--)
                {
                    // DataGridView in case the header cell is edited to an empty string replaces
                    // its value with DBNull (non existent value).
                    // So, substitute the cell value explicitly with the empty string.
                    if (columns[x].HeaderCell.Value is DBNull ||
                        columns[x].HeaderCell.Value == null)
                    {
                        columns[x].HeaderCell.Value = string.Empty;
                    }

                    try
                    {
                        tempFunction[x, -1] = RightValue.ConvertFrom(columns[x].HeaderText, true);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("The string " + columns[x].HeaderText + " (column " +
                                        "position " + (x + 1) + ") is invalid due to: " +
                                        e.Message, DataExchange.ApplicationName);
                        return(false);
                    }
                }

                // Then the rows and the cells.
                for (int y = rows.Count - 2; y >= 0; y--)
                {
                    // DataGridView in case the header cell is edited to an empty string replaces
                    // its value with DBNull (non existent value).
                    // So, substitute the cell value explicitly with the empty string.
                    if (rows[y].HeaderCell.Value is DBNull || rows[y].HeaderCell.Value == null)
                    {
                        rows[y].HeaderCell.Value = string.Empty;
                    }

                    try
                    {
                        tempFunction[-1, y] = RightValue.ConvertFrom(rows[y].HeaderCell.Value, true);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("The string " + rows[y].HeaderCell.Value + " (row " +
                                        "position " + (y + 1) + ") is invalid due to: " +
                                        e.Message, DataExchange.ApplicationName);
                        return(false);
                    }

                    for (int x = 0; x < columns.Count; x++)
                    {
                        // DataGridView in case the cell is edited to an empty string replaces
                        // its value with DBNull (non existent value).
                        // So, substitute the cell value explicitly with the empty string.
                        if (rows[y].Cells[x].Value is DBNull)
                        {
                            rows[y].Cells[x].Value = string.Empty;
                        }

                        try
                        {
                            tempFunction[x, y] = RightValue.ConvertFrom(rows[y].Cells[x].Value, true);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("The string " + rows[y].Cells[x].Value +
                                            " (position " + (x + 1) + ", " + (y + 1) + ") " +
                                            "is invalid due to: " + e.Message,
                                            DataExchange.ApplicationName);
                            return(false);
                        }
                    }
                }
            }

            try
            {
                // Save the information about the interpolation/extrapolation
                tempFunction.Interpolation            = this.interpolationExtrapolationControlPF.GetInterpolationType();
                tempFunction.LeastSquaresCoefficients = this.interpolationExtrapolationControlPF.LeastSquareCoefficients;
                tempFunction.Extrapolation            = this.interpolationExtrapolationControlPF.GetExtrapolationType();
            }
            catch (Exception e)
            {
                MessageBox.Show("The selected functionality is currently not usable: " + e.Message,
                                DataExchange.ApplicationName);
                return(false);
            }

            tempFunction.CopyTo(destination);
            return(true);
        }