Compare commits

...

10 Commits

Author SHA1 Message Date
1df542aaef 0.1.158 2025-03-01 22:01:26 +08:00
1eb25c142b 0.1.157 2025-03-01 02:06:50 +08:00
af7f4d0b8f 0.1.156 2025-02-28 03:44:59 +08:00
9660bd6fc6 0.1.155 2025-02-28 02:38:46 +08:00
319981909f 0.1.154 2025-02-27 18:23:03 +08:00
18a7236eff 0.1.153 2025-02-27 17:59:14 +08:00
780d9fdb11 0.1.152 2025-02-27 17:46:30 +08:00
0dc4255549 0.1.151 2025-02-26 18:11:17 +08:00
5d807ecd1d 0.1.150 2025-02-26 18:04:51 +08:00
f41deb1d33 0.1.149 2025-02-26 15:42:52 +08:00
10 changed files with 179 additions and 28 deletions

View File

@ -1,7 +1,7 @@
[metadata]
# replace with your username:
name = guan
version = 0.1.148
version = 0.1.158
author = guanjihuan
author_email = guanjihuan@163.com
description = An open source python package

View File

@ -1,6 +1,6 @@
Metadata-Version: 2.2
Name: guan
Version: 0.1.148
Version: 0.1.158
Summary: An open source python package
Home-page: https://py.guanjihuan.com
Author: guanjihuan

View File

@ -8,12 +8,14 @@ src/guan/Hamiltonian_of_examples.py
src/guan/__init__.py
src/guan/band_structures_and_wave_functions.py
src/guan/basic_functions.py
src/guan/custom_classes.py
src/guan/data_processing.py
src/guan/decorators.py
src/guan/density_of_states.py
src/guan/deprecated.py
src/guan/figure_plotting.py
src/guan/file_reading_and_writing.py
src/guan/functions_using_objects_of_custom_classes.py
src/guan/machine_learning.py
src/guan/quantum_transport.py
src/guan/topological_invariant.py

View File

@ -13,5 +13,7 @@ from .file_reading_and_writing import *
from .figure_plotting import *
from .data_processing import *
from .decorators import *
from .custom_classes import *
from .functions_using_objects_of_custom_classes import *
from .deprecated import *
statistics_of_guan_package()

View File

@ -0,0 +1,11 @@
# Module: custom_classes
# 原子类
class Atom:
def __init__(self, name='atom', index=0, x=0, y=0, z=0, energy=0):
self.name = name
self.index = index
self.x = x
self.y = y
self.z = z
self.energy = energy

View File

@ -38,26 +38,26 @@ def chat_with_function_code(function_name, prompt='', model=1, stream=1):
return response
# 机器人自动对话
def auto_chat(prompt='你好', round=2):
def auto_chat(prompt='你好', round=2, model=1, stream=1):
import guan
response0 = prompt
for i0 in range(round):
print(f'【对话第 {i0+1} 轮】\n')
print(f'\n【对话第 {i0+1} 轮】\n')
print('机器人 1: ')
response1 = guan.chat(prompt=response0, stream=1)
response1 = guan.chat(prompt=response0, model=model, stream=stream)
print('机器人 2: ')
response0 = guan.chat(prompt=response1, stream=1)
response0 = guan.chat(prompt=response1, model=model, stream=stream)
# 机器人自动对话(引导对话)
def auto_chat_with_guide(prompt='你好', guide_message='回答字数少于30个字最后反问我一个问题', round=5):
def auto_chat_with_guide(prompt='你好', guide_message='回答字数少于30个字最后反问我一个问题', round=5, model=1, stream=1):
import guan
response0 = prompt
for i0 in range(round):
print(f'【对话第 {i0+1} 轮】\n')
print(f'\n【对话第 {i0+1} 轮】\n')
print('机器人 1: ')
response1 = guan.chat(prompt=response0+guide_message, stream=1)
response1 = guan.chat(prompt=response0+guide_message, model=model, stream=stream)
print('机器人 2: ')
response0 = guan.chat(prompt=response1+guide_message, stream=1)
response0 = guan.chat(prompt=response1+guide_message, model=model, stream=stream)
# 在云端服务器上运行函数(需要函数是独立可运行的代码)
def run(function_name, *args, **kwargs):
@ -86,6 +86,46 @@ def run(function_name, *args, **kwargs):
pass
return return_data
# 获取矩阵的维度考虑单一数值的矩阵维度为1
def dimension_of_array(array):
import numpy as np
array = np.array(array)
if array.shape==():
dim = 1
else:
dim = array.shape[0]
return dim
# 获取旋转矩阵(输入为角度)
def get_rotation_matrix(angle_deg):
import numpy as np
angle_rad = np.radians(angle_deg)
matrix = np.array([
[np.cos(angle_rad), -np.sin(angle_rad)],
[np.sin(angle_rad), np.cos(angle_rad)]
])
return matrix
# 旋转某个点,返回新的点的坐标
def rotate_point(x, y, angle_deg):
import numpy as np
rotation_matrix = get_rotation_matrix(angle_deg)
x, y = np.dot(rotation_matrix, np.array([x, y]))
return x, y
# CPU性能测试十亿次循环的浮点加法运算的时间约30秒左右
def cpu_test_with_addition(print_show=1):
import time
result = 0.0
start_time = time.time()
for _ in range(int(1e9)):
result += 1e-9
end_time = time.time()
run_time = end_time - start_time
if print_show:
print(run_time)
return run_time
# 将XYZ数据转成矩阵数据说明x_array/y_array的输入和输出不一样。要求z_array数据中y对应的数据为小循环x对应的数据为大循环
def convert_xyz_data_into_matrix_data(x_array, y_array, z_array):
import numpy as np
@ -295,6 +335,14 @@ def get_string_between_two_patterns(original_string, start, end, include_start_a
else:
return ''
# 删除某个字符串中两个模式之间的内容,返回新字符串
def remove_substrings(original_string, start, end):
import re
escaped_start = re.escape(start)
escaped_end = re.escape(end)
pattern = f'{escaped_start}.*?{escaped_end}'
return re.sub(pattern, '', original_string, flags=re.DOTALL)
# 打印数组
def print_array(array, line_break=0):
if line_break == 0:

View File

@ -12,6 +12,38 @@ def import_plt_and_start_fig_ax(adjust_bottom=0.2, adjust_left=0.2, labelsize=20
[label.set_fontname('Times New Roman') for label in labels]
return plt, fig, ax
# 导入plt, fig, ax不显示坐标
def import_plt_and_start_fig_ax_without_axis():
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.subplots_adjust(left=0, bottom=0, right=1, top=1)
plt.axis('off')
return plt, fig, ax
# 保存图片为所有常见的格式
def savefig_with_all_formats(plt, filename, more_dpi=1, tiff=0):
plt.savefig(filename+'.eps')
plt.savefig(filename+'.svg')
plt.savefig(filename+'.pdf')
plt.savefig(filename+'.jpg')
plt.savefig(filename+'.png')
if tiff:
plt.savefig(filename+'.tiff')
if more_dpi:
try:
plt.savefig(filename+'_dpi=300.jpg', dpi=300)
plt.savefig(filename+'_dpi=600.jpg', dpi=600)
plt.savefig(filename+'_dpi=1000.jpg', dpi=1000)
plt.savefig(filename+'_dpi=300.png', dpi=300)
plt.savefig(filename+'_dpi=600.png', dpi=600)
plt.savefig(filename+'_dpi=1000.png', dpi=1000)
if tiff:
plt.savefig(filename+'_dpi=300.tiff', dpi=300)
plt.savefig(filename+'_dpi=600.tiff', dpi=600)
plt.savefig(filename+'_dpi=1000.tiff', dpi=1000)
except:
pass
# 基于plt, fig, ax画图
def plot_without_starting_fig_ax(plt, fig, ax, x_array, y_array, xlabel='x', ylabel='y', title='', fontsize=20, style='', y_min=None, y_max=None, linewidth=None, markersize=None, color=None, fontfamily='Times New Roman'):
if color==None:
@ -298,38 +330,37 @@ def plot_pcolor(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', font
plt.close('all')
# 基于plt, fig, ax通过坐标画点和线
def draw_dots_and_lines_without_starting_fig_ax(plt, fig, ax, coordinate_array, draw_dots=1, draw_lines=1, max_distance=1, line_style='-k', linewidth=1, dot_style='ro', markersize=3):
def draw_dots_and_lines_without_starting_fig_ax(plt, fig, ax, coordinate_array, draw_dots=1, draw_lines=1, max_distance=1.0001, line_style='-k', linewidth=1, dot_style='ro', markersize=3):
import numpy as np
coordinate_array = np.array(coordinate_array)
if draw_lines==1:
for i1 in range(coordinate_array.shape[0]):
for i2 in range(coordinate_array.shape[0]):
if np.sqrt((coordinate_array[i1, 0] - coordinate_array[i2, 0])**2+(coordinate_array[i1, 1] - coordinate_array[i2, 1])**2) <= max_distance:
ax.plot([coordinate_array[i1, 0], coordinate_array[i2, 0]], [coordinate_array[i1, 1], coordinate_array[i2, 1]], line_style, linewidth=linewidth)
if i1<i2:
x1 = coordinate_array[i1, 0]
x2 = coordinate_array[i2, 0]
y1 = coordinate_array[i1, 1]
y2 = coordinate_array[i2, 1]
if abs(x1-x2)<=max_distance and abs(y1-y2)<=max_distance:
if np.sqrt((x1 - x2)**2+(y1 - y2)**2) <= max_distance:
ax.plot([x1, x2], [y1, y2], line_style, linewidth=linewidth)
if draw_dots==1:
for i in range(coordinate_array.shape[0]):
ax.plot(coordinate_array[i, 0], coordinate_array[i, 1], dot_style, markersize=markersize)
ax.plot(coordinate_array[:, 0], coordinate_array[:, 1], dot_style, markersize=markersize)
# 通过坐标画点和线
def draw_dots_and_lines(coordinate_array, draw_dots=1, draw_lines=1, max_distance=1, line_style='-k', linewidth=1, dot_style='ro', markersize=3, show=1, save=0, filename='a', file_format='.eps', dpi=300):
def draw_dots_and_lines(coordinate_array, draw_dots=1, draw_lines=1, max_distance=1.0001, line_style='-k', linewidth=1, dot_style='ro', markersize=3, axis_off=1, show=1, save=0, filename='a', file_format='.eps', dpi=300):
import numpy as np
import matplotlib.pyplot as plt
import guan
coordinate_array = np.array(coordinate_array)
x_range = max(coordinate_array[:, 0])-min(coordinate_array[:, 0])
y_range = max(coordinate_array[:, 1])-min(coordinate_array[:, 1])
fig, ax = plt.subplots(figsize=(6*x_range/y_range,6))
ax.set_aspect('equal') # important code ensuring that the x and y axes have the same scale.
if axis_off==1:
plt.subplots_adjust(left=0, bottom=0, right=1, top=1)
plt.axis('off')
if draw_lines==1:
for i1 in range(coordinate_array.shape[0]):
for i2 in range(coordinate_array.shape[0]):
if np.sqrt((coordinate_array[i1, 0] - coordinate_array[i2, 0])**2+(coordinate_array[i1, 1] - coordinate_array[i2, 1])**2) <= max_distance:
ax.plot([coordinate_array[i1, 0], coordinate_array[i2, 0]], [coordinate_array[i1, 1], coordinate_array[i2, 1]], line_style, linewidth=linewidth)
if draw_dots==1:
for i in range(coordinate_array.shape[0]):
ax.plot(coordinate_array[i, 0], coordinate_array[i, 1], dot_style, markersize=markersize)
guan.draw_dots_and_lines_without_starting_fig_ax(plt, fig, ax, coordinate_array, draw_dots=draw_dots, draw_lines=draw_lines, max_distance=max_distance, line_style=line_style, linewidth=linewidth, dot_style=dot_style, markersize=markersize)
if show==1:
plt.show()
if save==1:

View File

@ -243,6 +243,40 @@ def write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix,
f.write(str(element)+' ')
f.write('\n')
# 把矩阵写入.md文件Markdown表格形式
def write_matrix_in_markdown_format(matrix, filename='a'):
import numpy as np
matrix = np.array(matrix)
dim_0 = matrix.shape[0]
dim_1 = matrix.shape[1]
with open(filename+'.md', 'w', encoding='UTF-8') as f:
for i1 in range(dim_1):
f.write(f'| column {i1+1} ')
f.write('|\n')
for i1 in range(dim_1):
f.write('| :---: ')
f.write('|\n')
for i0 in range(dim_0):
for i1 in range(dim_1):
f.write(f'| {matrix[i0, i1]} ')
f.write('|\n')
# 把矩阵写入.md文件Latex形式
def write_matrix_in_latex_format(matrix, filename='a', format='bmatrix'):
import numpy as np
matrix = np.array(matrix)
dim_0 = matrix.shape[0]
dim_1 = matrix.shape[1]
with open(filename+'.md', 'w', encoding='UTF-8') as f:
f.write(f'$$\\begin{{{format}}}\n')
for i0 in range(dim_0):
for i1 in range(dim_1):
if i1 != dim_1-1:
f.write(f'{matrix[i0, i1]} & ')
else:
f.write(f'{matrix[i0, i1]} \\\\\n')
f.write(f'\\end{{{format}}}$$')
# 如果不存在文件夹,则新建文件夹
def make_directory(directory='./test'):
import os

View File

@ -0,0 +1,21 @@
# functions_using_objects_of_custom_classes
# 从原子对象列表中获取 (x, y) 坐标数组
def get_coordinate_array_from_atom_object_list(atom_object_list):
coordinate_array = []
for atom in atom_object_list:
x = atom.x
y = atom.y
coordinate_array.append([x, y])
return coordinate_array
# 根据原子对象列表来初始化哈密顿量
def initialize_hamiltonian_from_atom_object_list(atom_object_list):
import numpy as np
import guan
dim = guan.dimension_of_array(atom_object_list[0].energy)
num = len(atom_object_list)
hamiltonian = np.zeros((dim*num, dim*num))
for i0 in range(num):
hamiltonian[i0*dim+0:i0*dim+dim, i0*dim+0:i0*dim+dim] = atom_object_list[i0].energy
return hamiltonian

View File

@ -26,12 +26,14 @@ import guan
+ file reading and writing
+ figure plotting
+ data processing
+ custom classes
+ functions using objects of custom classes
+ decorators
## About this package
+ The original motivation of this project is for self use. Based on this project, the frequent functions can be imported by “import guan” instead of repeated copies and pastes. You can also install and use this open-source package if some functions are helpful for you. If one function is not good enough, you can copy the Source Code and modify it. You can also feed back to guanjihuan@163.com. The modifications and supplements will be in the following updated version.
+ All realizations of this package are based on functions without any class, which are concise and convenient. The boring document is omitted and you have to read the Source Code for details if it is necessary. Nevertheless, you dont have to be worried about the difficulty, because all functions are simple enough without too many prejudgments and the variable names are written commonly as far as possible for the easy reading.
+ Most realizations of this package are only based on functions, which are concise and convenient. The boring document is omitted and you have to read the Source Code for details if it is necessary. Nevertheless, you dont have to be worried about the difficulty, because all functions are simple enough without too many prejudgments and the variable names are written commonly as far as possible for the easy reading.
+ Before the function calling in your project, you are recommended to briefly read the Source Code to know the specific formats of input and output about the function. Applying functions mechanically may cause errors. Notice that as the package is developed, the function names may be changed in the future version. Therefore, the latest API Reference is important and helpful.
## Citation