### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

file 249 lines (176 sloc) 8.353 kb
 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 `import _sparsefrom _sparse import *import numpydef jac_pat(tape_tag, x, options):    """ Computes the sparsity pattern of a Jacobian J of a function F: R^N -> R^M at the base point x, i.e. J = dF(x)/dx pat = jac_pat(tape_tag, x, options) pat is a nested list in compresses row format. E.g. the pattern of the function: F(x,y,z) = [x*y, y*z] is pat [[0,1],[1,2]] because the Jacobian has in the first row the 0'th and 1'st element nonzero and in the second row the 1'st and the 2'nd options[0] : way of sparsity pattern computation 0 - propagation of index domains (default) 1 - propagation of bit pattern options[1] : test the computational graph control flow 0 - safe mode (default) 1 - tight mode options[2] : way of bit pattern propagation 0 - automatic detection (default) 1 - forward mode 2 - reverse mode """        assert type(tape_tag) == int    options = numpy.asarray(options,dtype=numpy.int32)    assert numpy.ndim(options) == 1    assert numpy.size(options) == 4    assert numpy.ndim(x) == 1    x = numpy.asarray(x, dtype=float)        return _sparse.jac_pat(tape_tag, x, options)# def sparse_jac_no_repeat(tape_tag, x, options):# """# computes sparse Jacobian for a function F:R^N -> R^M without any prior information,# i.e. this function internally finds the sparsity pattern# of the Jacobian J, then uses Graph Coloring to find the smallest number P or Q of necessary directions# (P in the forward mode, Q in the reverse mode)# and computes dot(J,S) with S (N,P) array in forward mode# or dot(S^T,J) with S(N,Q) in the reverse mode    # [nnz, rind, cind, values] =sparse_jac_no_repeat(tape_tag, x, options)# INPUT:# The base point x at which the Jacobian should be computed, i.e. J = dF(x)/dx# options is a list or array of length 4# options[0] : way of sparsity pattern computation# 0 - propagation of index domains (default)# 1 - propagation of bit pattern# options[1] : test the computational graph control flow# 0 - safe mode (default)# 1 - tight mode# options[2] : way of bit pattern propagation# 0 - automatic detection (default)# 1 - forward mode# 2 - reverse mode# options[3] : way of compression# 0 - column compression (default)# 1 - row compression# OUTPUT:# nnz is the guessed number of nonzeros in the Jacobian. This can be larger than the true number of nonzeros.# sparse matrix representation in standard format:# rind is an nnz-array of row indices# cind is an nnz-array of column indices# values are the corresponding Jacobian entries# """# assert type(tape_tag) == int    # if options == None:# options = numpy.array([1,1,0,0], dtype=numpy.int32)# options = numpy.asarray(options,dtype=numpy.int32)# assert numpy.ndim(options) == 1# assert numpy.size(options) == 4# assert numpy.ndim(x) == 1# x = numpy.asarray(x, dtype=float)# return _colpack.sparse_jac_no_repeat(tape_tag, x, options)# def sparse_jac_repeat(tape_tag, x, nnz, rind, cind, values):# """# computes sparse Jacobian J for a function F:R^N -> R^M with# the sparsity pattern that has been computed previously (e.g. by calling sparse_jac_no_repeat)# I guess it also reuses the options that have been set previously. So it would be not necessary to set the options again.    # [nnz, rind, cind, values] = sparse_jac_repeat(tape_tag, x, rind, cind, values)# INPUT:# The base point x at which the Jacobian should be computed, i.e. J = dF(x)/dx    # OUTPUT:# nnz is the guessed number of nonzeros in the Jacobian. This can be larger than the true number of nonzeros.# sparse matrix representation in standard format:# rind is an nnz-array of row indices# cind is an nnz-array of column indices# values are the corresponding Jacobian entries# """# assert type(tape_tag) == int# assert type(nnz) == int# assert numpy.ndim(x) == 1# assert numpy.ndim(rind) == 1# assert numpy.ndim(cind) == 1# assert numpy.ndim(values) == 1# x = numpy.asarray(x, dtype=float)# rind= numpy.asarray(rind, dtype=numpy.uint32)# cind= numpy.asarray(cind, dtype=numpy.uint32)# values = numpy.asarray(values, dtype=float)# return _colpack.sparse_jac_repeat(tape_tag, x, nnz, rind, cind, values)def hess_pat(tape_tag, x, option = 0):    """ Computes the sparsity pattern of a Jacobian J of a function F: R^N -> R^M at the base point x, i.e. J = dF(x)/dx pat = hess_pat(tape_tag, x, options) pat is a nested list in compresses row format. option: option = 0 normal mode (default) option = 1 tight mode """        assert type(tape_tag) == int    assert type(option) == int    assert numpy.ndim(x) == 1    x = numpy.asarray(x, dtype=float)        return _sparse.hess_pat(tape_tag, x, option)# def sparse_hess_no_repeat(tape_tag, x, options = None):# """# computes sparse Hessian for a function F:R^N -> R without any prior information,# [nnz, rind, cind, values] =sparse_hess_no_repeat(tape_tag, x, options)# INPUT:# The base point x at which the Jacobian should be computed, i.e. J = dF(x)/dx# options is a list or array of length 2# options[0] :test the computational graph control flow# 0 - safe mode (default)# 1 - tight mode# options[1] : way of recovery# 0 - indirect recovery# 1 - direct recovery# OUTPUT:# nnz is the guessed number of nonzeros in the Hessian. This can be larger than the true number of nonzeros.# sparse matrix representation in standard format:# rind is an nnz-array of row indices# cind is an nnz-array of column indices# values are the corresponding Jacobian entries# """# assert type(tape_tag) == int    # if options == None:# options = numpy.array([0,0], dtype=numpy.int32)# options = numpy.asarray(options,dtype=numpy.int32)# assert numpy.ndim(options) == 1# assert numpy.size(options) == 2# assert numpy.ndim(x) == 1# x = numpy.asarray(x, dtype=float)# return _colpack.sparse_hess_no_repeat(tape_tag, x, options)# def sparse_hess_repeat(tape_tag, x, rind, cind, values):# """# computes sparse Hessian for a function F:R^N -> R without any prior information,# [nnz, rind, cind, values] =sparse_hess_no_repeat(tape_tag, x, options)# INPUT:# The base point x at which the Jacobian should be computed, i.e. J = dF(x)/dx# options is a list or array of length 2# options[0] :test the computational graph control flow# 0 - safe mode (default)# 1 - tight mode# options[1] : way of recovery# 0 - indirect recovery# 1 - direct recovery# OUTPUT:# nnz is the guessed number of nonzeros in the Hessian. This can be larger than the true number of nonzeros.# sparse matrix representation in standard format:# rind is an nnz-array of row indices# cind is an nnz-array of column indices# values are the corresponding Jacobian entries# """# assert type(tape_tag) == int# assert numpy.ndim(x) == 1# assert numpy.ndim(rind) == 1# assert numpy.ndim(cind) == 1# assert numpy.ndim(values) == 1# nnz = int(numpy.size(rind))# assert nnz == numpy.size(cind)# assert nnz == numpy.size(values)# x = numpy.asarray(x, dtype=float)# rind = numpy.asarray(rind, dtype=numpy.uint32)# cind = numpy.asarray(cind, dtype=numpy.uint32)# values = numpy.asarray(values, dtype=float)# return _colpack.sparse_hess_repeat(tape_tag, x, nnz, rind, cind, values)`
Something went wrong with that request. Please try again.