-
Notifications
You must be signed in to change notification settings - Fork 12
/
utility.py
executable file
·55 lines (46 loc) · 1.15 KB
/
utility.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
####################
# utility.py
# this file specifis some useful functions in computation
#
# Author: Jianyu Chen
# Copyright: 2016
####################
import math
import numpy as np
from cvxopt import matrix
# Vector to Angle
def vec2ang(vector):
x = vector[0]
y = vector[1]
return math.atan2(y,x)
# Angle to Vector
def ang2vec(theta):
return [math.cos(theta),math.sin(theta)]
def perpLength(v1,v2):
return np.cross(v2,v1)/np.linalg.norm(v2)
def normalize(v):
norm = np.linalg(np.array(v))
for i in range(len(v)):
v[i] = v[i]/norm
return v
# Minus of array
def substruct(v1,v2):
v = []
for i in range(len(v1)):
v.append(v1[i]-v2[i])
return v
# embedding a small matrix to a large matrix
def embed(Qaug, Q, ni, nj):
[li,lj] = Q.shape
for i in range(ni,ni+li):
for j in range(nj,nj+lj):
Qaug[i][j] = Q[i-ni][j-nj]
return Qaug
def matrixPower(A,n):
if n == 0:
return np.identity(round(math.sqrt(len(A))))
else:
newA = A
for i in range(n-1):
newA = newA*A
return newA