-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtools.R
More file actions
31 lines (26 loc) · 831 Bytes
/
tools.R
File metadata and controls
31 lines (26 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
##### (just a few utility functions)
delta<-function(x,t) ifelse(x==t,1,0)
delta_c<-function(x,t) ifelse(x!=t,1,0)
flatten<-function(A) as.vector(t(A)) ## makes it into a row vector
prune<-function(x,thres) ifelse(x>thres,x,0)
soft_threshold<-function(x,thres) ifelse(x>thres,x-thres,ifelse(x<(-1.0*thres),x+thres,0))
hard_threshold<-function(x,thres) ifelse(abs(x)>thres,x,0)
row_normalize<-function(A){
normA<-sqrt(apply(A^2, 1, sum))
B=A
for ( j in 1: nrow(A)){
B[j,]<-A[j,]/normA[j]
}
return(B)
}
ginv<-function(X, tol = sqrt(.Machine$double.eps))
{
## Generalized Inverse of a Matrix
dnx <- dimnames(X)
if(is.null(dnx)) dnx <- vector("list", 2)
s <- svd(X)
nz <- s$d > tol * s$d[1]
structure(
if(any(nz)) s$v[, nz] %*% (t(s$u[, nz])/s$d[nz]) else X,
dimnames = dnx[2:1])
}