MyNixOS website logo
Description

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 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})=x^TMx$ 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 $z^$ means the complex conjugate of $z^T$. 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.

Metadata

Version

0.0-2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows