| 情報学部 | 菅沼ホーム | 目次 | 索引 |
/****************************/
/* ガンマ関数の計算 */
/* coded by Y.Suganuma */
/****************************/
#include <stdio.h>
#include <math.h>
double gamma(double, int *);
int main()
{
double x, y;
int ind;
printf("data? ");
scanf("%lf", &x);
y = gamma(x, &ind);
printf(" x %f gamma(x) %f ind %d\n", x, y, ind);
return 0;
}
/****************************************/
/* Γ(x)の計算(ガンマ関数,近似式) */
/* ier : =0 : normal */
/* =-1 : x=-n (n=0,1,2,・・・) */
/* return : 結果 */
/****************************************/
#include <math.h>
double gamma(double x, int *ier)
{
double err, g, s, t, v, w, y;
long k;
*ier = 0;
if (x > 5.0) {
v = 1.0 / x;
s = ((((((-0.000592166437354 * v + 0.0000697281375837) * v +
0.00078403922172) * v - 0.000229472093621) * v -
0.00268132716049) * v + 0.00347222222222) * v +
0.0833333333333) * v + 1.0;
g = 2.506628274631001 * exp(-x) * pow(x,x-0.5) * s;
}
else {
err = 1.0e-20;
w = x;
t = 1.0;
if (x < 1.5) {
if (x < err) {
k = (long)x;
y = (double)k - x;
if (fabs(y) < err || fabs(1.0-y) < err)
*ier = -1;
}
if (*ier == 0) {
while (w < 1.5) {
t /= w;
w += 1.0;
}
}
}
else {
if (w > 2.5) {
while (w > 2.5) {
w -= 1.0;
t *= w;
}
}
}
w -= 2.0;
g = (((((((0.0021385778 * w - 0.0034961289) * w +
0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w +
0.0815652323) * w + 0.411849671) * w + 0.422784604) * w +
0.999999926;
g *= t;
}
return g;
}
/****************************/
/* ガンマ関数の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double x, y;
int ind[] = new int [1];
System.out.print("data? ");
x = Double.parseDouble(in.readLine());
y = gamma(x, ind);
System.out.println(" x " + x + " gamma(x) " + y + " ind " + ind[0]);
}
/****************************************/
/* Γ(x)の計算(ガンマ関数,近似式) */
/* ier : =0 : normal */
/* =-1 : x=-n (n=0,1,2,・・・) */
/* return : 結果 */
/****************************************/
static double gamma(double x, int ier[])
{
double err, g, s, t, v, w, y;
int k;
ier[0] = 0;
if (x > 5.0) {
v = 1.0 / x;
s = ((((((-0.000592166437354 * v + 0.0000697281375837) * v +
0.00078403922172) * v - 0.000229472093621) * v -
0.00268132716049) * v + 0.00347222222222) * v +
0.0833333333333) * v + 1.0;
g = 2.506628274631001 * Math.exp(-x) * Math.pow(x,x-0.5) * s;
}
else {
err = 1.0e-20;
w = x;
t = 1.0;
if (x < 1.5) {
if (x < err) {
k = (int)x;
y = (double)k - x;
if (Math.abs(y) < err || Math.abs(1.0-y) < err)
ier[0] = -1;
}
if (ier[0] == 0) {
while (w < 1.5) {
t /= w;
w += 1.0;
}
}
}
else {
if (w > 2.5) {
while (w > 2.5) {
w -= 1.0;
t *= w;
}
}
}
w -= 2.0;
g = (((((((0.0021385778 * w - 0.0034961289) * w +
0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w +
0.0815652323) * w + 0.411849671) * w + 0.422784604) * w +
0.999999926;
g *= t;
}
return g;
}
}
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>ガンマ関数</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<SCRIPT TYPE="text/javascript">
function main()
{
// データの設定と出力
let x = parseFloat(document.getElementById("data").value);
let ind = new Array();
let y = gamma(x, ind);
if (ind[0] < 0)
document.getElementById("ans").value = "値を求めることができません!";
else {
let str = "gamma(" + x + ") = " + y;
document.getElementById("ans").value = str;
}
}
/****************************************/
/* Γ(x)の計算(ガンマ関数,近似式) */
/* ier : =0 : normal */
/* =-1 : x=-n (n=0,1,2,・・・) */
/* return : 結果 */
/****************************************/
function gamma(x, ier)
{
let err;
let g;
let s;
let t;
let v;
let w;
let y;
let k;
ier[0] = 0;
if (x > 5.0) {
v = 1.0 / x;
s = ((((((-0.000592166437354 * v + 0.0000697281375837) * v +
0.00078403922172) * v - 0.000229472093621) * v -
0.00268132716049) * v + 0.00347222222222) * v +
0.0833333333333) * v + 1.0;
g = 2.506628274631001 * Math.exp(-x) * Math.pow(x,x-0.5) * s;
}
else {
err = 1.0e-20;
w = x;
t = 1.0;
if (x < 1.5) {
if (x < err) {
k = Math.floor(x);
y = k - x;
if (Math.abs(y) < err || Math.abs(1.0-y) < err)
ier[0] = -1;
}
if (ier[0] == 0) {
while (w < 1.5) {
t /= w;
w += 1.0;
}
}
}
else {
if (w > 2.5) {
while (w > 2.5) {
w -= 1.0;
t *= w;
}
}
}
w -= 2.0;
g = (((((((0.0021385778 * w - 0.0034961289) * w +
0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w +
0.0815652323) * w + 0.411849671) * w + 0.422784604) * w +
0.999999926;
g *= t;
}
return g;
}
</SCRIPT>
</HEAD>
<BODY STYLE="font-size: 130%; text-align:center; background-color: #eeffee;">
<H2 STYLE="text-align:center"><B>ガンマ関数</B></H2>
データ:<INPUT ID="data" STYLE="font-size: 100%" TYPE="text" SIZE="5" VALUE="">
<BUTTON STYLE="font-size: 100%; background-color: pink" onClick="main()">OK</BUTTON><BR><BR>
結果:<INPUT ID="ans" STYLE="font-size: 100%;" TYPE="text" SIZE="30" VALUE="">
</BODY>
</HTML>
<?php
/****************************/
/* ガンマ関数の計算 */
/* coded by Y.Suganuma */
/****************************/
printf("data? ");
fscanf(STDIN, "%lf", $x);
$y = gamma($x, $ind);
printf(" x %f gamma(x) %f ind %d\n", $x, $y, $ind);
/****************************************/
/* Γ(x)の計算(ガンマ関数,近似式) */
/* ier : =0 : normal */
/* =-1 : x=-n (n=0,1,2,・・・) */
/* return : 結果 */
/****************************************/
function gamma($x, &$ier)
{
$ier = 0;
if ($x > 5.0) {
$v = 1.0 / $x;
$s = ((((((-0.000592166437354 * $v + 0.0000697281375837) * $v +
0.00078403922172) * $v - 0.000229472093621) * $v -
0.00268132716049) * $v + 0.00347222222222) * $v +
0.0833333333333) * $v + 1.0;
$g = 2.506628274631001 * exp(-$x) * pow($x,$x-0.5) * $s;
}
else {
$err = 1.0e-20;
$w = $x;
$t = 1.0;
if ($x < 1.5) {
if ($x < $err) {
$k = intval($x);
$y = floatval($k) - $x;
if (abs($y) < $err || abs(1.0-$y) < $err)
$ier = -1;
}
if ($ier == 0) {
while ($w < 1.5) {
$t /= $w;
$w += 1.0;
}
}
}
else {
if ($w > 2.5) {
while ($w > 2.5) {
$w -= 1.0;
$t *= $w;
}
}
}
$w -= 2.0;
$g = (((((((0.0021385778 * $w - 0.0034961289) * $w +
0.0122995771) * $w - 0.00012513767) * $w + 0.0740648982) * $w +
0.0815652323) * $w + 0.411849671) * $w + 0.422784604) * $w +
0.999999926;
$g *= $t;
}
return $g;
}
?>
############################################
# ガンマ関数の計算
# coded by Y.Suganuma
############################################
############################################
# Γ(x)の計算(ガンマ関数,近似式)
# ier : =0 : normal
# =-1 : x=-n (n=0,1,2,・・・)
# return : 結果
# coded by Y.Suganuma
############################################
def gamma(x, ier)
ier[0] = 0
if x > 5.0
v = 1.0 / x
s = ((((((-0.000592166437354 * v + 0.0000697281375837) * v + 0.00078403922172) * v - 0.000229472093621) * v - 0.00268132716049) * v + 0.00347222222222) * v + 0.0833333333333) * v + 1.0
g = 2.506628274631001 * Math.exp(-x) * (x ** (x-0.5)) * s
else
err = 1.0e-20
w = x
t = 1.0
if x < 1.5
if x < err
k = Integer(x)
y = Float(k) - x
if y.abs() < err or (1.0-y).abs() < err
ier[0] = -1
end
end
if ier[0] == 0
while w < 1.5
t /= w
w += 1.0
end
end
else
if w > 2.5
while w > 2.5
w -= 1.0
t *= w
end
end
end
w -= 2.0
g = (((((((0.0021385778 * w - 0.0034961289) * w + 0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w + 0.0815652323) * w + 0.411849671) * w + 0.422784604) * w + 0.999999926
g *= t
end
return g
end
ind = Array.new(1)
print("data? ")
s = gets()
x = Float(s)
y = gamma(x, ind)
print(" x " + String(x) + " gamma(x) " + String(y) + " ind " + String(ind[0]) + "\n")
# -*- coding: UTF-8 -*-
import numpy as np
import sys
from math import *
############################################
# Γ(x)の計算(ガンマ関数,近似式)
# ier : =0 : normal
# =-1 : x=-n (n=0,1,2,・・・)
# return : 結果
# coded by Y.Suganuma
############################################
def gamma(x, ier) :
ier[0] = 0
if x > 5.0 :
v = 1.0 / x
s = ((((((-0.000592166437354 * v + 0.0000697281375837) * v + 0.00078403922172) * v - 0.000229472093621) * v - 0.00268132716049) * v + 0.00347222222222) * v + 0.0833333333333) * v + 1.0
g = 2.506628274631001 * exp(-x) * pow(x,x-0.5) * s
else :
err = 1.0e-20
w = x
t = 1.0
if x < 1.5 :
if x < err :
k = int(x)
y = float(k) - x
if abs(y) < err or abs(1.0-y) < err :
ier[0] = -1
if ier[0] == 0 :
while w < 1.5 :
t /= w
w += 1.0
else :
if w > 2.5 :
while w > 2.5 :
w -= 1.0
t *= w
w -= 2.0
g = (((((((0.0021385778 * w - 0.0034961289) * w + 0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w + 0.0815652323) * w + 0.411849671) * w + 0.422784604) * w + 0.999999926
g *= t
return g
############################################
# ガンマ関数の計算
# coded by Y.Suganuma
############################################
ind = np.empty(1, np.int)
s = input("data? ")
x = float(s)
y = gamma(x, ind)
print(" x " + str(x) + " gamma(x) " + str(y) + " ind " + str(ind[0]))
/****************************/
/* ガンマ関数の計算 */
/* coded by Y.Suganuma */
/****************************/
using System;
class Program
{
static void Main()
{
Test1 ts = new Test1();
}
}
class Test1
{
public Test1()
{
Console.Write("data? ");
double x = double.Parse(Console.ReadLine());
int ind = 0;
double y = gamma(x, ref ind);
Console.WriteLine(" x " + x + " gamma(x) " + y + " ind " + ind);
}
/****************************************/
/* Γ(x)の計算(ガンマ関数,近似式) */
/* ier : =0 : normal */
/* =-1 : x=-n (n=0,1,2,・・・) */
/* return : 結果 */
/****************************************/
static double gamma(double x, ref int ier)
{
double g;
ier = 0;
if (x > 5.0) {
double v = 1.0 / x;
double s = ((((((-0.000592166437354 * v + 0.0000697281375837) * v +
0.00078403922172) * v - 0.000229472093621) * v -
0.00268132716049) * v + 0.00347222222222) * v +
0.0833333333333) * v + 1.0;
g = 2.506628274631001 * Math.Exp(-x) * Math.Pow(x,x-0.5) * s;
}
else {
double err = 1.0e-20;
double w = x;
double t = 1.0;
if (x < 1.5) {
if (x < err) {
int k = (int)x;
double y = (double)k - x;
if (Math.Abs(y) < err || Math.Abs(1.0-y) < err)
ier = -1;
}
if (ier == 0) {
while (w < 1.5) {
t /= w;
w += 1.0;
}
}
}
else {
if (w > 2.5) {
while (w > 2.5) {
w -= 1.0;
t *= w;
}
}
}
w -= 2.0;
g = (((((((0.0021385778 * w - 0.0034961289) * w +
0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w +
0.0815652323) * w + 0.411849671) * w + 0.422784604) * w +
0.999999926;
g *= t;
}
return g;
}
}
'**************************'
' ガンマ関数の計算 '
' coded by Y.Suganuma '
'**************************'
Module Test
Sub Main()
Console.Write("data? ")
Dim x As Double = Double.Parse(Console.ReadLine())
Dim ind As Integer = 0
Dim y As Double = gamma(x, ind)
Console.WriteLine(" x " & x & " gamma(x) " & y & " ind " & ind)
End Sub
'**************************************'
' Γ(x)の計算(ガンマ関数,近似式) '
' ier : =0 : normal '
' =-1 : x=-n (n=0,1,2,・・・) '
' return : 結果 '
'**************************************'
Function gamma(x As Double, ByRef ier As Integer)
Dim g As Double
ier = 0
If x > 5.0
Dim v As Double = 1.0 / x
Dim s As Double = ((((((-0.000592166437354 * v + 0.0000697281375837) * v +
0.00078403922172) * v - 0.000229472093621) * v -
0.00268132716049) * v + 0.00347222222222) * v +
0.0833333333333) * v + 1.0
g = 2.506628274631001 * Math.Exp(-x) * Math.Pow(x,x-0.5) * s
Else
Dim err As Double = 1.0e-20
Dim w As Double = x
Dim t As Double = 1.0
If x < 1.5
If x < err
Dim k As Integer = Math.Floor(x)
Dim y As Double = k - x
If Math.Abs(y) < err or Math.Abs(1.0-y) < err
ier = -1
End If
End If
If ier = 0
Do While w < 1.5
t /= w
w += 1.0
Loop
End If
Else
If w > 2.5
Do While w > 2.5
w -= 1.0
t *= w
Loop
End If
End If
w -= 2.0
g = (((((((0.0021385778 * w - 0.0034961289) * w +
0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w +
0.0815652323) * w + 0.411849671) * w + 0.422784604) * w +
0.999999926
g *= t
End If
Return g
End Function
End Module
| 情報学部 | 菅沼ホーム | 目次 | 索引 |