gopl-zh.github.com/ch3/ch3-03.md

97 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## 3.3. 复数
Go语言提供了两种精度的复数类型complex64和complex128分别对应float32和float64两种浮点数精度。内置的complex函数用于构建复数内建的real和imag函数分别返回复数的实部和虚部
```Go
var x complex128 = complex(1, 2) // 1+2i
var y complex128 = complex(3, 4) // 3+4i
fmt.Println(x*y) // "(-5+10i)"
fmt.Println(real(x*y)) // "-5"
fmt.Println(imag(x*y)) // "10"
```
如果一个浮点数面值或一个十进制整数面值后面跟着一个i例如3.141592i或2i它将构成一个复数的虚部复数的实部是0
```Go
fmt.Println(1i * 1i) // "(-1+0i)", i^2 = -1
```
在常量算术规则下一个复数常量可以加到另一个普通数值常量整数或浮点数、实部或虚部我们可以用自然的方式书写复数就像1+2i或与之等价的写法2i+1。上面x和y的声明语句还可以简化
```Go
x := 1 + 2i
y := 3 + 4i
```
复数也可以用==和!=进行相等比较。只有两个复数的实部和虚部都相等的时候它们才是相等的(译注:浮点数的相等比较是危险的,需要特别小心处理精度问题)。
math/cmplx包提供了复数处理的许多函数例如求复数的平方根函数和求幂函数。
```Go
fmt.Println(cmplx.Sqrt(-1)) // "(0+1i)"
```
下面的程序使用complex128复数算法来生成一个Mandelbrot图像。
<u><i>gopl.io/ch3/mandelbrot</i></u>
```Go
// Mandelbrot emits a PNG image of the Mandelbrot fractal.
package main
import (
"image"
"image/color"
"image/png"
"math/cmplx"
"os"
)
func main() {
const (
xmin, ymin, xmax, ymax = -2, -2, +2, +2
width, height = 1024, 1024
)
img := image.NewRGBA(image.Rect(0, 0, width, height))
for py := 0; py < height; py++ {
y := float64(py)/height*(ymax-ymin) + ymin
for px := 0; px < width; px++ {
x := float64(px)/width*(xmax-xmin) + xmin
z := complex(x, y)
// Image point (px, py) represents complex value z.
img.Set(px, py, mandelbrot(z))
}
}
png.Encode(os.Stdout, img) // NOTE: ignoring errors
}
func mandelbrot(z complex128) color.Color {
const iterations = 200
const contrast = 15
var v complex128
for n := uint8(0); n < iterations; n++ {
v = v*v + z
if cmplx.Abs(v) > 2 {
return color.Gray{255 - contrast*n}
}
}
return color.Black
}
```
用于遍历1024x1024图像每个点的两个嵌套的循环对应-2到+2区间的复数平面。程序反复测试每个点对应复数值平方值加一个增量值对应的点是否超出半径为2的圆。如果超过了通过根据预设置的逃逸迭代次数对应的灰度颜色来代替。如果不是那么该点属于Mandelbrot集合使用黑色颜色标记。最终程序将生成的PNG格式分形图像输出到标准输出如图3.3所示。
![](../images/ch3-03.png)
**练习 3.5** 实现一个彩色的Mandelbrot图像使用image.NewRGBA创建图像使用color.RGBA或color.YCbCr生成颜色。
**练习 3.6** 升采样技术可以降低每个像素对计算颜色值和平均值的影响。简单的方法是将每个像素分成四个子像素,实现它。
**练习 3.7** 另一个生成分形图像的方式是使用牛顿法来求解一个复数方程,例如$z^4-1=0$。每个起点到四个根的迭代次数对应阴影的灰度。方程根对应的点用颜色表示。
**练习 3.8** 通过提高精度来生成更多级别的分形。使用四种不同精度类型的数字实现相同的分形complex64、complex128、big.Float和big.Rat。后面两种类型在math/big包声明。Float是有指定限精度的浮点数Rat是无限精度的有理数。它们间的性能和内存使用对比如何当渲染图可见时缩放的级别是多少
**练习 3.9** 编写一个web服务器用于给客户端生成分形的图像。运行客户端通过HTTP参数指定x、y和zoom参数。