< 0:ans = (-base) ** (1 / times)return eval(f"{ans}j")return base ** (1 / times)class write:def _write_t_x(self, formula, tmin_=-10, tmax_=10, tnums=100,min_=-10, max_=10, nums=100, printout=True):global x, y, z, tself.step = (tmax_ - tmin_) / tnumsself.aa = tmin_ - self.stepself.xstep = (max_ - min_) / numsr"""print(un)print(lf,toLeg(formula),sep="\n\n",end="\n\n")"""solves = []for iVal in range(tnums + 1):self.aa = self.aa + self.stepself.aa = float("%.10f" % self.aa)xValue = http://www.kingceram.com/post/min_ - self.xstepfor XVAL in range(nums + 1):lf, un = legalSet(toLeg(formula))xValue += self.xstepxValue = float("%.10f" % xValue)def key(item):if "t" in item[0] or "x" in item[0]:return 1else:return 999bf = dict(enumerate(lf))lf = sorted(lf, key=key)nun = []for i in range(len(lf)):nun.insert(lf.index(bf[i]), un[i])un = nundel nunidx = -1for imp in un:idx += 1for __imp in imp:if __imp == 't':un[idx].remove("t")if __imp == 'x':un[idx].remove("x")del idxrepl = {"t": [str(self.aa)], "x": [str(xValue)]}is_exit = Falsetimes = -1while not is_exit:times += 1if times >= 10:# print(f"\nmessage:目前已经循环求解超过10次,目前初始代入值为:x{repl['x']},t{repl['t']},\n"#f"已经求解{repl},剩{4-len(repl.values())}值不可求解,故不予求解并跳出循环")breakindx = -1for lf_ in lf:indx += 1for i in repl.keys():for j in range(2):lf_[j] = lf_[j].replace(" " + i, str(repl[i][0]))idx = -1for imp in un:idx += 1for __imp in imp:if __imp in repl.keys():un[idx].remove(__imp)del idxif un[indx] == []:del un[indx]del lf[indx]if indx == 0 or (indx - 1) >= len(lf): breakindx -= 1continuetry:ams = sympy.solve(tuple(lf_), tuple(un[indx]))if ams == []:newlf = []for i in lf_:newlf.append(i.replace(str(repl["x"]), str(repl["x"]) + "*sympy.I"))ams = sympy.solve(tuple(newlf), tuple(un[indx]))except NotImplementedError as err:print(err)no_ans()returnif isinstance(ams, (dict,)):for km, jj in ams.items():if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):if km.name in repl.keys():repl[km.name].append(str(jj))else:repl[km.name] = [str(jj)]else:ix = -1undo = 0for i in ams:ix += 1try:if isinstance(i, (sympy.core.numbers.Float,sympy.core.numbers.Integer,int, float, complex)):try:repl[tuple(un[indx])[ix]] = [str(eval(str(i)))]except IndexError:repl[tuple(un[indx])[undo]].append(str(eval(str(i))))for i in repl.keys():if i in lf_:lf_ = lf_.replace(i, str(repl[i][0]))elif isinstance(i, (tuple, list, set)):inxx = -1for jj in i:inxx += 1if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):try:repl[tuple(un[indx][inxx])[ix]] = [str(jj)]except IndexError:repl[tuple(un[indx][inxx])[undo]].append(str(jj))else:continueexcept TypeError as err:print("get:err", err)continueelse:undo = ixif len(repl.values()) >= 4: is_exit = True;breakif is_exit:solves.append(repl)if printout:print(f"\r{(iVal * (nums + 1) + XVAL + 1) / ((nums + 1) * (tnums + 1)) * 100}%", end="",flush=True)if printout:print("\nsucceed in Calculating Values...")num = 0for i in solves:if printout:print(f"\r{(num + 1) / len(solves) * 100}%", end="", flush=True)num += 1x_ = []for j in i["x"]:x_.append(eval(j))x.append(x_)y_ = []for j in i["y"]:y_.append(eval(j))y.append(y_)z_ = []for j in i["z"]:z_.append(eval(j))z.append(z_)t_ = []for j in i["t"]:t_.append(eval(j))t.append(t_)del x_, y_, z_, t_, solvesif printout:print("\nsucceed in Saving Values...")def _write_t_y(self, formula, tmin_=-10, tmax_=10, tnums=100,min_=-10, max_=10, nums=100, printout=True):global x, y, z, tself.step = (tmax_ - tmin_) / tnumsself.aa = tmin_ - self.stepself.xstep = (max_ - min_) / numsr"""print(un)print(lf,toLeg(formula),sep="\n\n",end="\n\n")"""solves = []for iVal in range(tnums + 1):self.aa = self.aa + self.stepself.aa = float("%.10f" % self.aa)xValue = http://www.kingceram.com/post/min_ - self.xstepfor XVAL in range(nums + 1):lf, un = legalSet(toLeg(formula))xValue += self.xstepxValue = float("%.10f" % xValue)def key(item):if "t" in item[0] or "y" in item[0]:return 1else:return 999bf = dict(enumerate(lf))lf = sorted(lf, key=key)nun = []for i in range(len(lf)):nun.insert(lf.index(bf[i]), un[i])un = nundel nunidx = -1for imp in un:idx += 1for __imp in imp:if __imp == 't':un[idx].remove("t")if __imp == 'y':un[idx].remove("x")del idxrepl = {"t": [str(self.aa)], "y": [str(xValue)]}is_exit = Falsetimes = -1while not is_exit:times += 1if times >= 20:print(f"\nmessage:目前已经循环求解超过20次,目前初始代入值为:y{repl['y']},t{repl['t']},\n"f"已经求解{repl},剩{4 - len(repl.values())}值不可求解,故不予求解并跳出循环")breakindx = -1for lf_ in lf:indx += 1for i in repl.keys():for j in range(2):lf_[j] = lf_[j].replace(" " + i, str(repl[i][0]))idx = -1for imp in un:idx += 1for __imp in imp:if __imp in repl.keys():un[idx].remove(__imp)del idxif un[indx] == []:del un[indx]del lf[indx]if indx == 0 or (indx - 1) >= len(lf): breakindx -= 1continuetry:ams = sympy.solve(tuple(lf_), tuple(un[indx]))if ams == []:newlf = []for i in lf_:newlf.append(i.replace(str(repl["y"]), str(repl["y"]) + "*sympy.I"))ams = sympy.solve(tuple(newlf), tuple(un[indx]))except NotImplementedError as err:print(err)no_ans()returnif isinstance(ams, (dict,)):for km, jj in ams.items():if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):if km.name in repl.keys():repl[km.name].append(str(jj))else:repl[km.name] = [str(jj)]else:ix = -1undo = 0for i in ams:ix += 1try:if isinstance(i, (sympy.core.numbers.Float,sympy.core.numbers.Integer,int, float, complex)):try:repl[tuple(un[indx])[ix]] = [str(eval(str(i)))]except IndexError:repl[tuple(un[indx])[undo]].append(str(eval(str(i))))for i in repl.keys():if i in lf_:lf_ = lf_.replace(i, str(repl[i][0]))elif isinstance(i, (tuple, list, set)):inxx = -1for jj in i:inxx += 1if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):try:repl[tuple(un[indx][inxx])[ix]] = [str(jj)]except IndexError:repl[tuple(un[indx][inxx])[undo]].append(str(jj))else:continueexcept TypeError as err:print("get:err", err)continueelse:undo = ixif len(repl.values()) >= 4: is_exit = True;breakif is_exit:solves.append(repl)if printout:print(f"\r{(iVal * (nums + 1) + XVAL + 1) / ((nums + 1) * (tnums + 1)) * 100}%", end="",flush=True)if printout:print("\nsucceed in Calculating Values...")num = 0for i in solves:if printout:print(f"\r{(num + 1) / len(solves) * 100}%", end="", flush=True)num += 1x_ = []for j in i["x"]:x_.append(eval(j))x.append(x_)y_ = []for j in i["y"]:y_.append(eval(j))y.append(y_)z_ = []for j in i["z"]:z_.append(eval(j))z.append(z_)t_ = []for j in i["t"]:t_.append(eval(j))t.append(t_)del x_, y_, z_, t_, solvesif printout:print("\nsucceed in Saving Values...")def _write_t_z(self, formula, tmin_=-10, tmax_=10, tnums=100,min_=-10, max_=10, nums=100, printout=True):global x, y, z, tself.step = (tmax_ - tmin_) / tnumsself.aa = tmin_ - self.stepself.xstep = (max_ - min_) / numsr"""print(un)print(lf,toLeg(formula),sep="\n\n",end="\n\n")"""solves = []for iVal in range(tnums + 1):self.aa = self.aa + self.stepself.aa = float("%.10f" % self.aa)xValue = http://www.kingceram.com/post/min_ - self.xstepfor XVAL in range(nums + 1):lf, un = legalSet(toLeg(formula))xValue += self.xstepxValue = float("%.10f" % xValue)def key(item):if "t" in item[0] or "z" in item[0]:return 1else:return 999bf = dict(enumerate(lf))lf = sorted(lf, key=key)nun = []for i in range(len(lf)):nun.insert(lf.index(bf[i]), un[i])un = nundel nunidx = -1for imp in un:idx += 1for __imp in imp:if __imp == 't':un[idx].remove("t")if __imp == 'z':un[idx].remove("z")del idxrepl = {"t": [str(self.aa)], "z": [str(xValue)]}is_exit = Falsetimes = -1while not is_exit:times += 1if times >= 20:print(f"\nmessage:目前已经循环求解超过20次,目前初始代入值为:z{repl['z']},t{repl['t']},\n"f"已经求解{repl},剩{4 - len(repl.values())}值不可求解,故不予求解并跳出循环")breakindx = -1for lf_ in lf:indx += 1for i in repl.keys():for j in range(2):lf_[j] = lf_[j].replace(" " + i, str(repl[i][0]))idx = -1for imp in un:idx += 1for __imp in imp:if __imp in repl.keys():un[idx].remove(__imp)del idxif un[indx] == []:del un[indx]del lf[indx]if indx == 0 or (indx - 1) >= len(lf): breakindx -= 1continuetry:ams = sympy.solve(tuple(lf_), tuple(un[indx]))if ams == []:newlf = []for i in lf_:newlf.append(i.replace(str(repl["z"]), str(repl["z"]) + "*sympy.I"))ams = sympy.solve(tuple(newlf), tuple(un[indx]))except NotImplementedError as err:print(err)no_ans()returnif isinstance(ams, (dict,)):for km, jj in ams.items():if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):if km.name in repl.keys():repl[km.name].append(str(jj))else:repl[km.name] = [str(jj)]else:ix = -1undo = 0for i in ams:ix += 1try:if isinstance(i, (sympy.core.numbers.Float,sympy.core.numbers.Integer,int, float, complex)):try:repl[tuple(un[indx])[ix]] = [str(eval(str(i)))]except IndexError:repl[tuple(un[indx])[undo]].append(str(eval(str(i))))for i in repl.keys():if i in lf_:lf_ = lf_.replace(i, str(repl[i][0]))elif isinstance(i, (tuple, list, set)):inxx = -1for jj in i:inxx += 1if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):try:repl[tuple(un[indx][inxx])[ix]] = [str(jj)]except IndexError:repl[tuple(un[indx][inxx])[undo]].append(str(jj))else:continueexcept TypeError as err:print("get:err", err)continueelse:undo = ixif len(repl.values()) >= 4: is_exit = True;breakif is_exit:solves.append(repl)if printout:print(f"\r{(iVal * (nums + 1) + XVAL + 1) / ((nums + 1) * (tnums + 1)) * 100}%", end="",flush=True)if printout:print("\nsucceed in Calculating Values...")num = 0for i in solves:if printout:print(f"\r{(num + 1) / len(solves) * 100}%", end="", flush=True)num += 1x_ = []for j in i["x"]:x_.append(eval(j))x.append(x_)y_ = []for j in i["y"]:y_.append(eval(j))y.append(y_)z_ = []for j in i["z"]:z_.append(eval(j))z.append(z_)t_ = []for j in i["t"]:t_.append(eval(j))t.append(t_)del x_, y_, z_, t_, solvesif printout:print("\nsucceed in Saving Values...")def _write_t(self, formula, tmin_=-10, tmax_=10, tnums=100, printout=True):global x, y, z, tI = 1joo = infself.step = (tmax_ - tmin_) / tnumsself.aa = tmin_ - self.stepr"""print(un)print(lf,toLeg(formula),sep="\n\n",end="\n\n")"""solves = []for iVal in range(tnums + 1):self.aa = self.aa + self.stepself.aa = float("%.10f" % self.aa)lf, un = legalSet(toLeg(formula))def key(item):if "t" in item[0]:return 1else:return 999bf = dict(enumerate(lf))lf = sorted(lf, key=key)nun = []for i in range(len(lf)):nun.insert(lf.index(bf[i]), un[i])un = nundel nunidx = -1for imp in un:idx += 1for __imp in imp:if __imp == 't':un[idx].remove("t")del idxrepl = {"t": [str(self.aa)]}is_exit = Falsetimes = -1while not is_exit:times += 1if times >= 20:print(f"\nmessage:目前已经循环求解超过20次,目前初始代入值为:t{repl['t']},\n"f"已经求解{repl},剩{4 - len(repl.values())}值不可求解,故不予求解并跳出循环")breakindx = -1for lf_ in lf:indx += 1for i in repl.keys():for j in range(2):if str(i) in str(lf_[j]):lf_[j] = lf_[j].replace(i, str(repl[i][0]))idx = -1for imp in un:idx += 1for __imp in imp:if __imp in repl.keys():un[idx].remove(__imp)del idxif un[indx] == []:del un[indx]del lf[indx]if indx == 0 or (indx - 1) >= len(lf): breakindx -= 1continuetry:ams = sympy.solve(tuple(lf_), tuple(un[indx]))except NotImplementedError as err:print(err)no_ans()returnif isinstance(ams, (dict,)):for km, jj in ams.items():if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):if km.name in repl.keys():repl[km.name].append(str(jj))else:repl[km.name] = [str(jj)]else:ix = -1undo = 0for i in ams:ix += 1try:if isinstance(i, (sympy.core.numbers.Float,sympy.core.numbers.Integer,int, float, complex)):try:repl[tuple(un[indx])[ix]] = [str(eval(str(i)))]except IndexError:repl[tuple(un[indx])[undo]].append(str(eval(str(i))))for i in repl.keys():if i in lf_:lf_ = lf_.replace(i, str(repl[i][0]))elif isinstance(i, (tuple, list, set)):inxx = -1for jj in i:inxx += 1if not ('x' in str(jj) or'y' in str(jj) or'z' in str(jj) or't' in str(jj)):try:repl[tuple(un[indx][inxx])[ix]] = [str(jj)]except IndexError:repl[tuple(un[indx][inxx])[undo]].append(str(jj))else:continueexcept TypeError as err:print("get:err", err)continueelse:undo = ixif len(repl.values()) >= 4: is_exit = True;breakif is_exit:solves.append(repl)if printout:print(f"\r{(iVal + 1) / (tnums + 1) * 100}%", end="",flush=True)if printout:print("\nsucceed in Calculating Values...")num = 0for i in solves:if printout:print(f"\r{(num + 1) / len(solves) * 100}%", end="", flush=True)num += 1x_ = []for j in i["x"]:x_.append(eval(j))x.append(x_)y_ = []for j in i["y"]:y_.append(eval(j))y.append(y_)z_ = []for j in i["z"]:z_.append(eval(j))z.append(z_)t_ = []for j in i["t"]:t_.append(eval(j))t.append(t_)del x_, y_, z_, t_, solvesif printout:print("\nsucceed in Saving Values...")def format():global x,y,z,tnx={}ny={}nz={}nt=[]vx=[]vy=[]vz=[]for i,j,k,v in zip(x,y,z,t):for it,jt,kt,vt in zip(i,j,k,v):if vt not in nx.keys():nx[vt]=[]if vt not in ny.keys():ny[vt]=[]if vt not in nz.keys():nz[vt]=[]nx[vt].append(it)ny[vt].append(jt)nz[vt].append(kt)nt.append(vt)nt.sort()for num in set(nt):for i,j,k in zip(nx[num],ny[num],nz[num]):vx.append(i)vy.append(j)vz.append(k)x=vxy=vyz=vzt=ntdef create_img(tnum, fig=None, ax=None):global x, y, z, tif fig is None:fig = plt.figure()if ax is None:ax = Axes3D(fig)ax.legend(loc='best')ax.axis('off')if isinstance(tnum, list):tnum = tnum[0]ax.cla()x1 = []y1 = []z1 = []try:i = t.index(tnum)except (IndexError, ValueError) as err:i = 0try:while t[i] == tnum:if isinstance(x[i], (tuple, list, set)):x[i] = x[i][0]if isinstance(x[i], complex):i += 1continueif isinstance(y[i], (tuple, list, set)):y[i] = y[i][0]if isinstance(y[i], complex):i += 1continueif isinstance(z[i], (tuple, list, set)):z[i] = z[i][0]if isinstance(z[i], complex):i += 1continuex1.append(x[i])y1.append(y[i])z1.append(z[i])i += 1except IndexError:passscatter = ax.scatter(x1, y1, z1, c='r')return scatter,# plt.savefig('./1.jpg',)def Graphic_main(formula, modefunc, tmin_=-10, tmax_=10, tnums=100,min_=-10, max_=10, nums=100, printout=True):global x, y, z, ttry:modefunc(formula=formula, tmin_=tmin_, tmax_=tmax_, tnums=tnums,min_=min_, max_=max_, nums=nums, printout=printout)except Exception:modefunc(formula=formula, tmin_=tmin_, tmax_=tmax_, tnums=tnums, printout=printout)format()fig = plt.figure()ax = Axes3D(fig, auto_add_to_figure=False)print(min(x),min(y),min(z),"\n",max(x),max(y),max(z))ax.set_ylim(min(y),max(y))ax.set_xlim(xmin=min(x), xmax=max(x))ax.set_zlim(zmin=min(z), zmax=max(z))fig.add_axes(ax)# ax.legend(loc="best")def demo(num):a = tmin_ + ((tmax_ - tmin_) / tnums) * (num % tnums)print(f"\rwait:{num-tnums}/{tnums}",end="")return create_img(a, fig, ax),ani = animation.FuncAnimation(fig, demo, fargs=[],interval=int(((tmax_ - tmin_) / tnums) * 1000))ani.save('test.mp4')# 调试部分print("program running")"""write()._write_t_x("z=t\n3x+y=z\nx=sin(y)",tnums=100,printout=True)print(t)create_img([0.0])"""Graphic_main("sin(x)=y\nz=t\ncos(y)=z", modefunc=write()._write_t_x, tnums=10, nums=10)print("end...")
- 一 Python与设计模式——Abstract Factory
- python爬取学校新闻_python爬虫获取校园网新闻
- 使用python的Selenium启动chromedriver,无界面浏览器
- Python3趣味系列题9-------一笔画完
- 使用Python做一张满足基本样式要求的Excel表格
- 使用OpenCV和Python构建自己的车辆检测模型!
- python datetime.datetime、datetime
- chatgpt赋能python:Python编程:求解二元二次方程的简单方法
- 2023年最新的Python操控微信教程
- Python 获取 网易云音乐热门评论,轻松搞定!