/// <summary>
        /// Removes the third order of a matrix by calculating the parameters a, b, .. i, j of the regression of the matrix in the form: z = a + b*x + c*y + d*x*x + e*x*y + f*y*y + g*x*x*x + h*x*x*y + i*x*y*y + j*y*y*y (x and y are the rows and columns of the matrix, z the matrix elements ),
        /// and then subtracting the regression function from each matrix element, thus effectively removing the third order.
        /// Here, only matrix elements that have a finite value are included in the calculation of the regression.
        /// </summary>
        /// <param name="m">The matrix to change.</param>
        public static void RemoveThirdOrderFromMatrixIgnoringInvalidElements(IMatrix <double> m)
        {
            int rows = m.RowCount;
            int cols = m.ColumnCount;

            int rowsBy2 = rows / 2;
            int colsBy2 = cols / 2;

            var independent = new List <double> [9];

            for (int i = 0; i < independent.Length; ++i)
            {
                independent[i] = new List <double>();
            }

            var dependent = new List <double>();

            for (int r = 0; r < rows; ++r)
            {
                for (int c = 0; c < cols; ++c)
                {
                    var z = m[r, c];
                    if (RMath.IsFinite(z))
                    {
                        long x = c - colsBy2;
                        long y = r - rowsBy2;

                        independent[0].Add(x);
                        independent[1].Add(y);
                        independent[2].Add(x * x);
                        independent[3].Add(x * y);
                        independent[4].Add(y * y);
                        independent[5].Add(x * x * x);
                        independent[6].Add(x * x * y);
                        independent[7].Add(x * y * y);
                        independent[8].Add(y * y * y);
                        dependent.Add(z);
                    }
                }
            }

            var regress = new Altaxo.Calc.Regression.LinearFitBySvd(new MyPriv2ndOrderIndependentMatrix(independent), dependent.ToArray(), null, dependent.Count, 1 + independent.Length, 1e-6);
            var coef    = regress.Parameter;

            for (int r = 0; r < rows; ++r)
            {
                for (int c = 0; c < cols; ++c)
                {
                    var z = m[r, c];
                    if (RMath.IsFinite(z))
                    {
                        long x = c - colsBy2;
                        long y = r - rowsBy2;

                        double offs = coef[0] + coef[1] * x + coef[2] * y + coef[3] * x * x + coef[4] * x * y + coef[5] * y * y + coef[6] * x * x * x + coef[7] * x * x * y + coef[8] * x * y * y + coef[9] * y * y * y;
                        m[r, c] -= offs;
                    }
                }
            }
        }
		/// <summary>
		/// Removes the third order of a matrix by calculating the parameters a, b, .. i, j of the regression of the matrix in the form: z = a + b*x + c*y + d*x*x + e*x*y + f*y*y + g*x*x*x + h*x*x*y + i*x*y*y + j*y*y*y (x and y are the rows and columns of the matrix, z the matrix elements ),
		/// and then subtracting the regression function from each matrix element, thus effectively removing the third order.
		/// Here, only matrix elements that have a finite value are included in the calculation of the regression.
		/// </summary>
		/// <param name="m">The matrix to change.</param>
		public static void RemoveThirdOrderFromMatrixIgnoringInvalidElements(IMatrix m)
		{
			int rows = m.Rows;
			int cols = m.Columns;

			int rowsBy2 = rows / 2;
			int colsBy2 = cols / 2;

			var independent = new List<double>[9];
			for (int i = 0; i < independent.Length; ++i)
				independent[i] = new List<double>();

			var dependent = new List<double>();
			for (int r = 0; r < rows; ++r)
			{
				for (int c = 0; c < cols; ++c)
				{
					var z = m[r, c];
					if (RMath.IsFinite(z))
					{
						long x = c - colsBy2;
						long y = r - rowsBy2;

						independent[0].Add(x);
						independent[1].Add(y);
						independent[2].Add(x * x);
						independent[3].Add(x * y);
						independent[4].Add(y * y);
						independent[5].Add(x * x * x);
						independent[6].Add(x * x * y);
						independent[7].Add(x * y * y);
						independent[8].Add(y * y * y);
						dependent.Add(z);
					}
				}
			}

			var regress = new Altaxo.Calc.Regression.LinearFitBySvd(new MyPriv2ndOrderIndependentMatrix(independent), dependent.ToArray(), null, dependent.Count, 1 + independent.Length, 1e-6);
			var coef = regress.Parameter;

			for (int r = 0; r < rows; ++r)
			{
				for (int c = 0; c < cols; ++c)
				{
					var z = m[r, c];
					if (RMath.IsFinite(z))
					{
						long x = c - colsBy2;
						long y = r - rowsBy2;

						double offs = coef[0] + coef[1] * x + coef[2] * y + coef[3] * x * x + coef[4] * x * y + coef[5] * y * y + coef[6] * x * x * x + coef[7] * x * x * y + coef[8] * x * y * y + coef[9] * y * y * y;
						m[r, c] -= offs;
					}
				}
			}
		}