我可以在一行中定义一个规则折线函数吗?(1)y=x,y=0
Can I define a regular polyline function in one line?(1)y=x,y=0
我可以在一行中定义一个线函数吗,因为它是正则的?
图形橙色线:myPoly
条件:
图像在 Y=0 和 Y=X 之间摆动。
直线的斜率为 2 或 -2。
你可以从 (2,0) 开始。
我要
①是否可以在一行中定义以下部分 def myPolyY(Ho,myX): ?
②能分段做还是串联做?
from sympy import *
var('x y')
import matplotlib.pyplot as plt
def myPolyY(Ho,myX):
myY=0
xx=[x[0] for x in Ho]
yy=[x[1] for x in Ho]
for i in range(len(xx)-1):
if xx[i] <= myX & myX<= xx[i+1]:
myY= (yy[i+1]-yy[i])/(xx[i+1]-xx[i])* (myX - xx[i])+yy[i]
return myY
def myPolyDef(nMax):
myPoly=[[0,0],[2,0]]
i=2
ans = solve([y - x,
y - myPoly[i-1][1] - 2 * (x - myPoly[i-1][0])], [x, y])
myPoly=myPoly+[[ans[x], ans[y]]]
i=1
for j in range(nMax):
i=i+1
ans=solve([y-0,y-myPoly[i][1]+2*(x-myPoly[i][0])],[x,y])
myPoly=myPoly+[[ans[x],ans[y]]]
i=i+1
ans=solve([y-x,
y-myPoly[i ][1]-2*(x-myPoly[i ][0])], [x,y])
myPoly=myPoly+[[ans[x],ans[y]]]
return myPoly
def myPolyPlot(Ho,myLabel,myLinestyle):
plt.plot([x[0] for x in Ho],[x[1] for x in Ho], mec='none', ms=4, lw=1, label=myLabel,linestyle=myLinestyle)
for i in range(len(Ho)):
if i % 2==0:
myPos='right'
else:
myPos='left'
plt.text(Ho[i][0], Ho[i][1], '({x}, {y})'.format(x=Ho[i][0], y=Ho[i][1]),
fontsize=6, horizontalalignment=myPos)
return
def main():
# myN=4
myN=2
myX=30
myPoly=myPolyDef(myN)
myY=myPolyY(myPoly,30)
# matplotlib
myH=max(list(map(lambda x: max(x), myPoly)))
plt.axes().set_aspect('equal')
plt.text(myX,myY, '({x}, {y})'.format(x=myX, y=myY),
fontsize=6, horizontalalignment='right')
myPolyPlot([[myH,myH],[0,0]],'Y=X','--')
myPolyPlot(myPoly,'myPoly','-')
myPolyPlot([[myH, 0],[0,0]],'Y=0','--')
plt.legend(frameon=False, fontsize=10, numpoints=1, loc='upper left')
plt.savefig('myPoly.png', dpi=200)
plt.show()
if __name__ == '__main__':
main()
(2022-03-03)‖我用的是log.
from sympy import *
import matplotlib.pyplot as plt
var('x y')
def myCal_PolyTopBottom(myT):
myPolyTB = [[0, 0], [2, 0]]
for x in range(myT):
xt = 4*(3**x)
yt = 4*pow(3, int(log(xt // 2, 3)))
myPolyTB = myPolyTB + [[xt , yt ]]
myPolyTB = myPolyTB + [[xt + yt * 0.5, 0.0]]
return myPolyTB
def myCal_PolyXYN(myPolyTB,myXmax):
myPolyXYN = [[0, 0], [2, 0]]
for x in range(3, int(myXmax)+1):
y=myCal_PolyXYi(myPolyTB,x)
myPolyXYN = myPolyXYN + [[float(x), y]]
return myPolyXYN
def myCal_PolyXYi(Ho,myX):
xx=[x[0] for x in Ho]
yy=[x[1] for x in Ho]
for i in range(len(xx)-1):
if xx[i] <= myX & myX<= xx[i+1]:
myY= (yy[i+1]-yy[i])/(xx[i+1]-xx[i])* (myX - xx[i])+yy[i]
return myY
def myPlot_Poly(Ho,myLabel,myLinestyle):
plt.plot([x[0] for x in Ho],[x[1] for x in Ho],
mec='none', ms=4, lw=1, label=myLabel,linestyle=myLinestyle)
return
def myPlot_Text(Ho,myPos):
for i in range(len(Ho)):
plt.text(Ho[i][0], Ho[i][1], '({x}, {y})'.format(x=Ho[i][0], y=Ho[i][1]),
fontsize=6, horizontalalignment=myPos)
return
def main():
myT=3
myPolyTB=myCal_PolyTopBottom(myT)
myH=max(list(map(lambda x: max(x),myPolyTB)))
myPolyN=myCal_PolyXYN(myPolyTB,myH)
# matplotlib
plt.axes().set_aspect('equal')
myPlot_Text(myPolyTB,'right' )
myPlot_Text(myPolyN ,'left' )
myPlot_Poly([[myH,myH],[0,0]],'Y=X' ,'--')
myPlot_Poly( myPolyTB ,'myPolyTB','-' )
myPlot_Poly([[myH, 0],[0,0]],'Y=0' ,'--')
plt.legend (frameon=False, fontsize=10, numpoints=1, loc='upper left')
# plt.savefig('myPoly.png', dpi=200)
plt.show()
if __name__ == '__main__':
main()
(20220322)OEIS
OEIS:0 2 4 6 12 18
https://oeis.org/search?q=0+2++4++6+12+18+&sort=&language=&go=Search
0 2 4 6 12 18 36
https://oeis.org/search?q=0+2++4++6+12+18+36&sort=&language=&go=Search
对不起......
如果你看第一个三角形,很容易看出方程可以表示为
y = 4 - 2 * abs(4-x)
更一般地,每个三角形都有一个顶值,对应的方程为
y = top - 2 * abs(top-x)
然后,对于每个坐标x
,我们要确定对应的top
的值
top = 4 * pow(3, int(log(x//2,3)))
from math import *
myPoly=[[0,0],[2,0]]
for x in range(3, 37):
val = 4 * pow(3, int(log(x//2,3)))
y = val - 2 * abs(x - val)
myPoly = myPoly+[[x,y]]
我可以在一行中定义一个线函数吗,因为它是正则的?
图形橙色线:myPoly
条件:
图像在 Y=0 和 Y=X 之间摆动。
直线的斜率为 2 或 -2。
你可以从 (2,0) 开始。
我要
①是否可以在一行中定义以下部分 def myPolyY(Ho,myX): ?
②能分段做还是串联做?
from sympy import *
var('x y')
import matplotlib.pyplot as plt
def myPolyY(Ho,myX):
myY=0
xx=[x[0] for x in Ho]
yy=[x[1] for x in Ho]
for i in range(len(xx)-1):
if xx[i] <= myX & myX<= xx[i+1]:
myY= (yy[i+1]-yy[i])/(xx[i+1]-xx[i])* (myX - xx[i])+yy[i]
return myY
def myPolyDef(nMax):
myPoly=[[0,0],[2,0]]
i=2
ans = solve([y - x,
y - myPoly[i-1][1] - 2 * (x - myPoly[i-1][0])], [x, y])
myPoly=myPoly+[[ans[x], ans[y]]]
i=1
for j in range(nMax):
i=i+1
ans=solve([y-0,y-myPoly[i][1]+2*(x-myPoly[i][0])],[x,y])
myPoly=myPoly+[[ans[x],ans[y]]]
i=i+1
ans=solve([y-x,
y-myPoly[i ][1]-2*(x-myPoly[i ][0])], [x,y])
myPoly=myPoly+[[ans[x],ans[y]]]
return myPoly
def myPolyPlot(Ho,myLabel,myLinestyle):
plt.plot([x[0] for x in Ho],[x[1] for x in Ho], mec='none', ms=4, lw=1, label=myLabel,linestyle=myLinestyle)
for i in range(len(Ho)):
if i % 2==0:
myPos='right'
else:
myPos='left'
plt.text(Ho[i][0], Ho[i][1], '({x}, {y})'.format(x=Ho[i][0], y=Ho[i][1]),
fontsize=6, horizontalalignment=myPos)
return
def main():
# myN=4
myN=2
myX=30
myPoly=myPolyDef(myN)
myY=myPolyY(myPoly,30)
# matplotlib
myH=max(list(map(lambda x: max(x), myPoly)))
plt.axes().set_aspect('equal')
plt.text(myX,myY, '({x}, {y})'.format(x=myX, y=myY),
fontsize=6, horizontalalignment='right')
myPolyPlot([[myH,myH],[0,0]],'Y=X','--')
myPolyPlot(myPoly,'myPoly','-')
myPolyPlot([[myH, 0],[0,0]],'Y=0','--')
plt.legend(frameon=False, fontsize=10, numpoints=1, loc='upper left')
plt.savefig('myPoly.png', dpi=200)
plt.show()
if __name__ == '__main__':
main()
(2022-03-03)‖我用的是log.
from sympy import *
import matplotlib.pyplot as plt
var('x y')
def myCal_PolyTopBottom(myT):
myPolyTB = [[0, 0], [2, 0]]
for x in range(myT):
xt = 4*(3**x)
yt = 4*pow(3, int(log(xt // 2, 3)))
myPolyTB = myPolyTB + [[xt , yt ]]
myPolyTB = myPolyTB + [[xt + yt * 0.5, 0.0]]
return myPolyTB
def myCal_PolyXYN(myPolyTB,myXmax):
myPolyXYN = [[0, 0], [2, 0]]
for x in range(3, int(myXmax)+1):
y=myCal_PolyXYi(myPolyTB,x)
myPolyXYN = myPolyXYN + [[float(x), y]]
return myPolyXYN
def myCal_PolyXYi(Ho,myX):
xx=[x[0] for x in Ho]
yy=[x[1] for x in Ho]
for i in range(len(xx)-1):
if xx[i] <= myX & myX<= xx[i+1]:
myY= (yy[i+1]-yy[i])/(xx[i+1]-xx[i])* (myX - xx[i])+yy[i]
return myY
def myPlot_Poly(Ho,myLabel,myLinestyle):
plt.plot([x[0] for x in Ho],[x[1] for x in Ho],
mec='none', ms=4, lw=1, label=myLabel,linestyle=myLinestyle)
return
def myPlot_Text(Ho,myPos):
for i in range(len(Ho)):
plt.text(Ho[i][0], Ho[i][1], '({x}, {y})'.format(x=Ho[i][0], y=Ho[i][1]),
fontsize=6, horizontalalignment=myPos)
return
def main():
myT=3
myPolyTB=myCal_PolyTopBottom(myT)
myH=max(list(map(lambda x: max(x),myPolyTB)))
myPolyN=myCal_PolyXYN(myPolyTB,myH)
# matplotlib
plt.axes().set_aspect('equal')
myPlot_Text(myPolyTB,'right' )
myPlot_Text(myPolyN ,'left' )
myPlot_Poly([[myH,myH],[0,0]],'Y=X' ,'--')
myPlot_Poly( myPolyTB ,'myPolyTB','-' )
myPlot_Poly([[myH, 0],[0,0]],'Y=0' ,'--')
plt.legend (frameon=False, fontsize=10, numpoints=1, loc='upper left')
# plt.savefig('myPoly.png', dpi=200)
plt.show()
if __name__ == '__main__':
main()
(20220322)OEIS
OEIS:0 2 4 6 12 18
https://oeis.org/search?q=0+2++4++6+12+18+&sort=&language=&go=Search
0 2 4 6 12 18 36
https://oeis.org/search?q=0+2++4++6+12+18+36&sort=&language=&go=Search
对不起......
如果你看第一个三角形,很容易看出方程可以表示为
y = 4 - 2 * abs(4-x)
更一般地,每个三角形都有一个顶值,对应的方程为
y = top - 2 * abs(top-x)
然后,对于每个坐标x
,我们要确定对应的top
top = 4 * pow(3, int(log(x//2,3)))
from math import *
myPoly=[[0,0],[2,0]]
for x in range(3, 37):
val = 4 * pow(3, int(log(x//2,3)))
y = val - 2 * abs(x - val)
myPoly = myPoly+[[x,y]]