本文整理汇总了C++中DoubleVector::scale方法的典型用法代码示例。如果您正苦于以下问题:C++ DoubleVector::scale方法的具体用法?C++ DoubleVector::scale怎么用?C++ DoubleVector::scale使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DoubleVector
的用法示例。
在下文中一共展示了DoubleVector::scale方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: conjugateGradient
DoubleVector RowDoubleMatrix::conjugateGradient(const DoubleVector &B, double epsilon, unsigned int niter, bool printMessages, unsigned int messageStep) const
{
DoubleVector X(size_, 0.0); // начальное приближение - вектор нулей
DoubleVector resid(size_); // невязка
DoubleVector direction; // направление поиска
DoubleVector temp(size_); // ременное хранилище для обмена данными
double resid_norm; // норма невязки
double alpha;
double beta;
double resid_resid, resid_resid_new;
residual(X, B, resid);
direction = resid;
resid_norm = resid.norm_2();
if (printMessages) std::cout << "Начальная невязка: " << resid_norm << std::endl;
if (resid_norm > epsilon)
{
resid_resid = resid * resid;
for (unsigned int i = 0; i < niter; i++)
{
product(direction, temp);
// std::cout << direction.norm_2() << " " << temp.norm_2() << std::endl;
alpha = (resid_resid) / (direction * temp);
X += alpha * direction;
resid -= alpha * temp;
resid_resid_new = resid * resid;
resid_norm = sqrt(resid_resid_new);
if (resid_norm <= epsilon)
{
if (printMessages)
std::cout << "Решение найдено. Итераций: " << i << ", невязка: " << resid_norm << std::endl;
break;
}
if (printMessages && (i % messageStep == 0))
std::cout << i << ", невязка: " << resid_norm << std::endl;
beta = (resid_resid_new) / (resid_resid);
// d = r + d*beta
direction.scale(beta);
direction += resid;
//
resid_resid = resid_resid_new;
}
}
return X;
}