Skip to contents

stm_fsystem() converts a character vector of expressions into a function that evaluates each expression using vectors of provided arguments.

Usage

stm_fsystem(fsystem, vars = NULL)

Arguments

fsystem

A character vector where each element is a mathematical expression as a string, representing the equations.

vars

Optional. A character vector specifying the variable names used in the expressions. Default is NULL, meaning that variables are automatically detected.

Value

A function that takes vectors of arguments and evaluates each expression in fsystem with the given values, returning a numeric vector of results.

Details

This function takes a system of mathematical expressions and converts them into a callable R function. Each expression is evaluated with corresponding input vectors. If the number of provided arguments does not match the number of expressions, an error is raised.

Examples

f <- c("2 * x + y", "x^2 + sqrt(y)")
f_list <- stm_fsystem(f, vars = c("x", "y"))
f_list(c(x = 4, y = 9), c(x = 12, y = 7))
#> [1]  17.0000 146.6458

f <- c("a + b", "a^2 - b")
f_list <- stm_fsystem(f, vars = c("a", "b"))
f_list(c(a = 3, b = 2), c(a = 5, b = 1))
#> [1]  5 24