예제 #1
0
def sparse_rc_xam() :
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    #
    # create an empty sparsity pattern
    pattern = cppad_py.sparse_rc()
    ok      = ok and pattern.nr()  == 0
    ok      = ok and pattern.nc()  == 0
    ok      = ok and pattern.nnz() == 0
    #
    # resize
    nr = 6
    nc = 5
    nnz = 4
    pattern.resize(nr, nc, nnz)
    ok = ok and pattern.nr()  == nr
    ok = ok and pattern.nc()  == nc
    ok = ok and pattern.nnz() == nnz
    #
    # indices corresponding to upper-diagonal
    for k in range( nnz ) :
        pattern.put(k, k, k+1)
    #
    #
    # row and column indices
    row = pattern.row()
    col = pattern.col()
    #
    # check row and column indices
    for k in range( nnz ) :
        ok = ok and row[k] == k
        ok = ok and col[k] == k+1
    #
    #
    # For this case, row_major and col_major order are same as original order
    row_major = pattern.row_major()
    col_major = pattern.col_major()
    for k in range( nnz ) :
        ok = ok and row_major[k] == k
        ok = ok and col_major[k] == k
    #
    #
    return( ok )
예제 #2
0
def sparse_rcv_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # create an empty matrix
    matrix = cppad_py.sparse_rcv()
    ok = ok and matrix.nr() == 0
    ok = ok and matrix.nc() == 0
    ok = ok and matrix.nnz() == 0
    #
    # create sparsity pattern for n by n identity matrix
    pattern = cppad_py.sparse_rc()
    n = 5
    pattern.resize(n, n, n)
    for k in range(n):
        pattern.put(k, k, k)
    #
    # create n by n sparse representation of identity matrix
    matrix.pat(pattern)
    for k in range(n):
        matrix.put(k, 1.0)
    #
    # row, column indices
    row = matrix.row()
    col = matrix.col()
    val = matrix.val()
    #
    # check results
    for k in range(n):
        ok = ok and row[k] == k
        ok = ok and col[k] == k
        ok = ok and val[k] == 1.0
    #
    # For this case, row_major and col_major order are same as original order
    row_major = matrix.row_major()
    col_major = matrix.col_major()
    for k in range(n):
        ok = ok and row_major[k] == k
        ok = ok and col_major[k] == k
    #
    #
    return (ok)
예제 #3
0
def sparse_jac_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    n = 3
    # one
    aone = cppad_py.a_double(1.0)
    #
    # create the independent variables ax
    x = numpy.empty(n, dtype=float)
    for i in range(n):
        x[i] = i + 2.0
    #
    ax = cppad_py.independent(x)
    #
    # create dependent variables ay with ay[i] = (j+1) * ax[j]
    # where i = mod(j + 1, n)
    ay = numpy.empty(n, dtype=cppad_py.a_double)
    for j in range(n):
        i = j + 1
        if i >= n:
            i = i - n
        #
        aj = cppad_py.a_double(j)
        ay_i = (aj + aone) * ax[j]
        ay[i] = ay_i
    #
    #
    # define af corresponding to f(x)
    f = cppad_py.d_fun(ax, ay)
    #
    # sparsity pattern for identity matrix
    pat_eye = cppad_py.sparse_rc()
    pat_eye.resize(n, n, n)
    for k in range(n):
        pat_eye.put(k, k, k)
    #
    #
    # sparsity pattern for the Jacobian
    pat_jac = cppad_py.sparse_rc()
    f.for_jac_sparsity(pat_eye, pat_jac)
    #
    # loop over forward and reverse mode
    for mode in range(2):
        # compute all possibly non-zero entries in Jacobian
        subset = cppad_py.sparse_rcv(pat_jac)
        # work space used to save time for multiple calls
        work = cppad_py.sparse_jac_work()
        if mode == 0:
            f.sparse_jac_for(subset, x, pat_jac, work)
        #
        if mode == 1:
            f.sparse_jac_rev(subset, x, pat_jac, work)
        #
        #
        # check result
        ok = ok and n == subset.nnz()
        col_major = subset.col_major()
        row = subset.row()
        col = subset.col()
        val = subset.val()
        for k in range(n):
            ell = col_major[k]
            r = row[ell]
            c = col[ell]
            v = val[ell]
            i = c + 1
            if i >= n:
                i = i - n
            #
            ok = ok and c == k
            ok = ok and r == i
            ok = ok and v == c + 1.0
        #
    #
    #
    return (ok)
예제 #4
0
def sparse_hes_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    n = 3
    #
    # create the independent variables ax
    x = numpy.empty(n, dtype=float)
    for i in range(n):
        x[i] = i + 2.0
    #
    ax = cppad_py.independent(x)
    #
    # ay[i] = j * ax[j] * ax[i]
    # where i = mod(j + 1, n)
    ay = numpy.empty(n, dtype=cppad_py.a_double)
    for j in range(n):
        i = j + 1
        if i >= n:
            i = i - n
        #
        aj = cppad_py.a_double(j)
        ax_j = ax[j]
        ax_i = ax[i]
        ay[i] = aj * ax_j * ax_i
    #
    #
    # define af corresponding to f(x)
    f = cppad_py.d_fun(ax, ay)
    #
    # Set select_d (domain) to all true,
    # initial select_r (range) to all false
    # initialize r to all zeros
    select_d = numpy.empty(n, dtype=bool)
    select_r = numpy.empty(n, dtype=bool)
    r = numpy.empty(n, dtype=float)
    for i in range(n):
        select_d[i] = True
        select_r[i] = False
        r[i] = 0.0
    #
    #
    # only select component n-1 of the range function
    # f_0 (x) = (n-1) * x_{n-1} * x_0
    select_r[0] = True
    r[0] = 1.0
    #
    # sparisty pattern for Hessian
    pattern = cppad_py.sparse_rc()
    f.for_hes_sparsity(select_d, select_r, pattern)
    #
    # compute all possibly non-zero entries in Hessian
    # (should only compute lower triangle becuase matrix is symmetric)
    subset = cppad_py.sparse_rcv(pattern)
    #
    # work space used to save time for multiple calls
    work = cppad_py.sparse_hes_work()
    #
    f.sparse_hes(subset, x, r, pattern, work)
    #
    # check that result is sparsity pattern for Hessian of f_0 (x)
    ok = ok and subset.nnz() == 2
    row = subset.row()
    col = subset.col()
    val = subset.val()
    for k in range(2):
        i = row[k]
        j = col[k]
        v = val[k]
        if i <= j:
            ok = ok and i == 0
            ok = ok and j == n - 1
        #
        if i >= j:
            ok = ok and i == n - 1
            ok = ok and j == 0
        #
        ok = ok and v == n - 1
    #
    #
    return (ok)
예제 #5
0
def sparse_jac_pattern_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    n = 3
    #
    # create the independent variables ax
    x = numpy.empty(n, dtype=float)
    for i in range(n):
        x[i] = i + 2.0
    #
    ax = cppad_py.independent(x)
    #
    # create dependent variables ay with ay[i] = ax[j]
    # where i = mod(j + 1, n)
    ay = numpy.empty(n, dtype=cppad_py.a_double)
    for j in range(n):
        i = j + 1
        if i >= n:
            i = i - n
        #
        ay_i = ax[j]
        ay[i] = ay_i
    #
    #
    # define af corresponding to f(x)
    f = cppad_py.d_fun(ax, ay)
    #
    # sparsity pattern for identity matrix
    pat_in = cppad_py.sparse_rc()
    pat_in.resize(n, n, n)
    for k in range(n):
        pat_in.put(k, k, k)
    #
    #
    # loop over forward and reverse mode
    for mode in range(2):
        pat_out = cppad_py.sparse_rc()
        if mode == 0:
            f.for_jac_sparsity(pat_in, pat_out)
        #
        if mode == 1:
            f.rev_jac_sparsity(pat_in, pat_out)
        #
        #
        # check that result is sparsity pattern for Jacobian
        ok = ok and n == pat_out.nnz()
        col_major = pat_out.col_major()
        row = pat_out.row()
        col = pat_out.col()
        for k in range(n):
            ell = col_major[k]
            r = row[ell]
            c = col[ell]
            i = c + 1
            if i >= n:
                i = i - n
            #
            ok = ok and c == k
            ok = ok and r == i
        #
    #
    #
    return (ok)
예제 #6
0
def sparse_hes_pattern_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    n = 3
    #
    # create the independent variables ax
    x = numpy.empty(n, dtype=float)
    for i in range(n):
        x[i] = i + 2.0
    #
    ax = cppad_py.independent(x)
    #
    # create dependent variables ay with ay[i] = ax[j] * ax[i]
    # where i = mod(j + 1, n)
    ay = numpy.empty(n, dtype=cppad_py.a_double)
    for j in range(n):
        i = j + 1
        if i >= n:
            i = i - n
        #
        ay_i = ax[i] * ax[j]
        ay[i] = ay_i
    #
    #
    # define af corresponding to f(x)
    f = cppad_py.d_fun(ax, ay)
    #
    # Set select_d (domain) to all true, initial select_r (range) to all false
    select_d = numpy.empty(n, dtype=bool)
    select_r = numpy.empty(n, dtype=bool)
    for i in range(n):
        select_d[i] = True
        select_r[i] = False
    #
    #
    # only select component 0 of the range function
    # f_0 (x) = x_0 * x_{n-1}
    select_r[0] = True
    #
    # loop over forward and reverse mode
    for mode in range(2):
        pat_out = cppad_py.sparse_rc()
        if mode == 0:
            f.for_hes_sparsity(select_d, select_r, pat_out)
        #
        if mode == 1:
            f.rev_hes_sparsity(select_d, select_r, pat_out)
        #
        #
        # check that result is sparsity pattern for Hessian of f_0 (x)
        ok = ok and pat_out.nnz() == 2
        row = pat_out.row()
        col = pat_out.col()
        for k in range(2):
            r = row[k]
            c = col[k]
            if r <= c:
                ok = ok and r == 0
                ok = ok and c == n - 1
            #
            if r >= c:
                ok = ok and r == n - 1
                ok = ok and c == 0
            #
        #
    #
    #
    return (ok)