/// <summary>Matrix-matrix multiplication.</summary>
        public IMapackMatrix Multiply(IMapackMatrix B)
        {
            if (B.Rows != this.columns)
            {
                throw new ArgumentException("Matrix dimensions are not valid.");
            }

            int    columns = B.Columns;
            Matrix X       = new Matrix(rows, columns);

            double[][] x = X.Array;

            int size = this.columns;

            double[] column = new double[size];
            for (int j = 0; j < columns; j++)
            {
                for (int k = 0; k < size; k++)
                {
                    column[k] = B[k, j];
                }
                for (int i = 0; i < rows; i++)
                {
                    double[] row = data[i];
                    double   s   = 0;
                    for (int k = 0; k < size; k++)
                    {
                        s += row[k] * column[k];
                    }
                    x[i][j] = s;
                }
            }

            return(X);
        }
            public IMapackMatrix Solve(IMapackMatrix B)
            {
                if (B.Rows != LU.Rows)
                {
                    throw new ArgumentException("Invalid matrix dimensions.");
                }
                if (!IsNonSingular)
                {
                    throw new InvalidOperationException("Matrix is singular");
                }

                // Copy right hand side with pivoting
                int           count = B.Columns;
                IMapackMatrix X     = B.Submatrix(pivotVector, 0, count - 1);

                int rows    = LU.Rows;
                int columns = LU.Columns;

                double[][] lu = LU.Array;

                // Solve L*Y = B(piv,:)
                for (int k = 0; k < columns; k++)
                {
                    for (int i = k + 1; i < columns; i++)
                    {
                        for (int j = 0; j < count; j++)
                        {
                            X[i, j] -= X[k, j] * lu[i][k];
                        }
                    }
                }

                // Solve U*X = Y;
                for (int k = columns - 1; k >= 0; k--)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[k, j] /= lu[k][k];
                    }

                    for (int i = 0; i < k; i++)
                    {
                        for (int j = 0; j < count; j++)
                        {
                            X[i, j] -= X[k, j] * lu[i][k];
                        }
                    }
                }

                return(X);
            }
        /// <summary>Matrix subtraction.</summary>
        public IMapackMatrix Subtraction(IMapackMatrix B)
        {
            if ((rows != B.Rows) || (columns != B.Columns))
            {
                throw new ArgumentException("Matrix dimension do not match.");
            }
            Matrix X = new Matrix(rows, columns);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[i][j] = data[i][j] - B[i, j];
                }
            }
            return(X);
        }
Esempio n. 4
0
        /// <summary>
        /// Calculate Savitzky-Golay coefficients.
        /// </summary>
        /// <param name="leftpoints">Points on the left side included in the regression.</param>
        /// <param name="rightpoints">Points to the right side included in the regression.</param>
        /// <param name="derivativeorder">Order of derivative for which the coefficients are calculated.</param>
        /// <param name="polynomialorder">Order of the regression polynomial.</param>
        /// <param name="coefficients">Output: On return, contains the calculated coefficients.</param>
        public static void GetCoefficients(int leftpoints, int rightpoints, int derivativeorder, int polynomialorder, IVector <double> coefficients)
        {
            int totalpoints = leftpoints + rightpoints + 1;

            // Presumtions leftpoints and rightpoints must be >=0
            if (leftpoints < 0)
            {
                throw new ArgumentException("Argument leftpoints must not be <=0!");
            }
            if (rightpoints < 0)
            {
                throw new ArgumentException("Argument rightpoints must not be <=0!");
            }
            if (totalpoints <= 1)
            {
                throw new ArgumentException("Argument leftpoints and rightpoints must not both be zero!");
            }
            if (polynomialorder >= totalpoints)
            {
                throw new ArgumentException("Argument polynomialorder must not be smaller than total number of points");
            }
            if (derivativeorder > polynomialorder)
            {
                throw new ArgumentException("Argument derivativeorder must not be greater than polynomialorder!");
            }
            if (coefficients == null || coefficients.Length < totalpoints)
            {
                throw new ArgumentException("Vector of coefficients is either null or too short");
            }
            // totalpoints must be greater than 1

            // Set up the design matrix
            // this is the matrix of i^j where i ranges from -leftpoints..rightpoints and j from 0 to polynomialorder
            // as usual for regression, we not use the matrix directly, but instead the covariance matrix At*A
            var mat = new Matrix(polynomialorder + 1, polynomialorder + 1);

            double[] val = new double[totalpoints];
            for (int i = 0; i < totalpoints; i++)
            {
                val[i] = 1;
            }

            for (int ord = 0; ord <= polynomialorder; ord++)
            {
                double sum = VectorMath.Sum(val);
                for (int i = 0; i <= ord; i++)
                {
                    mat[ord - i, i] = sum;
                }
                for (int i = 0; i < totalpoints; i++)
                {
                    val[i] *= (i - leftpoints);
                }
            }

            for (int ord = polynomialorder - 1; ord >= 0; ord--)
            {
                double sum = VectorMath.Sum(val);
                for (int i = 0; i <= ord; i++)
                {
                    mat[polynomialorder - i, polynomialorder - ord + i] = sum;
                }
                for (int i = 0; i < totalpoints; i++)
                {
                    val[i] *= (i - leftpoints);
                }
            }

            // now solve the equation
            ILuDecomposition decompose = mat.GetLuDecomposition();
            // ISingularValueDecomposition decompose = mat.GetSingularValueDecomposition();
            var y = new Matrix(polynomialorder + 1, 1);

            y[derivativeorder, 0] = 1;
            IMapackMatrix result = decompose.Solve(y);

            // to get the coefficients, the parameter have to be multiplied by i^j and summed up
            for (int i = -leftpoints; i <= rightpoints; i++)
            {
                double sum = 0;
                double x   = 1;
                for (int j = 0; j <= polynomialorder; j++, x *= i)
                {
                    sum += result[j, 0] * x;
                }
                coefficients[i + leftpoints] = sum;
            }
        }
 /// <summary>Returns the LHS solution vetor if the matrix is square or the least squares solution otherwise.</summary>
 public IMapackMatrix Solve(IMapackMatrix rhs)
 {
     System.Diagnostics.Debug.WriteLine("There is an issue in IMapackMatrix");
     return(GetLuDecomposition().Solve(rhs));
 }
Esempio n. 6
0
			public IMapackMatrix Solve(IMapackMatrix rhs)
			{
				if (rhs.Rows != L.Rows)
				{
					throw new ArgumentException("Matrix dimensions do not match.");
				}
				if (!isSymmetric)
				{
					throw new InvalidOperationException("Matrix is not symmetric.");
				}
				if (!isPositiveDefinite)
				{
					throw new InvalidOperationException("Matrix is not positive definite.");
				}

				int dimension = L.Rows;
				int count = rhs.Columns;

				IMapackMatrix B = (IMapackMatrix)rhs.Clone();
				double[][] l = L.Array;

				// Solve L*Y = B;
				for (int k = 0; k < L.Rows; k++)
				{
					for (int i = k + 1; i < dimension; i++)
					{
						for (int j = 0; j < count; j++)
						{
							B[i, j] -= B[k, j] * l[i][k];
						}
					}

					for (int j = 0; j < count; j++)
					{
						B[k, j] /= l[k][k];
					}
				}

				// Solve L'*X = Y;
				for (int k = dimension - 1; k >= 0; k--)
				{
					for (int j = 0; j < count; j++)
					{
						B[k, j] /= l[k][k];
					}

					for (int i = 0; i < k; i++)
					{
						for (int j = 0; j < count; j++)
						{
							B[i, j] -= B[k, j] * l[k][i];
						}
					}
				}

				return B;
			}
Esempio n. 7
0
		/*
		/// <summary>Adds a matrix to the current matrix.</summary>
		public void Add(Matrix A)
		{
		if ((rows != A.Rows) || (columns != A.Columns)) throw new ArgumentException();
		double[][] a = A.Array;
		for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
		data[i][j] += a[i][j];
		}

		/// <summary>Subtracts a matrix from the current matrix.</summary>
		public void Sub(Matrix A)
		{
		if ((rows != A.Rows) || (this.columns != A.Columns)) throw new ArgumentException();
		double[][] a = A.Array;
		for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
		data[i][j] -= a[i][j];
		}

		/// <summary>Multiplies the current matrix with a scalar factor.</summary>
		public void Times(double s)
		{
		for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
		data[i][j] *= s;
		}
		*/

		/// <summary>Returns the LHS solution vetor if the matrix is square or the least squares solution otherwise.</summary>
		public IMapackMatrix Solve(IMapackMatrix rhs)
		{
			return (rows == columns) ? GetLuDecomposition().Solve(rhs) : GetQrDecomposition().Solve(rhs);
		}
Esempio n. 8
0
		/// <summary>Matrix-matrix multiplication.</summary>
		public IMapackMatrix Multiply(IMapackMatrix B)
		{
			if (B.Rows != this.columns)
			{
				throw new ArgumentException("Matrix dimensions are not valid.");
			}

			int columns = B.Columns;
			Matrix X = new Matrix(rows, columns);
			double[][] x = X.Array;

			int size = this.columns;
			double[] column = new double[size];
			for (int j = 0; j < columns; j++)
			{
				for (int k = 0; k < size; k++)
				{
					column[k] = B[k, j];
				}
				for (int i = 0; i < rows; i++)
				{
					double[] row = data[i];
					double s = 0;
					for (int k = 0; k < size; k++)
					{
						s += row[k] * column[k];
					}
					x[i][j] = s;
				}
			}

			return X;
		}
Esempio n. 9
0
		/// <summary>Matrix subtraction.</summary>
		public IMapackMatrix Subtraction(IMapackMatrix B)
		{
			if ((rows != B.Rows) || (columns != B.Columns))
			{
				throw new ArgumentException("Matrix dimension do not match.");
			}
			Matrix X = new Matrix(rows, columns);
			double[][] x = X.Array;
			for (int i = 0; i < rows; i++)
			{
				for (int j = 0; j < columns; j++)
				{
					x[i][j] = data[i][j] - B[i, j];
				}
			}
			return X;
		}
Esempio n. 10
0
			public IMapackMatrix Solve(IMapackMatrix rhs)
			{
				if (rhs.Rows != QR.Rows) throw new ArgumentException("Matrix row dimensions must agree.");
				if (!IsFullRank) throw new InvalidOperationException("Matrix is rank deficient.");

				// Copy right hand side
				int count = rhs.Columns;
				IMapackMatrix X = rhs.Clone();
				int m = QR.Rows;
				int n = QR.Columns;
				double[][] qr = QR.Array;

				// Compute Y = transpose(Q)*B
				for (int k = 0; k < n; k++)
				{
					for (int j = 0; j < count; j++)
					{
						double s = 0.0;
						for (int i = k; i < m; i++)
							s += qr[i][k] * X[i, j];
						s = -s / qr[k][k];
						for (int i = k; i < m; i++)
							X[i, j] += s * qr[i][k];
					}
				}

				// Solve R*X = Y;
				for (int k = n - 1; k >= 0; k--)
				{
					for (int j = 0; j < count; j++)
						X[k, j] /= Rdiag[k];

					for (int i = 0; i < k; i++)
						for (int j = 0; j < count; j++)
							X[i, j] -= X[k, j] * qr[i][k];
				}

				return X.Submatrix(0, n - 1, 0, count - 1);
			}
Esempio n. 11
0
			public IMapackMatrix Solve(IMapackMatrix B)
			{
				if (B.Rows != LU.Rows) throw new ArgumentException("Invalid matrix dimensions.");
				if (!IsNonSingular) throw new InvalidOperationException("Matrix is singular");

				// Copy right hand side with pivoting
				int count = B.Columns;
				IMapackMatrix X = B.Submatrix(pivotVector, 0, count - 1);

				int rows = LU.Rows;
				int columns = LU.Columns;
				double[][] lu = LU.Array;

				// Solve L*Y = B(piv,:)
				for (int k = 0; k < columns; k++)
				{
					for (int i = k + 1; i < columns; i++)
					{
						for (int j = 0; j < count; j++)
						{
							X[i, j] -= X[k, j] * lu[i][k];
						}
					}
				}

				// Solve U*X = Y;
				for (int k = columns - 1; k >= 0; k--)
				{
					for (int j = 0; j < count; j++)
					{
						X[k, j] /= lu[k][k];
					}

					for (int i = 0; i < k; i++)
					{
						for (int j = 0; j < count; j++)
						{
							X[i, j] -= X[k, j] * lu[i][k];
						}
					}
				}

				return X;
			}