print.sparseness: Show sparseness structure of matrix

Description Usage Arguments Value Author(s) See Also Examples

Description

This function shows the sparseness structure of a matrix in the format that is required by ipoptr.

Usage

1

Arguments

x

list of vectors with indices. Each element of the list corresponds to a row in the matrix. Each index corresponds to a non-zero element in the matrix.

indices

Logical. Should we show the order of the non-zero elements or just whether an element is non-zero?

data

vector with non-zero elements of the sparse matrix.

ncol

integer supplying the number of columns of the sparse matrix. If this is not supplied, we take the number of columns as the largest index in s.

...

further arguments passed to or from other methods.

Value

A matrix showing the sparseness structure is returned.

Author(s)

Jelmer Ypma

See Also

ipoptr plot.sparseness make.sparse

Examples

 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
library('ipoptr')

# print lower-diagonal 4x4 matrix
print.sparseness( list( c(1), c(1,2), c(1,2,3), c(1,2,3,4) ) )

# print diagonal 3x3 matrix without indices counts
print.sparseness( list( c(1), c(2), c(3) ), indices=FALSE )

# print a third sparse matrix
print.sparseness( list( c(1,3,6,8), c(2,5), c(3,7,9) ) )

# and a fourth one, where the elements are in a different order
print.sparseness( list( c(3,1,6,8), c(2,5), c(3,9,7) ) )

# print lower-diagonal 5x5 matrix generated with make.sparse
A_lower <- make.sparse( lower.tri( matrix(1, nrow=5, ncol=5), diag=TRUE ) )
print.sparseness( A_lower )

# print a diagonal 5x5 matrix without indices counts
A_diag  <- make.sparse( diag(5) > 0 )
print.sparseness( A_diag )

# example from tests/lasso.R
n <- 100    # number of observations
m <- 5      # number of variables

# define hessian function
hessian <- function( A ) {
    H <- t(A) 
    H <- unlist( lapply( 1:m, function(i) { H[i,1:i] } ) )
    
    return( H )
}

# define the structure
hessian_structure <- c( lapply( 1:m, function(x) { return( c(1:x) ) } ),
                        lapply( 1:m, function(x) { return( c() ) } ) )

# generate data
set.seed( 3141 )
A <- hessian( matrix( rnorm( n*m ), nrow=n, ncol=m ) )                        
print.sparseness( x       = hessian_structure,
                  indices = TRUE,
                  data    = format( A, digits=2, nsmall=2, justify='right'),
                  ncol    = 2*m )

jyypma/ipoptr documentation built on May 20, 2019, 6:28 a.m.