Exemplo n.º 1
0
MyVector<T>
TridiagonalMatrix<T>::operator*(const MyVector<T>& vec) const
{
        if(size != vec.getSize())
	{
		throw typename Matrix<T>::
		      IncompatibleMatrixSizes(size, size,
		                              vec.getSize(), vec.getSize());
	}

	MyVector<T> rv(size);
	if(size == 1)
	{
		rv[0] = getNonZeroElement(0,0) * vec[0];
	}
	else
	{
		// add the appropriate sums from the first column of the matrix
		rv[0] += getNonZeroElement(0, 0) * vec[0];
		rv[1] += getNonZeroElement(1, 0) * vec[0];

		// add the appropriate sums from the second through n-1 columns
		// of the matrix
		for(size_t j = 1; j < size - 1; ++j)
		{
			for(size_t i = j - 1; i <= j + 1; ++i)
			{
				rv[i] += getNonZeroElement(i, j) * vec[j];
			}
		}

		// add the appropriate sums from the last column of the matrix
		rv[size - 2] += 
			getNonZeroElement(size - 2, size - 1) * vec[size - 1];
		rv[size - 1] += 
			getNonZeroElement(size - 1, size - 1) * vec[size - 1];
	}

	return rv;
}
Exemplo n.º 2
0
MyVector<T>
DenseMatrix<T>::operator*(const MyVector<T>& v) const
{
	if(numCols != v.getSize())
	{
		throw typename Matrix<T>::IncompatibleMatrixSizes
			(numRows, numCols, v.getSize(), 1);
		// vectors always have one column
	}

	// again, go with the column number in the outer loop because
	// matrices are stored in column major order
	MyVector<T> result(numRows);
	for(size_t j = 0; j < numCols; ++j)
	{                            
		for(size_t i = 0; i < numRows; ++i)
		{
			result[i] += a(i, j) * v[j];
		}
	}
	return result;
}
Exemplo n.º 3
0
DenseMatrix<T>::DenseMatrix(const MyVector<T>& v)
	: numRows(v.getSize()), numCols((numRows != 0)?1:0)
{
	if(numRows == 0)
	{
		data = NULL;
	}
	else
	{
		data = new T[numRows];
		for(size_t i = 0; i < numRows; ++i)
		{
			data[i] = v[i];
		}
	}
}
Exemplo n.º 4
0
MyVector<T>
LUDecompositionSolver<T>::
operator()(const typename LUDecomposition<T>::Solution& decomposition,
           const MyVector<T>& b) const
{
    const LowerTriangularMatrix<T>& L = decomposition.L();
    const UpperTriangularMatrix<T>& U = decomposition.U();
    const typename LUDecomposition<T>::OrderType& order = 
        decomposition.orderVector();

    if(L.getNumRows() != b.getSize())
    {
        throw typename LUDecompositionSolver<T>::IncompatibleMatrixAndVector();
    }

    MyVector<T> bPermutation(b);

    for(size_t i = 1; i < L.getNumRows(); ++i)
    {
        T sum = bPermutation[order[i]];
        for(size_t j = 0; j < i; ++j)
        {
            sum -= L[order[i]][j] * bPermutation[order[j]];
        }
        bPermutation[order[i]] = sum;
    }

    MyVector<T> x(U.getNumCols());

    const size_t lastRow = x.getSize() - 1;
    x[lastRow] = bPermutation[order[lastRow]] / U[order[lastRow]][lastRow];

    for(int i = L.getNumRows() - 1; i >= 0; --i)
    {
        T sum = 0;
        for(size_t j = i+1; j < L.getNumRows(); ++j)
        {
            sum += U[order[i]][j]*x[j];
        }
        x[i] = (bPermutation[order[i]] - sum) / U[order[i]][i];
    }

    return x;
}