36 #ifndef OPENVDB_MATH_CONJGRADIENT_HAS_BEEN_INCLUDED
37 #define OPENVDB_MATH_CONJGRADIENT_HAS_BEEN_INCLUDED
39 #include <openvdb/Exceptions.h>
40 #include <openvdb/Types.h>
41 #include <openvdb/util/logging.h>
42 #include <openvdb/util/NullInterrupter.h>
44 #include <tbb/parallel_for.h>
45 #include <tbb/parallel_reduce.h>
64 template<
typename ValueType>
class Vector;
82 template<
typename ValueType>
90 s.
absoluteError = std::numeric_limits<ValueType>::epsilon() * 100.0;
117 template<
typename PositiveDefMatrix>
120 const PositiveDefMatrix& A,
121 const Vector<typename PositiveDefMatrix::ValueType>& b,
122 Vector<typename PositiveDefMatrix::ValueType>& x,
123 Preconditioner<typename PositiveDefMatrix::ValueType>& preconditioner,
124 const State& termination = terminationDefaults<typename PositiveDefMatrix::ValueType>());
149 template<
typename PositiveDefMatrix,
typename Interrupter>
152 const PositiveDefMatrix& A,
153 const Vector<typename PositiveDefMatrix::ValueType>& b,
154 Vector<typename PositiveDefMatrix::ValueType>& x,
155 Preconditioner<typename PositiveDefMatrix::ValueType>& preconditioner,
156 Interrupter& interrupter,
157 const State& termination = terminationDefaults<typename PositiveDefMatrix::ValueType>());
178 ~Vector() { mSize = 0;
delete[] mData; mData =
nullptr; }
188 bool empty()
const {
return (mSize == 0); }
195 void swap(
Vector& other) { std::swap(mData, other.mData); std::swap(mSize, other.mSize); }
198 void fill(
const ValueType& value);
201 template<
typename Scalar>
void scale(
const Scalar& s);
207 ValueType dot(
const Vector&)
const;
210 ValueType infNorm()
const;
219 template<
typename OtherValueType>
224 std::string str()
const;
227 inline T& at(
SizeType i) {
return mData[i]; }
235 inline T* data() {
return mData; }
237 inline const T*
data()
const {
return mData; }
243 template<
typename Scalar>
struct ScaleOp;
244 struct DeterministicDotProductOp;
246 template<
typename OtherValueType>
struct EqOp;
263 template<
typename ValueType_, SizeType STENCIL_SIZE>
271 class ConstValueIter;
284 SizeType numRows()
const {
return mNumRows; }
303 ConstRow getConstRow(
SizeType row)
const;
306 RowEditor getRowEditor(
SizeType row);
309 template<
typename Scalar>
void scale(
const Scalar& s);
311 template<
typename Scalar>
318 template<
typename VecValueType>
325 template<
typename VecValueType>
326 void vectorMultiply(
const VecValueType* inVec, VecValueType* resultVec)
const;
330 template<
typename OtherValueType>
338 std::string str()
const;
342 RowData(ValueType* v,
SizeType* c,
SizeType& s): mVals(v), mCols(c), mSize(s) {}
346 struct ConstRowData {
348 mVals(v), mCols(c), mSize(s) {}
353 template<
typename DataType_ = RowData>
357 using DataType = DataType_;
359 static SizeType capacity() {
return STENCIL_SIZE; }
361 RowBase(
const DataType& data): mData(data) {}
363 bool empty()
const {
return (mData.mSize == 0); }
364 const SizeType& size()
const {
return mData.mSize; }
366 const ValueType& getValue(
SizeType columnIdx,
bool& active)
const;
367 const ValueType& getValue(
SizeType columnIdx)
const;
370 ConstValueIter cbegin()
const;
374 template<
typename OtherDataType>
375 bool eq(
const RowBase<OtherDataType>& other,
376 ValueType eps = Tolerance<ValueType>::value())
const;
381 template<
typename VecValueType>
382 VecValueType dot(
const VecValueType* inVec,
SizeType vecSize)
const;
385 template<
typename VecValueType>
386 VecValueType dot(
const Vector<VecValueType>& inVec)
const;
389 std::string str()
const;
392 friend class ConstValueIter;
394 const ValueType& value(
SizeType i)
const {
return mData.mVals[i]; }
406 using ConstRowBase = RowBase<ConstRowData>;
415 if (mData.mSize == 0)
return SparseStencilMatrix::sZeroValue;
416 return mData.mVals[mCursor];
423 operator bool()
const {
return (mCursor < mData.mSize); }
429 ConstValueIter(
const RowData& d): mData(d.mVals, d.mCols, d.mSize), mCursor(0) {}
432 const ConstRowData mData;
459 template<
typename Scalar>
void scale(
const Scalar&);
461 template<
typename Scalar>
472 template<
typename VecValueType>
struct VecMultOp;
473 template<
typename Scalar>
struct RowScaleOp;
477 template<
typename OtherValueType>
struct EqOp;
480 std::unique_ptr<ValueType[]> mValueArray;
481 std::unique_ptr<SizeType[]> mColumnIdxArray;
482 std::unique_ptr<SizeType[]> mRowSizeArray;
519 CopyOp(
const T* from_, T* to_): from(from_), to(to_) {}
522 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) to[n] = from[n];
534 FillOp(T* data_,
const T& val_): data(data_), val(val_) {}
537 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) data[n] = val;
549 LinearOp(
const T& a_,
const T* x_,
const T* y_, T* out_): a(a_), x(x_), y(y_), out(out_) {}
553 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = x[n] + y[n];
555 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = -x[n] + y[n];
557 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = a * x[n] + y[n];
574 os << (state.
success ?
"succeeded with " :
"")
599 if (mSize != other.mSize) {
602 mData =
new T[mSize];
618 if (mData)
delete[] mData;
634 template<
typename Scalar>
637 ScaleOp(T* data_,
const Scalar& s_): data(data_), s(s_) {}
639 void operator()(
const SizeRange& range)
const {
640 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) data[n] *= s;
649 template<
typename Scalar>
653 tbb::parallel_for(
SizeRange(0, mSize), ScaleOp<Scalar>(mData, s));
662 a(a_), b(b_), binCount(binCount_), arraySize(arraySize_), reducetmp(reducetmp_) {}
666 const SizeType binSize = arraySize / binCount;
669 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
671 const SizeType end = (n == binCount-1) ? arraySize : begin + binSize;
674 T sum = zeroVal<T>();
675 for (
SizeType i = begin; i < end; ++i) { sum += a[i] * b[i]; }
693 assert(this->size() == other.size());
695 const T* aData = this->data();
696 const T* bData = other.data();
700 T result = zeroVal<T>();
702 if (arraySize < 1024) {
706 for (
SizeType n = 0; n < arraySize; ++n) {
707 result += aData[n] * bData[n];
719 tbb::parallel_for(
SizeRange(0, binCount),
720 DeterministicDotProductOp(aData, bData, binCount, arraySize, partialSums));
722 for (
SizeType n = 0; n < binCount; ++n) {
723 result += partialSums[n];
738 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
739 maxValue =
Max(maxValue,
Abs(data[n]));
753 T result = tbb::parallel_reduce(
SizeRange(0, this->size()), zeroVal<T>(),
754 InfNormOp(this->data()), [](T max1, T max2) {
return Max(max1, max2); });
767 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
768 if (!std::isfinite(data[n]))
return false;
783 bool finite = tbb::parallel_reduce(
SizeRange(0, this->size()),
true,
784 IsFiniteOp(this->data()),
785 [](
bool finite1,
bool finite2) {
return (finite1 && finite2); });
791 template<
typename OtherValueType>
794 EqOp(
const T* a_,
const OtherValueType* b_, T e): a(a_), b(b_), eps(e) {}
796 bool operator()(
const SizeRange& range,
bool equal)
const
799 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
807 const OtherValueType* b;
813 template<
typename OtherValueType>
817 if (this->size() != other.size())
return false;
818 bool equal = tbb::parallel_reduce(
SizeRange(0, this->size()),
true,
819 EqOp<OtherValueType>(this->data(), other.data(), eps),
820 [](
bool eq1,
bool eq2) {
return (eq1 && eq2); });
829 std::ostringstream ostr;
832 for (
SizeType n = 0, N = this->size(); n < N; ++n) {
833 ostr << sep << (*this)[n];
844 template<
typename ValueType, SizeType STENCIL_SIZE>
848 template<
typename ValueType, SizeType STENCIL_SIZE>
852 , mValueArray(new
ValueType[mNumRows * STENCIL_SIZE])
853 , mColumnIdxArray(new
SizeType[mNumRows * STENCIL_SIZE])
854 , mRowSizeArray(new
SizeType[mNumRows])
857 tbb::parallel_for(
SizeRange(0, mNumRows),
862 template<
typename ValueType, SizeType STENCIL_SIZE>
866 from(&from_), to(&to_) {}
870 const ValueType* fromVal = from->mValueArray.get();
871 const SizeType* fromCol = from->mColumnIdxArray.get();
872 ValueType* toVal = to->mValueArray.get();
873 SizeType* toCol = to->mColumnIdxArray.get();
874 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
875 toVal[n] = fromVal[n];
876 toCol[n] = fromCol[n];
884 template<
typename ValueType, SizeType STENCIL_SIZE>
887 : mNumRows(other.mNumRows)
888 , mValueArray(new
ValueType[mNumRows * STENCIL_SIZE])
889 , mColumnIdxArray(new
SizeType[mNumRows * STENCIL_SIZE])
890 , mRowSizeArray(new
SizeType[mNumRows])
892 SizeType size = mNumRows * STENCIL_SIZE;
895 tbb::parallel_for(
SizeRange(0, size), MatrixCopyOp(other, *
this));
898 tbb::parallel_for(
SizeRange(0, mNumRows),
903 template<
typename ValueType, SizeType STENCIL_SIZE>
908 assert(row < mNumRows);
909 this->getRowEditor(row).setValue(col, val);
913 template<
typename ValueType, SizeType STENCIL_SIZE>
914 inline const ValueType&
917 assert(row < mNumRows);
918 return this->getConstRow(row).getValue(col);
922 template<
typename ValueType, SizeType STENCIL_SIZE>
923 inline const ValueType&
926 return this->getValue(row,col);
930 template<
typename ValueType, SizeType STENCIL_SIZE>
931 template<
typename Scalar>
936 void operator()(
const SizeRange& range)
const
938 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
939 RowEditor row = mat->getRowEditor(n);
944 SparseStencilMatrix* mat;
949 template<
typename ValueType, SizeType STENCIL_SIZE>
950 template<
typename Scalar>
955 tbb::parallel_for(
SizeRange(0, mNumRows), RowScaleOp<Scalar>(*
this, s));
959 template<
typename ValueType, SizeType STENCIL_SIZE>
960 template<
typename VecValueType>
964 mat(&m), in(i), out(o) {}
966 void operator()(
const SizeRange& range)
const
968 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
969 ConstRow row = mat->getConstRow(n);
970 out[n] = row.dot(in, mat->numRows());
974 const SparseStencilMatrix* mat;
975 const VecValueType* in;
980 template<
typename ValueType, SizeType STENCIL_SIZE>
981 template<
typename VecValueType>
986 if (inVec.size() != mNumRows) {
988 << mNumRows <<
"x" << mNumRows <<
" vs. " << inVec.size() <<
")");
990 if (resultVec.size() != mNumRows) {
992 << mNumRows <<
"x" << mNumRows <<
" vs. " << resultVec.size() <<
")");
995 vectorMultiply(inVec.data(), resultVec.data());
999 template<
typename ValueType, SizeType STENCIL_SIZE>
1000 template<
typename VecValueType>
1003 const VecValueType* inVec, VecValueType* resultVec)
const
1006 tbb::parallel_for(
SizeRange(0, mNumRows),
1007 VecMultOp<VecValueType>(*
this, inVec, resultVec));
1011 template<
typename ValueType, SizeType STENCIL_SIZE>
1012 template<
typename OtherValueType>
1017 a(&a_), b(&b_), eps(e) {}
1019 bool operator()(
const SizeRange& range,
bool equal)
const
1022 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1023 if (!a->getConstRow(n).eq(b->getConstRow(n), eps))
return false;
1029 const SparseStencilMatrix* a;
1030 const SparseStencilMatrix<OtherValueType, STENCIL_SIZE>* b;
1031 const ValueType eps;
1035 template<
typename ValueType, SizeType STENCIL_SIZE>
1036 template<
typename OtherValueType>
1041 if (this->numRows() != other.
numRows())
return false;
1042 bool equal = tbb::parallel_reduce(
SizeRange(0, this->numRows()),
true,
1043 EqOp<OtherValueType>(*
this, other, eps),
1044 [](
bool eq1,
bool eq2) {
return (eq1 && eq2); });
1049 template<
typename ValueType, SizeType STENCIL_SIZE>
1057 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1058 const ConstRow row = mat->getConstRow(n);
1060 if (!std::isfinite(*it))
return false;
1071 template<
typename ValueType, SizeType STENCIL_SIZE>
1076 bool finite = tbb::parallel_reduce(
SizeRange(0, this->numRows()),
true,
1077 IsFiniteOp(*
this), [](
bool finite1,
bool finite2) {
return (finite1&&finite2); });
1082 template<
typename ValueType, SizeType STENCIL_SIZE>
1086 std::ostringstream ostr;
1087 for (
SizeType n = 0, N = this->size(); n < N; ++n) {
1088 ostr << n <<
": " << this->getConstRow(n).
str() <<
"\n";
1094 template<
typename ValueType, SizeType STENCIL_SIZE>
1098 assert(i < mNumRows);
1099 const SizeType head = i * STENCIL_SIZE;
1100 return RowEditor(&mValueArray[head], &mColumnIdxArray[head], mRowSizeArray[i], mNumRows);
1104 template<
typename ValueType, SizeType STENCIL_SIZE>
1108 assert(i < mNumRows);
1109 const SizeType head = i * STENCIL_SIZE;
1110 return ConstRow(&mValueArray[head], &mColumnIdxArray[head], mRowSizeArray[i]);
1114 template<
typename ValueType, SizeType STENCIL_SIZE>
1115 template<
typename DataType>
1119 if (this->empty())
return mData.mSize;
1123 const SizeType* colPtr = std::lower_bound(mData.mCols, mData.mCols + mData.mSize, columnIdx);
1125 return static_cast<SizeType>(colPtr - mData.mCols);
1129 template<
typename ValueType, SizeType STENCIL_SIZE>
1130 template<
typename DataType>
1131 inline const ValueType&
1132 SparseStencilMatrix<ValueType, STENCIL_SIZE>::RowBase<DataType>::getValue(
1133 SizeType columnIdx,
bool& active)
const
1136 SizeType idx = this->find(columnIdx);
1137 if (idx < this->size() && this->column(idx) == columnIdx) {
1139 return this->value(idx);
1141 return SparseStencilMatrix::sZeroValue;
1144 template<
typename ValueType, SizeType STENCIL_SIZE>
1145 template<
typename DataType>
1146 inline const ValueType&
1147 SparseStencilMatrix<ValueType, STENCIL_SIZE>::RowBase<DataType>::getValue(
SizeType columnIdx)
const
1149 SizeType idx = this->find(columnIdx);
1150 if (idx < this->size() && this->column(idx) == columnIdx) {
1151 return this->value(idx);
1153 return SparseStencilMatrix::sZeroValue;
1157 template<
typename ValueType, SizeType STENCIL_SIZE>
1158 template<
typename DataType>
1159 inline typename SparseStencilMatrix<ValueType, STENCIL_SIZE>::ConstValueIter
1160 SparseStencilMatrix<ValueType, STENCIL_SIZE>::RowBase<DataType>::cbegin()
const
1162 return ConstValueIter(mData);
1166 template<
typename ValueType, SizeType STENCIL_SIZE>
1167 template<
typename DataType>
1168 template<
typename OtherDataType>
1170 SparseStencilMatrix<ValueType, STENCIL_SIZE>::RowBase<DataType>::eq(
1171 const RowBase<OtherDataType>& other, ValueType eps)
const
1173 if (this->size() != other.size())
return false;
1174 for (ConstValueIter it = cbegin(), oit = other.cbegin(); it || oit; ++it, ++oit) {
1175 if (it.column() != oit.column())
return false;
1182 template<
typename ValueType, SizeType STENCIL_SIZE>
1183 template<
typename DataType>
1184 template<
typename VecValueType>
1186 SparseStencilMatrix<ValueType, STENCIL_SIZE>::RowBase<DataType>::dot(
1187 const VecValueType* inVec,
SizeType vecSize)
const
1189 VecValueType result = zeroVal<VecValueType>();
1190 for (
SizeType idx = 0, N =
std::min(vecSize, this->size()); idx < N; ++idx) {
1191 result += static_cast<VecValueType>(this->value(idx) * inVec[this->column(idx)]);
1196 template<
typename ValueType, SizeType STENCIL_SIZE>
1197 template<
typename DataType>
1198 template<
typename VecValueType>
1200 SparseStencilMatrix<ValueType, STENCIL_SIZE>::RowBase<DataType>::dot(
1201 const Vector<VecValueType>& inVec)
const
1203 return dot(inVec.data(), inVec.size());
1207 template<
typename ValueType, SizeType STENCIL_SIZE>
1208 template<
typename DataType>
1210 SparseStencilMatrix<ValueType, STENCIL_SIZE>::RowBase<DataType>::str()
const
1212 std::ostringstream ostr;
1214 for (
SizeType n = 0, N = this->size(); n < N; ++n) {
1215 ostr << sep <<
"(" << this->column(n) <<
", " << this->value(n) <<
")";
1222 template<
typename ValueType, SizeType STENCIL_SIZE>
1226 ConstRowBase(ConstRowData(const_cast<
ValueType*>(valueHead),
1232 template<
typename ValueType, SizeType STENCIL_SIZE>
1236 RowBase<>(RowData(valueHead, columnHead, rowSize)), mNumColumns(colSize)
1241 template<
typename ValueType, SizeType STENCIL_SIZE>
1246 RowBase<>::mData.mSize = 0;
1250 template<
typename ValueType, SizeType STENCIL_SIZE>
1255 assert(column < mNumColumns);
1257 RowData& data = RowBase<>::mData;
1261 SizeType offset = this->find(column);
1263 if (offset < data.mSize && data.mCols[offset] == column) {
1265 data.mVals[offset] = value;
1270 assert(data.mSize < this->capacity());
1272 if (offset >= data.mSize) {
1274 data.mVals[data.mSize] = value;
1275 data.mCols[data.mSize] = column;
1278 for (
SizeType i = data.mSize; i > offset; --i) {
1279 data.mVals[i] = data.mVals[i - 1];
1280 data.mCols[i] = data.mCols[i - 1];
1282 data.mVals[offset] = value;
1283 data.mCols[offset] = column;
1291 template<
typename ValueType, SizeType STENCIL_SIZE>
1292 template<
typename Scalar>
1296 for (
int idx = 0, N = this->
size(); idx < N; ++idx) {
1297 RowBase<>::mData.mVals[idx] *= s;
1306 template<
typename MatrixType>
1322 tbb::parallel_for(
SizeRange(0, A.numRows()), InitOp(A, mDiag.data()));
1344 InitOp(
const MatrixType& m,
ValueType* v): mat(&m), vec(v) {}
1346 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1347 const ValueType val = mat->getValue(n, n);
1349 vec[n] = static_cast<ValueType>(1.0 / val);
1359 x(x_), y(y_), out(out_) {}
1361 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = x[n] * y[n];
1372 template<
typename MatrixType>
1373 class IncompleteCholeskyPreconditioner:
public Preconditioner<typename MatrixType::ValueType>
1376 struct CopyToLowerOp;
1390 , mLowerTriangular(matrix.
numRows())
1391 , mUpperTriangular(matrix.
numRows())
1398 tbb::parallel_for(
SizeRange(0,
numRows), CopyToLowerOp(matrix, mLowerTriangular));
1418 mPassedCompatibilityCondition =
true;
1423 ValueType diagonalValue = crow_k.getValue(k);
1426 if (diagonalValue < 1.e-5) {
1427 mPassedCompatibilityCondition =
false;
1431 diagonalValue =
Sqrt(diagonalValue);
1434 row_k.setValue(k, diagonalValue);
1437 typename MatrixType::ConstRow srcRow = matrix.getConstRow(k);
1438 typename MatrixType::ConstValueIter citer = srcRow.cbegin();
1439 for ( ; citer; ++citer) {
1441 if (ii < k+1)
continue;
1445 row_ii.setValue(k, *citer / diagonalValue);
1450 for ( ; citer; ++citer) {
1452 if (j < k+1)
continue;
1459 typename MatrixType::ConstRow mask = matrix.getConstRow(j);
1460 typename MatrixType::ConstValueIter maskIter = mask.cbegin();
1461 for ( ; maskIter; ++maskIter) {
1463 if (i < j)
continue;
1469 a_ij -= a_ik * a_jk;
1471 row_i.setValue(j, a_ij);
1478 TransposeOp(matrix, mLowerTriangular, mUpperTriangular));
1483 bool isValid()
const override {
return mPassedCompatibilityCondition; }
1487 if (!mPassedCompatibilityCondition) {
1495 zVec.
fill(zeroVal<ValueType>());
1498 if (
size == 0)
return;
1504 mTempVec.fill(zeroVal<ValueType>());
1510 typename TriangularMatrix::ConstRow row = mLowerTriangular.getConstRow(i);
1513 tmpData[i] = (rData[i] - dot) / diagonal;
1514 if (!std::isfinite(tmpData[i])) {
1523 typename TriangularMatrix::ConstRow row = mUpperTriangular.getConstRow(i);
1526 zData[i] = (tmpData[i] - dot) / diagonal;
1527 if (!std::isfinite(zData[i])) {
1538 struct CopyToLowerOp
1540 CopyToLowerOp(
const MatrixType& m, TriangularMatrix& l): mat(&m), lower(&l) {}
1542 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1543 typename TriangularMatrix::RowEditor outRow = lower->getRowEditor(n);
1545 typename MatrixType::ConstRow inRow = mat->getConstRow(n);
1546 for (
typename MatrixType::ConstValueIter it = inRow.cbegin(); it; ++it) {
1547 if (it.column() > n)
continue;
1548 outRow.setValue(it.column(), *it);
1552 const MatrixType* mat; TriangularMatrix* lower;
1558 TransposeOp(
const MatrixType& m,
const TriangularMatrix& l, TriangularMatrix& u):
1559 mat(&m), lower(&l), upper(&u) {}
1561 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1562 typename TriangularMatrix::RowEditor outRow = upper->getRowEditor(n);
1565 typename MatrixType::ConstRow inRow = mat->getConstRow(n);
1566 for (
typename MatrixType::ConstValueIter it = inRow.cbegin(); it; ++it) {
1567 const SizeType column = it.column();
1568 if (column < n)
continue;
1569 outRow.setValue(column, lower->getValue(column, n));
1573 const MatrixType* mat;
const TriangularMatrix* lower; TriangularMatrix* upper;
1576 TriangularMatrix mLowerTriangular;
1577 TriangularMatrix mUpperTriangular;
1578 Vector<ValueType> mTempVec;
1579 bool mPassedCompatibilityCondition;
1586 namespace internal {
1589 template<
typename T>
1597 template<
typename T>
1601 assert(xVec.size() == yVec.size());
1602 assert(xVec.size() == result.size());
1603 axpy(a, xVec.data(), yVec.data(), result.data(), xVec.size());
1608 template<
typename MatrixOperator,
typename VecValueType>
1611 const VecValueType* b, VecValueType* r)
1614 A.vectorMultiply(x, r);
1620 template<
typename MatrixOperator,
typename T>
1624 assert(x.size() == b.size());
1625 assert(x.size() == r.size());
1626 assert(x.size() == A.numRows());
1637 template<
typename PositiveDefMatrix>
1640 const PositiveDefMatrix& Amat,
1644 const State& termination)
1647 return solve(Amat, bVec, xVec, precond, interrupter, termination);
1651 template<
typename PositiveDefMatrix,
typename Interrupter>
1654 const PositiveDefMatrix& Amat,
1658 Interrupter& interrupter,
1659 const State& termination)
1661 using ValueType =
typename PositiveDefMatrix::ValueType;
1675 if (
size != bVec.size()) {
1677 <<
size <<
"x" <<
size <<
" vs. " << bVec.size() <<
")");
1679 if (
size != xVec.size()) {
1681 <<
size <<
"x" <<
size <<
" vs. " << xVec.size() <<
")");
1698 assert(rVec.isFinite());
1717 for ( ; iteration < termination.
iterations; ++iteration) {
1719 if (interrupter.wasInterrupted()) {
1729 precond.apply(rVec, zVec);
1733 assert(std::isfinite(rDotZ));
1735 if (0 == iteration) {
1739 const ValueType beta = rDotZ / rDotZPrev;
1745 Amat.vectorMultiply(pVec, qVec);
1749 assert(std::isfinite(pAp));
1761 l2Error = rVec.l2Norm();
1762 minL2Error =
Min(l2Error, minL2Error);
1767 if (l2Error > 2 * minL2Error) {
1798 #endif // OPENVDB_MATH_CONJGRADIENT_HAS_BEEN_INCLUDED