-
Notifications
You must be signed in to change notification settings - Fork 1
/
opt_utils.py
48 lines (46 loc) · 1.71 KB
/
opt_utils.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
from __future__ import division, print_function
import cvxpy as cvx
import random
import numpy
def rand_matrix(m, n, sigma=None, positive=False, symmetric=False, integer=False):
"""Generate a matrix of random values.
sigma: standard deviation. Defaults to 1 for floats or 100 for ints.
positive: if true, use only non-negative entries.
symmetrix: if true, return a symmetric matrix.
integer: if true, return a matrix of integers.
"""
if integer:
if sigma is None:
sigma = 1000
ret = numpy.empty((m,n), dtype=int)
for i in range(m):
if(symmetric):
for j in range(i+1):
if positive:
ret[i,j] = ret[j,i] = round(abs(random.gauss(0,sigma)))
else:
ret[i,j] = ret[j,i] = round(random.gauss(0,sigma))
else:
for j in range(n):
if positive:
ret[i,j] = round(abs(random.gauss(0,sigma)))
else:
ret[i,j] = round(random.gauss(0,sigma))
else:
if sigma is None:
sigma = 1
ret = numpy.empty((m,n))
for i in range(m):
if(symmetric):
for j in range(i+1):
if positive:
ret[i,j] = ret[j,i] = abs(random.gauss(0,sigma))
else:
ret[i,j] = ret[j,i] = random.gauss(0,sigma)
else:
for j in range(n):
if positive:
ret[i,j] = abs(random.gauss(0,sigma))
else:
ret[i,j] = random.gauss(0,sigma)
return ret