# -*- encoding: utf-8 -*- """ @Author: packy945 @FileName: function.py @DateTime: 2023/6/6 14:45 @SoftWare: PyCharm """ import numpy as np import math import data import pandas as pd from pandas import DataFrame # 函数库 Show = [] Fun = [] Fit_type_library = {} class Function: def __init__(self, no, name, v, fun, complete='', demo=''): # 函数名称 self.name = name # 变量个数 self.variable = v self.function = fun # 函数表达式 # 函数编号 self.no = no # 是否显示 self.show = 0 # 完整函数 self.complete = complete # self.print = '' self.demo = demo def get_fun(self): if self.variable == 2: return self.fun2 elif self.variable == 3: # 以二次函数为例,省略其他函数 return self.fun3 elif self.variable == 4: return self.fun4 elif self.variable == 5: return self.fun5 elif self.variable == 6: return self.fun6 def fun2(self, x, a0, a1): return eval(self.function) def fun3(self, x, a0, a1, a2): return eval(self.function) def fun4(self, x, a0, a1, a2, a3): return eval(self.function) def fun5(self, x, a0, a1, a2, a3, a4): return eval(self.function) def fun6(self, x, a0, a1, a2, a3, a4, a5): return eval(self.function) def print_f(self, a): # print('self.show='+self.complete) exec('self.show='+self.complete) # print(self.show) return self.show # def f_init(): # cur = 0 # 函数编号 # # 一次函数 # newfun = Function(cur, '一次函数', 2, 'a0*x+a1', "f'{a[0]}x+{a[1]}'", 'ax+b') # newfun.show = 1 # Fun.append(newfun) # cur += 1 # # 二次函数 # newfun = Function(cur, '二次函数', 3, 'a0*x*x+a1*x+a2', "f'{a[0]}x^2+{a[1]}x+{a[2]}'", 'ax^2+bx+c') # newfun.show = 1 # Fun.append(newfun) # cur += 1 # # 三次函数 # newfun = Function(cur, '三次函数', 4, 'a0*x*x*x+a1*x*x+a2*x+a3', "f'{a[0]}x^3+{a[1]}x^2+{a[2]}x+{a[3]}'", 'ax^3+bx^2+cx+d') # newfun.show = 0 # Fun.append(newfun) # cur += 1 # # 四次函数 # newfun = Function(cur, '四次函数', 5, 'a0*x*x*x*x+a1*x*x*x+a2*x*x+a3*x+a4' # , "f'{a[0]}x^4+{a[1]}x^3+{a[2]}x^2+{a[3]}x+{a[4]}'", 'ax^4+bx^3+cx^2+dx+e') # newfun.show = 0 # Fun.append(newfun) # cur += 1 # # 指数函数 # newfun = Function(cur, '指数函数', 2, 'a0*np.exp(0.01*x)+a1', "f'{a[0]}e^(0.01x)+{a[1]}'", 'a*e^(0.01x)+b') # newfun.show = 0 # Fun.append(newfun) # cur += 1 # # 对数函数 # newfun = Function(cur, '对数函数', 3, 'a0*(np.log(x) + 1e-5) / (np.log(a1) + 1e-5) + a2', # "f'{a[0]}log(x,{a[1]})+{a[2]}'", 'alog(x,b)+c') # newfun.show = 0 # Fun.append(newfun) # cur += 1 # # 高斯函数 # F_init() def write(): file_path = 'functions.xlsx'#设置路径 df = pd.read_excel(io=file_path, header=1)#设置表格 print(df) df.columns = ['no', 'name', 'variable', 'function', 'complete', 'demo', 'show']#设置表格标题 cur = 0#重置计数 for f in Fun:#遍历函数类型 df.loc[cur] = [cur, f.name, f.variable, f.function, f.complete, f.demo, f.show]#写进表格 cur += 1#计数加一 DataFrame(df).to_excel(file_path, sheet_name='Sheet1', index=False, header=True)#保存xlsx def f_read(): file_path = 'functions.xlsx'#设置路径 df = pd.read_excel(io=file_path, header=0)#设置表格 cur = 0 for i in range(len(df)):#逐行读取excel newfun = Function(df.loc[i][0], df.loc[i][1], df.loc[i][2], df.loc[i][3], df.loc[i][4], df.loc[i][5]) newfun.show = df.loc[i][6] Show.append(df.loc[i][6])#读取函数显示信息 Fun.append(newfun)#读取函数信息并添加 cur += 1 for f in Fun:#建立简表fit_type_library Fit_type_library[f.name] = [f.no, f.demo, f.name] if __name__ == '__main__': f_read() # for f in fun: # print([f.no, f.demo, f.name]) for f in Fun: Fit_type_library[f.name] = [f.no, f.demo, f.name] print("Fun",Fun) print(Fit_type_library) a = [1,2,3,4,5] x = 2 pass