def containsPoint(orderedCartesianVertices, point): if (len(orderedCartesianVertices) == 1): if (M.norm(M.subVek(point, orderedCartesianVertices[1])) <= test_eps): return True else: return False if (len(orderedCartesianVertices) == 2): d = M.subVek(orderedCartesianVertices[0], orderedCartesianVertices[1]) normal = [d[1], -d[0]] if (math.fabs( M.scal(point, normal) - M.scal(orderedCartesianVertices[1], normal)) <= containsPoint_eps): return True else: return False i = 0 n = len(orderedCartesianVertices) for v in orderedCartesianVertices: w = orderedCartesianVertices[(i + 1) % n] d = M.subVek(w, v) normal = [d[1], -d[0]] if (M.scal(normal, point) - containsPoint_eps > M.scal(normal, v)): return False i = i + 1 return True
def getBFGS(cD, params_new, params_k, A_k): s_vector = M.subVek(params_new, params_k) y_vector = M.subVek(cV.gradPhi(params_new, cD), cV.gradPhi(params_k, cD)) # s_Matrix and y_Matix is a column, not a row, therefor copyTrans s_Matrix = M.Matrix([s_vector]).copyTrans() y_Matrix = M.Matrix([y_vector]).copyTrans() dividend = A_k.mult(s_Matrix) dividend = dividend.mult(dividend.copyTrans()) divisor = M.scal(s_vector, A_k.image(s_vector)) quotient = dividend quotient.scale(1.0 / divisor) rankOneMod = M.subMatrix(A_k, quotient) dividend2 = y_Matrix.mult(y_Matrix.copyTrans()) # here could be a division through zero. If this occurence, then I should just translate params_new a liiiitle bit... divisor2 = M.scal(y_vector, s_vector) quotient = dividend2 #print( 'vectors are') #print(y_vector) #print( s_vector) #print( cV.gradPhi(params_new , cD)) quotient.scale(1.0 / divisor2) rankTwoMod = M.addMatrix(rankOneMod, quotient) return rankTwoMod
def isConvexRun(orderedCartesianVertices): n = len(orderedCartesianVertices) for i in range(n): v = orderedCartesianVertices[i % n] w = orderedCartesianVertices[(i + 1) % n] x = orderedCartesianVertices[(i + 2) % n] d = M.subVek(w, v) e = M.subVek(x, w) if (isNotStrictlyLeft(d, e)): return False return True
def diffConeVolIteratorApprox(params, cD, h): e_1 = [1, 0] e_2 = [0, 1] point_diff1 = M.addVek(params, M.scaleVek(h, e_1)) quotient_diff1 = M.subVek(coneVolIterator(cD, point_diff1), coneVolIterator(cD, params)) quotient_diff1 = M.scaleVek(1.0 / h, quotient_diff1) point_diff2 = M.addVek(params, M.scaleVek(h, e_2)) quotient_diff2 = M.subVek(coneVolIterator(cD, point_diff2), coneVolIterator(cD, params)) quotient_diff2 = M.scaleVek(1.0 / h, quotient_diff2) return M.Matrix([quotient_diff1, quotient_diff2]).copyTrans()
def hMethodConeVolIteratorCheck(cD, point, eps, h): while (True): try: vektor = cV.coneVolIterator(cD, point) break except ZeroDivisionError: return True break diff = cV.diffConeVolumeIterator(cD, point) e_1 = [1, 0] e_2 = [0, 1] diff_1 = diff.image(e_1) diff_2 = diff.image(e_2) point_diff11 = M.addVek(point, M.scaleVek(h, e_1)) point_diff12 = M.subVek(point, M.scaleVek(h, e_1)) quotient_diff1 = M.subVek( M.scaleVek(1 / (2 * h), cV.coneVolIterator(cD, point_diff11)), M.scaleVek(1 / (2 * h), cV.coneVolIterator(cD, point_diff12))) point_diff21 = M.addVek(point, M.scaleVek(h, e_2)) point_diff22 = M.subVek(point, M.scaleVek(h, e_2)) quotient_diff2 = M.subVek( M.scaleVek(1 / (2 * h), cV.coneVolIterator(cD, point_diff21)), M.scaleVek(1 / (2 * h), cV.coneVolIterator(cD, point_diff22))) difference_1 = M.dist(quotient_diff1, diff_1) difference_2 = M.dist(quotient_diff2, diff_2) print(' the differences equals') print(diff_1) print(quotient_diff1) print(diff_2) print(quotient_diff2) if difference_1 >= eps or difference_2 >= eps: print(' the differences equals') print(difference_1) print(difference_2) return False return True
def getMin(v, w, angle): u_1 = polar(angle, 1) u_2 = M.subVek(v, w) U = M.Matrix([u_1, u_2]).copyTrans() result = U.lgsSolve(v) if (len(result) == 0): return 0 if (result[0] <= 0): return 0 return result[0]
def gradPolyFunctional(coneData, point): B = M.idMatrix(2) A = gradConeVolumeIterator(coneData, point) A.scale(-1) B.add(A) b = M.subVek(point, getConeVolIterator(coneData, point)) b = M.scaleVek(2, b) result = B.image(b) return result
def getMinForSupport2(v, w, u): u_1 = u u_2 = M.subVek(v, w) #print(u_1) U = M.Matrix([u_1, u_2]).copyTrans() #U.list() result = U.lgsSolve2x2(v) # my lgsSolver returns a 'solution' even if v is not in im(M) . Therefore the if-check needs to get adjusted if (M.dist(U.image(result), v) > 0.0001): return -math.inf return result[0]
def getMax(v, w, angle): u_1 = polar(angle, 1) u_2 = M.subVek(v, w) U = M.Matrix([u_1, u_2]).copyTrans() result = U.lgsSolve(v) # my lgsSolver returns a 'solution' even if v is not in im(M) . Therefore the if-check needs to get adjusted if (len(result) == 0): return math.inf if (result[0] <= 0): return math.inf return result[0]
def scalarDifferenceGradient( point , cD): v = cV.getConeVolIterator( cD , point ) d = M.subVek( v , point ) diff = cV.diffConeVolumeIterator( cD , point ) diff.scale(-1) diff.add(M.idMatrix(2)) u = M.Matrix([d]).mult(diff) # equal to scalar product of u and d : return u.image(d)[0]
def hMethodNextVertexCheck(u, V, point, eps, h): nextVektor = cV.getNextVertex(u, V, point) while (True): try: nextVektor = cV.getNextVertex(u, V, point) break except ZeroDivisionError: return True break diff = cV.diffGetNextVertex(u, V, point) e_1 = [1, 0] e_2 = [0, 1] diff_1 = diff.image(e_1) diff_2 = diff.image(e_2) point_diff1 = M.addVek(point, M.scaleVek(h, e_1)) quotient_diff1 = M.subVek( M.scaleVek(1 / h, cV.getNextVertex(u, V, point_diff1)), M.scaleVek(1 / h, cV.getNextVertex(u, V, point))) point_diff2 = M.addVek(point, M.scaleVek(h, e_2)) quotient_diff2 = M.subVek( M.scaleVek(1 / h, cV.getNextVertex(u, V, point_diff2)), M.scaleVek(1 / h, cV.getNextVertex(u, V, point))) difference_1 = M.dist(quotient_diff1, diff_1) difference_2 = M.dist(quotient_diff2, diff_2) print(' the differences equals') print(difference_1) print(difference_2) if difference_1 >= eps or difference_2 >= eps: print(' the differences euqal') print(difference_1) print(difference_2) return False return True
def coneVolumeNewton(cD, params): value = float("inf") print(' newton begins ') print(params) n = 0 while (regulator(cD, params) > eps_newton): #print(regulator(cD , params)) v = F(cD, params) C = diffF(cD, params) params = M.subVek(params, C.lgsSolve(v)) #print(value) if n % 50 == 0: print(params) n = n + 1 return params
def getPseudoConeVolRational(polygonRational): result = [] P = polygonRational n = len(P) for i in range(n): v = M.subVek(P[i], P[(i - 1 + n) % n]) u = getClockRotation(v) # this equals 1/2 * determinant, since norm of u is the same as norm of v, both get cancelled volume = f.Fraction(1, 2) * M.scal(u, P[i - 1]) if volume < 0: print('volume is negativ at ') print(i) result.append([u[0], u[1], volume]) return result
def transGridMid( orderedGrid , newMidPoint): length = len(orderedGrid) start = orderedGrid[0] end_leftup = orderedGrid[ length -1 ] end_rightdown = [ end_leftup[1] , start[1] ] midOfGrid = M.getMidPoint( end_leftup , end_rightdown ) translation = M.subVek( newMidPoint , midOfGrid ) i = 0 # delete every point in orderedGrid, that is not in first (postive) quadrant. Because of 'index out of bounce' error, i and length may have to be reduced while i < length: orderedGrid[i] = M.addVek( orderedGrid[i] , translation ) if orderedGrid[i][0] < 0 or orderedGrid[i][1] < 0: orderedGrid.pop(i) i = i - 1 length = length - 1 i = i + 1
def getConeVol(vertices): result = [] n = len(vertices) for i in range(n): v = M.subVek(vertices[i], vertices[(i - 1 + n) % n]) u_tilde = getClockRotation(v) divisor = M.norm(v) if u_tilde[0] == 0 and u_tilde[1] == 0: divisor = mE.getMachEps() u = M.scaleVek(1.0 / divisor, u_tilde) height = M.scal(u, vertices[i - 1]) if height < 0: print('height is negativ at ') print(i) facetVolume = M.norm(v) coneVolume = 0.5 * height * facetVolume result.append([u[0], u[1], coneVolume]) return result
def F(cD, params): return M.subVek(getConeVolIterator(cD, gamma(cD, params)), gamma(cD, params))
def makeCentered( vertices ): lastPoint = [ 0 , 0 ] for v in vertices: lastPoint = M.subVek( lastPoint , v ) vertices.append( lastPoint )
U = np.zeros(x_shape) V = np.zeros(x_shape) x_fix = [] y_fix = [] x_sing = [] y_sing = [] #cD_test = cV.getConeVol( [[1,0],[0,1],[-1,0],[0,-1]]) #cD_test = cV.getConeVol( [[1,1],[-1,1],[-1,-1],[1,-1]]) P = [[1, 1], [-1, 1], [-1, -1], [2, -1]] cD_test = cV.getConeVol(P) for i in range(x_shape[0]): for j in range(x_shape[1]): point = [X[i][j], Y[i][j]] try: v = M.subVek(cV.getConeVolIterator(cD_test, point), point) if v[0]**2 + v[1]**2 < 0: v = [0, 0] x_sing.append(point[0]) y_sing.append(point[1]) else: if (M.norm(v) != 0): v = M.scaleVek(density / M.norm(v), v) else: v = [0, 0] x_fix.append(point[0]) y_fix.append(point[1]) except ZeroDivisionError: #print(point) x_sing.append(point[0]) y_sing.append(point[1])
def transGridEnd( grid , newEndPoint): for i in range( len( grid ) ): grid[i] = M.subVek( newEndPoint , grid[i] )