cancel_double_expr {lazy.symbolic}R Documentation

Simplify repeated unary operators (t / inv / -) in an expression

Description

Japanese help file: cancel_double_expr_JPH

Usage

cancel_double_expr(
  expr,
  double_op = c("t", "inv", "-"),
  sym,
  use_unary_reorder = FALSE
)

Arguments

expr

Input expression. A quoted expression or a character string. If a string is supplied, it is parsed via 'parse(text = ...)'. On parse failure, a warning is issued and 'NULL' is returned.

double_op

Character vector of unary operator names to target for double-application detection/simplification. Default is 'c("t", "inv", "-")'. Patterns like 't(t(X)) -> X' and 'inv(inv(X)) -> X' are simplified. For the unary minus, simplification may happen after reordering when 'use_unary_reorder = TRUE'. You may add other unary operator names if needed.

sym

Character vector of symbols assumed to be symmetric. For any 'S' listed here, 't(S) = S' is used during simplification. By default, the identity matrix '"I"' is included (so 't(I) = I').

use_unary_reorder

Logical. If 'TRUE', applies 'unary_reorder_expr()' to normalize the placement of unary operators, which can expose double applications (e.g., 't(inv(t(inv(A)))) simplification can occur.

Details

'cancel_double_expr()' walks an expression tree and algebraically simplifies consecutive applications of the same unary operator (e.g., 't(t(X)) -> X', 'inv(inv(X)) -> X'). Optionally, it reorders unary operators first to expose such patterns before simplifying.

Value

an expression

Examples


modify_math_operators()
cancel_double_expr("t(t(A))")
cancel_double_expr("t(A)")
cancel_double_expr("A")
cancel_double_expr("t(t(t(A)))")
cancel_double_expr("t(t(t(A))*B)")
cancel_double_expr("t(t(t(t(A))*t(B)))")
cancel_double_expr("t(t(A))*inv(inv(t(B)))")
cancel_double_expr("inv(inv(t(B)))")
cancel_double_expr("t(I)")
cancel_double_expr("inv(I)")
cancel_double_expr("B%*%t(S)%*%inv(S)")
cancel_double_expr("B%*%t(S)%*%inv(S)", sym = "S")
cancel_double_expr("t(I)%*%B%*%t(S)%*%inv(I)")

cancel_double_expr("t(inv(t(inv(A))))", use_unary_reorder=TRUE)
cancel_double_expr("-inv(t(-(B)))", use_unary_reorder=TRUE)



[Package lazy.symbolic version 1.0.0.20250830 ]