R/npfNLP.R

Defines functions npfNLP

npfNLP <- function(W,A,H,r){
  
  WVec <- as.vector(W)
  
  #Constraint
  hinfun <- function(WVec,A,H,r){
    W <- matrix(WVec,nrow=nrow(A),byrow=FALSE)
  
    Result <- npffAH(A,H,W)
    
    if(Result < 1e-8){
      Result <- 0
    }
    
    return(Result)
  }
  
  #Objective
  fn <- function(WVec,r,A,H){
    W <- matrix(WVec,nrow=nrow(A),byrow=FALSE)
    W1 <- W[,1:r]
    W2 <- W[,-c(1:r)]
    Result <- sum(W2^2)
    
    return(Result)
  }
  
  #Optimization
  
  S <- cobyla(W, fn, hin = hinfun, control = list(xtol_rel = 1e-8, maxeval = 2000),A=A,H=H,r=r)
  
  #Results
    
  Wvec <- S$par
  W <- matrix(WVec,nrow=nrow(A),byrow=FALSE)
  W1 <- W[,1:r]
  
  return(W1)
} 

Try the Datasmith package in your browser

Any scripts or data that you put into this service are public.

Datasmith documentation built on May 29, 2017, 10:19 p.m.