Skip to content

Instantly share code, notes, and snippets.

@gravicle
Created August 22, 2014 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravicle/a9a6452f993b978c125b to your computer and use it in GitHub Desktop.
Save gravicle/a9a6452f993b978c125b to your computer and use it in GitHub Desktop.
Extremum of a homogeneous quadratic function
# [9/20/2012] Challenge #100 [difficult] (Min-Max of N-Dimensional Quadratic) : dailyprogrammer http://www.reddit.com/r/dailyprogrammer/comments/106hps/9202012_challenge_100_difficult_minmax_of/
import numpy as np
import copy as copy
# quadratic form input
original = [[2, -2, -1], [-2, 1, 0], [-1, 0, 3]]
q = copy.deepcopy(original)
d = len(q) - 1
# take partial derivatives. Off-diag elements do not change.
for i in range(0, d):
q[i][i] = 2 * q[i][i] # multiply diagonal elements by 2
# form the coefficient and auxillary matrices
C = np.delete(q, d, 0)
C = np.delete(C, d, 1)
A = q[d]
A.remove(A[d])
for i in range(0, len(A) - 1):
A[i] = -A[i]
# solve the system
zero = np.zeros(d)
s = np.linalg.solve(C, A)
s
if np.array_equal(s, zero):
print("No extremum exists.")
else:
print("An extremum occurs at " + str(s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment