How to calculate the distance between two coordinate points (knowing longitude and latitude) in Python? Three methods are given here:

Implement the Haversine Formula

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
import math
from math import radians, sin, cos, asin

def haversine(lat1, lon1, lat2, lon2):
# The average radius of the earth is approximately 6371.393km
EARTH_RADIUS = 6371

# Convert to radians
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)

# Calculate the difference between converted latitude and longitude
delta_lat = lat2 - lat1
delta_lon = lon2 - lon1

# Haversine formula
temp = math.pow(sin(delta_lat / 2), 2) + cos(lat1) * cos(lat2) * math.pow(sin(delta_lon / 2), 2)
distance = 2 * asin(math.sqrt(temp)) * EARTH_RADIUS

return distance

new_dist = haversine(36.1628, 114.2581, 36.5935, 114.6296)
print("The distance calculated using the Haversine formula is:", new_dist, "km")

The output is as follows:

Read more »

Install GitHub Copilot for PyCharm Plugin

Click Settings and search Copilot in the plug-in market:

Install the plugin and restart the IDE:

Read more »

Primal Problem

Max:2x1+x2+6x3x4s.t.{x1+2x2+x3x4<=10x1+2x3+x4>=5x2+x3+2x4=20x1>=0,x2<=0,x3<=0Max:2x_1+x_2+6x_3-x_4\\ s.t.\begin{cases} x_1+2x_2+x_3-x_4<=10\\ x_1+2x_3+x_4>=5\\ x_2+x_3+2x_4=20\\ x_1>=0,x_2<=0,x_3<=0 \end{cases}

The general vector form of the primal problem can be expressed as:

Max:z=CXs.t.{AX<=BX>=0Max:z=CX\\ s.t.\begin{cases}AX<=B\\ X>=0 \end{cases}

We can represent the coefficients of the original problem as matrices:

Read more »

Now, let's use this tool to solve a simple mixed integer programming problem in Python.


Install the OR-Tools Library

1
pip install ortools
Read more »

itertools.combinations

Generates all possible combinations of elements of a specified length:

C(5,2)C(5,2)

1
2
3
4
5
6
import itertools

str = ['A', 'B', 'C', 'D', 'E']
# Compute combinations of length 2 without replacement
combinations_result = list(itertools.combinations(str, 2))
print(combinations_result)

The output is as follows:

Read more »

Installation and Import of OpenCV in Python

Execute the following pip command to complete the installation of the OpenCV library:

1
pip install opencv-python

After the installation is complete, import the cv2 module:

Read more »

Install NexT Theme

First, you need to install the NexT theme, you can enter the following code:

1
npm install hexo-theme-next

Read more »
0%