27 const std::vector<Eigen::MatrixXd> &diagonal,
28 const std::vector<Eigen::VectorXd> &gradient,
29 const std::vector<Eigen::MatrixXd> &normals,
30 const Eigen::MatrixXd &coupling,
const Eigen::MatrixXd &twist,
31 const Eigen::VectorXd &correction)
33 using Matrix = Eigen::MatrixXd;
34 using Vector = Eigen::VectorXd;
35 if (diagonal.empty() || gradient.size() != diagonal.size() ||
36 normals.size() != diagonal.size() || !coupling.allFinite() ||
37 !correction.allFinite())
38 throw std::invalid_argument(
"Invalid twisted constrained chain");
39 const std::size_t n = diagonal.size();
40 const Eigen::Index d = gradient.front().size(), k = correction.size();
41 if (coupling.rows() != d || coupling.cols() != d ||
42 (coupling - coupling.transpose()).norm() >
43 64 * std::numeric_limits<double>::epsilon() * d *
44 std::max(1., coupling.norm()))
45 throw std::invalid_argument(
46 "Finite symmetric matrix coupling required");
47 if (!d || twist.rows() != d || twist.cols() != d || !twist.allFinite() ||
48 (twist.transpose() * twist - Matrix::Identity(d, d)).norm() > 1e-12 * d)
49 throw std::invalid_argument(
"Expected orthogonal twist");
50 Matrix gram = Matrix::Zero(k, k);
51 for (std::size_t j = 0; j < n; ++j) {
52 if (diagonal[j].rows() != d || diagonal[j].cols() != d ||
53 gradient[j].size() != d || normals[j].rows() != d ||
54 normals[j].cols() != k || !diagonal[j].allFinite() ||
55 !gradient[j].allFinite() || !normals[j].allFinite() ||
56 (diagonal[j] - diagonal[j].transpose()).norm() >
57 64 * std::numeric_limits<double>::epsilon() * d *
59 throw std::invalid_argument(
"Inconsistent constrained blocks");
60 gram += normals[j].transpose() * normals[j];
62 if ((gram - Matrix::Identity(k, k)).norm() > 1e-10 * k)
63 throw std::invalid_argument(
64 "Constraint columns must be globally orthonormal");
66 Matrix reduced = Matrix::Zero(d + k, d + k);
68 std::vector<Matrix> solved, inverse;
70 reduced.topLeftCorner(d, d) =
71 diagonal[0] - coupling * twist - twist.transpose() * coupling;
72 reduced.topRightCorner(d, k) = normals[0];
73 reduced.bottomLeftCorner(k, d) = normals[0].transpose();
74 right.head(d) = -gradient[0];
75 right.tail(k) = correction;
77 std::vector<Matrix> forward;
78 for (std::size_t j = 1; j < n; ++j) {
79 Matrix pivot = diagonal[j], rhs = Matrix::Zero(d, d + 1 + k);
80 if (j == 1) rhs.leftCols(d) -= coupling;
81 if (j + 1 == n) rhs.leftCols(d) -= coupling * twist;
82 rhs.col(d) = gradient[j];
83 rhs.rightCols(k) = normals[j];
85 pivot -= coupling * inverse.back() * coupling;
86 rhs += coupling * forward.back();
88 Eigen::FullPivLU<Matrix> factor(pivot);
89 if (!factor.isInvertible())
return result;
90 inverse.push_back(factor.solve(Matrix::Identity(d, d)));
91 forward.push_back(factor.solve(rhs));
92 if (!inverse.back().allFinite() || !forward.back().allFinite())
96 for (std::size_t j = n - 2; j > 0; --j)
97 solved[j - 1] += inverse[j - 1] * coupling * solved[j];
98 const Matrix btSolve = -coupling * solved.front() -
99 twist.transpose() * coupling * solved.back();
100 reduced.topLeftCorner(d, d) = diagonal[0] - btSolve.leftCols(d);
101 reduced.topRightCorner(d, k) = normals[0] - btSolve.rightCols(k);
102 reduced.bottomLeftCorner(k, d) =
103 reduced.topRightCorner(d, k).transpose();
104 Matrix qSolveQ = Matrix::Zero(k, k);
105 Vector qSolveG = Vector::Zero(k);
106 for (std::size_t j = 1; j < n; ++j) {
107 qSolveQ += normals[j].transpose() * solved[j - 1].rightCols(k);
108 qSolveG += normals[j].transpose() * solved[j - 1].col(d);
110 reduced.bottomRightCorner(k, k) = -qSolveQ;
111 right.head(d) = -gradient[0] + btSolve.col(d);
112 right.tail(k) = correction + qSolveG;
114 if (!reduced.allFinite() || !right.allFinite())
return result;
115 Eigen::FullPivLU<Matrix> border(reduced);
116 if (!border.isInvertible())
return result;
117 const Vector endpoint = border.solve(right);
118 if (!endpoint.allFinite())
return result;
121 for (std::size_t j = 1; j < n; ++j)
123 -solved[j - 1].col(d) -
124 solved[j - 1].leftCols(d) * endpoint.head(d) -
126 double error = 0, scale = 0;
127 Vector projected = Vector::Zero(k);
128 for (std::size_t j = 0; j < n; ++j) {
131 if (j) residual -= coupling * result.
displacement[j - 1];
132 if (j + 1 < n) residual -= coupling * result.
displacement[j + 1];
135 twist.transpose() * coupling * result.
displacement.back();
137 residual -= coupling * twist * result.
displacement.front();
138 error = std::max(error, residual.norm());
139 scale = std::max(scale, gradient[j].norm());
140 projected += normals[j].transpose() * result.
displacement[j];