# [R] optim or nlm with matrices

Thomas Lumley tlumley at u.washington.edu
Fri Apr 26 23:04:26 CEST 2002

```On Fri, 26 Apr 2002, Ahmad Abu Hammour wrote:

> Hi, I have the following hypothetical optimization problem:
> -det(t(x-A%*%x1)%*%(x-A%*%x1)) where A,x,x1 are matrices. A coefficients
> and x and x1 are variable matrices or vectors. I tried to apply optim
> and nlm functions but I kept receive the following massage: Error in
> A%*%x1 : non-conformable arguments. The massage appears even the -det()
> can be calculated and the dimensions are checked. here is my example
> although there might be no solution for the optimization problem. A=A
> myfn=function(A){ x=matrix(c(1.8,0),byrow=T)
> x1=matrix(c(.8,1.8),byrow=T) -det((t(x)-t(x1)%*%A)%*%(x-A%*%x1)) }
> A=matrix(c(1,.3,2,-1.2),byrow=T,nrow=2) optim(A,myfn)

The first argument to optim() is a vector, not a matrix.  You need to
coerce to a matrix insider myfn(), eg
myfn=function(A){
A<-matrix(A,ncol=2)
x=matrix(c(1.8,0),byrow=T)
x1=matrix(c(.8,1.8),byrow=T)
-det((t(x)-t(x1)%*%A)%*%(x-A%*%x1))
}

> Another question
> regarding optimization: is there any chance that I can find a function
> or package that can do a constrained optimization such as:
> -det(x-A*x1-B*x2)' (x-A*x1-B*x2))  subject to p-f(A,B,...)=0, where f

I don't know of such a function. You probably have to rewrite the function
being optimised, eg with Lagrange multipliers.

-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```