// Old implementation of Jacobian DenseMatrix BSplineBasis::evalBasisJacobianOld(DenseVector &x) const { // Jacobian basis matrix DenseMatrix J; J.setZero(getNumBasisFunctions(), numVariables); // Calculate partial derivatives for (unsigned int i = 0; i < numVariables; i++) { // One column in basis jacobian DenseVector bi; bi.setOnes(1); for (unsigned int j = 0; j < numVariables; j++) { DenseVector temp = bi; DenseVector xi; if (j == i) { // Differentiated basis xi = bases.at(j).evaluateFirstDerivative(x(j)); } else { // Normal basis xi = bases.at(j).evaluate(x(j)); } bi = kroneckerProduct(temp, xi); } // Fill out column J.block(0,i,bi.rows(),1) = bi.block(0,0,bi.rows(),1); } return J; }
void BSpline::setControlPoints(const DenseMatrix &controlPoints) { if (controlPoints.cols() != numVariables + 1) throw Exception("BSpline::setControlPoints: Incompatible size of control point matrix."); int nc = controlPoints.rows(); knotaverages = controlPoints.block(0, 0, nc, numVariables); coefficients = controlPoints.block(0, numVariables, nc, 1); checkControlPoints(); }
SparseMatrix BSplineBasis1D::reduceSupport(double lb, double ub) { // Check bounds if (lb < knots.front() || ub > knots.back()) throw Exception("BSplineBasis1D::reduceSupport: Cannot increase support!"); unsigned int k = degree + 1; int index_lower = indexSupportedBasisfunctions(lb).front(); int index_upper = indexSupportedBasisfunctions(ub).back(); // Check lower bound index if (k != knotMultiplicity(knots.at(index_lower))) { int suggested_index = index_lower - 1; if (0 <= suggested_index) { index_lower = suggested_index; } else { throw Exception("BSplineBasis1D::reduceSupport: Suggested index is negative!"); } } // Check upper bound index if (knotMultiplicity(ub) == k && knots.at(index_upper) == ub) { index_upper -= k; } // New knot vector std::vector<double> si; si.insert(si.begin(), knots.begin()+index_lower, knots.begin()+index_upper+k+1); // Construct selection matrix A int numOld = knots.size()-k; // Current number of basis functions int numNew = si.size()-k; // Number of basis functions after update if (numOld < numNew) throw Exception("BSplineBasis1D::reduceSupport: Number of basis functions is increased instead of reduced!"); DenseMatrix Ad = DenseMatrix::Zero(numOld, numNew); Ad.block(index_lower, 0, numNew, numNew) = DenseMatrix::Identity(numNew, numNew); SparseMatrix A = Ad.sparseView(); // Update knots knots = si; return A; }
template<typename SparseMatrixType> void sparse_block(const SparseMatrixType& ref) { const Index rows = ref.rows(); const Index cols = ref.cols(); const Index inner = ref.innerSize(); const Index outer = ref.outerSize(); typedef typename SparseMatrixType::Scalar Scalar; typedef typename SparseMatrixType::StorageIndex StorageIndex; double density = (std::max)(8./(rows*cols), 0.01); typedef Matrix<Scalar,Dynamic,Dynamic,SparseMatrixType::IsRowMajor?RowMajor:ColMajor> DenseMatrix; typedef Matrix<Scalar,Dynamic,1> DenseVector; typedef Matrix<Scalar,1,Dynamic> RowDenseVector; typedef SparseVector<Scalar> SparseVectorType; Scalar s1 = internal::random<Scalar>(); { SparseMatrixType m(rows, cols); DenseMatrix refMat = DenseMatrix::Zero(rows, cols); initSparse<Scalar>(density, refMat, m); VERIFY_IS_APPROX(m, refMat); // test InnerIterators and Block expressions for (int t=0; t<10; ++t) { Index j = internal::random<Index>(0,cols-2); Index i = internal::random<Index>(0,rows-2); Index w = internal::random<Index>(1,cols-j); Index h = internal::random<Index>(1,rows-i); VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w)); for(Index c=0; c<w; c++) { VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c)); for(Index r=0; r<h; r++) { VERIFY_IS_APPROX(m.block(i,j,h,w).col(c).coeff(r), refMat.block(i,j,h,w).col(c).coeff(r)); VERIFY_IS_APPROX(m.block(i,j,h,w).coeff(r,c), refMat.block(i,j,h,w).coeff(r,c)); } } for(Index r=0; r<h; r++) { VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r)); for(Index c=0; c<w; c++) { VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c)); VERIFY_IS_APPROX(m.block(i,j,h,w).coeff(r,c), refMat.block(i,j,h,w).coeff(r,c)); } } VERIFY_IS_APPROX(m.middleCols(j,w), refMat.middleCols(j,w)); VERIFY_IS_APPROX(m.middleRows(i,h), refMat.middleRows(i,h)); for(Index r=0; r<h; r++) { VERIFY_IS_APPROX(m.middleCols(j,w).row(r), refMat.middleCols(j,w).row(r)); VERIFY_IS_APPROX(m.middleRows(i,h).row(r), refMat.middleRows(i,h).row(r)); for(Index c=0; c<w; c++) { VERIFY_IS_APPROX(m.col(c).coeff(r), refMat.col(c).coeff(r)); VERIFY_IS_APPROX(m.row(r).coeff(c), refMat.row(r).coeff(c)); VERIFY_IS_APPROX(m.middleCols(j,w).coeff(r,c), refMat.middleCols(j,w).coeff(r,c)); VERIFY_IS_APPROX(m.middleRows(i,h).coeff(r,c), refMat.middleRows(i,h).coeff(r,c)); if(m.middleCols(j,w).coeff(r,c) != Scalar(0)) { VERIFY_IS_APPROX(m.middleCols(j,w).coeffRef(r,c), refMat.middleCols(j,w).coeff(r,c)); } if(m.middleRows(i,h).coeff(r,c) != Scalar(0)) { VERIFY_IS_APPROX(m.middleRows(i,h).coeff(r,c), refMat.middleRows(i,h).coeff(r,c)); } } } for(Index c=0; c<w; c++) { VERIFY_IS_APPROX(m.middleCols(j,w).col(c), refMat.middleCols(j,w).col(c)); VERIFY_IS_APPROX(m.middleRows(i,h).col(c), refMat.middleRows(i,h).col(c)); } } for(Index c=0; c<cols; c++) { VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c)); VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c)); } for(Index r=0; r<rows; r++) { VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r)); VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r)); } } // test innerVector() { DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); SparseMatrixType m2(rows, cols); initSparse<Scalar>(density, refMat2, m2); Index j0 = internal::random<Index>(0,outer-1); Index j1 = internal::random<Index>(0,outer-1); Index r0 = internal::random<Index>(0,rows-1); Index c0 = internal::random<Index>(0,cols-1); VERIFY_IS_APPROX(m2.innerVector(j0), innervec(refMat2,j0)); VERIFY_IS_APPROX(m2.innerVector(j0)+m2.innerVector(j1), innervec(refMat2,j0)+innervec(refMat2,j1)); m2.innerVector(j0) *= Scalar(2); innervec(refMat2,j0) *= Scalar(2); VERIFY_IS_APPROX(m2, refMat2); m2.row(r0) *= Scalar(3); refMat2.row(r0) *= Scalar(3); VERIFY_IS_APPROX(m2, refMat2); m2.col(c0) *= Scalar(4); refMat2.col(c0) *= Scalar(4); VERIFY_IS_APPROX(m2, refMat2); m2.row(r0) /= Scalar(3); refMat2.row(r0) /= Scalar(3); VERIFY_IS_APPROX(m2, refMat2); m2.col(c0) /= Scalar(4); refMat2.col(c0) /= Scalar(4); VERIFY_IS_APPROX(m2, refMat2); SparseVectorType v1; VERIFY_IS_APPROX(v1 = m2.col(c0) * 4, refMat2.col(c0)*4); VERIFY_IS_APPROX(v1 = m2.row(r0) * 4, refMat2.row(r0).transpose()*4); SparseMatrixType m3(rows,cols); m3.reserve(VectorXi::Constant(outer,int(inner/2))); for(Index j=0; j<outer; ++j) for(Index k=0; k<(std::min)(j,inner); ++k) m3.insertByOuterInner(j,k) = internal::convert_index<StorageIndex>(k+1); for(Index j=0; j<(std::min)(outer, inner); ++j) { VERIFY(j==numext::real(m3.innerVector(j).nonZeros())); if(j>0) VERIFY(j==numext::real(m3.innerVector(j).lastCoeff())); } m3.makeCompressed(); for(Index j=0; j<(std::min)(outer, inner); ++j) { VERIFY(j==numext::real(m3.innerVector(j).nonZeros())); if(j>0) VERIFY(j==numext::real(m3.innerVector(j).lastCoeff())); } VERIFY(m3.innerVector(j0).nonZeros() == m3.transpose().innerVector(j0).nonZeros()); // m2.innerVector(j0) = 2*m2.innerVector(j1); // refMat2.col(j0) = 2*refMat2.col(j1); // VERIFY_IS_APPROX(m2, refMat2); } // test innerVectors() { DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); SparseMatrixType m2(rows, cols); initSparse<Scalar>(density, refMat2, m2); if(internal::random<float>(0,1)>0.5f) m2.makeCompressed(); Index j0 = internal::random<Index>(0,outer-2); Index j1 = internal::random<Index>(0,outer-2); Index n0 = internal::random<Index>(1,outer-(std::max)(j0,j1)); if(SparseMatrixType::IsRowMajor) VERIFY_IS_APPROX(m2.innerVectors(j0,n0), refMat2.block(j0,0,n0,cols)); else VERIFY_IS_APPROX(m2.innerVectors(j0,n0), refMat2.block(0,j0,rows,n0)); if(SparseMatrixType::IsRowMajor) VERIFY_IS_APPROX(m2.innerVectors(j0,n0)+m2.innerVectors(j1,n0), refMat2.middleRows(j0,n0)+refMat2.middleRows(j1,n0)); else VERIFY_IS_APPROX(m2.innerVectors(j0,n0)+m2.innerVectors(j1,n0), refMat2.block(0,j0,rows,n0)+refMat2.block(0,j1,rows,n0)); VERIFY_IS_APPROX(m2, refMat2); VERIFY(m2.innerVectors(j0,n0).nonZeros() == m2.transpose().innerVectors(j0,n0).nonZeros()); m2.innerVectors(j0,n0) = m2.innerVectors(j0,n0) + m2.innerVectors(j1,n0); if(SparseMatrixType::IsRowMajor) refMat2.middleRows(j0,n0) = (refMat2.middleRows(j0,n0) + refMat2.middleRows(j1,n0)).eval(); else refMat2.middleCols(j0,n0) = (refMat2.middleCols(j0,n0) + refMat2.middleCols(j1,n0)).eval(); VERIFY_IS_APPROX(m2, refMat2); } // test generic blocks { DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); SparseMatrixType m2(rows, cols); initSparse<Scalar>(density, refMat2, m2); Index j0 = internal::random<Index>(0,outer-2); Index j1 = internal::random<Index>(0,outer-2); Index n0 = internal::random<Index>(1,outer-(std::max)(j0,j1)); if(SparseMatrixType::IsRowMajor) VERIFY_IS_APPROX(m2.block(j0,0,n0,cols), refMat2.block(j0,0,n0,cols)); else VERIFY_IS_APPROX(m2.block(0,j0,rows,n0), refMat2.block(0,j0,rows,n0)); if(SparseMatrixType::IsRowMajor) VERIFY_IS_APPROX(m2.block(j0,0,n0,cols)+m2.block(j1,0,n0,cols), refMat2.block(j0,0,n0,cols)+refMat2.block(j1,0,n0,cols)); else VERIFY_IS_APPROX(m2.block(0,j0,rows,n0)+m2.block(0,j1,rows,n0), refMat2.block(0,j0,rows,n0)+refMat2.block(0,j1,rows,n0)); Index i = internal::random<Index>(0,m2.outerSize()-1); if(SparseMatrixType::IsRowMajor) { m2.innerVector(i) = m2.innerVector(i) * s1; refMat2.row(i) = refMat2.row(i) * s1; VERIFY_IS_APPROX(m2,refMat2); } else { m2.innerVector(i) = m2.innerVector(i) * s1; refMat2.col(i) = refMat2.col(i) * s1; VERIFY_IS_APPROX(m2,refMat2); } Index r0 = internal::random<Index>(0,rows-2); Index c0 = internal::random<Index>(0,cols-2); Index r1 = internal::random<Index>(1,rows-r0); Index c1 = internal::random<Index>(1,cols-c0); VERIFY_IS_APPROX(DenseVector(m2.col(c0)), refMat2.col(c0)); VERIFY_IS_APPROX(m2.col(c0), refMat2.col(c0)); VERIFY_IS_APPROX(RowDenseVector(m2.row(r0)), refMat2.row(r0)); VERIFY_IS_APPROX(m2.row(r0), refMat2.row(r0)); VERIFY_IS_APPROX(m2.block(r0,c0,r1,c1), refMat2.block(r0,c0,r1,c1)); VERIFY_IS_APPROX((2*m2).block(r0,c0,r1,c1), (2*refMat2).block(r0,c0,r1,c1)); if(m2.nonZeros()>0) { VERIFY_IS_APPROX(m2, refMat2); SparseMatrixType m3(rows, cols); DenseMatrix refMat3(rows, cols); refMat3.setZero(); Index n = internal::random<Index>(1,10); for(Index k=0; k<n; ++k) { Index o1 = internal::random<Index>(0,outer-1); Index o2 = internal::random<Index>(0,outer-1); if(SparseMatrixType::IsRowMajor) { m3.innerVector(o1) = m2.row(o2); refMat3.row(o1) = refMat2.row(o2); } else { m3.innerVector(o1) = m2.col(o2); refMat3.col(o1) = refMat2.col(o2); } if(internal::random<bool>()) m3.makeCompressed(); } if(m3.nonZeros()>0) VERIFY_IS_APPROX(m3, refMat3); } } }
bool ConstraintBSpline::controlPointBoundsDeduction() const { // Get variable bounds auto xlb = bspline.getDomainLowerBound(); auto xub = bspline.getDomainUpperBound(); // Use these instead? // for (unsigned int i = 0; i < bspline.getNumVariables(); i++) // { // xlb.at(i) = variables.at(i)->getLowerBound(); // xub.at(i) = variables.at(i)->getUpperBound(); // } double lowerBound = variables.back()->getLowerBound(); // f(x) = y > lowerBound double upperBound = variables.back()->getUpperBound(); // f(x) = y < upperBound // Get knot vectors and basis degrees auto knotVectors = bspline.getKnotVectors(); auto basisDegrees = bspline.getBasisDegrees(); // Compute n value for each variable // Total number of control points is ns(0)*...*ns(d-1) std::vector<unsigned int> numBasisFunctions = bspline.getNumBasisFunctions(); // Get matrix of coefficients DenseMatrix cps = controlPoints; DenseMatrix coeffs = cps.block(bspline.getNumVariables(), 0, 1, cps.cols()); for (unsigned int d = 0; d < bspline.getNumVariables(); d++) { if (assertNear(xlb.at(d), xub.at(d))) continue; auto n = numBasisFunctions.at(d); auto p = basisDegrees.at(d); std::vector<double> knots = knotVectors.at(d); assert(knots.size() == n+p+1); // Tighten lower bound unsigned int i = 1; for (; i <= n; i++) { // Knot interval of interest: [t_0, t_i] // Selection matrix DenseMatrix S = DenseMatrix::Ones(1,1); for (unsigned int d2 = 0; d2 < bspline.getNumVariables(); d2++) { DenseMatrix temp(S); DenseMatrix Sd_full = DenseMatrix::Identity(numBasisFunctions.at(d2),numBasisFunctions.at(d2)); DenseMatrix Sd(Sd_full); if (d == d2) Sd = Sd_full.block(0,0,n,i); S = kroneckerProduct(temp, Sd); } // Control points that have support in [t_0, t_i] DenseMatrix selc = coeffs*S; DenseVector minCP = selc.rowwise().minCoeff(); DenseVector maxCP = selc.rowwise().maxCoeff(); double minv = minCP(0); double maxv = maxCP(0); // Investigate feasibility if (minv > upperBound || maxv < lowerBound) continue; // infeasible else break; // feasible } // New valid lower bound on x(d) is knots(i-1) if (i > 1) { if (!variables.at(d)->updateLowerBound(knots.at(i-1))) return false; } // Tighten upper bound i = 1; for (; i <= n; i++) { // Knot interval of interest: [t_{n+p-i}, t_{n+p}] // Selection matrix DenseMatrix S = DenseMatrix::Ones(1,1); for (unsigned int d2 = 0; d2 < bspline.getNumVariables(); d2++) { DenseMatrix temp(S); DenseMatrix Sd_full = DenseMatrix::Identity(numBasisFunctions.at(d2),numBasisFunctions.at(d2)); DenseMatrix Sd(Sd_full); if (d == d2) Sd = Sd_full.block(0,n-i,n,i); S = kroneckerProduct(temp, Sd); } // Control points that have support in [t_{n+p-i}, t_{n+p}] DenseMatrix selc = coeffs*S; DenseVector minCP = selc.rowwise().minCoeff(); DenseVector maxCP = selc.rowwise().maxCoeff(); double minv = minCP(0); double maxv = maxCP(0); // Investigate feasibility if (minv > upperBound || maxv < lowerBound) continue; // infeasible else break; // feasible } // New valid lower bound on x(d) is knots(n+p-(i-1)) if (i > 1) { if (!variables.at(d)->updateUpperBound(knots.at(n+p-(i-1)))) return false; // NOTE: the upper bound seems to not be tight! can we use knots.at(n+p-i)? } } return true; }
bool BSplineBasis1D::reduceSupport(double lb, double ub, SparseMatrix &A) { // Check bounds if(lb < knots.front() || ub > knots.back()) { return false; } unsigned int k = degree + 1; int index_lower = indexSupportedBasisfunctions(lb).front(); int index_upper = indexSupportedBasisfunctions(ub).back(); // Check lower bound index unsigned int count = knotMultiplicity(knots.at(index_lower)); bool is_p_regular = (k == count); if(!is_p_regular) { int suggested_index = index_lower - 1; if(0 <= suggested_index) { index_lower = suggested_index; } else { #ifndef NDEBUG std::cout << "\n\n----------------adjust_index_for_domain_reduction-----------------" << std::endl; std::cout << "Error: not enough knots to guarantee controlpoint convergence" << std::endl; std::cout << "----------------adjust_index_for_domain_reduction-----------------\n\n" << std::endl; #endif // NDEBUG return false; } } // Check upper bound index if(knotMultiplicity(ub) == k && knots.at(index_upper) == ub) { index_upper -= k; } // New knot vector std::vector<double> si; si.insert(si.begin(), knots.begin()+index_lower, knots.begin()+index_upper+k+1); // Construct selection matrix A int n_old = knots.size()-k; // Current number of basis functions int n_new = si.size()-k; // Number of basis functions after update if (n_old < n_new) return false; DenseMatrix Ad = DenseMatrix::Zero(n_old, n_new); Ad.block(index_lower, 0, n_new, n_new) = DenseMatrix::Identity(n_new, n_new); A = Ad.sparseView(); // Update knots knots = si; return true; }