Ejemplo n.º 1
0
        public static VectorR Transform(VectorR v, MatrixR m)
        {
            VectorR result = new VectorR(v.GetSize());

            if (!m.IsSquared())
            {
                throw new ArgumentOutOfRangeException(
                          "Dimension", m.GetRows(), "The matrix must be squared!");
            }
            if (m.GetRows() != v.GetSize())
            {
                throw new ArgumentOutOfRangeException(
                          "Size", v.GetSize(), "The size of the vector must be equal"
                          + "to the number of rows of the matrix!");
            }
            for (int i = 0; i < m.GetRows(); i++)
            {
                result[i] = 0.0;
                for (int j = 0; j < m.GetCols(); j++)
                {
                    result[i] += v[j] * m[j, i];
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public VectorR GetUnitVector()
        {
            VectorR result = new VectorR(vector);

            result.Normalize();
            return(result);
        }
Ejemplo n.º 3
0
        public VectorR Clone()
        {
            // returns a deep copy of the vector
            VectorR v = new VectorR(vector);

            v.vector = (double[])vector.Clone();
            return(v);
        }
Ejemplo n.º 4
0
        public static double DotProduct(VectorR v1, VectorR v2)
        {
            double result = 0.0;

            for (int i = 0; i < v1.size; i++)
            {
                result += v1[i] * v2[i];
            }
            return(result);
        }
Ejemplo n.º 5
0
        public static VectorR operator /(double d, VectorR v)
        {
            VectorR result = new VectorR(v.size);

            for (int i = 0; i < v.size; i++)
            {
                result[i] = d / v[i];
            }
            return(result);
        }
Ejemplo n.º 6
0
        public static VectorR operator /(VectorR v, double d)
        {
            VectorR result = new VectorR(v.size);

            for (int i = 0; i < v.size; i++)
            {
                result[i] = v[i] / d;
            }
            return(result);
        }
Ejemplo n.º 7
0
        public static VectorR operator -(VectorR v1, VectorR v2)
        {
            VectorR result = new VectorR(v1.size);

            for (int i = 0; i < v1.size; i++)
            {
                result[i] = v1[i] - v2[i];
            }
            return(result);
        }
Ejemplo n.º 8
0
        public VectorR GetColVector(int n)
        {
            if (n < 0 || n > Cols)
            {
                throw new ArgumentOutOfRangeException(
                          "n", n, "n is out of range!");
            }
            VectorR v = new VectorR(Rows);

            for (int i = 0; i < Rows; i++)
            {
                v[i] = matrix[i, n];
            }
            return(v);
        }
Ejemplo n.º 9
0
        public static VectorR LinearRegression(double[] xarray, double[] yarray, ModelFunction[] f, out double sigma)
        {
            int     m = f.Length;
            MatrixR A = new MatrixR(m, m);
            VectorR b = new VectorR(m);
            int     n = xarray.Length;

            for (int k = 0; k < m; k++)
            {
                b[k] = 0.0;
                for (int i = 0; i < n; i++)
                {
                    b[k] += f[k](xarray[i]) * yarray[i];
                }
            }

            for (int j = 0; j < m; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    A[j, k] = 0.0;
                    for (int i = 0; i < n; i++)
                    {
                        A[j, k] += f[j](xarray[i]) * f[k](xarray[i]);
                    }
                }
            }

            //LinearSystem ls = new LinearSystem();
            //VectorR coef = ls.GaussJordan(A, b);
            VectorR coef = GaussJordan(A, b);

            // Calculate the standard deviation:
            double s = 0.0;

            for (int i = 0; i < n; i++)
            {
                double s1 = 0.0;
                for (int j = 0; j < m; j++)
                {
                    s1 += coef[j] * f[j](xarray[i]);
                }
                s += (yarray[i] - s1) * (yarray[i] - s1);
            }
            sigma = Math.Sqrt(s / (n - m));

            return(coef);
        }
Ejemplo n.º 10
0
        public static VectorR PolynomialFit(double[] xarray, double[] yarray, int m, out double sigma)
        {
            m++;
            MatrixR A = new MatrixR(m, m);
            VectorR b = new VectorR(m);
            int     n = xarray.Length;

            for (int k = 0; k < m; k++)
            {
                b[k] = 0.0;
                for (int i = 0; i < n; i++)
                {
                    b[k] += Math.Pow(xarray[i], k) * yarray[i];
                }
            }

            for (int j = 0; j < m; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    A[j, k] = 0.0;
                    for (int i = 0; i < n; i++)
                    {
                        A[j, k] += Math.Pow(xarray[i], j + k);
                    }
                }
            }

            VectorR coef = GaussJordan(A, b);

            // Calculate the standard deviation:
            double s = 0.0;

            for (int i = 0; i < n; i++)
            {
                double s1 = 0.0;
                for (int j = 0; j < m; j++)
                {
                    s1 += coef[j] * Math.Pow(xarray[i], j);
                }
                s += (yarray[i] - s1) * (yarray[i] - s1);
            }
            sigma = Math.Sqrt(s / (n - m));

            return(coef);
        }
Ejemplo n.º 11
0
        public static VectorR GaussJordan(MatrixR A, VectorR b)
        {
            Triangulate(A, b);
            int     n = b.GetSize();
            VectorR x = new VectorR(n);

            for (int i = n - 1; i >= 0; i--)
            {
                double d = A[i, i];
                if (Math.Abs(d) < 1.0e-500)
                {
                    throw new ArgumentException("Diagonal element is too small!");
                }
                x[i] = (b[i] - VectorR.DotProduct(A.GetRowVector(i), x)) / d;
            }
            return(x);
        }
Ejemplo n.º 12
0
 public MatrixR ReplaceCol(VectorR v, int n)
 {
     if (n < 0 || n > Cols)
     {
         throw new ArgumentOutOfRangeException(
                   "n", n, "n is out of range!");
     }
     if (v.GetSize() != Rows)
     {
         throw new ArgumentOutOfRangeException(
                   "Vector size", v.GetSize(), "vector size is out of range!");
     }
     for (int i = 0; i < Rows; i++)
     {
         matrix[i, n] = v[i];
     }
     return(new MatrixR(matrix));
 }
Ejemplo n.º 13
0
        public static MatrixR Transform(VectorR v1, VectorR v2)
        {
            if (v1.GetSize() != v2.GetSize())
            {
                throw new ArgumentOutOfRangeException(
                          "v1", v1.GetSize(), "The vectors must have the same size!");
            }
            MatrixR result = new MatrixR(v1.GetSize(), v1.GetSize());

            for (int i = 0; i < v1.GetSize(); i++)
            {
                for (int j = 0; j < v1.GetSize(); j++)
                {
                    result[j, i] = v1[i] * v2[j];
                }
            }
            return(result);
        }
Ejemplo n.º 14
0
 public static VectorR TriVectorProduct(VectorR v1, VectorR v2, VectorR v3)
 {
     if (v1.size != 3)
     {
         throw new ArgumentOutOfRangeException(
                   "v1", v1, "Vector v1 must be 3 dimensional!");
     }
     if (v1.size != 3)
     {
         throw new ArgumentOutOfRangeException(
                   "v2", v2, "Vector v2 must be 3 dimensional!");
     }
     if (v1.size != 3)
     {
         throw new ArgumentOutOfRangeException(
                   "v3", v3, "Vector v3 must be 3 dimensional!");
     }
     return(v2 * VectorR.DotProduct(v1, v3) - v3 * VectorR.DotProduct(v1, v2));
 }
Ejemplo n.º 15
0
        public static VectorR CrossProduct(VectorR v1, VectorR v2)
        {
            if (v1.size != 3)
            {
                throw new ArgumentOutOfRangeException(
                          "v1", v1, "Vector v1 must be 3 dimensional!");
            }
            if (v2.size != 3)
            {
                throw new ArgumentOutOfRangeException(
                          "v2", v2, "Vector v2 must be 3 dimensional!");
            }
            VectorR result = new VectorR(3);

            result[0] = v1[1] * v2[2] - v1[2] * v2[1];
            result[1] = v1[2] * v2[0] - v1[0] * v2[2];
            result[2] = v1[0] * v2[1] - v1[1] * v2[0];
            return(result);
        }
Ejemplo n.º 16
0
        private static double Pivot(MatrixR A, VectorR b, int q)
        {
            int    n = b.GetSize();
            int    i = q;
            double d = 0.0;

            for (int j = q; j < n; j++)
            {
                double dd = Math.Abs(A[j, q]);
                if (dd > d)
                {
                    d = dd;
                    i = j;
                }
            }
            if (i > q)
            {
                A.GetRowSwap(q, i);
                b.GetSwap(q, i);
            }
            return(A[q, q]);
        }
Ejemplo n.º 17
0
        public static MatrixR operator *(MatrixR m1, MatrixR m2)
        {
            if (m1.GetCols() != m2.GetRows())
            {
                throw new ArgumentOutOfRangeException(
                          "Columns", m1, "The numbers of columns of the first matrix must be equal to" +
                          " the number of rows of the second matrix!");
            }
            MatrixR result = new MatrixR(m1.GetRows(), m2.GetCols());
            VectorR v1     = new VectorR(m1.GetCols());
            VectorR v2     = new VectorR(m2.GetRows());

            for (int i = 0; i < m1.GetRows(); i++)
            {
                v1 = m1.GetRowVector(i);
                for (int j = 0; j < m2.GetCols(); j++)
                {
                    v2           = m2.GetColVector(j);
                    result[i, j] = VectorR.DotProduct(v1, v2);
                }
            }
            return(result);
        }
Ejemplo n.º 18
0
        private static void Triangulate(MatrixR A, VectorR b)
        {
            int     n = A.GetRows();
            VectorR v = new VectorR(n);

            for (int i = 0; i < n - 1; i++)
            {
                double d = Pivot(A, b, i);
                if (Math.Abs(d) < 1.0e-500)
                {
                    throw new ArgumentException("Diagonal element is too small!");
                }
                for (int j = i + 1; j < n; j++)
                {
                    double dd = A[j, i] / d;
                    for (int k = i + 1; k < n; k++)
                    {
                        A[j, k] -= dd * A[i, k];
                    }
                    b[j] -= dd * b[i];
                }
            }
        }
Ejemplo n.º 19
0
        public static double TriScalarProduct(VectorR v1, VectorR v2, VectorR v3)
        {
            if (v1.size != 3)
            {
                throw new ArgumentOutOfRangeException(
                          "v1", v1, "Vector v1 must be 3 dimensional!");
            }
            if (v1.size != 3)
            {
                throw new ArgumentOutOfRangeException(
                          "v2", v2, "Vector v2 must be 3 dimensional!");
            }
            if (v1.size != 3)
            {
                throw new ArgumentOutOfRangeException(
                          "v3", v3, "Vector v3 must be 3 dimensional!");
            }
            double result = v1[0] * (v2[1] * v3[2] - v2[2] * v3[1]) +
                            v1[1] * (v2[2] * v3[0] - v2[0] * v3[2]) +
                            v1[2] * (v2[0] * v3[1] - v2[1] * v3[0]);

            return(result);
        }
        private void AddData()
        {
            double[] x0 = new double[] { 1, 2, 3, 4, 5 };
            double[] y0 = new double[] { 5.5, 43.1, 128, 290.7, 498.4 };

            VectorR[] results = new VectorR[3];

            for (int i = 0; i < results.Length; i++)
            {
                double sigma = 0;
                results[i] = CurveFittingAlgorithms.PolynomialFit(x0, y0, i + 1, out sigma);
            }

            // Plot results:
            myChart.DataCollection.DataList.Clear();
            LineCharts.DataSeries ds;

            // Plot original data:
            ds                     = new LineCharts.DataSeries();
            ds.LineColor           = Brushes.Transparent;
            ds.SeriesName          = "Original";
            ds.Symbols.SymbolType  = LineCharts.Symbols.SymbolTypeEnum.Triangle;
            ds.Symbols.BorderColor = Brushes.Black;
            for (int i = 0; i < x0.Length; i++)
            {
                ds.LineSeries.Points.Add(new Point(x0[i], y0[i]));
            }
            myChart.DataCollection.DataList.Add(ds);

            // 1st order fitting data:
            ds               = new LineCharts.DataSeries();
            ds.LineColor     = Brushes.DarkGreen;
            ds.LineThickness = 2;
            ds.SeriesName    = "1st Order Fitting";
            for (int i = 0; i < 141; i++)
            {
                double x = -1.0 + i / 20.0;
                double y = results[0][0] + results[0][1] * x;
                ds.LineSeries.Points.Add(new Point(x, y));
            }
            myChart.DataCollection.DataList.Add(ds);

            // 2nd order fitting data:
            ds               = new LineCharts.DataSeries();
            ds.LineColor     = Brushes.Red;
            ds.LineThickness = 2;
            ds.LinePattern   = LineCharts.DataSeries.LinePatternEnum.Dash;
            ds.SeriesName    = "2nd Order Fitting";
            for (int i = 0; i < 141; i++)
            {
                double x = -1.0 + i / 20.0;
                double y = results[1][0] + results[1][1] * x + results[1][2] * x * x;
                ds.LineSeries.Points.Add(new Point(x, y));
            }
            myChart.DataCollection.DataList.Add(ds);

            // 3rd order fitting data:
            ds               = new LineCharts.DataSeries();
            ds.LineColor     = Brushes.DarkBlue;
            ds.LineThickness = 2;
            ds.LinePattern   = LineCharts.DataSeries.LinePatternEnum.DashDot;
            ds.SeriesName    = "3rd Order Fitting";
            for (int i = 0; i < 141; i++)
            {
                double x = -1.0 + i / 20.0;
                double y = results[2][0] + results[2][1] * x + results[2][2] * x * x + results[2][3] * x * x * x;
                ds.LineSeries.Points.Add(new Point(x, y));
            }
            myChart.DataCollection.DataList.Add(ds);

            myChart.IsLegend       = true;
            myChart.LegendPosition = LineCharts.Legend.LegendPositionEnum.NorthWest;
        }
Ejemplo n.º 21
0
        private void AddData()
        {
            double[] x0 = new double[] { 0, 1, 2, 3, 4, 5 };
            double[] y0 = new double[] { 2, 1, 4, 4, 3, 2 };

            // First order polynormial (m = 1):
            CurveFittingAlgorithms.ModelFunction[] f = new CurveFittingAlgorithms.ModelFunction[] { f0, f1 };
            double  sigma    = 0.0;
            VectorR results1 = CurveFittingAlgorithms.LinearRegression(x0, y0, f, out sigma);

            // Second order polynormial (m = 2):
            f = new CurveFittingAlgorithms.ModelFunction[] { f0, f1, f2 };
            VectorR results2 = CurveFittingAlgorithms.LinearRegression(x0, y0, f, out sigma);

            // Third order polynormial (m = 3):
            f = new CurveFittingAlgorithms.ModelFunction[] { f0, f1, f2, f3 };
            VectorR results3 = CurveFittingAlgorithms.LinearRegression(x0, y0, f, out sigma);

            // Plot results:
            myChart.DataCollection.DataList.Clear();
            LineCharts.DataSeries ds;

            // Plot original data:
            ds                     = new LineCharts.DataSeries();
            ds.LineColor           = Brushes.Transparent;
            ds.SeriesName          = "Original";
            ds.Symbols.SymbolType  = LineCharts.Symbols.SymbolTypeEnum.Triangle;
            ds.Symbols.BorderColor = Brushes.Black;
            for (int i = 0; i < x0.Length; i++)
            {
                ds.LineSeries.Points.Add(new Point(x0[i], y0[i]));
            }
            myChart.DataCollection.DataList.Add(ds);

            // 1st order fitting data:
            ds               = new LineCharts.DataSeries();
            ds.LineColor     = Brushes.DarkGreen;
            ds.LineThickness = 2;
            ds.SeriesName    = "1st Order Fitting";
            for (int i = 0; i < 141; i++)
            {
                double x = -1.0 + i / 20.0;
                double y = results1[0] + results1[1] * x;
                ds.LineSeries.Points.Add(new Point(x, y));
            }
            myChart.DataCollection.DataList.Add(ds);

            // 2nd order fitting data:
            ds               = new LineCharts.DataSeries();
            ds.LineColor     = Brushes.Red;
            ds.LineThickness = 2;
            ds.LinePattern   = LineCharts.DataSeries.LinePatternEnum.Dash;
            ds.SeriesName    = "2nd Order Fitting";
            for (int i = 0; i < 141; i++)
            {
                double x = -1.0 + i / 20.0;
                double y = results2[0] + results2[1] * x + results2[2] * x * x;
                ds.LineSeries.Points.Add(new Point(x, y));
            }
            myChart.DataCollection.DataList.Add(ds);

            // 3rd order fitting data:
            ds               = new LineCharts.DataSeries();
            ds.LineColor     = Brushes.DarkBlue;
            ds.LineThickness = 2;
            ds.LinePattern   = LineCharts.DataSeries.LinePatternEnum.DashDot;
            ds.SeriesName    = "3rd Order Fitting";
            for (int i = 0; i < 141; i++)
            {
                double x = -1.0 + i / 20.0;
                double y = results3[0] + results3[1] * x + results3[2] * x * x + results3[3] * x * x * x;
                ds.LineSeries.Points.Add(new Point(x, y));
            }
            myChart.DataCollection.DataList.Add(ds);

            myChart.IsLegend       = true;
            myChart.LegendPosition = LineCharts.Legend.LegendPositionEnum.NorthWest;
        }
Ejemplo n.º 22
0
 public bool Equals(VectorR v)
 {
     return(vector == v.vector);
 }