-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_functions.py
91 lines (71 loc) · 2.31 KB
/
my_functions.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
import numpy as np
np.random.seed(0)
import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
from matplotlib.image import imread
def main_func(A,b, U, lam, fun_num=1):
if fun_num==1:
temp_sum = 0
count = 0
for item in A:
temp_ent = 0.25*(U.T.dot(item.dot(U)) - b[count])**2
count +=1
temp_sum = temp_sum + temp_ent
temp_sum = temp_sum + lam*(np.sum(np.abs(U)))
return temp_sum
if fun_num==2:
temp_sum = 0
count = 0
for item in A:
temp_ent = 0.25*(U.T.dot(item.dot(U)) - b[count])**2
count +=1
temp_sum = temp_sum + temp_ent
temp_sum = temp_sum + lam*(U.T.dot(U))
return temp_sum
def grad(A,b, U, lam, fun_num=1):
if fun_num==1:
temp_grad = 0
count = 0
for item in A:
temp_grad = temp_grad + (U.T.dot(item.dot(U)) - b[count])*(item.dot(U))
count +=1
return temp_grad
if fun_num == 2:
temp_grad = 0
count = 0
for item in A:
temp_grad = temp_grad + (U.T.dot(item.dot(U)) - b[count])*(item.dot(U))
count +=1
return temp_grad
def abs_func(A,b, U, U1, lam, abs_fun_num=1, fun_num=1):
if abs_fun_num == 1:
G = grad(A,b, U1, lam, fun_num=fun_num)
return main_func(A,b, U1, lam, fun_num=fun_num)\
+ np.sum(np.multiply(U-U1,G)) \
-lam*(np.sum(np.abs(U1))) + lam*(np.sum(np.abs(U)))
if abs_fun_num == 2:
G = grad(A,b, U1, lam, fun_num=fun_num)
return main_func(A,b, U1, lam, fun_num=fun_num) \
+ np.sum(np.multiply(U-U1,G))-lam*(U1.T.dot(U1))+lam*(U.T.dot(U))
if abs_fun_num == 3:
G = grad(A, b, U1, lam, fun_num=fun_num)
return np.abs(main_func(A, b, U1, lam, fun_num=fun_num)\
+ np.sum(np.multiply(U-U1, G)) \
- lam*(np.sum(np.abs(U1)))) + lam*(np.sum(np.abs(U)))
if abs_fun_num == 4:
G = grad(A, b, U1, lam, fun_num=fun_num)
return np.abs(main_func(A, b, U1, lam, fun_num=fun_num) \
+ np.sum(np.multiply(U-U1, G)) \
- lam*(U1.T.dot(U1))) + lam*(U.T.dot(U))
def breg( U, U1, breg_num=1, A=1,b=1, lam=1):
if breg_num==1:
grad_U1 = (np.sum(np.multiply(U1,U1)))*U1 + U1
temp = 0.25*(np.linalg.norm(U)**4) + 0.5*(np.linalg.norm(U)**2) \
- 0.25*(np.linalg.norm(U1)**4) - 0.5*(np.linalg.norm(U1)**2)\
- np.sum(np.multiply(U-U1,grad_U1))
if temp >=1e-15:
return temp
else:
return 0