作者adnodnya (N.D.)
看板Python
标题Re: [问题] power math.pow(x,y)
时间Mon Nov 8 13:43:28 2010
※ 引述《adnodnya (N.D.)》之铭言:
: 我试图想要求一个方程式的图形
: f(x)= x ^(2/3)
: 念做: x的三分之二次方 (怕有误解@@")
: 我找了一下math模组发现
: math.pow(x,y)
: y值说一定要是整数
: 所以不能用math(x,2/3)
: 而math.sqrt(x)
: 又不能开奇数方根
: 请问版上的高手们, 有没有其他可以替代的写法或模组?
: 拜托了~~~感谢~~~
以下是我修改网友的程式
不全是我写的
程式目的: 输出一个f(x)= 2x-3x^(2/3) 的图形
#========================================================
#!/usr/bin/env python
#coding=utf-8
import math
import cmath
import Image
import ImageDraw
class DrawFun():
#画布像素尺寸
width = 512
height = 512
#X取值范围
min = -10
max = 10
#X的布长值
step = 1
#X的单位值像素
unit = 1
#画板对象
canvas = None
#画笔对象
draw = None
#画布中心值偏移值
ctrx = 0
ctry = 0
def __init__(self, width, height, min, max, step, unit):
self.width = width
self.height = height
self.min = min
self.max = max
self.step = step
self.unit = unit
#创建画板对象
self.canvas = Image.new("RGB", [self.width,self.height]/
,(255,255,255))
#创建画笔对象
self.draw = ImageDraw.Draw(self.canvas)
#撷取画布中心点
self.ctrx = math.floor(self.width/2)
self.ctry = math.floor(self.height/2)
#绘制中心轴线
self.draw.line([(0,self.ctry),(self.width,self.ctry)],/
fill =(0,0,0))
self.draw.line([(self.ctrx,0),(self.ctrx,self.height)],/
fill =(0,0,0))
#显示单位格数 左右对称计算
viewx = int(math.floor((self.width/self.unit + 1)/2))
viewy = int(math.floor((self.height/self.unit + 1)/2))
#绘制单元格
for x in range(1,viewx+1):
offsetx = x * self.unit
self.draw.line([(self.ctrx + offsetx,0),(self.ctrx /
+ offsetx,self.height)], fill =(200,200,200))
self.draw.line([(self.ctrx - offsetx,0),(self.ctrx /
- offsetx,self.height)], fill =(200,200,200))
for y in range(1,viewy+1):
offsety = y * self.unit
self.draw.line([(0,self.ctry + offsety),(self.width,/
self.ctry + offsety)], fill =(200,200,200))
self.draw.line([(0,self.ctry - offsety),(self.width,/
self.ctry - offsety)], fill =(200,200,200))
def paint(self, fun, color):
#结果点集
aryPoint = []
#转换为1为布长
min = int(math.floor(self.min/self.step))
max = int(math.floor(self.max/self.step))
for i in range(min, max):
#实际函数值
x = i * self.step
y = fun(x)
#笛卡尔座标系到屏幕座标系
#比例变换
x = x * self.unit
y = y * self.unit
#座标变换
curX = self.ctrx + x
curY = self.ctry - y
#保存座标到数组
aryPoint.append((curX,curY))
#绘制函数
self.draw.point(aryPoint,fill = color)
#储存到PNG
def saveToPNG(self, path):
self.canvas.save(path,"PNG")
def test(x):
###########################################################
abc = 2*x-3*(math.pow(x,2/3))
return abc
###########################################################
if __name__ == "__main__":
#DrawFun("画布宽度","画布高度","x最小值","x最大值",/
"x步长值","x单位值像素大小")
drawfun = DrawFun(512, 512, -10, 10, 0.001, 50)
#DrawFun.paint(f(x)函数,RGB颜色)
drawfun.paint(test,(0,0,255))
#储存图片
drawfun.saveToPNG("demo.png")
#========================================================
python math的模组叙述说 math.pow(x,y)
y 只能接受整数值
所以现在卡关卡在这
不知道有什麽方法可以求的立方根
解决x的2/3次方
拜托各位高手大大了~~~
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.127.220.24
1F:→ ya790206:y也可以接受浮点数... math.pow(27,1/3.0)=>3.0 11/08 18:20
2F:→ ya790206:你的问题在於x为负数时,他无法开立方。 11/08 18:21
3F:→ ya790206:改成math.pow(x**2,1/3.0)看看 11/08 18:22
4F:推 suzuke:推楼上,这样就可以避免掉负数的问题~ 11/08 21:18
5F:→ adnodnya:哇!!!好威喔!!! 原来是这麽回事~ 感谢大大们~~~ 11/09 01:12