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))) |
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))) |
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) |
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) |
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) |
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) |
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) |
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) |
# # 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) |
# # 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) |
Print to the screen a matrix containing as columns image patches.
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.
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.
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.
# # 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 # |
# # 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 # |
# # Name: spams.readGroupStruct # # Usage: spams.readGroupStruct(file) # # Description: # reads a text file 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. # 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: # file: the file name # # 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 # |
# # Name: spams.treeOfGroupStruct # # Usage: spams.treeOfGroupStruct(gstruct) # # Description: # converts a group structure into the tree structure # used by spams.proximalTree, spams.fistaTree or spams.structTrainDL # # Inputs: # gstruct: the structure of groups as a list, one element per node # an element is itself a 4 lements list: # nodeid (>= 0), weight (double), array of vars associated to the node, # array of children (nodeis's) # # Output: # permutations: permutation vector that must be applied to the result of the # programm using the tree. Empty if no permutation is needed. # tree: named list (see documentation of spams.proximalTree) # nbvars : number of variables in the tree # res <- spams.treeOfGroupStruct(gstruct) # permutations = res[[1]] # tree = res[[2]] # nbvars = res[[3]] # # Authors: # Jean-Paul CHIEZE, 2012 # |
# # Name: spams.simpleGroupTree # # Usage: spams.simpleGroupTree(degrees) # # Description: # makes a structure representing a tree given the # degree of each level. # # Inputs: # degrees: int vector; degrees(i) is the number of children of each node at level i # # Output: # group_struct: list, one element for each node # an element is itsel a 4 elements list : # nodeid (int >= 0), weight (double), array of vars attached to the node # (here equal to [nodeid]), array of children (nodeid's) # # Authors: # Jean-Paul CHIEZE, 2012 # |