예제 #1
0
def gradients_univariate():
    
    # Parameters!
    pt = 8
    x1 = Parameter(param_type="Uniform", lower=-1.0, upper=1.0, points=pt, derivative_flag=1)
    x2 = Parameter(param_type="Uniform", lower=-1.0, upper=1.0, points=pt, derivative_flag=1)
    parameters = [x1, x2]
    dims = len(parameters)

    # Basis selection!
    hyperbolic_cross = IndexSet("Total order", orders=[pt-1,pt-1])
    esq = EffectiveSubsampling(parameters, hyperbolic_cross)
    A , p, w = esq.getAmatrix()
    C = esq.getCmatrix()

    # Matrix sizes
    m, n  = A.shape
    print m, n
    print '*****************'
    m, n = C.shape
    print m, n
    
    # Now perform least squares!
    W = np.mat(np.diag(np.sqrt(w)))
    b = W.T * evalfunction(p, fun)
    d = evalgradients(p, fungrad, 'vector')
    x = qr.solveLSQ(np.vstack([A, C]), np.vstack([b, d])  )
    print x
예제 #2
0
def nogradients_univariate():
    
    # Parameters!
    pt = 6
    x1 = Parameter(param_type="Uniform", lower=-1.0, upper=1.0, points=pt)
    parameters = [x1, x1]

    # Effective subsampling object!
    esq = EffectiveSubsampling(parameters)
    
    # Solve the least squares problem
    A, p, w = esq.getAmatrix() # Is this always square??
    W = np.mat(np.diag(np.sqrt(w) ) )
    b = W.T  * evalfunction(p, fun)
    x = qr.solveLSQ(A,b)
    print x
예제 #3
0
def gradients_multivariate_subsampled():
    
    # Parameters!
    pt = 3
    x1 = Parameter(param_type="Uniform", lower=-1.0, upper=1.0, points=pt, derivative_flag=1)
    x2 = Parameter(param_type="Uniform", lower=-1.0, upper=1.0, points=pt, derivative_flag=1)
    parameters = [x1, x2]
    dims = len(parameters)

    # Basis selection!
    basis = IndexSet("Total order", orders=[pt-1,pt-1])
    esq = EffectiveSubsampling(parameters, basis)
    A , p, w = esq.getAmatrix()
    C = esq.getCmatrix()

    # QR column pivotings
    P = qr.mgs_pivoting(A.T)
    
    # Now perform least squares!
    basis_terms_required = basis.getCardinality() 
    minimum_points = np.int( (basis_terms_required + dims)/(dims + 1.) )  + 5 
    nodes = P[0:minimum_points]
    A = getRows(A, nodes)
    C = getRowsC(C, nodes, dims)

    m, n = A.shape
    #print m , n
    m, n = C.shape
    #print m, n

    w = w[nodes]
    p = p[nodes,:]
    #print p, w
    W = np.mat(np.diag(np.sqrt(w)))
    b = W.T * evalfunction(p, fun)
    d = evalgradients(p, fungrad, 'vector')
    R = np.vstack([A, C])
    print np.linalg.cond(R)
    print R
    print np.vstack([b, d])
    x = qr.solveLSQ(np.vstack([A, C]), np.vstack([b, d])  )
    print '\n'
    print x

    """
예제 #4
0
 def test_least_squares(self):
     A = [[0.129783563321146, 0.693035803160460, 0.542987352374312],
          [0.971602452337381, 0.702475484644310, 0.540162073918995],
          [0.940153258292462, 0.371312184808707, 0.786284036629558],
          [0.933119647195370, 0.064105941014906, 0.601409398329654],
          [0.983758270635912, 0.443451755787831, 0.947013554266040]]
     b = [
         0.631218455441575, 0.878837482885143, 0.122686301726737,
         0.813548224731922, 0.977604352516636
     ]
     b = np.mat(b)
     b = b.T
     x = qr.solveLSQ(A, b)
     x_MATLAB = [0.356346872539370, 0.515012358852366, 0.221551548589301]
     x_MATLAB = np.mat(x_MATLAB)
     x_MATLAB = x_MATLAB.T
     residual = x - x_MATLAB
     if np.linalg.norm(residual, 2) < 4e-15:
         print 'Success!'
     else:
         raise (RuntimeError, 'QR testing failed')