Efficient Evaluation of Quadratic Forms

A range of quadratic forms are evaluated, using efficient methods. Unnecessary transposes are not performed. Complex values are handled consistently.


Quadratic forms in R: the quadform package

CRAN_Status_Badge

Quadratic forms are polynomials with all terms of degree 2. Given a real column vector ${\mathbf x}=(x_1,\ldots,x_n)^\top$ and an $n\times n$ matrix $M$ then the function $f\colon\mathbb{R}^n\longrightarrow\mathbb{R}$ given by $f({\mathbf x})={\mathbf x}^\top M{\mathbf x}$ is a quadratic form; we extend to complex vectors by mapping ${\mathbf z}=(z_1,\ldots, z_n)^\top$ to ${\mathbf z}^M{\mathbf z}$, where ${\mathbf z}^$ means the complex conjugate of ${\mathbf z}^\top$. These are implemented in the package with quad.form(M,x) which is essentially

quad.form <- function(M,x){crossprod(crossprod(M, Conj(x)), x)}.

This is preferable to t(x) %*% M %*% x on several grounds. Firstly, it streamlines and simplifies code; secondly, it is more efficient; and thirdly it handles the complex case consistently. The package includes similar functionality for other related expressions.

The main motivation for the package is nicer code. For example, the emulator package has to manipulate the following expression:

$$ \left[H_x-H^\top A^{-1}U\right]^\top \left[H^\top\left(H^\top A^{-1}H\right)^{-1}H\right] \left[H_x-H^\top A^{-1}U\right]. $$

Direct R idiom would be:

t(Hx - t(H) %*% solve(A) %*% U) %*%  t(H) %*% solve(t(H) %*% solve(A) %*% H) %*% H %*%  (Hx - t(H) %*% solve(A) %*% U)

But quadform idiom is:

quad.form(quad.form.inv(quad.form.inv(A,H),H), Hx - quad3.form.inv(A,H,U))

and in terse form becomes:

qf(qfi(qfi(A,H),H), Hx - q3fi(A,H,U))

which is certainly shorter, arguably more elegant, and possibly faster.

The package is maintained on github.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("quadform")

0.0-4 by Robin K. S. Hankin, a year ago


https://github.com/RobinHankin/quadform


Report a bug at https://github.com/RobinHankin/quadform/issues


Browse source code at https://github.com/cran/quadform


Authors: Robin K. S. Hankin [aut, cre] (ORCID:


Documentation:   PDF Manual  


GPL license


Suggests testthat


Imported by MM, approximator, catalytic, cmvnorm, emulator, jordan, lorentz, multivator.

Suggested by clifford, stokes.


See at CRAN