File:Lua Gamma Function in Chinese Wiki.svg

原始檔案 (SVG 檔案,表面大小:539 × 482 像素,檔案大小:10 KB)


摘要

描述
中文(臺灣):w:zh:Module:Complex Number/Functions中Gamma Function的定義方式
  • 共分成4個部分
    • 中間藍色部分是利用從零展開w:Reciprocal gamma function的泰勒級數定義
      展開至前30項
      [1]
    • 兩側橘紅色部分是利用中間藍色代Gamma Function的recurrence relation定義用For迴圈實作
    • 上下的綠色部分則是使用Robert H. Windschitl (2002) 所提出的公式近似
      [2]
    • 最後黃色部分則是使用帶有斯特靈級數的斯特靈公式近似
      [3]
      展開至前16項 ( 來源 : https://oeis.org/A001163 , https://oeis.org/A001164 )
    • 而背景透明標記 (灰白相間) 部分則為超出福點數可儲存範圍,會出現inf或nan
    • 最左邊土黃色則是可能出現低於設計的精確度小數12位而回傳0
日期
來源 自己的作品
作者 A2569875

授權條款

我,本作品的著作權持有者,決定用以下授權條款發佈本作品:
w:zh:創用CC
姓名標示 相同方式分享
您可以自由:
  • 分享 – 複製、發佈和傳播本作品
  • 重新修改 – 創作演繹作品
惟需遵照下列條件:
  • 姓名標示 – 您必須指名出正確的製作者,和提供授權條款的連結,以及表示是否有對內容上做出變更。您可以用任何合理的方式來行動,但不得以任何方式表明授權條款是對您許可或是由您所使用。
  • 相同方式分享 – 如果您利用本素材進行再混合、轉換或創作,您必須基於如同原先的相同或兼容的條款,來分布您的貢獻成品。

code

local Reciprocal_gamma_coeff = {1,0.577215664901532860607,-0.655878071520253881077,-0.0420026350340952355290,0.166538611382291489502,-0.0421977345555443367482,-0.00962197152787697356211,0.00721894324666309954240,-0.00116516759185906511211,-0.000215241674114950972816,0.000128050282388116186153,-0.0000201348547807882386557,-1.25049348214267065735e-6,1.13302723198169588237e-6,-2.05633841697760710345e-7,6.11609510448141581786e-9,5.00200764446922293006e-9,-1.18127457048702014459e-9,1.04342671169110051049e-10,7.78226343990507125405e-12,-3.69680561864220570819e-12,5.10037028745447597902e-13,-2.05832605356650678322e-14,-5.34812253942301798237e-15,1.22677862823826079016e-15,-1.18125930169745876951e-16,1.18669225475160033258e-18,1.41238065531803178156e-18,-2.29874568443537020659e-19,1.71440632192733743338e-20}
--https://oeis.org/A001163 、 https://oeis.org/A001164
local stirling_series_coeff = {1,0.0833333333333333333333333,0.00347222222222222222222222,-0.00268132716049382716049383,-0.000229472093621399176954733,0.000784039221720066627474035,0.0000697281375836585777429399,-0.000592166437353693882864836,-0.0000517179090826059219337058,0.000839498720672087279993358,0.0000720489541602001055908572,-0.00191443849856547752650090,-0.000162516262783915816898635,0.00640336283380806979482364,0.000540164767892604515180468,-0.0295278809456991205054407,-0.00248174360026499773091566,0.179540117061234856107699,0.0150561130400264244123842,-1.39180109326533748139915,-0.116546276599463200850734}
function p._gamma_high_imag(cal_z)
	local z = to_number(cal_z)
	if z ~= nil and math_lib.abs(math_lib.nonRealPart(z)) > 1 then
		local inv_z = math_lib.inverse(z)
		return math_lib.sqrt((math_lib.pi * 2) * inv_z) * math_lib.pow(z * math_lib.exp(-1) *
			math_lib.sqrt( (z * math_lib.sinh(inv_z) ) + math_lib.inverse(to_number(810) * z * z * z * z * z * z) ),z)
	end
	return nil
end
function p._gamma_morethen_lua_int(cal_z)
	local z = to_number(cal_z) - to_number(1)
	local lua_int_term = 18.1169 --FindRoot[ Factorial[ x ] == 2 ^ 53, {x, 20} ]
	if math_lib.abs(z) > (lua_int_term - 1) or (math_lib.re(z) < 0 and math_lib.abs(math_lib.nonRealPart(z)) > 1 ) then
		local sum = 1
		for i = 1, #stirling_series_coeff - 1 do
			local a, n = to_number(z), tonumber(i) local y, k, f = to_number(1), n, to_number(a)
			while k ~= 0 do 
				if k % 2 == 1 then y = y * f end 
				k = math.floor(k / 2); f = f * f
			end
			sum = sum + stirling_series_coeff[i + 1] * math_lib.inverse(y)
		end
		return math_lib.sqrt( (2 * math.pi) * z ) * math_lib.pow( z * math.exp(-1), z ) * sum
	end
	return nil
end
function p._gamma_abs_less1(cal_z)
	local z = to_number(cal_z)
	if math_lib.abs(z) <=1.001 then
		if math_lib.abs(math_lib.nonRealPart(z)) < 1e-14 and ( (math.abs(math_lib.re(z) - 1) < 1e-14) or (math.abs(math_lib.re(z) - 2) < 1e-14) ) then return to_number(1)end
		return math_lib.inverse(p._recigamma_abs_less1(z))
	end
	return nil
end
function p._recigamma_abs_less1(z)
	local result = to_number(0)
	for i=1,#Reciprocal_gamma_coeff do
		result = result + Reciprocal_gamma_coeff[i] * math_lib.pow(z,i)
	end
	return result
end
function p._gamma(cal_z)
	local z = to_number(cal_z)
	if math_lib.abs(math_lib.nonRealPart(z)) < 1e-14 and ((math_lib.re(z) < 0 or math.abs(math_lib.re(z)) < 1e-14)
		and math.abs(math.floor(math_lib.re(z)) - math_lib.re(z)) < 1e-14 ) then return tonumber("nan") end
	local pre_result = p._gamma_morethen_lua_int(z) or p._gamma_high_imag(z) or p._gamma_abs_less1(z)
	if pre_result then return pre_result end
	local real_check = math_lib.re(z)
	local loop_count = math.floor(real_check)
	local start_number, zero_flag = z - loop_count, false
	if math_lib.abs(start_number) <= 1e-14 then start_number = to_number(1);zero_flag = true end
	local result = math_lib.inverse(p._recigamma_abs_less1(start_number))
	if math_lib.abs(math_lib.nonRealPart(z)) < 1e-14 and ((math_lib.re(z) > 1e-14 )and math.abs(math.floor(math_lib.re(z)) - math_lib.re(z)) < 1e-14 ) then result = to_number(1)  end
	local j = to_number(start_number)
	for i=1,math.abs(loop_count) do
		if loop_count > 0 then result = result * j else result = result * math_lib.inverse(j-1) end
		if zero_flag==true and loop_count > 0 then zero_flag=false else if loop_count > 0 then j = j + 1 else j = j - 1 end end
	end
	if math_lib.abs(math_lib.nonRealPart(z)) < 1e-14 and ((math_lib.re(z) > 1e-14 )and math.abs(math.floor(math_lib.re(z)) - math_lib.re(z)) < 1e-14 ) then return math_lib.floor(result) end
	return result
end

Reference

  1. Wrench, J.W. (1968). Concerning two series for the gamma function. Mathematics of Computation, 22, 617–626. and
    Wrench, J.W. (1973). Erratum: Concerning two series for the gamma function. Mathematics of Computation, 27, 681–682.
  2. Viktor T. Toth (2006). "Programmable Calculators: Calculators and the Gamma Function". Archived from the original on 2007-02-23.
  3. NIST Digital Library of Mathematical Functions.

說明

添加單行說明來描述出檔案所代表的內容

在此檔案描寫的項目

描繪內容

檔案來源 Chinese (Taiwan) (已轉換拼寫)

image/svg+xml

檔案歷史

點選日期/時間以檢視該時間的檔案版本。

日期/時間縮⁠圖尺寸使用者備⁠註
目前2018年11月18日 (日) 17:51於 2018年11月18日 (日) 17:51 版本的縮圖539 × 482(10 KB)A2569875User created page with UploadWizard

詮釋資料