Agregat
Jan 25, 2005, 18:04
Господа - решение системы линейных уравнений методом гаусса.
Предлагаю лишь основную функцию:
void GaussEliminator::solve()
{
m_vecResult.clear();
std::vector<double> v(m_dataMatrix.size() + 1);
unsigned char curr_index = 0, add_index = 0;
//now triagonalize matrix
for (data_matrix::iterator i = m_dataMatrix.begin(), e = m_dataMatrix.end(), k; i != e; ++i, ++curr_index) {
//normalize next row
std::transform(i->begin(), i->end(), i->begin(), std::bind2nd(std::divides<double>(), *(i->begin() + curr_index)));
//remove that item from the next ones
for (k = i + 1; k != e; ++k) {
//base row
v.assign(i->begin(), i->end());
//multiply by the next modified row and then substract
std::transform(v.begin(), v.end(), v.begin(), std::bind2nd(std::multiplies<double>(), *(k->begin() + curr_index)));
std::transform(k->begin(), k->end(), v.begin(), k->begin(), std::minus<double>());
}
}
//now perform reverse process
curr_index = m_dataMatrix.size();
for (data_matrix::reverse_iterator i = m_dataMatrix.rbegin(), e = m_dataMatrix.rend(); i != e; ++i, ++add_index, --curr_index)
m_vecResult.push_front((i->back() - std::inner_product<std::vector<double>::iterator, std::deque<double>::iterator, double>(i->begin() + curr_index, i->end() - 1, m_vecResult.begin(), 0)));
}
где - m_dataMatrix - это vector vector - ов.
кто может скатать короче :)
Предлагаю лишь основную функцию:
void GaussEliminator::solve()
{
m_vecResult.clear();
std::vector<double> v(m_dataMatrix.size() + 1);
unsigned char curr_index = 0, add_index = 0;
//now triagonalize matrix
for (data_matrix::iterator i = m_dataMatrix.begin(), e = m_dataMatrix.end(), k; i != e; ++i, ++curr_index) {
//normalize next row
std::transform(i->begin(), i->end(), i->begin(), std::bind2nd(std::divides<double>(), *(i->begin() + curr_index)));
//remove that item from the next ones
for (k = i + 1; k != e; ++k) {
//base row
v.assign(i->begin(), i->end());
//multiply by the next modified row and then substract
std::transform(v.begin(), v.end(), v.begin(), std::bind2nd(std::multiplies<double>(), *(k->begin() + curr_index)));
std::transform(k->begin(), k->end(), v.begin(), k->begin(), std::minus<double>());
}
}
//now perform reverse process
curr_index = m_dataMatrix.size();
for (data_matrix::reverse_iterator i = m_dataMatrix.rbegin(), e = m_dataMatrix.rend(); i != e; ++i, ++add_index, --curr_index)
m_vecResult.push_front((i->back() - std::inner_product<std::vector<double>::iterator, std::deque<double>::iterator, double>(i->begin() + curr_index, i->end() - 1, m_vecResult.begin(), 0)));
}
где - m_dataMatrix - это vector vector - ов.
кто может скатать короче :)