その他関数ライブラリ 使用例



[ 簡単な説明 ]

その他関数ライブラリの使用例。

test31.c は、minmax 関数の使用例ですが、科学技術計算ライブラリ以外に、ソートライブラリ、乱数ライブラリを使用しています。


プログラム・ソース("test28.c")           top (先頭に戻る)
/*		test28.c			*/
#include	<stdio.h>
#include	"sslib.h"

int main(void)
{
	double b[12], av, pro, rn, st, sum, var;
	int i, k, n;

	n = 1000;

	av = 5.;
	st = 2.;
	sum = var = 0.;
	printf("* A Normal Random Number\n");
	for(i = 0; i < 11; i++)	b[i] = 0.;
	printf("   n=%4d     av=%4.1f            st=%4.1f\n", n, av, st);
	for(i = 0; i < n; i++)
	{
		rn = normal(av, st);
		sum += rn;
		var += (rn * rn);
		k = (int)rn + 1;
		if(k <= 11)	b[k]++;
	}
	av = sum / (double)n;
	st = sqrt(var / (double)n - av * av);
	printf("* Observed Number\n");
	printf("              av=%13.6e   st=%13.6e\n", av, st);
	for(i = 0; i < 12; i++)
	{
		pro = b[i] * 100. / (double)n;
		printf("   %3d > rn >= %3d ----- %6.2f %%\n", i + 1, i, pro);
	}

	av = 5.;
	sum = 0.;
	printf("* A Poison Random Number\n");
	for(i = 0; i < 11; i++)	b[i] = 0.;
	printf("   n=%4d     av=%4.1f\n", n, av);
	for(i = 0; i < n; i++)
	{
		rn = poison(av);
		sum += rn;
		k = (int)rn + 1;
		if(k <= 11)	b[k]++;
	}
	av = sum / (double)n;
	printf("* Observed Number\n");
	printf("              av=%13.6e\n", av);
	for(i = 0; i < 12; i++)
	{
		pro = b[i] * 100. / (double)n;
		printf("   %3d > rn >= %3d ----- %6.2f %%\n", i + 1, i, pro);
	}

	return 1;
}


出力結果 1           top (先頭に戻る)
* A Normal Random Number
   n=1000     av= 5.0            st= 2.0
* Observed Number
              av= 5.085868e+00   st= 1.993456e+00
     1 > rn >=   0 -----   0.10 %
     2 > rn >=   1 -----   1.90 %
     3 > rn >=   2 -----   3.80 %
     4 > rn >=   3 -----   9.70 %
     5 > rn >=   4 -----  13.50 %
     6 > rn >=   5 -----  19.10 %
     7 > rn >=   6 -----  19.80 %
     8 > rn >=   7 -----  15.40 %
     9 > rn >=   8 -----   9.00 %
    10 > rn >=   9 -----   5.60 %
    11 > rn >=  10 -----   1.30 %
    12 > rn >=  11 -----   0.70 %
* A Poison Random Number
   n=1000     av= 5.0
* Observed Number
              av= 4.972000e+00
     1 > rn >=   0 -----   0.00 %
     2 > rn >=   1 -----   1.30 %
     3 > rn >=   2 -----   3.80 %
     4 > rn >=   3 -----   8.30 %
     5 > rn >=   4 -----  13.00 %
     6 > rn >=   5 -----  18.30 %
     7 > rn >=   6 -----  16.10 %
     8 > rn >=   7 -----  16.30 %
     9 > rn >=   8 -----   9.40 %
    10 > rn >=   9 -----   6.90 %
    11 > rn >=  10 -----   3.50 %
    12 > rn >=  11 -----   2.40 %

プログラム・ソース("test29.c")           top (先頭に戻る)
/*		test29.c				*/
#include	<stdio.h>
#include	"sslib.h"

int main(void)
{
	static int a[3] = { 5, 10, 50};
	static int b[3] = { 3,  5, 30};
	int i, m, n;

	printf("* A Binoninal Coefficient\n");
	for(i = 0; i < 3; i++)
	{
		m = a[i];
		n = b[i];
		printf("  m=%2d   n=%2d  mCn=%20.13e\n", m, n, bino(m, n));
	}

	return 1;
}


出力結果 2           top (先頭に戻る)
* A Binoninal Coefficient
  m= 5   n= 3  mCn= 1.0000000000000e+01
  m=10   n= 5  mCn= 2.5200000000000e+02
  m=50   n=30  mCn= 4.7129212243959e+13

プログラム・ソース("test30.c")           top (先頭に戻る)
/*		test30.c				*/
#include	<stdio.h>
#include	"sslib.h"

#define		N		200			/* number of data */
#define		M		3			/* number of line */

static double f[M][N];							/* analysis data */

/* a1[0] + a1[1]*s + a1[2]*s**2 + ... + a1[n-1]*s**(n-1)	*/
static int n = 1;								/* Order of numerator + 1 */
static double a1[10] = { 1.};					/* numerator */
static double a2[10] = { 0.17892};
static double a3[10] = { 1.};
static int m = 7;								/* Order of denominator + 1 */
static double b1[10] = { 0., 1., 3.23607, 5.23607,	/* denominator */
						 5.23607, 3.23607, 1.};
static double b2[10] = { 0., 0.17892, 0.75252, 1.30957,
						 1.93737, 1.17249, 1.};
static double b3[10] = { 0., 1., 3.93628, 6.88637,
						 6.77667, 3.81070, 1.};
static double a = 4.;							/* a (of exp(a)) */
										/* Number of series   k = k1 + t * k2 */
static double k1 = 5.;							/* fixed part */
static double k2 = 0.5;							/* coeff. part */
static int p = 6;								/* Number of Euler coeffs */

main()
{
	double ts, te;
	int i;

	ts = 0.;
	te = 12.6;

	filt(ts, te, f[0], N, n, m, a1, b1, a, k1, k2, p);
	filt(ts, te, f[1], N, n, m, a2, b2, a, k1, k2, p);
	filt(ts, te, f[2], N, n, m, a3, b3, a, k1, k2, p);

	printf("< f[0] >\n");
	for(i = 0; i < N; i++)
	{
		printf("%13.6e  ", f[0][i]);
		if(i % 5 == 4)	putchar('\n');
	}
	printf("< f[1] >\n");
	for(i = 0; i < N; i++)
	{
		printf("%13.6e  ", f[1][i]);
		if(i % 5 == 4)	putchar('\n');
	}
	printf("< f[2] >\n");
	for(i = 0; i < N; i++)
	{
		printf("%13.6e  ", f[2][i]);
		if(i % 5 == 4)	putchar('\n');
	}

	return 1;
}

出力結果 3           top (先頭に戻る)
< f[0] >
 0.000000e+00   7.387414e-09   2.297042e-07   1.694138e-06   6.930544e-06  
 2.052333e-05   4.953294e-05   1.037963e-04   1.961160e-04   3.423489e-04  
 5.614035e-04   8.751585e-04   1.308310e-03   1.888163e-03   2.644365e-03  
 3.608610e-03   4.814305e-03   6.296210e-03   8.090069e-03   1.023223e-02  
 1.275926e-02   1.570757e-02   1.911304e-02   2.301066e-02   2.743418e-02  
 3.241582e-02   3.798592e-02   4.417271e-02   5.100203e-02   5.849710e-02  
 6.667837e-02   7.556328e-02   8.516612e-02   9.549811e-02   1.065670e-01  
 1.183772e-01   1.309296e-01   1.442220e-01   1.582483e-01   1.729994e-01  
 1.884627e-01   2.046224e-01   2.214595e-01   2.389519e-01   2.570746e-01  
 2.757998e-01   2.950971e-01   3.149336e-01   3.352741e-01   3.560812e-01  
 3.773156e-01   3.989362e-01   4.209005e-01   4.431645e-01   4.656833e-01  
 4.884107e-01   5.113002e-01   5.343046e-01   5.573763e-01   5.804678e-01  
 6.035317e-01   6.265208e-01   6.493885e-01   6.720888e-01   6.945769e-01  
 7.168082e-01   7.387401e-01   7.603311e-01   7.815411e-01   8.023319e-01  
 8.226667e-01   8.425108e-01   8.618313e-01   8.805975e-01   8.987808e-01  
 9.163548e-01   9.332953e-01   9.495804e-01   9.651906e-01   9.801089e-01  
 9.943204e-01   1.007813e+00   1.020576e+00   1.032602e+00   1.043887e+00  
 1.054426e+00   1.064219e+00   1.073268e+00   1.081577e+00   1.089150e+00  
 1.095996e+00   1.102124e+00   1.107547e+00   1.112276e+00   1.116327e+00  
 1.119717e+00   1.122464e+00   1.124586e+00   1.126106e+00   1.127044e+00  
 1.127423e+00   1.127268e+00   1.126602e+00   1.125452e+00   1.123844e+00  
 1.121803e+00   1.119357e+00   1.116534e+00   1.113360e+00   1.109864e+00  
 1.106073e+00   1.102014e+00   1.097715e+00   1.093203e+00   1.088505e+00  
 1.083646e+00   1.078653e+00   1.073552e+00   1.068366e+00   1.063119e+00  
 1.057835e+00   1.052535e+00   1.047242e+00   1.041976e+00   1.036756e+00  
 1.031602e+00   1.026529e+00   1.021557e+00   1.016698e+00   1.011969e+00  
 1.007381e+00   1.002949e+00   9.986819e-01   9.945908e-01   9.906846e-01  
 9.869710e-01   9.834569e-01   9.801482e-01   9.770495e-01   9.741646e-01  
 9.714964e-01   9.690466e-01   9.668161e-01   9.648049e-01   9.630123e-01  
 9.614366e-01   9.600753e-01   9.589253e-01   9.579827e-01   9.572431e-01  
 9.567012e-01   9.563514e-01   9.561874e-01   9.562027e-01   9.563900e-01  
 9.567418e-01   9.572503e-01   9.579073e-01   9.587043e-01   9.596326e-01  
 9.606838e-01   9.618486e-01   9.631181e-01   9.644832e-01   9.659347e-01  
 9.674636e-01   9.690608e-01   9.707174e-01   9.724246e-01   9.741735e-01  
 9.759559e-01   9.777633e-01   9.795876e-01   9.814211e-01   9.832561e-01  
 9.850854e-01   9.869020e-01   9.886992e-01   9.904709e-01   9.922109e-01  
 9.939136e-01   9.955739e-01   9.971867e-01   9.987477e-01   1.000253e+00  
 1.001698e+00   1.003080e+00   1.004396e+00   1.005643e+00   1.006819e+00  
 1.007923e+00   1.008952e+00   1.009905e+00   1.010782e+00   1.011582e+00  
 1.012304e+00   1.012950e+00   1.013519e+00   1.014013e+00   1.014431e+00  
< f[1] >
 0.000000e+00   1.345802e-09   4.262171e-08   3.202709e-07   1.335262e-06  
 4.030797e-06   9.919394e-06   2.119897e-05   4.085755e-05   7.276613e-05  
 1.217593e-04   1.937033e-04   2.955506e-04   4.353820e-04   6.224340e-04  
 8.671133e-04   1.180997e-03   1.576820e-03   2.068447e-03   2.670833e-03  
 3.399973e-03   4.272834e-03   5.307282e-03   6.521997e-03   7.936373e-03  
 9.570420e-03   1.144465e-02   1.357996e-02   1.599751e-02   1.871860e-02  
 2.176455e-02   2.515655e-02   2.891552e-02   3.306206e-02   3.761623e-02  
 4.259745e-02   4.802441e-02   5.391492e-02   6.028583e-02   6.715286e-02  
 7.453059e-02   8.243227e-02   9.086978e-02   9.985356e-02   1.093925e-01  
 1.194938e-01   1.301631e-01   1.414043e-01   1.532195e-01   1.656088e-01  
 1.785707e-01   1.921017e-01   2.061963e-01   2.208471e-01   2.360450e-01  
 2.517785e-01   2.680347e-01   2.847983e-01   3.020525e-01   3.197783e-01  
 3.379553e-01   3.565608e-01   3.755709e-01   3.949598e-01   4.147001e-01  
 4.347628e-01   4.551178e-01   4.757334e-01   4.965769e-01   5.176142e-01  
 5.388106e-01   5.601301e-01   5.815362e-01   6.029917e-01   6.244589e-01  
 6.458998e-01   6.672758e-01   6.885488e-01   7.096802e-01   7.306318e-01  
 7.513658e-01   7.718446e-01   7.920314e-01   8.118899e-01   8.313848e-01  
 8.504815e-01   8.691469e-01   8.873485e-01   9.050556e-01   9.222386e-01  
 9.388693e-01   9.549215e-01   9.703701e-01   9.851922e-01   9.993664e-01  
 1.012873e+00   1.025695e+00   1.037817e+00   1.049224e+00   1.059906e+00  
 1.069854e+00   1.079059e+00   1.087516e+00   1.095223e+00   1.102179e+00  
 1.108384e+00   1.113841e+00   1.118557e+00   1.122537e+00   1.125792e+00  
 1.128332e+00   1.130171e+00   1.131324e+00   1.131807e+00   1.131638e+00  
 1.130838e+00   1.129428e+00   1.127432e+00   1.124873e+00   1.121777e+00  
 1.118172e+00   1.114085e+00   1.109544e+00   1.104580e+00   1.099223e+00  
 1.093505e+00   1.087456e+00   1.081110e+00   1.074497e+00   1.067651e+00  
 1.060604e+00   1.053389e+00   1.046038e+00   1.038583e+00   1.031056e+00  
 1.023488e+00   1.015910e+00   1.008351e+00   1.000842e+00   9.934088e-01  
 9.860807e-01   9.788835e-01   9.718425e-01   9.649819e-01   9.583246e-01  
 9.518922e-01   9.457049e-01   9.397817e-01   9.341401e-01   9.287960e-01  
 9.237640e-01   9.190571e-01   9.146867e-01   9.106628e-01   9.069937e-01  
 9.036863e-01   9.007458e-01   8.981759e-01   8.959789e-01   8.941548e-01  
 8.927039e-01   8.916234e-01   8.909098e-01   8.905580e-01   8.905616e-01  
 8.909130e-01   8.916035e-01   8.926230e-01   8.939604e-01   8.956036e-01  
 8.975395e-01   8.997541e-01   9.022326e-01   9.049595e-01   9.079185e-01  
 9.110928e-01   9.144651e-01   9.180176e-01   9.217322e-01   9.255905e-01  
 9.295739e-01   9.336636e-01   9.378410e-01   9.420873e-01   9.463839e-01  
 9.507125e-01   9.550547e-01   9.593929e-01   9.637094e-01   9.679875e-01  
 9.722105e-01   9.763631e-01   9.804289e-01   9.843938e-01   9.882441e-01  
 9.919664e-01   9.955486e-01   9.989790e-01   1.002247e+00   1.005343e+00  
< f[2] >
 0.000000e+00   7.350205e-09   2.273640e-07   1.667992e-06   6.786717e-06  
 1.998706e-05   4.797032e-05   9.995686e-05   1.877919e-04   3.259507e-04  
 5.314580e-04   8.237350e-04   1.224389e-03   1.756954e-03   2.446602e-03  
 3.319823e-03   4.404098e-03   5.727552e-03   7.318624e-03   9.205729e-03  
 1.141694e-02   1.397967e-02   1.692040e-02   2.026440e-02   2.403550e-02  
 2.825585e-02   3.294573e-02   3.812341e-02   4.380496e-02   5.000419e-02  
 5.673249e-02   6.399883e-02   7.180963e-02   8.016892e-02   8.907808e-02  
 9.853606e-02   1.085393e-01   1.190818e-01   1.301554e-01   1.417494e-01  
 1.538512e-01   1.664458e-01   1.795167e-01   1.930451e-01   2.070109e-01  
 2.213922e-01   2.361657e-01   2.513070e-01   2.667902e-01   2.825889e-01  
 2.986754e-01   3.150215e-01   3.315986e-01   3.483774e-01   3.653284e-01  
 3.824222e-01   3.996291e-01   4.169195e-01   4.342644e-01   4.516346e-01  
 4.690019e-01   4.863383e-01   5.036165e-01   5.208099e-01   5.378933e-01  
 5.548411e-01   5.716299e-01   5.882368e-01   6.046398e-01   6.208185e-01  
 6.367532e-01   6.524255e-01   6.678183e-01   6.829156e-01   6.977028e-01  
 7.121663e-01   7.262939e-01   7.400746e-01   7.534986e-01   7.665574e-01  
 7.792435e-01   7.915509e-01   8.034743e-01   8.150100e-01   8.261549e-01  
 8.369074e-01   8.472666e-01   8.572328e-01   8.668069e-01   8.759910e-01  
 8.847880e-01   8.932015e-01   9.012357e-01   9.088959e-01   9.161878e-01  
 9.231176e-01   9.296922e-01   9.359193e-01   9.418065e-01   9.473623e-01  
 9.525952e-01   9.575143e-01   9.621289e-01   9.664485e-01   9.704828e-01  
 9.742418e-01   9.777354e-01   9.809738e-01   9.839672e-01   9.867258e-01  
 9.892598e-01   9.915793e-01   9.936944e-01   9.956151e-01   9.973512e-01  
 9.989126e-01   1.000309e+00   1.001549e+00   1.002643e+00   1.003599e+00  
 1.004426e+00   1.005133e+00   1.005728e+00   1.006219e+00   1.006613e+00  
 1.006919e+00   1.007143e+00   1.007292e+00   1.007372e+00   1.007391e+00  
 1.007354e+00   1.007267e+00   1.007135e+00   1.006964e+00   1.006758e+00  
 1.006522e+00   1.006260e+00   1.005976e+00   1.005674e+00   1.005358e+00  
 1.005031e+00   1.004696e+00   1.004355e+00   1.004012e+00   1.003668e+00  
 1.003326e+00   1.002987e+00   1.002654e+00   1.002328e+00   1.002010e+00  
 1.001702e+00   1.001404e+00   1.001117e+00   1.000843e+00   1.000581e+00  
 1.000333e+00   1.000098e+00   9.998775e-01   9.996708e-01   9.994787e-01  
 9.993004e-01   9.991362e-01   9.989859e-01   9.988492e-01   9.987260e-01  
 9.986159e-01   9.985185e-01   9.984334e-01   9.983602e-01   9.982984e-01  
 9.982474e-01   9.982069e-01   9.981761e-01   9.981547e-01   9.981420e-01  
 9.981375e-01   9.981407e-01   9.981509e-01   9.981677e-01   9.981904e-01  
 9.982187e-01   9.982518e-01   9.982895e-01   9.983310e-01   9.983760e-01  
 9.984241e-01   9.984747e-01   9.985274e-01   9.985820e-01   9.986378e-01  
 9.986947e-01   9.987517e-01   9.988095e-01   9.988674e-01   9.989251e-01  
 9.989823e-01   9.990388e-01   9.990944e-01   9.991488e-01   9.992019e-01  

プログラム・ソース("test31.c")          top (先頭に戻る)
/*		test31.c		*/
#include <stdio.h>
#include "random.h"
#include "sort.h"
#include "sslib.h"

int main(void)
{
	int i, n;
	int *data_i, min_i, max_i;
	double *data_d, min_d, max_d;
	unsigned char **data_c, *min_c, *max_c;
	long stime, etime;

	n = 1000;
	init_mkdata(init_ornd0, ornd0, 1L);

	data_i = mkdata_i(n, 0, 9999);
	minmax_i(data_i, n, &min_i, &max_i);
	printf("minmax_i() : min = %d, max = %d\n", min_i, max_i);

	data_d = mkdata_d(n, 0., 9999.);
	minmax_d(data_d, n, &min_d, &max_d);
	printf("minmax_d() : min = %.0f, max = %.0f\n", min_d, max_d);

	data_c = mkdata_s(n, 4, 4, 'a', 'z');
	minmax_c(data_c, n, &min_c, &max_c);
	printf("minmax_c() : min = %s, max = %s\n", min_c, max_c);

	return 1;
}

出力結果 4           top (先頭に戻る)
minmax_i() : min = 3, max = 9998
minmax_d() : min = 39, max = 9999
minmax_c() : min = mglf, max = uosx