/// <summary>Places the values of the specified row into the vector parameter.</summary> /// <remarks>Places the values of the specified row into the vector parameter.</remarks> /// <param name="row">the target row number</param> /// <param name="vector">the vector into which the row values will be placed</param> public void GetRow(int row, GVector vector) { if (vector.GetSize() < nCol) { vector.SetSize(nCol); } for (int i = 0; i < nCol; i++) { vector.values[i] = values[row][i]; } }
/// <summary> /// Computes the outer product of the two vectors; multiplies the /// the first vector by the transpose of the second vector and places /// the matrix result into this matrix. /// </summary> /// <remarks> /// Computes the outer product of the two vectors; multiplies the /// the first vector by the transpose of the second vector and places /// the matrix result into this matrix. This matrix must be /// be as big or bigger than getSize(v1)xgetSize(v2). /// </remarks> /// <param name="v1">the first vector, treated as a row vector</param> /// <param name="v2">the second vector, treated as a column vector</param> public void Mul(GVector v1, GVector v2) { int i; int j; if (nRow < v1.GetSize()) { throw new MismatchedSizeException("GMatrix.mul(GVector, GVector): matrix does not have enough rows"); } if (nCol < v2.GetSize()) { throw new MismatchedSizeException("GMatrix.mul(GVector, GVector): matrix does not have enough columns"); } for (i = 0; i < v1.GetSize(); i++) { for (j = 0; j < v2.GetSize(); j++) { values[i][j] = v1.values[i] * v2.values[j]; } } }
/// <summary>Places the values of the specified column into the vector parameter.</summary> /// <remarks>Places the values of the specified column into the vector parameter.</remarks> /// <param name="col">the target column number</param> /// <param name="vector">the vector into which the column values will be placed</param> public void GetColumn(int col, GVector vector) { if (vector.GetSize() < nRow) { vector.SetSize(nRow); } for (int i = 0; i < nRow; i++) { vector.values[i] = values[i][col]; } }
/// <summary> /// LU Decomposition: this matrix must be a square matrix and the /// LU GMatrix parameter must be the same size as this matrix. /// </summary> /// <remarks> /// LU Decomposition: this matrix must be a square matrix and the /// LU GMatrix parameter must be the same size as this matrix. /// The matrix LU will be overwritten as the combination of a /// lower diagonal and upper diagonal matrix decompostion of this /// matrix; the diagonal /// elements of L (unity) are not stored. The GVector parameter /// records the row permutation effected by the partial pivoting, /// and is used as a parameter to the GVector method LUDBackSolve /// to solve sets of linear equations. /// This method returns +/- 1 depending on whether the number /// of row interchanges was even or odd, respectively. /// </remarks> /// <param name="Lu"> /// The matrix into which the lower and upper decompositions /// will be placed. /// </param> /// <param name="permutation"> /// The row permutation effected by the partial /// pivoting /// </param> /// <returns> /// +-1 depending on whether the number of row interchanges /// was even or odd respectively /// </returns> public int Lud(GMatrix Lu, GVector permutation) { int size = Lu.nRow * Lu.nCol; double[] temp = new double[size]; int[] even_row_exchange = new int[1]; int[] row_perm = new int[Lu.nRow]; int i; int j; if (nRow != nCol) { throw new MismatchedSizeException("cannot perform LU decomposition on a non square matrix"); } if (nRow != Lu.nRow) { throw new MismatchedSizeException("LU must have same dimensions as this matrix"); } if (nCol != Lu.nCol) { throw new MismatchedSizeException("LU must have same dimensions as this matrix"); } if (Lu.nRow != permutation.GetSize()) { throw new MismatchedSizeException("row permutation must be same dimension as matrix"); } for (i = 0; i < nRow; i++) { for (j = 0; j < nCol; j++) { temp[i * nCol + j] = values[i][j]; } } // Calculate LU decomposition: Is the matrix singular? if (!LuDecomposition(Lu.nRow, temp, row_perm, even_row_exchange)) { // Matrix has no inverse throw new SingularMatrixException("cannot invert matrix"); } for (i = 0; i < nRow; i++) { for (j = 0; j < nCol; j++) { Lu.values[i][j] = temp[i * nCol + j]; } } for (i = 0; i < Lu.nRow; i++) { permutation.values[i] = (double)row_perm[i]; } return even_row_exchange[0]; }