From 542040f55a14dee08494d8ffddaf6508741ff7ba Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 06:00:18 +0000 Subject: [PATCH 1/3] Initial plan From 2d35334fad4833bef47eb336f4ae7182277dbd2f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 06:38:57 +0000 Subject: [PATCH 2/3] Add math_demo function with LaTeX math formatting in Roxygen Co-authored-by: d-morrison <2474437+d-morrison@users.noreply.github.com> --- NAMESPACE | 1 + R/math_demo.R | 83 +++++++++++++++++++++++++++++++++ inst/WORDLIST | 2 + man/math_demo.Rd | 64 +++++++++++++++++++++++++ tests/testthat/test-math_demo.R | 40 ++++++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 R/math_demo.R create mode 100644 man/math_demo.Rd create mode 100644 tests/testthat/test-math_demo.R diff --git a/NAMESPACE b/NAMESPACE index 79e14c4f..07acbd3b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,4 @@ # Generated by roxygen2: do not edit by hand export(example_function) +export(math_demo) diff --git a/R/math_demo.R b/R/math_demo.R new file mode 100644 index 00000000..ab69b32c --- /dev/null +++ b/R/math_demo.R @@ -0,0 +1,83 @@ +#' Demonstrate LaTeX Math Formatting in Roxygen +#' +#' This function demonstrates how to include formatted mathematical expressions +#' in roxygen2 documentation using LaTeX syntax. +#' +#' @description +#' The quadratic formula is given by: +#' \deqn{x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}}{ +#' x = (-b ± sqrt(b^2 - 4ac)) / (2a)} +#' +#' For inline math, the variance is denoted \eqn{\sigma^2}{sigma^2}, +#' and the standard deviation is \eqn{\sigma}{sigma}. +#' +#' @details +#' This function computes the roots of a quadratic equation of the form: +#' \deqn{ax^2 + bx + c = 0}{ax^2 + bx + c = 0} +#' +#' The discriminant is \eqn{\Delta = b^2 - 4ac}{Delta = b^2 - 4ac}. +#' When \eqn{\Delta > 0}{Delta > 0}, there are two real roots. +#' When \eqn{\Delta = 0}{Delta = 0}, there is one repeated real root. +#' When \eqn{\Delta < 0}{Delta < 0}, there are two complex conjugate roots. +#' +#' Additional mathematical notation examples: +#' \itemize{ +#' \item Sum notation: \eqn{\sum_{i=1}^{n} x_i}{sum(x_i, i=1..n)} +#' \item Integral: \eqn{\int_{0}^{\infty} e^{-x} dx = 1}{ +#' integral from 0 to infinity of e^(-x) dx = 1} +#' \item Matrix multiplication: +#' \eqn{\mathbf{Y} = \mathbf{X}\boldsymbol{\beta} + +#' \boldsymbol{\epsilon}}{Y = X*beta + epsilon} +#' \item Greek letters: \eqn{\alpha, \beta, \gamma, \delta}{ +#' alpha, beta, gamma, delta} +#' } +#' +#' @param a Numeric coefficient of \eqn{x^2}{x^2} +#' @param b Numeric coefficient of \eqn{x}{x} +#' @param c Numeric constant term +#' +#' @return A numeric vector of length 1 or 2 containing the root(s) of +#' the equation. Complex roots are returned as complex numbers. +#' +#' @export +#' +#' @examples +#' # Two real roots: x^2 - 5x + 6 = 0 +#' # Solution: x = 2 or x = 3 +#' math_demo(1, -5, 6) +#' +#' # One repeated root: x^2 - 4x + 4 = 0 +#' # Solution: x = 2 +#' math_demo(1, -4, 4) +#' +#' # Complex roots: x^2 + 2x + 5 = 0 +#' # Solution: x = -1 ± 2i +#' math_demo(1, 2, 5) +math_demo <- function(a, b, c) { + # Check for valid input + if (a == 0) { + stop("Coefficient 'a' must be non-zero for a quadratic equation") + } + + # Calculate discriminant + discriminant <- b^2 - 4 * a * c + + # Calculate roots based on discriminant + if (discriminant > 0) { + # Two distinct real roots + root1 <- (-b + sqrt(discriminant)) / (2 * a) + root2 <- (-b - sqrt(discriminant)) / (2 * a) + return(c(root1, root2)) + } else if (discriminant == 0) { + # One repeated real root + root <- -b / (2 * a) + return(root) + } else { + # Two complex conjugate roots + real_part <- -b / (2 * a) + imaginary_part <- sqrt(-discriminant) / (2 * a) + root1 <- complex(real = real_part, imaginary = imaginary_part) + root2 <- complex(real = real_part, imaginary = -imaginary_part) + return(c(root1, root2)) + } +} diff --git a/inst/WORDLIST b/inst/WORDLIST index 28833c01..88e508cc 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -2,5 +2,7 @@ CMD CodeFactor Codecov Lifecycle +Roxygen ackage emplate +roxygen diff --git a/man/math_demo.Rd b/man/math_demo.Rd new file mode 100644 index 00000000..f2969f84 --- /dev/null +++ b/man/math_demo.Rd @@ -0,0 +1,64 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/math_demo.R +\name{math_demo} +\alias{math_demo} +\title{Demonstrate LaTeX Math Formatting in Roxygen} +\usage{ +math_demo(a, b, c) +} +\arguments{ +\item{a}{Numeric coefficient of \eqn{x^2}{x^2}} + +\item{b}{Numeric coefficient of \eqn{x}{x}} + +\item{c}{Numeric constant term} +} +\value{ +A numeric vector of length 1 or 2 containing the root(s) of +the equation. Complex roots are returned as complex numbers. +} +\description{ +The quadratic formula is given by: +\deqn{x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}}{ + x = (-b ± sqrt(b^2 - 4ac)) / (2a)} + +For inline math, the variance is denoted \eqn{\sigma^2}{sigma^2}, +and the standard deviation is \eqn{\sigma}{sigma}. +} +\details{ +This function demonstrates how to include formatted mathematical expressions +in roxygen2 documentation using LaTeX syntax. + +This function computes the roots of a quadratic equation of the form: +\deqn{ax^2 + bx + c = 0}{ax^2 + bx + c = 0} + +The discriminant is \eqn{\Delta = b^2 - 4ac}{Delta = b^2 - 4ac}. +When \eqn{\Delta > 0}{Delta > 0}, there are two real roots. +When \eqn{\Delta = 0}{Delta = 0}, there is one repeated real root. +When \eqn{\Delta < 0}{Delta < 0}, there are two complex conjugate roots. + +Additional mathematical notation examples: +\itemize{ +\item Sum notation: \eqn{\sum_{i=1}^{n} x_i}{sum(x_i, i=1..n)} +\item Integral: \eqn{\int_{0}^{\infty} e^{-x} dx = 1}{ + integral from 0 to infinity of e^(-x) dx = 1} +\item Matrix multiplication: +\eqn{\mathbf{Y} = \mathbf{X}\boldsymbol{\beta} + + \boldsymbol{\epsilon}}{Y = X*beta + epsilon} +\item Greek letters: \eqn{\alpha, \beta, \gamma, \delta}{ + alpha, beta, gamma, delta} +} +} +\examples{ +# Two real roots: x^2 - 5x + 6 = 0 +# Solution: x = 2 or x = 3 +math_demo(1, -5, 6) + +# One repeated root: x^2 - 4x + 4 = 0 +# Solution: x = 2 +math_demo(1, -4, 4) + +# Complex roots: x^2 + 2x + 5 = 0 +# Solution: x = -1 ± 2i +math_demo(1, 2, 5) +} diff --git a/tests/testthat/test-math_demo.R b/tests/testthat/test-math_demo.R new file mode 100644 index 00000000..1ac626bf --- /dev/null +++ b/tests/testthat/test-math_demo.R @@ -0,0 +1,40 @@ +test_that("math_demo handles two distinct real roots", { + # x^2 - 5x + 6 = 0 has roots x = 2 and x = 3 + result <- math_demo(1, -5, 6) + expect_length(result, 2) + expect_type(result, "double") + expect_equal(sort(result), c(2, 3)) +}) + +test_that("math_demo handles one repeated real root", { + # x^2 - 4x + 4 = 0 has root x = 2 (repeated) + result <- math_demo(1, -4, 4) + expect_length(result, 1) + expect_type(result, "double") + expect_equal(result, 2) +}) + +test_that("math_demo handles complex roots", { + # x^2 + 2x + 5 = 0 has complex roots x = -1 ± 2i + result <- math_demo(1, 2, 5) + expect_length(result, 2) + expect_type(result, "complex") + expect_equal(Re(result[1]), -1) + expect_equal(Re(result[2]), -1) + expect_equal(abs(Im(result[1])), 2) + expect_equal(abs(Im(result[2])), 2) +}) + +test_that("math_demo throws error when a = 0", { + expect_error( + math_demo(0, 5, 3), + "Coefficient 'a' must be non-zero for a quadratic equation" + ) +}) + +test_that("math_demo works with different coefficients", { + # 2x^2 - 8x + 6 = 0 has roots x = 1 and x = 3 + result <- math_demo(2, -8, 6) + expect_length(result, 2) + expect_equal(sort(result), c(1, 3)) +}) From 8c2172a7b07c4bd6b8a7c0a7f99b1cb5ba5a5ca5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 06:39:56 +0000 Subject: [PATCH 3/3] Fix floating-point comparison in math_demo Co-authored-by: d-morrison <2474437+d-morrison@users.noreply.github.com> --- R/math_demo.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/math_demo.R b/R/math_demo.R index ab69b32c..81866732 100644 --- a/R/math_demo.R +++ b/R/math_demo.R @@ -55,7 +55,7 @@ #' math_demo(1, 2, 5) math_demo <- function(a, b, c) { # Check for valid input - if (a == 0) { + if (isTRUE(all.equal(a, 0))) { stop("Coefficient 'a' must be non-zero for a quadratic equation") }