Keyword: プサイ関数, 対数関数
概要
本サンプルはプサイ関数と対数関数との差分(Ψ(x)−ln x)を求めるC言語によるサンプルプログラムです。 本サンプルは引数xを読み込み、xの各値についてΨ(x)−ln xを求めて出力します。この場合Ψは以下で表わされます。
※本サンプルはnAG Cライブラリに含まれる関数 nag_polygamma_fun() のExampleコードです。本サンプル及び関数の詳細情報は nag_polygamma_fun のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_polygamma_fun のマニュアルページを参照)- 1行目はタイトル行で読み飛ばされます。
- 2〜5行目に引数xの値を指定しています。
出力結果
(本関数の詳細はnag_polygamma_fun のマニュアルページを参照)| この出力例をダウンロード |
nag_polygamma_fun (s14acc) Example Program Results
x psi(x)-log(x)
0.100 -8.1212
0.500 -1.2704
3.600 -0.1453
8.000 -0.0638
- 3〜6行目に引数xの値とΨ(x)−ln xの値が出力されています。
ソースコード
(本関数の詳細はnag_polygamma_fun のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
/* nag_polygamma_fun (s14acc) Example Program.
*
* CLL6I261D/CLL6I261DL Version.
*
* Copyright 2017 Numerical Algorithms Group.
*
* Mark 26.1, 2017.
*/
#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nags.h>
int main(void)
{
Integer exit_status = 0;
double f, x;
NagError fail;
INIT_FAIL(fail);
/* Skip heading in data file */
scanf("%*[^\n]");
printf("nag_polygamma_fun (s14acc) Example Program Results\n");
printf(" x psi(x)-log(x)\n");
while (scanf("%lf", &x) != EOF)
{
/* nag_polygamma_fun (s14acc).
* psi(x) - ln(x)
*/
f = nag_polygamma_fun(x, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_polygamma_fun (s14acc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("%8.3f %14.4f\n", x, f);
}
END:
return exit_status;
}
