Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/gc/test_LB_mandel.lua
2725 views
1
--[[
2
MIT License
3
4
Copyright (c) 2017 Gabriel de Quadros Ligneul
5
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
SOFTWARE.
23
]]
24
local function prequire(name) local success, result = pcall(require, name); return success and result end
25
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
26
27
function test()
28
29
local Complex={type="package"}
30
31
local function complex(x,y)
32
return setmetatable({ re=x, im=y }, Complex.metatable)
33
end
34
35
function Complex.conj(x,y)
36
return complex(x.re,-x.im)
37
end
38
39
function Complex.norm2(x)
40
local n=Complex.mul(x,Complex.conj(x))
41
return n.re
42
end
43
44
function Complex.abs(x)
45
return sqrt(Complex.norm2(x))
46
end
47
48
function Complex.add(x,y)
49
return complex(x.re+y.re,x.im+y.im)
50
end
51
52
function Complex.mul(x,y)
53
return complex(x.re*y.re-x.im*y.im,x.re*y.im+x.im*y.re)
54
end
55
56
Complex.metatable={
57
__add = Complex.add,
58
__mul = Complex.mul,
59
}
60
61
local function abs(x)
62
return math.sqrt(Complex.norm2(x))
63
end
64
65
xmin=-2.0 xmax=2.0 ymin=-2.0 ymax=2.0
66
N=(arg and arg[1]) or 64
67
68
function level(x,y)
69
local c=complex(x,y)
70
local l=0
71
local z=c
72
repeat
73
z=z*z+c
74
l=l+1
75
until abs(z)>2.0 or l>255
76
return l-1
77
end
78
79
dx=(xmax-xmin)/N
80
dy=(ymax-ymin)/N
81
82
print("P2")
83
print("# mandelbrot set",xmin,xmax,ymin,ymax,N)
84
print(N,N,255)
85
local S = 0
86
for i=1,N do
87
local x=xmin+(i-1)*dx
88
for j=1,N do
89
local y=ymin+(j-1)*dy
90
S = S + level(x,y)
91
end
92
-- if i % 10 == 0 then print(collectgarbage("count")) end
93
end
94
print(S)
95
96
end
97
98
bench.runCode(test, "mandel")
99
100