   ## 7  Miscellaneous Functions

Implementation of a conjugate gradient for solving a linear system Ax=b when A is positive definite. In some cases, it is faster than the Matlab function `pcg`, especially when the library uses the Intel Math Kernel Library.

 # # Name: spams.conjGrad # # Usage: spams.conjGrad(A,b,x0 = NULL,tol = 1e-10,itermax = NULL) # # Description: #     Conjugate gradient algorithm, sometimes faster than the  #    equivalent R function solve. In order to solve Ax=b; # # Inputs: #       A:  double square n x n matrix. HAS TO BE POSITIVE DEFINITE #       b:  double vector of length n. #       x0: double vector of length n. (optional) initial guess. #       tol: (optional) tolerance. #       itermax: (optional) maximum number of iterations. # # Output: #       x: double vector of length n. # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) A = matrix(rnorm(500 * 5000),nrow = 5000,ncol = 500) A = t(A) %*% A b = as.vector(rep(1,ncol(A))) x0 = b tol = 1e-4 itermax = floor(0.5 * length(b)) CGtest <- function(txt,expr) {   tic = proc.time()   for (i in 1:20)     y = eval(expr)   tac = proc.time()   cat(sprintf("  Time (%s): %f\n",txt,(tac - tic)[['user.self']]))   x1 = abs(b - (A %*% y))   cat(sprintf("Mean error on b : %f\n\n",sum(x1) / length(b))) } y2 = CGtest("R",quote(solve(A,b))) y1 = CGtest("spams",quote(spams.conjGrad(A,b,x0,tol,itermax)))

### 7.2  Function spams.bayer

Apply a Bayer pattern to an input image

 # # Name: spams.conjGrad # # Usage: spams.conjGrad(A,b,x0 = NULL,tol = 1e-10,itermax = NULL) # # Description: #     Conjugate gradient algorithm, sometimes faster than the  #    equivalent R function solve. In order to solve Ax=b; # # Inputs: #       A:  double square n x n matrix. HAS TO BE POSITIVE DEFINITE #       b:  double vector of length n. #       x0: double vector of length n. (optional) initial guess. #       tol: (optional) tolerance. #       itermax: (optional) maximum number of iterations. # # Output: #       x: double vector of length n. # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) A = matrix(rnorm(500 * 5000),nrow = 5000,ncol = 500) A = t(A) %*% A b = as.vector(rep(1,ncol(A))) x0 = b tol = 1e-4 itermax = floor(0.5 * length(b)) CGtest <- function(txt,expr) {   tic = proc.time()   for (i in 1:20)     y = eval(expr)   tac = proc.time()   cat(sprintf("  Time (%s): %f\n",txt,(tac - tic)[['user.self']]))   x1 = abs(b - (A %*% y))   cat(sprintf("Mean error on b : %f\n\n",sum(x1) / length(b))) } y2 = CGtest("R",quote(solve(A,b))) y1 = CGtest("spams",quote(spams.conjGrad(A,b,x0,tol,itermax)))

### 7.3  Function spams.calcAAt

For an input sparse matrix A, this function returns AAT. For some reasons, when A has a lot more columns than rows, this function can be much faster than the equivalent matlab command `X*A'`.

 # # Name: spams.calcAAt # # Usage: spams.calcAAt(A) # # Description: #     Compute efficiently AAt = A*A', when A is sparse  #   and has a lot more columns than rows. In some cases, it is #   up to 20 times faster than the equivalent R expression #   AAt=A*A'; # # Inputs: #       A:  double sparse m x n matrix    # # Output: #       AAt: double m x m matrix  # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) m = 200;n = 200000; d= 0.05 A = rSpMatrix(m,n,floor(m * n * d)) spams.calcAAt(A)

### 7.4  Function spams.calcXAt

For an input sparse matrix A and a matrix X, this function returns XAT. For some reasons, when A has a lot more columns than rows, this function can be much faster than the equivalent matlab command `X*A'`.

 # # Name: spams.calcXAt # # Usage: spams.calcXAt(X,A) # # Description: #     Compute efficiently XAt = X*A', when A is sparse and has a  #   lot more columns than rows. In some cases, it is up to 20 times  #   faster than the equivalent R expression; # # Inputs: #       X:  double m x n matrix #       A:  double sparse p x n matrix    # # Output: #       XAt: double m x p matrix  # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) m = 200;n = 200000; d= 0.05 A = rSpMatrix(m,n,floor(m * n * d)) X = matrix(rnorm(64 * n),nrow = 64,ncol = n) spams.calcXAt(X,A)

### 7.5  Function spams.calcXY

For two input matrices X and Y, this function returns XY.

 # # Name: spams.calcXY # # Usage: spams.calcXY(X,Y) # # Description: #     Compute Z=XY using the BLAS library used by SPAMS. # # Inputs: #       X:  double m x n matrix #       Y:  double n x p matrix    # # Output: #       Z: double m x p matrix  # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) X = matrix(rnorm(64 * 200),nrow = 64,ncol = 200,byrow = FALSE) Y = matrix(rnorm(200 * 20000),nrow = 200,ncol = 20000,byrow = FALSE) spams.calcXY(X,Y)

### 7.6  Function spams.calcXYt

For two input matrices X and Y, this function returns XYT.

 # # Name: spams.calcXYt # # Usage: spams.calcXYt(X,Y) # # Description: #     Compute Z=XY' using the BLAS library used by SPAMS. # # Inputs: #       X:  double m x n matrix #       Y:  double p x n matrix    # # Output: #       Z: double m x p matrix  # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) X = matrix(rnorm(64 * 200),nrow = 64,ncol = 200,byrow = FALSE) Y = matrix(rnorm(200 * 20000),nrow = 20000,ncol = 200,byrow = FALSE) spams.calcXYt(X,Y)

### 7.7  Function spams.calcXtY

For two input matrices X and Y, this function returns XTY.

 # # Name: spams.calcXtY # # Usage: spams.calcXtY(X,Y) # # Description: #     Compute Z=X'Y using the BLAS library used by SPAMS. # # Inputs: #       X:  double n x m matrix #       Y:  double n x p matrix    # # Output: #       Z: double m x p matrix  # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) X = matrix(rnorm(64 * 200),nrow = 200,ncol = 64,byrow = FALSE) Y = matrix(rnorm(200 * 20000),nrow = 200,ncol = 20000,byrow = FALSE) spams.calcXtY(X,Y)

### 7.8  Function spams.invSym

For an input symmetric matrices A in ℝn × n, this function returns A−1.

 # # Name: spams.invSym # # Usage: spams.invSym(A) # # Description: #     returns the inverse of a symmetric matrix A # # Inputs: #       A:  double n x n matrix    # # Output: #       B: double n x n matrix  # # Authors: # Julien MAIRAL, 2009 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) A = matrix(runif(1000 * 1000,0,1),nrow = 1000,ncol = 1000,byrow = FALSE) A = t(A) %*% A spams.invSym(A)

### 7.9  Function spams.normalize

 # # Name: spams.normalize # # Usage: spams.normalize(X) # # Description: #     rescale the columns of X so that they have #        unit l2-norm. # # Inputs: #       X:  double m x n matrix    # # Output: #       Y: double m x n matrix  # # Authors: # Julien MAIRAL, 2010 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) A = matrix(runif(100 * 1000,0,1),nrow = 100,ncol = 1000) y2 = spams.normalize(A)

### 7.10  Function spams.sort

 # # Name: spams.sort # # Usage: spams.sort(X,mode=T) # # Description: #     sort the elements of X using quicksort # # Inputs: #       X:  double vector of size n #       mode: false for decreasing order (true by default) # # Output: #       Y: double  vector of size n # # Authors: # Julien MAIRAL, 2010 (spams, matlab interface and documentation) # Jean-Paul CHIEZE 2011-2012 (R interface) #

The following piece of code illustrates how to use this function. The following piece of code contains usage examples:

 library(spams) x = rnorm(2000000,0,1) spams.sort(x,TRUE)

### 7.11  Function mexDisplayPatches

Print to the screen a matrix containing as columns image patches.

### 7.12  Function spams.countPathsDAG

This function counts the number of paths in a DAG using dynamic programming.

 # # The R function is not yet implemented. #

The following piece of code illustrates how to use this function.

### 7.13  Function spams.removeCyclesGraph

One heuristic to remove cycles from a graph.

 # # The R function is not yet implemented. #

The following piece of code illustrates how to use this function.

### 7.14  Function spams.countConnexComponents

Count the number of connected components of a subgraph from a graph.

 # # The R function is not yet implemented. #

The following piece of code illustrates how to use this function.

### 7.15  Function spams.graphOfGroupStruct

 # # Name: spams.graphOfGroupStruct # # Usage: spams.graphOfGroupStruct(gstruct) # # Description: #     converts a group structure into the graph structure #    used by spams.proximalGraph, spams.fistaGraph or spams.structTrainDL # # Inputs: #       gstruct: the structure of groups as a list, one element per node #     an element is itself a 4 elements list: #       nodeid (>= 0), weight (double), array of vars associated to the node, #       array of children (nodeis's) # # Output: #       graph: struct (see documentation of spams.proximalGraph) # # Authors: # Jean-Paul CHIEZE, 2012 #

### 7.16  Function spams.groupStructOfString

 # # Name: spams.groupStructOfString # # Usage: spams.groupStructOfString(s) # # Description: #     decode a multi-line string describing "simply" the structure of groups #    of variables needed by spams.proximalGraph, spams.proximalTree, spams.fistaGraph, #    spams.fistaTree and spams.structTrainDL and builds the corresponding group structure. #    Each line describes a group of variables as a node of a tree. #    It has up to 4 fields separated by spaces: #        node-id node-weight [variables-list] -> children-list #    Let's define Ng = number of groups, and Nv = number of variables. #    node-id must be in the range (0 - Ng-1), and there must be Ng nodes #    weight is a float #    variables-list : a space separated list of integers, maybe empty, #         but '[' and '] must be present. Numbers in the range (0 - Nv-1) #    children-list : a space separated list of node-id's #        If the list is empty, '->' may be omitted. #    The data must obey some rules :  #        - A group contains the variables of the corresponding node and of the whole subtree. #        - Variables attached to a node are those that are not int the subtree. #        - If the data destination is a Graph, there may be several independant trees, #          and a varibale may appear in several trees. #    If the destination is a Tree, there must be only one tree, the root node #    must have id == 0 and each variable must appear only once. # # Inputs: #       s:  the multi-lines string # # Output: #       groups: list, one element for each node #                an element is itsel a 4 elements list: #           nodeid (int >= 0), weight (double), array of vars of the node, #                array of children (nodeid's) # # Authors: # Jean-Paul CHIEZE, 2012 #   