Find maximum and minimum of multivariable function in sympy - sympy

I have the following function:
f = x**2 + y**2
I would like to use sympy to find the maximum of and minimum value in the unit square [0,1] in x and [0,1] in y.
The expected outcome would be 0 for point [0,0] and 2 for point [1,1]
Can this be achieved?

I did something clunky, but appears to work [although not fast]:
def findMaxMin(f):
# find stationary points:
stationary_points = sym.solve([f.diff(x), f.diff(y)], [x, y], dict=True)
# Append boundary points
stationary_points.append({x:0, y:0})
stationary_points.append({x:1, y:0})
stationary_points.append({x:1, y:1})
stationary_points.append({x:0, y:1})
# store results after evaluation
results = []
# iteration counter
j = -1
for i in range(len(stationary_points)):
j = j+1
x1 = stationary_points[j].get(x)
y1 = stationary_points[j].get(y)
# If point is in the domain evalute and append it
if (0 <= x1 <= 1) and ( 0 <= y1 <= 1):
tmp = f.subs({x:x1, y:y1})
results.append(tmp)
else:
# else remove the point
stationary_points.pop(j)
j = j-1
# Variables to store info
returnMax = []
returnMin = []
# Get the maximum value
maximum = max(results)
# Get the position of all the maximum values
maxpos = [i for i,j in enumerate(results) if j==maximum]
# Append only unique points
append = False
for item in maxpos:
for i in returnMax:
if (stationary_points[item] in i.values()):
append = True
if (not(append)):
returnMax.append({maximum: stationary_points[item]})
# Get the minimum value
minimum = min(results)
# Get the position of all the minimum values
minpos = [i for i,j in enumerate(results) if j==minimum ]
# Append only unique points
append = False
for item in minpos:
for i in returnMin:
if (stationary_points[item] in i.values()):
append = True
if (not(append)):
returnMin.append({minimum: stationary_points[item]})
return [returnMax, returnMin]

Related

Implementation of Karger's Algorithm in Python Taking too Long

Wondering if you can help me understand where the critical flaw may be with my attempt at implementing Karger's algorithm in python. My program appears to take far too long to run and my computer starts to overwork running large sets of vertices. The purpose of the program is to output the minimum cut of the graph.
from random import choice
from statistics import mode
import math
fhand = open("mincuts.txt", "r")
vertices = fhand.readlines()
d = {}
for index,line in enumerate(vertices):
d["{0}".format(index+1)] = line.split()
def randy(graph, x):
y = str(choice(list(graph)))
if x == y:
y = randy(graph, x)
return y
count = 0
def contract(graph):
global count
if len(graph) == 2:
a = list(graph.keys())[0]
b = list(graph.keys())[1]
for i in range(1, len(graph[a])):
if graph[a][i] in graph[b]:
count = count + 1
#print(graph)
return
x = str(choice(list(graph)))
y = randy(graph, x)
#print(x)
#print(y)
graph[x] = graph[x] + graph[y]
graph.pop(y)
#remove self loops
for key in graph:
#method to remove duplicate entries in the arrays of the vertices. Source: www.w3schools.com
graph[key] = list(dict.fromkeys(graph[key]))
contract(graph)
N = len(d)
runs = int(N*N*(math.log(N)))
outcomes = []
for i in range(runs):
e = d.copy()
count = 0
contract(e)
outcomes.append(count)
print(outcomes)
#returns most common minimum cut value
print(mode(outcomes))
Below is a link to the graph I am running in mincuts.txt:
https://github.com/BigSoundCode/Misc-Algorithm-Implementations/blob/main/mincuts.txt

Replace items in list if condition

I need to replace temperature values in list depends on negative/positive and get rid of float at the same time. I.e. value '-0.81' should be '-1' (round) or '0.88' should be '1'.
myList = ['-1.02', '-1.03', '-0.81', '-0.17', '-0.07', '0.22', '0.88', '0.88', '0.69']
for i in range (len(myList)):
if myList[i][0] == '-' and int(myList[i][-2]) > 5:
do sth...
At the end I need new list with new values. Thank you for any tips.
Your code is already almost there. It's not necessary to reference the elements by index.
myList = ['-1.02', '-1.03', '-0.81', '-0.17', '-0.07', '0.22', '0.88', '0.88', '0.69']
for i in myList:
if i[0] == '-' and int(i[-2]) > 5:
do sth...
If all you want to do is rounding then you can use a list comprehension.
roundlist = [round(float(i)) for i in myList]
You could parse the string into number, check for rounding (whether the decimal is higher or lower than 0.5), and convert it back to string
import math
myList = ['-1.02', '-1.03', '-0.81', '-0.17', '-0.07', '0.22', '0.88', '0.88', '0.69']
result = [0] * len(myList)
for i in range (len(myList)):
num = float(myList[i])
if num - math.floor(num) < 0.5:
result[i] = str(math.floor(num)) # round down
else:
result[i] = str(math.ceil(num)) # round up
print(result)

count index in a list to x number python

I have a list with like this that goes from 0 to 1000 .
x = ['0_1','0_2','0_3' ..., '1000_1','1000_2','1000_3']
I need it to count every time the char in index changes like I show below.
list_leng = len(x)
for i in range(0, list_leng):
y = x[i]
z = y[0]
print this should iterate through all the list and only print when the z number changes ' # how should I make it print '
If I understood your question well, the answer must be something like this;
comparisonText = ""
for each in x:
preNumber = each.split('_')[0]
if comparisonText != preNumber:
print preNumber, 'is different than previous.'
comparisonText = preNumber #change the comparisonText

Combine overlapping rectangles (python)

After researching, I came across few questions similar to this:OpenCV groupRectangles - getting grouped and ungrouped rectangles (most are in c++). However, none of them are solid. I want to combine the overlapping rectangles into a single one.
Image
My progress:
for cnt in large_contours:
x,y,w,h = cv2.boundingRect(cnt)
mec=x,y,w,h
rectVec=cv2.rectangle(img_and_contours,(x,y),(x+w,y+h),(0,255,0),2)
#cv2.rectangle(img_and_contours, cv2.boundingRect(large_contours[cnt]),(0,255,0));
rectList, weights = cv2.groupRectangles(mec, 3,0.2)
I only posted piece of my code.I was hoping groupRectangle would do what I wanted, but did nothing and instead gives me an error
rectList,weights = cv2.groupRectangles(mec,3,0.2)
TypeError: rectList
Blockquote
Here is the piece of code which worked for me
def merge_overlapping_zones(zones,delta_overpap = 30):
index = 0
if zones is None: return zones
while index < len(zones):
no_Over_Lap = False
while no_Over_Lap == False and len(zones) > 1 and index < len(zones):
zone1 = zones[index]
tmpZones = np.delete(zones, index, 0)
tmpZones = [tImageZone(*a) for a in tmpZones]
for i in range(0, len(tmpZones)):
zone2 = tmpZones[i]
# check left side broken
if zone2.x >= delta_overpap and zone2.y >= delta_overpap:
t = tImageZone(zone2.x - delta_overpap, zone2.y - delta_overpap, zone2.w + 2 * delta_overpap,
zone2.h + 2 * delta_overpap)
elif zone2.x >= delta_overpap:
t = tImageZone(zone2.x - delta_overpap, zone2.y, zone2.w + 2 * delta_overpap,
zone2.h + 2 * delta_overpap)
else:
t = tImageZone(zone2.x, zone2.y - delta_overpap, zone2.w + 2 * delta_overpap,
zone2.h + 2 * delta_overpap)
if (is_zone_overlap(zone1, t) or is_zone_overlap(zone1, zone2)):
tmpZones[i] = merge_zone(zone1, zone2)
zones = tmpZones
no_Over_Lap = False
break
no_Over_Lap = True
index += 1
return zones
`
There is an algorithm called **Non max suppression**. The function takes the rectangle array as input, and output the maximum rectangle. Here is the code (from pyimagesearch):
def non_max_suppression_fast(boxes, overlapThresh):
# if there are no boxes, return an empty list
if len(boxes) == 0:
return []
# if the bounding boxes integers, convert them to floats --
# this is important since we'll be doing a bunch of divisions
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
#
# initialize the list of picked indexes
pick = []
# grab the coordinates of the bounding boxes
x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]
# compute the area of the bounding boxes and sort the bounding
# boxes by the bottom-right y-coordinate of the bounding box
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
# keep looping while some indexes still remain in the indexes
# list
while len(idxs) > 0:
# grab the last index in the indexes list and add the
# index value to the list of picked indexes
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
# find the largest (x, y) coordinates for the start of
# the bounding box and the smallest (x, y) coordinates
# for the end of the bounding box
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
# compute the width and height of the bounding box
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
# compute the ratio of overlap
overlap = (w * h) / area[idxs[:last]]
# delete all indexes from the index list that have
idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))
# return only the bounding boxes that were picked using the
# integer data type
return boxes[pick].astype("int")
Hope it can help you.

Random walk code in python [2 dimensions]

Please could you help me by figuring out what is the wrong with my code.
I am trying to write a program that generates random walks in two dimensions and that determines statistics about the position of the walker after 500 steps when the number of walks is 1000, the max step size is 0.9, and the separation between the two positions is 0.001.
import math
import random
import time
print "RANDOM WALKS ANALYSIS IN ONE DIMENSION"
NOFW_ = 1000 #The number of walks
NOFS_= 500 #The number of steps in each walk
MSS_ = 0.9 # The maximum step size[m]
SOFP_ = 0.001 # The separation of positions considered equal[m]
print " Number of walks: %3g"% NOFW_
print " Number of steps in each Walk: %3g"% NOFS_
print " Maximum step size: %3g"% MSS_,"m"
print "Separation of positions considered equal: %3g"% SOFP_,"m"
print
print "Please wait while random walks are generated and analyzed..."
print "Date:" + time.ctime()
print
def initialPosition():
return (0.0, 0.0)
def distance(posA, posB):
"""Calculates the distance between two positions posA and posB"""
distance = math.sqrt((posB[0] - posA[0])**2 + (posB[1] - posA[1])**2)
return distance
def printstats(description, numbers):
minimum_value_ = min(numbers)
numbers.sort()
Tenth_percentile = abs(0.10*len(numbers) + 0.5)
Mean_value_ = (1./float(len(numbers))*sum(numbers))
A = 0
for values in numbers:
B = distance(values, Mean_value_)
B = B**2
A = B + A
Standard_deviation = math.sqrt((1./(len(numbers)-1))*A)
Newposition_ = int(0.90*(len(numbers) + 0.5))
Ninetieth_percentile =numbers[Newposition_]
maximum_value_ = max(numbers)
print "Analysis for"""+ description
print "Minimum value: %9.1f" % minimum_value_
print "10th percentile: %7.1f" % Tenth_percentile
print "Mean value: %12.1f" % Mean_value_
print "Standard deviation: %4.1f" % Standard_deviation
print "90th percentile: %7.1f" % Ninetieth_percentile
print "Maximum value: %9.1f" % maximum_value_
list_1 = [minimum_value_, Tenth_percentile, Mean_value_, Standard_deviation, Ninetieth_percentile,maximum_value_]
return list_1
def takeStep(prevPosition, maxStep):
x = random.random()
y = random.random()
minStep = -maxStep
Z = random.random()*2*math.pi
stepsize_ = random.random()*0.9
Stepx= stepsize_*math.cos(Z)
Stepy= stepsize_*math.sin(Z)
New_positionx = prevPosition[0] + Stepx
New_positiony = prevPosition[1] + Stepy
return (New_positionx, New_positiony)
Step_100 = []
Step_500 = []
count_list = []
for walk in range(NOFW_):
Step1 = []
Position = (0.0,0.0)
count = 0
for step in range(NOFS_):
Next_Step_ = takeStep(Position, MSS_)
for word in Step1:
if distance(Next_Step_, word) <= SOFP_:
count +=1
position = Next_Step_
Step1.append(Next_Step_)
Step_100.append(Step1[-1])
Step_500.append(Step1[-1])
count_list.append(count)
Step_100 = printstats("distance from start at step 100 [m]", Step_100)
Step_500 = printstats("distance from start at step 500 [m]", Step_500)
count_list = printstats("times position revisited", count_list)
Your problem is here
Mean_value_ = (1./float(len(numbers))*sum(numbers))
sum() is supposed to get some numbers but your variable numbers is actually containing some tuples of 2 values
You may want to define your own sum function for tuples of 2 numbers, or to sum the first values and second values separately