matReplace {lazy.symbolic2}R Documentation

Replace the Elements of a Symbolic Matrix

Description

Replace the Elements of a Symbolic Matrix

Usage

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

Arguments

...

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.

fullsymb

= 1 to treat exp as a full symbolic expression.
This has priority over the attributes of exp.

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

exp

A symbolic matrix

Details

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

Consider the following example:

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

Here, the elements of AB matrix are symbolic, e.g.,
AB[1,2]="a11*b12+a12*b22"
and we would like to replace the i-j element of A matrix, aij, with the corresponding i-j element of An matrix, 10*i+j, and the i-j elements of B matrix with the corresponding element of Bn matrix, 100+10*i+j, and so on.
The finall result is in ABn whose 1-2 element, for example, is
"11*112+12*122".

When exp is a fully symbolic matrix as seen in the last example below, exp contain an expression consisting of the matrices which appear to the left of equal signs as pat1 in the replacement specification such as A11, A12.
In this case, those matrices will be replaced by the corresponding matrices to the right of equal sign of the specification, pat2.

When exp is a fully symbolic expression w/o fullsymb attribute, specifying fullsymb=1 will treat the expression as fully symbolic one.

Details of semi symbolic case.

If ... consists of A=An, B=Bn
the following code:

  atd <- analyze_three_dots(...)
  pat2 <- atd$val
  name <- as.list(atd$name2)
  names(name) <- atd$name

results in the following:

 pat2: list consisting of the value of \code{An} and \code{Bn}
        in the parent environment
 name: list of characters "An" and "Bn"
 names(name): vector consisting of "A" and "B"
 

However, the value of A and B in the parent environment is missing.

For example, ... argument of the above example 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


Since exp consists of the elements of A and B defined in the parent environment, in order to replace those by the elements of An and Bn, we must retrieve the values of f A and B from the parent environment,

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.

If the replacements such as values="a=1; b=2;..." are given,
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")


# Below gives unexpected result.
matReplace( "cos(theta)*sin(a)", a=1, theta=0 )
matReplace( "cos(theta)*sin(a)", values="a=1; theta=0" )
# This works as expected.
matReplace( "cos(theta)*sin(a)", theta=0, a=1 )
matReplace( "cos(theta)*sin(a)", values="theta=0; a=1" )

# Treating exp as a fully symbolic expression with fullsymb=1.
matReplace("A", A=demomat(3,3), fullsymb=1)
matReplace("matTimes(t(A),B)"
     , A=demomat(2,2, root="a"), B=demomat(2,2, root="b"), fullsymb=1)


## Not run: 
library(lazy.sumbolic2)

# Fully symbolic matrix
# define a fully symbolic matrix A
A <- demomat( 2,2, root="A", fullsymb=1 )
tA <- tp(A)
expr=matTimes(tA,A)

A11s <- demomat(2,2,root="a11_")
A21s <- demomat(2,2,root="a21_")
A12s <- demomat(2,2,root="a12_")
A22s <- demomat(2,2,root="a22_")

valuelist <- "A11=A11s; A21=A21s; A12=A12s; A22=A22s"
matReplace( expr, values=valuelist )
matReplace( expr, A11=A11s, A21=A21s, A12=A12s, A22=A22s )


## End(Not run)


[Package lazy.symbolic2 version 0.1.4 ]