Learn R Programming

editrules (version 2.2-0)

editmatrix: Create an editmatrix

Description

Transforms a list of R (in)equalities into an edit matrix.

Usage

editmatrix(editrules, normalize = TRUE)

Arguments

Value

an object of class "editmatrix" which is a matrix with extra attributes

Details

Transforms a list of R (in)equalities into an edit matrix with coefficients (A) for each variable, and a constant (b) and operator (ops) for each edit rule.

Each row in the resulting editmatrix represents an linear (in) equality. Each column in the resulting editmatrix represents a variable.

There are three forms of creating an editmatrix:

  1. acharactervector with (in)equalities written in R syntax
  2. aexpressionvector with (in)equalities written in R syntax
  3. adata.framewith three columns:
    • name = acharacterwith the name of each rule
    • edit = acharacterwith (in)equalities written in R syntax
    • description = acharacterdescribing the intention of the rule
    Typically these rules are stored in a external csv file (or database).

Numerical edits can also be read from a free-form file using editfile. The function as.data.frame extracts the third form, which can be used to store edit rules externally or to recreate an editmatrix later on. Functions as.character and as.expression extract the first and second form.

The matrix is created by retrieving the coefficients of the variables in the equalities. i.e. x == y results in c(x=1, y=-1) and x == y + w results in c(x=1, y=-1, w=-1)

By default the editmatrix is created using the comparison operators (==,<=,>=,<,>) in the edits. If option normalize=TRUE is used all edits are transformed into an A == b, A < b or A

See Also

as.editmatrix

Examples

Run this code
# Using a character vector to define contraints
E <- editmatrix(c("x+3*y==2*z", "x==z"))
print(E)

# Using a expression vector to define contraints
E <- editmatrix(expression(x+3*y==2*z, x==z))
print(E)

# an editmatrix also has a summary method:
summary(E)

# select rows from an editmatrix:
E <- editmatrix(c("x+3*y==2*z", "x >= z"))
E[getOps(E) == "=="]


#Using data.frame to define constraints
E.df <- data.frame(
    name =c("A","B","C"),
    edit = c("x == y",    
            "z + w == y + x",
            "z == y + 2*w"),
    description = c(
            "these variables should be equal","","")

)
print(E.df)

E <- editmatrix(E.df)
print(E)

Run the code above in your browser using DataLab