Пример #1
0
Matrix *Matrix::SolveWith(Matrix *v)
{
	if (rows != cols)
	{
		throw MException(L"The matrix is not square!");
	}
	if (rows != v->rows)
	{
		throw MException(L"Wrong number of results in solution vector!");
	}
	if (L == NULL)
	{
		MakeLU();
	}

	Matrix *b = new Matrix(rows,cols);
	for (int i = 0; i < rows; i++) // switch two items in "v" due to permutation matrix
	{
		(*b)(pi[i],0) = (*v)(i,0);
	}

	Matrix *z = SubsForth(L,b);
	Matrix *x = SubsBack(U,z);

	return x;
}
Пример #2
0
void UTester::test (const char *testName, bool result)
{
    if (_closed)
        throw MException(TESTERCLOSED);
    _errors += !result;
    print("%s: %s", testName, result?"passed (+)":"failed (-)");
}
Пример #3
0
void UTester::add_children (UTester *ch)
{
    if (_closed)
        throw MException(TESTERCLOSED);
    if(ch) children.push_back (ch);
    print(" ");
}
Пример #4
0
void UTester::print (char *format...)
{
    if (_closed)
        throw MException(TESTERCLOSED);
    if (!_started){
        _started = 1;
        clear_print((char*)_start_str.c_str(), _name.c_str());
    }
    char buf[256];
    va_list args;
    va_start (args, format);
    vsprintf(buf, format, args);
    clear_print ("| %s", buf);
    va_end (args);
}
Пример #5
0
Matrix *Matrix::Add(Matrix *m1, Matrix *m2)
{
	if (m1->rows != m2->rows || m1->cols != m2->cols)
	{
		throw MException(L"Matrices must have the same dimensions!");
	}
	Matrix *r = new Matrix(m1->rows, m1->cols);
	for (int i = 0; i < r->rows; i++)
	{
		for (int j = 0; j < r->cols; j++)
		{
			r->operator ()(i,j) = m1->operator ()(i,j) + m2->operator ()(i,j);
		}
	}

	return r;
}
Пример #6
0
Matrix *Matrix::Multiply(Matrix *m1, Matrix *m2)
{
	if (m1->cols != m2->rows)
	{
		throw MException(L"Wrong dimensions of matrix!");
	}

	Matrix *result = ZeroMatrix(m1->rows, m2->cols);
	for (int i = 0; i < result->rows; i++)
	{
		for (int j = 0; j < result->cols; j++)
		{
			for (int k = 0; k < m1->cols; k++)
			{
				result->operator ()(i,j) += m1->operator ()(i,k) * m2->operator ()(k,j);
			}
		}
	}
	return result;
}
Пример #7
0
void Matrix::MakeLU()
{
	if (!IsSquare())
	{
		throw MException(L"The matrix is not square!");
	}
	L = IdentityMatrix(rows, cols);
	U = Duplicate();

	pi = new int[rows];
	for (int i = 0; i < rows; i++)
	{
		pi[i] = i;
	}

	double p = 0;
	double pom2;
	int k0 = 0;
	int pom1 = 0;

	for (int k = 0; k < cols - 1; k++)
	{
		p = 0;
		for (int i = k; i < rows; i++) // find the row with the biggest pivot
		{
			if (abs((*U)(i,k)) > p)
			{
				p = abs((*U)(i,k));
				k0 = i;
			}
		}
		if (p == 0) // samé nuly ve sloupci
		{
			throw MException(L"The matrix is singular!");
		}

		pom1 = pi[k]; // switch two rows in permutation matrix
		pi[k] = pi[k0];
		pi[k0] = pom1;

		for (int i = 0; i < k; i++)
		{
			pom2 = (*L)(k,i);
			(*L)(k,i) = (*L)(k0,i);
			(*L)(k0,i) = pom2;
		}

		if (k != k0)
		{
			detOfP *= -1;
		}

		for (int i = 0; i < cols; i++) // Switch rows in U
		{
			pom2 = (*U)(k,i);
			(*U)(k,i) = (*U)(k0,i);
			(*U)(k0,i) = pom2;
		}

		for (int i = k + 1; i < rows; i++)
		{
			(*L)(i,k) = (*U)(i,k) / (*U)(k,k);
			for (int j = k; j < cols; j++)
			{
				(*U)(i,j) = (*U)(i,j) - (*L)(i,k) * (*U)(k,j);
			}
		}
	}
}