% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/process_qp.R
\name{.build_deriv_qp}
\alias{.build_deriv_qp}
\title{Build Derivative QP Constraints in Full \eqn{P}-Dimensional Space}
\usage{
.build_deriv_qp(
  X_block,
  sign_mult,
  just_first,
  p_expansions,
  K,
  colnm_expansions,
  power1_cols,
  power2_cols,
  nonspline_cols,
  interaction_single_cols,
  interaction_quad_cols,
  triplet_cols,
  include_2way_interactions,
  include_3way_interactions,
  include_quadratic_interactions,
  expansion_scales,
  target_vars = NULL,
  og_cols = NULL
)
}
\arguments{
\item{X_block}{Numeric matrix, \eqn{N_{\mathrm{sub}} \times P}.
Block-diagonal design matrix for the QP-selected observations.}

\item{sign_mult}{Numeric scalar, \code{+1} for positive constraints
or \code{-1} for negative constraints.}

\item{just_first}{Logical. If \code{TRUE}, constrain first derivatives;
if \code{FALSE}, constrain second derivatives.}

\item{p_expansions}{Integer. Number of basis expansions per partition.}

\item{K}{Integer. Number of interior knots (partitions minus 1).}

\item{colnm_expansions}{Character vector of length \code{p_expansions}.
Column names of the expansion matrix.}

\item{power1_cols}{Integer vector of linear-term column indices.}

\item{power2_cols}{Integer vector of quadratic-term column indices.}

\item{nonspline_cols}{Integer vector of non-spline linear column indices.}

\item{interaction_single_cols}{Integer vector of linear-by-linear
interaction column indices.}

\item{interaction_quad_cols}{Integer vector of linear-by-quadratic
interaction column indices.}

\item{triplet_cols}{Integer vector of three-way interaction column indices.}

\item{include_2way_interactions}{Logical switch forwarded to
\code{make_derivative_matrix}.}

\item{include_3way_interactions}{Logical switch forwarded to
\code{make_derivative_matrix}.}

\item{include_quadratic_interactions}{Logical switch forwarded to
\code{make_derivative_matrix}.}

\item{expansion_scales}{Numeric vector of length \code{p_expansions - 1}.
Passed as unit scales (\code{1 + 0 * expansion_scales}) so that the
constraint matrix stays on the expansion-standardized coefficient scale.}

\item{target_vars}{Optional. Integer vector of predictor column indices
or character vector of predictor names identifying which predictors
to constrain. When \code{NULL} (default), all derivative variables
are constrained.}

\item{og_cols}{Optional character vector of original predictor column
names, used to resolve character \code{target_vars} to integer indices.}
}
\value{
A list with components:
\describe{
  \item{Amat}{\eqn{P \times M} constraint matrix, where \eqn{M} is the
    number of unique constraint columns (after deduplication).}
  \item{bvec}{Numeric vector of length \eqn{M}, all zeros.}
  \item{meq}{Integer, always \code{0} (inequality constraints).}
}
}
\description{
Internal helper that constructs the \code{Amat} / \code{bvec} pair
enforcing derivative sign constraints at every row of a block-diagonal
design matrix.

Given an \eqn{N_{\mathrm{sub}} \times P} block-diagonal design matrix
\code{X_block}, where \eqn{P = p \times (K+1)}, this function:
\enumerate{
  \item Recovers the per-partition \eqn{p}-column expansion matrix
    \code{C_qp} and records each row's partition assignment.
  \item Calls \code{make_derivative_matrix} on \code{C_qp} to obtain
    first or second derivative matrices with respect to each predictor.
  \item Optionally selects only derivatives for a subset of predictor
    variables (\code{target_vars}).
  \item Maps each derivative row into the full \eqn{P}-dimensional
    coefficient space, yielding one constraint column per (observation,
    variable) pair.
}

The result is a constraint pair \eqn{\mathbf{A}^{\top}\boldsymbol{\beta}
\ge \mathbf{b}} (with \eqn{\mathbf{b} = \mathbf{0}}) suitable for
\code{\link[quadprog]{solve.QP}}.
}
\seealso{
\code{\link{process_qp}}, \code{\link[quadprog]{solve.QP}}
}
\keyword{internal}
