| Title: | Unit Tests for Mathematical Properties |
|---|---|
| Description: | Inspired by 'testthat', 'testmath' aims to provide some functions for unit tests of mathematical properties in a readable manner. At the moment, these functions cover a few aspects of two distinct areas - unit tests for matrices and functions. Failure should provide detailed information for what condition led to the test failing. |
| Authors: | Stefano Barbieri [aut, cre] |
| Maintainer: | Stefano Barbieri <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-11-08 11:41:47 UTC |
| Source: | https://github.com/stefanobarbieri4/testmath |
Do you expect a matrix to be diagonal?
expect_diagonal(matrix, tolerance = testthat::testthat_tolerance(), ...)expect_diagonal(matrix, tolerance = testthat::testthat_tolerance(), ...)
matrix |
matrix. The matrix you are checking to be diagonal. |
tolerance |
numeric. The tolerance of difference between compared
entries. Defaults to |
... |
Additional arguments passed to |
Other matrix properties:
expect_orthogonal(),
expect_psd(),
expect_spd(),
expect_symmetric()
mat_diag <- diag(c(1, 5, 3)) mat_not_diag <- matrix(c(1, 2, 0, 3), nrow = 2) expect_diagonal(mat_diag) testthat::expect_failure(expect_diagonal(mat_not_diag))mat_diag <- diag(c(1, 5, 3)) mat_not_diag <- matrix(c(1, 2, 0, 3), nrow = 2) expect_diagonal(mat_diag) testthat::expect_failure(expect_diagonal(mat_not_diag))
This function tests whether applying a function twice yields the same result as applying it once.
expect_idempotent(f, x, tolerance = testthat::testthat_tolerance(), ...)expect_idempotent(f, x, tolerance = testthat::testthat_tolerance(), ...)
f |
function. The function you are testing. |
x |
parameter(s). The value under which you test the function. |
tolerance |
Passed on to |
... |
Additional arguments passed into |
Other function properties:
expect_involutory(),
expect_n_idempotent(),
expect_n_involutory(),
expect_periodic()
x <- c(2, 1, 3) expect_idempotent(mean, x)x <- c(2, 1, 3) expect_idempotent(mean, x)
This function tests whether a function, is its own
inverse for a given value of .
expect_involutory(f, x, tolerance = testthat::testthat_tolerance(), ...)expect_involutory(f, x, tolerance = testthat::testthat_tolerance(), ...)
f |
function. The function you are testing to be involutory |
x |
parameter(s). The value under which you test the function. |
tolerance |
Passed on to |
... |
Additional arguments passed into |
Tests that . A function is called involutory (or a
self-inverse function) if this applies at each element in its domain.
Other function properties:
expect_idempotent(),
expect_n_idempotent(),
expect_n_involutory(),
expect_periodic()
x <- c(2, 1, 3) expect_involutory(rev, x) # passes testthat::expect_failure(expect_involutory(sort, x)) # failsx <- c(2, 1, 3) expect_involutory(rev, x) # passes testthat::expect_failure(expect_involutory(sort, x)) # fails
-idempotent?This function tests whether a function, , applied
times on yields the same result as applying it a single time.
expect_n_idempotent(f, x, n, tolerance = testthat::testthat_tolerance(), ...)expect_n_idempotent(f, x, n, tolerance = testthat::testthat_tolerance(), ...)
f |
function. The function you are testing. |
x |
parameter(s). The value under which you test the function |
n |
integer. The expected order of idempotence. |
tolerance |
Passed on to |
... |
Additional arguments passed into |
Tests that . For n = 2, this is equivalent to
expect_idempotent.
Other function properties:
expect_idempotent(),
expect_involutory(),
expect_n_involutory(),
expect_periodic()
# A function that reaches its fixed/stable state after 3 steps custom_func <- function(x) { pmin(pmax(x, 0), 1) # e.g., clipping logic } expect_n_idempotent(custom_func, -5:5, n = 3)# A function that reaches its fixed/stable state after 3 steps custom_func <- function(x) { pmin(pmax(x, 0), 1) # e.g., clipping logic } expect_n_idempotent(custom_func, -5:5, n = 3)
-involutory?This function tests whether a function, , applied
times on returns .
expect_n_involutory(f, x, n, tolerance = testthat::testthat_tolerance(), ...)expect_n_involutory(f, x, n, tolerance = testthat::testthat_tolerance(), ...)
f |
function. The function you are testing. |
x |
parameter(s). The value under which you test the function |
n |
integer. The expected order of involution. |
tolerance |
Passed on to |
... |
Additional arguments passed into |
Tests that . For n = 2, this is equivalent to
expect_involutory.
Other function properties:
expect_idempotent(),
expect_involutory(),
expect_n_idempotent(),
expect_periodic()
mult_by_im <- function(x) { x * 1i } expect_n_involutory(mult_by_im, 1, n = 4)mult_by_im <- function(x) { x * 1i } expect_n_involutory(mult_by_im, 1, n = 4)
Do you expect a matrix to be orthogonal?
expect_orthogonal(matrix, tolerance = testthat::testthat_tolerance(), ...)expect_orthogonal(matrix, tolerance = testthat::testthat_tolerance(), ...)
matrix |
matrix. The matrix you are checking for orthogonality. |
tolerance |
numeric. The tolerance of difference between compared
entries. Defaults to |
... |
Additional arguments passed to |
Other matrix properties:
expect_diagonal(),
expect_psd(),
expect_spd(),
expect_symmetric()
# Rotation matrix is orthogonal theta <- pi / 4 R <- matrix(c(cos(theta), -sin(theta), sin(theta), cos(theta)), nrow = 2) expect_orthogonal(R) # Non-orthogonal matrix mat_bad <- matrix(1:4, nrow = 2) testthat::expect_failure(expect_orthogonal(mat_bad))# Rotation matrix is orthogonal theta <- pi / 4 R <- matrix(c(cos(theta), -sin(theta), sin(theta), cos(theta)), nrow = 2) expect_orthogonal(R) # Non-orthogonal matrix mat_bad <- matrix(1:4, nrow = 2) testthat::expect_failure(expect_orthogonal(mat_bad))
This function tests whether a function, , is periodic on
with period
expect_periodic(f, x, period, tolerance = testthat::testthat_tolerance(), ...)expect_periodic(f, x, period, tolerance = testthat::testthat_tolerance(), ...)
f |
function. The function you are testing to be periodic. |
x |
parameter(s). The value under which you test the function |
period |
numeric. The expected period of |
tolerance |
Passed on to |
... |
Additional arguments passed into |
Tests that
Other function properties:
expect_idempotent(),
expect_involutory(),
expect_n_idempotent(),
expect_n_involutory()
expect_periodic(cos, c(0, 1, 2), 2*pi) expect_periodic(cos, c(0, 1, 2), 4*pi) expect_periodic(cos, c(pi/2), pi)expect_periodic(cos, c(0, 1, 2), 2*pi) expect_periodic(cos, c(0, 1, 2), 4*pi) expect_periodic(cos, c(pi/2), pi)
Do you expect a matrix to be positive semidefinite?
expect_psd(matrix, tolerance = testthat::testthat_tolerance())expect_psd(matrix, tolerance = testthat::testthat_tolerance())
matrix |
matrix. The matrix you are checking to be PSD. |
tolerance |
numeric. The tolerance for symmetry checks and eigenvalues.
Defaults to |
In addition to checking whether the matrix is symmetric, it also
checks if the eigenvalues of matrix are all positive. Equivalent
to checking if .
Other matrix properties:
expect_diagonal(),
expect_orthogonal(),
expect_spd(),
expect_symmetric()
# Matrix with zero eigenvalue (PSD, but not strictly PD) mat_psd <- matrix(c(1, 1, 1, 1), nrow = 2) expect_psd(mat_psd) # Indefinite matrix fails PSD mat_bad <- matrix(c(1, 2, 2, 1), nrow = 2) testthat::expect_failure(expect_psd(mat_bad))# Matrix with zero eigenvalue (PSD, but not strictly PD) mat_psd <- matrix(c(1, 1, 1, 1), nrow = 2) expect_psd(mat_psd) # Indefinite matrix fails PSD mat_bad <- matrix(c(1, 2, 2, 1), nrow = 2) testthat::expect_failure(expect_psd(mat_bad))
Do you expect a matrix to be symmetric positive definite?
expect_spd(matrix, tolerance = testthat::testthat_tolerance())expect_spd(matrix, tolerance = testthat::testthat_tolerance())
matrix |
matrix. The matrix you are checking to be SPD. |
tolerance |
numeric. The tolerance for symmetry checks and eigenvalues.
Defaults to |
In addition to checking whether the matrix is symmetric, it also
checks if the eigenvalues of matrix are all strictly positive. Equivalent
to checking if .
Other matrix properties:
expect_diagonal(),
expect_orthogonal(),
expect_psd(),
expect_symmetric()
mat_spd <- matrix(c(2, -1, -1, 2), nrow = 2) # spd mat_not_spd <- matrix(c(1, 2, 2, 1), nrow = 2) # symmetric, but not positive definite expect_spd(mat_spd) testthat::expect_failure(expect_spd(mat_not_spd))mat_spd <- matrix(c(2, -1, -1, 2), nrow = 2) # spd mat_not_spd <- matrix(c(1, 2, 2, 1), nrow = 2) # symmetric, but not positive definite expect_spd(mat_spd) testthat::expect_failure(expect_spd(mat_not_spd))
Do you expect a matrix to be symmetric?
expect_symmetric(matrix, tolerance = testthat::testthat_tolerance())expect_symmetric(matrix, tolerance = testthat::testthat_tolerance())
matrix |
matrix. The matrix you are checking for symmetry. |
tolerance |
numeric. The tolerance of difference between compared
entries. Defaults to |
Checks whether for all i, j. As soon as an
infringement is found, it breaks early.
Other matrix properties:
expect_diagonal(),
expect_orthogonal(),
expect_psd(),
expect_spd()
mat1 <- matrix(c(7, 4, 4, 5), nrow = 2, ncol = 2) # symmetric mat2 <- matrix(c(4, 1, 3, 9), nrow = 2, ncol = 2) # not symmetric expect_symmetric(mat1) testthat::expect_failure(expect_symmetric(mat2))mat1 <- matrix(c(7, 4, 4, 5), nrow = 2, ncol = 2) # symmetric mat2 <- matrix(c(4, 1, 3, 9), nrow = 2, ncol = 2) # not symmetric expect_symmetric(mat1) testthat::expect_failure(expect_symmetric(mat2))