-
Notifications
You must be signed in to change notification settings - Fork 0
/
q4.py
144 lines (121 loc) · 4.33 KB
/
q4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
__author__ = 'julie'
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import time
def f_model_1(z_list,n,x):
functionsum=0
A, c=construct_A_and_C(n,x)
for i in range(len(z_list)): #length m
functionsum+=(compute_r_i_1(z_list[i],A,c))**2
return functionsum
def compute_r_i_1(z_list_i,A,c):
if z_list_i[0]>0:
return max([np.dot((z_list_i[1:] - c), (np.matmul(A, (z_list_i[1:] - c)))) - 1, 0])
else:
return max([1-np.dot((z_list_i[1:]-c),(np.matmul(A,(z_list_i[1:]-c)))),0])
def f_model_2(z_list,n,x):
A,b=construct_A_and_C(n,x) #endre på construct??
functionsum=0
for i in range(len(z_list)): #length m
functionsum+=compute_r_i_2(z_list[i],A,b)**2
return functionsum
def compute_r_i_2(z_list_i,A,b):
if z_list_i[0]>0:
return max([np.dot(z_list_i[1:],np.matmul(A,z_list_i[1:]))+np.dot(b,z_list_i[1:])-1,0])
else:
return max([1-np.dot(z_list_i[1:],np.matmul(A,z_list_i[1:]))-np.dot(b,z_list_i[1:]),0])
def construct_A_and_C(n,x): # Her har Even vært og endret ting 26.02
C=x[int(n*(n+1)/2):]
A=np.zeros((n,n))
index=0
for h in range(n):
for j in range(h,n):
A[h][j] = x[index]
A[j][h] = x[index]
index+=1
return A, C
def df_model_1(z_list,n,x):
A,c = construct_A_and_C(n,x)
dfx=np.zeros(int(n*(n+1)/2)+n)
for i in range(len(z_list)): #length m
index = 0
ri=compute_r_i_1(z_list[i], A, c)
if ri==0:
continue
else:
#find the first n*(n+1)/2 x-entries
for h in range(n): #length n
for j in range(h,n):
if h==j:
dfx[index] += z_list[i][0]*2*ri*(z_list[i][h + 1] - c[h]) ** 2
else:
dfx[index] += z_list[i][0]*4 * ri*(z_list[i][j + 1] - c[j]) * (z_list[i][h + 1] - c[h])
index+=1
#find the last n x-entries for or c's
for h in range(n):
for j in range(n):
dfx[int(n * (n + 1) / 2) + h] += -z_list[i][0] * 4 * ri * A[j][h] * (z_list[i][j + 1] - c[j])
return dfx
def df_model_2(z_list,n,x):
A,b = construct_A_and_C(n,x)
dfx=np.zeros(int(n*(n+1)/2)+n)
for i in range(len(z_list)): #length m
index = 0
ri=compute_r_i_2(z_list[i], A, b)
if ri==0:
continue
else:
#find the first n*(n+1)/2 x-entries
for h in range(n): #length n
for j in range(h,n):
if h==j:
dfx[index] += z_list[i][0]*2*ri*z_list[i][h + 1] ** 2
else:
dfx[index] += z_list[i][0]*4* ri*(z_list[i][j + 1]) * (z_list[i][h + 1])
index+=1
#find the last n x-entries
for h in range(n):
dfx[int(n * (n + 1) / 2) + h] += z_list[i][0]*2*ri*z_list[i][h+1]
return dfx
def test_derivatives(m,n,N,funcval,d_funcval): #Ferdig
# generate random point and direction
x = np.random.randn(N)
z = np.random.randn(m,n+1)
for i in range(m):
if i%2==0:
z[i][0]=1
else:
z[i][0]=-1
p = np.random.randn(N)
f0 = funcval(z, n, x)
g = d_funcval(z,n,x).dot(p)
#compare directional derivative with finite differences
for ep in 10.0 ** np.arange(-1, -13, -1):
g_app = (funcval(z,n,x + ep * p) - f0) / ep #z_list,A,c
error = abs(g_app - g) / abs(g)
print('ep = %e, error = %e' % (ep, error))
if __name__ == "__main__": # Her har Even vært og endret ting 26.02
n = 3
N = int(n*(n+1)/2+n)
m=10
test_derivatives(m,n,N,f_model_2,df_model_2)
#Noke du vil ha her Even?
# dimensions must be n = int(k*(k+1)/2) such that k is an integer
#n = 6
#dim = int(n*(n+1)/2) + n
#m = 3 # number of z points
#x = np.ones(dim)
#z_list = np.zeros((m, n + 1))
#for i in range(m):
# z_list[i] = np.ones(n + 1) * i
# if i < int(m / 2):
# z_list[i][0] = -1
# else:
# z_list[i][0] = 1
#print("n", n)
#print("z_list\n",z_list)
#print("dim of x", dim)
## m=3 gir to w = -1 og en w = 1
#A, c = construct_A_and_C(n, x)
#print("value of model 1:", f_model_1(z_list, A, c))