matReplace {lazy.symbolic}R Documentation

Replace Symbolic Expressions with Others

Description

Replace Symbolic Expressions with Others

Usage

matReplace(exp, ..., values = NULL, ntogoback = 1, print = 0, debug = 0)

Arguments

exp

A symbolic expression

...

Replacement of the form pat1=pat2
where the size of pat1 must be equal to pat2.

values

A character string consisting of several replacements separated by semicolons.

ntogoback

n parameter of sys.parent function

print

= 2 to show the details of the replacement = 1 to print the result

debug

= 1 to print the additional info

Details

This function replaces the elements of pat1 matrix included in exp with the corresponding elements of pat2 matrix.
A symbolic expression containing symbolic matrix pat1 can be converted to a numeric one by specifying a numeric pat2 matrix.

If the i-th element of ... contains pat1=pat2,
when ... is decomposed into name and val as
name <- as.list(substitute(list(...)))[-1L]
val <- list(...)
name[[i]] will contain "pat2" and val[[i]] will contain the value of pat2, and names(name) will contain "pat1".
That is, the value of pat1 is missing.
For example, ... argument of

  A <- demomat(3,2,root="a"); An <- demomat(3,2)
  B <- demomat(2,3,root="b"); Bn <- demomat(2,3)
  AB <- matReplace( exp, A=An, B=Bn )

will be decomposed into the following lists of length two:

name:
$A
An

$B
Bn


val:
$A
   c1 c2
r1 11 12
r2 21 22
r3 31 32

$B
   c1 c2 c3
r1 11 12 13
r2 21 22 23


Therefore, the value of A, which is the A matrix, is missing, and we must retrieve the original value of A in the parent frame from which matReplace function was executed.
In order to do so, use get function with pos=parent.frame(n=ntogoback).

The replacement can be given in two ways.
In the first method, in order to replace the elements of A matrix by the corresponding elements of An matrix, and, B by Bn,
we write
matReplace( exp, A=An, B=Bn )
Another way is to write
matReplace( exp, values="A=An; B=Bn" )
The second method is easy to use when there are many similar replacements.
The second method has the proprity over the first method.

When the values parameter is given as values="a=1; b=2;...",
and the values of a, b etc in the parent environment are scalars, or not defined,
the occurrence of literal "a" is replaced by 1, "b" by 2, and so on.

Value

exp A symbolic expression of the same size as exp with the desired replacement.

Examples


# matrix replacement
A <- demomat(3,2,root="a"); An <- demomat(3,2)
B <- demomat(2,3,root="b"); Bn <- demomat(2,3)
AB <- matTimes(A,B)
ABn <- matReplace( AB, A=An, B=Bn, print=2 )
Print( Eval(ABn)-An%*%Bn )

val <- "A=An; B=Bn"
ABn2 <- matReplace(AB, values=val)
Print( Eval(ABn2)-An%*%Bn )

# scalar replacement
val <- "a=1; b=2; c=3; d=4; e=5"
exp <- "(a+b*c-d)/e"
matReplace(exp,values=val)

sexp <- "s( a[i,j]*v[k], {k,1,ncol(A)})"
matReplace(sexp,values="ncol(A)=2")


[Package lazy.symbolic version 0.1.3 Index]