import numpy as np # This function implements S(x) as given in the exercise sheet # If matrix A or vector b are not specified through "args", the function assumes # that A and b are those given in Exercise 1 # Note that you CANNOT only specify A or b, both must be specified such that # args[0] = A and args[1] = b # Note also that the function is implemented with the factor 1/2 as in Exercise 2 # Hence the scaling of matrix A with 2 in order to reproduce the case in Exercise 1 def quadratic_function(x, *args): if len(args) == 0: A = np.array([[4.0, 0], [1, 3]]) * 2.0 b = np.array([0, 1]) else: A = args[0] b = args[1] # reshape into column vectors x.reshape(-1, 1) b.reshape(-1, 1) # return S(x) return 0.5*((x.transpose() @ A) @ x) - (x.transpose() @ b) # TESTING AND VALIDATING FUNCTION #print(quadratic_function(np.array([1,2]) )) #print(quadratic_function(np.array([1,2]), np.array([[4, 0], [1, 3]]), np.array([0, 1]) ))