예제 #1
0
from demos.setup import np, tic, toc
from numpy.linalg import solve

""" Solving linear equations by different methods """

# Print table header
print('Hundreds of seconds required to solve n by n linear equation Ax=b')
print('m times using solve(A, b) and dot(inv(A), b), computing inverse only once.\n')
print('{:>5s} {:>5s} {:>12s} {:>12s}'.format('m', 'n', 'solve(A,b)', 'dot(inv(A), b)'))
print('-' * 40)

for m in [1, 100]:
    for n in [50, 500]:
        A = np.random.rand(n, n)
        b = np.random.rand(n, 1)

        tt = tic()
        for j in range(m):
            x = solve(A, b)

        f1 = 100 * toc(tt)

        tt = tic()
        Ainv = np.linalg.inv(A)
        for j in range(m):
            x = np.dot(Ainv, b)

        f2 = 100 * toc(tt)
        print('{:5d} {:5d} {:12.2f} {:12.2f}'.format(m, n, f1, f2))
예제 #2
0
b = np.inf

Billups = MCP(billups, a, b)


# ### Solve by applying Newton method
# * Using minmax formulation
# Initial guess is $x=0$
Billups.x0 = 0.0


# In[4]:

t1 = tic()
x1 =  Billups.zero(transform='minmax')
t1 = 100*toc(t1)
n1 = Billups.fnorm

# * Using semismooth formulation

# In[5]:

t2 = tic()
x2 = Billups.zero(transform='ssmooth')
t2 = 100*toc(t2)
n2 = Billups.fnorm

# ### Print results
# Hundreds of seconds required to solve hard nonlinear complementarity problem using Newton minmax and semismooth formulations

# In[6]:
예제 #3
0
from numpy.linalg import solve
""" Solving linear equations by different methods """

# Print table header
print('Hundreds of seconds required to solve n by n linear equation Ax=b')
print(
    'm times using solve(A, b) and dot(inv(A), b), computing inverse only once.\n'
)
print('{:>5s} {:>5s} {:>12s} {:>12s}'.format('m', 'n', 'solve(A,b)',
                                             'dot(inv(A), b)'))
print('-' * 40)

for m in [1, 100]:
    for n in [50, 500]:
        A = np.random.rand(n, n)
        b = np.random.rand(n, 1)

        tt = tic()
        for j in range(m):
            x = solve(A, b)

        f1 = 100 * toc(tt)

        tt = tic()
        Ainv = np.linalg.inv(A)
        for j in range(m):
            x = np.dot(Ainv, b)

        f2 = 100 * toc(tt)
        print('{:5d} {:5d} {:12.2f} {:12.2f}'.format(m, n, f1, f2))
예제 #4
0
    return np.sqrt(x)

problem_as_fixpoint = NLP(g, xinit)

''' Equivalent Rootfinding Formulation '''
def f(x):
    fval = x - np.sqrt(x)
    fjac = 1-0.5 / np.sqrt(x)
    return fval, fjac

problem_as_zero = NLP(f, xinit)

''' Compute fixed-point using Newton method '''
t0 = tic()
x1 = problem_as_zero.newton()
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm

''' Compute fixed-point using Broyden method '''
t0 = tic()
x2 = problem_as_zero.broyden()
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm

''' Compute fixed-point using function iteration '''
t0 = tic()
x3 = problem_as_fixpoint.fixpoint()
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - x3)

예제 #5
0
problem_as_fixpoint = NLP(g, xinit)
''' Equivalent Rootfinding Formulation '''


def f(x):
    fval = x - np.sqrt(x)
    fjac = 1 - 0.5 / np.sqrt(x)
    return fval, fjac


problem_as_zero = NLP(f, xinit)
''' Compute fixed-point using Newton method '''
t0 = tic()
x1 = problem_as_zero.newton()
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm
''' Compute fixed-point using Broyden method '''
t0 = tic()
x2 = problem_as_zero.broyden()
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm
''' Compute fixed-point using function iteration '''
t0 = tic()
x3 = problem_as_fixpoint.fixpoint()
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - x3)

print('Hundredths of seconds required to compute fixed-point of g(x)=sqrt(x)')
print('using Newton, Broyden, and function iteration methods, starting at')
print('x = %4.2f\n' % xinit)
예제 #6
0
a = 0
b = np.inf

Billups = MCP(billups, a, b)

# ### Solve by applying Newton method
# * Using minmax formulation
# Initial guess is $x=0$
Billups.x0 = 0.0

# In[4]:

t1 = tic()
x1 = Billups.zero(transform='minmax')
t1 = 100 * toc(t1)
n1 = Billups.fnorm

# * Using semismooth formulation

# In[5]:

t2 = tic()
x2 = Billups.zero(transform='ssmooth')
t2 = 100 * toc(t2)
n2 = Billups.fnorm

# ### Print results
# Hundreds of seconds required to solve hard nonlinear complementarity problem using Newton minmax and semismooth formulations

# In[6]:
예제 #7
0
# Define the problem by creating an LCP instance
L = LCP(M, q, a, b)

# Set 100 random initial points
nrep = 100
x0 = np.random.randn(nrep, n)

# Solve by applying Newton method to semi-smooth formulation
t0 = tic()
it1 = 0
L.opts.transform = 'ssmooth'
for k in range(nrep):
    L.newton(x0[k])
    it1 += L.it
t1 = toc(t0)
n1 = L.fnorm

# Solve by applying Newton method to minmax formulation
t0 = tic()
it2 = 0
L.opts.transform = 'minmax'
for k in range(nrep):
    L.newton(x0[k])
    it2 += L.it
t2 = toc(t0)
n2 = L.fnorm


print('Hundredths of seconds required to solve randomly generated linear \n',
      'complementarity problem on R^8 using Newton and Lemke methods')
예제 #8
0
# Define the problem by creating an LCP instance
L = LCP(M, q, a, b)

# Set 100 random initial points
nrep = 100
x0 = np.random.randn(nrep, n)

# Solve by applying Newton method to semi-smooth formulation
t0 = tic()
it1 = 0
L.opts.transform = 'ssmooth'
for k in range(nrep):
    L.newton(x0[k])
    it1 += L.it
t1 = toc(t0)
n1 = L.fnorm

# Solve by applying Newton method to minmax formulation
t0 = tic()
it2 = 0
L.opts.transform = 'minmax'
for k in range(nrep):
    L.newton(x0[k])
    it2 += L.it
t2 = toc(t0)
n2 = L.fnorm

print('Hundredths of seconds required to solve randomly generated linear \n',
      'complementarity problem on R^8 using Newton and Lemke methods')
print('\nAlgorithm           Time      Norm   Iterations  Iters/second\n' +
예제 #9
0
bb = np.random.rand(1000, 1)
for i in range(1000):
    for j in range(1000):
        if abs(i - j) > 1:
            AA[i,j] = 0

n = np.hstack((np.arange(50, 250, 50), np.arange(300, 1100, 100)))
ratio = np.empty(n.size)
for k in range(n.size):
    A = AA[:n[k], :n[k]]
    b = bb[:n[k]]
    tt = tic()
    for i in range(100):
        x = solve(A, b)

    toc1 = toc(tt)

    S = csc_matrix(A)
    tt = tic()
    for i in range(100):
        x = spsolve(S, b)

    toc2 = toc(tt)
    ratio[k] = toc2 / toc1

# Plot effort ratio
plt.figure(figsize=[6, 6])
plt.plot(n, ratio)
plt.xlabel('n')
plt.ylabel('Ratio')
plt.title('Ratio of Sparse Solve Time to Full Solve Time')