| 情報学部 | 菅沼ホーム | 目次 | 索引 |
/****************************/
/* 一様分布の計算 */
/* coded by Y.Suganuma */
/****************************/
#include <stdio.h>
/****************************************/
/* 一様分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* a,b : 区間 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
#include <math.h>
double uniform(double x, double a, double b, double *pr)
{
double f = 0.0, y;
if (x < a) {
*pr = 0.0;
f = 0.0;
}
else if (x > b) {
*pr = 0.0;
f = 1.0;
}
else {
y = 1.0 / (b - a);
*pr = y;
f = y * (x - a);
}
return f;
}
/********/
/* main */
/********/
int main()
{
double a, b, x, pr, f, h, p;
int sw;
char file1[100], file2[100];
FILE *out1, *out2;
printf("下限(a)は? ");
scanf("%lf", &a);
printf("上限(b)は? ");
scanf("%lf", &b);
printf("目的とする結果は? \n");
printf(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n");
printf(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
scanf("%d", &sw);
if (sw == 0) {
printf("グラフ出力?(=1: yes, =0: no) ");
scanf("%d", &sw);
// 密度関数と分布関数の値
if (sw == 0) {
printf(" データは? ");
scanf("%lf", &x);
f = uniform(x, a, b, &pr);
printf("P(X = %f) = %f, P( X < %f) = %f (区間 [%f, %f])\n", x, pr, x, f, a, b);
}
// グラフ出力
else {
printf(" 密度関数のファイル名は? ");
scanf("%s", file1);
printf(" 分布関数のファイル名は? ");
scanf("%s", file2);
out1 = fopen(file1,"w");
out2 = fopen(file2,"w");
printf(" 刻み幅は? ");
scanf("%lf", &h);
for (x = a; x < b+0.5*h; x += h) {
if (x > b-0.5*h && x < b+0.5*h)
x = b;
f = uniform(x, a, b, &pr);
fprintf(out1, "%f %f\n", x, pr);
fprintf(out2, "%f %f\n", x, f);
}
}
}
// %値
else {
printf("%の値は? ");
scanf("%lf", &x);
p = 0.01 * x;
f = b - p * (b - a);
printf("%f%値 = %f (区間 [%f, %f])\n", x, f, a, b);
}
return 0;
}
/****************************/
/* 一様分布の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
import java.text.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test {
/********/
/* main */
/********/
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double a, b, x, f, h, p;
double pr[] = new double [1];
int sw;
System.out.print("下限(a)? ");
a = Double.parseDouble(in.readLine());
System.out.print("上限(b)? ");
b = Double.parseDouble(in.readLine());
System.out.println("目的とする結果は? ");
System.out.print(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n");
System.out.print(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
sw = Integer.parseInt(in.readLine());
if (sw == 0) {
System.out.print("グラフ出力?(=1: yes, =0: no) ");
sw = Integer.parseInt(in.readLine());
// 密度関数と分布関数の値
if (sw == 0) {
System.out.print(" データは? ");
x = Double.parseDouble(in.readLine());
f = uniform(x, a, b, pr);
System.out.println("P(X = " + x + ") = " + pr[0] + ", P( X < " + x + ") = " + f + " (区間 [" + a + ", " + b + "])");
}
// グラフ出力
else {
String file1, file2;
System.out.print(" 密度関数のファイル名は? ");
file1 = in.readLine();
System.out.print(" 分布関数のファイル名は? ");
file2 = in.readLine();
PrintStream out1 = new PrintStream(new FileOutputStream(file1));
PrintStream out2 = new PrintStream(new FileOutputStream(file2));
System.out.print(" 刻み幅は? ");
h = Double.parseDouble(in.readLine());
// データ取得
ArrayList <Double> x1 = new ArrayList <Double> ();
ArrayList <Double> y1 = new ArrayList <Double> ();
ArrayList <Double> y2 = new ArrayList <Double> ();
for (x = a; x < b+0.5*h; x += h) {
if (x > b-0.5*h && x < b+0.5*h)
x = b;
f = uniform(x, a, b, pr);
out1.println(x + " " + pr[0]);
out2.println(x + " " + f);
x1.add(x);
y1.add(pr[0]);
y2.add(f);
}
// グラフの描画
graph(x1, y1, y2);
}
}
// %値
else {
System.out.print("%の値は? ");
x = Double.parseDouble(in.readLine());
p = 0.01 * x;
f = b - p * (b - a);
System.out.println(x + "%値 = " + f + " (区間 [" + a + ", " + b + "])");
}
}
/****************************************/
/* 一様分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* a,b : 区間 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
static double uniform(double x, double a, double b, double pr[])
{
double f = 0.0, y;
if (x < a) {
pr[0] = 0.0;
f = 0.0;
}
else if (x > b) {
pr[0] = 0.0;
f = 1.0;
}
else {
y = 1.0 / (b - a);
pr[0] = y;
f = y * (x - a);
}
return f;
}
/*************************/
/* グラフの描画 */
/* x1 : x座標データ */
/* y1 : 密度関数 */
/* y2 : 分布関数 */
/*************************/
static void graph(ArrayList <Double> x1, ArrayList <Double> y1, ArrayList <Double> y2)
{
// 密度関数
String title1[]; // グラフ,x軸,及び,y軸のタイトル
String g_title1[]; // 凡例(グラフの内容)
double x_scale[]; // x軸目盛り
double y_scale1[]; // y軸目盛り
double data_x[][], data1_y[][]; // データ
int n = x1.size();
double x_max = (x1.get(n-1)).doubleValue();
double x_min = (x1.get(0)).doubleValue();
// グラフ,x軸,及び,y軸のタイトル
title1 = new String [3];
title1[0] = "密度関数(一様分布 区間 [" + x_min + ", " + x_max + "])";
title1[1] = "x";
title1[2] = "f(x)";
// 凡例
g_title1 = new String [1];
g_title1[0] = "密度関数";
// x軸目盛り
x_scale = new double[3];
double x_step = (x_max - x_min) / 5;
int x_p = 0;
boolean ok = true;
int k = 0;
while (ok) {
if (x_step < 1.0) {
x_step *= 10;
k++;
}
else if (x_step >= 10.0) {
x_step /= 10;
k--;
}
else {
ok = false;
if (x_step-(int)x_step > 1.0e-5)
x_step = (int)x_step + 1;
else
x_step = (int)x_step;
if (k != 0) {
x_step = x_step * Math.pow(10, -k);
if (k > 0)
x_p = k;
}
double t = x_min;
while (t < x_max-0.001*x_step)
t += x_step;
x_max = t;
}
}
x_scale[0] = x_min; // 最小値
x_scale[1] = x_max; // 最大値
x_scale[2] = x_step; // 刻み幅
// y軸目盛り
y_scale1 = new double[3];
double y_max = (y1.get(0)).doubleValue();
double y_step = y_max / 5;
int y_p1 = 0;
ok = true;
k = 0;
while (ok) {
if (y_step < 1.0) {
y_step *= 10;
k++;
}
else if (y_step >= 10.0) {
y_step /= 10;
k--;
}
else {
ok = false;
if (y_step-(int)y_step > 1.0e-5)
y_step = (int)y_step + 1;
else
y_step = (int)y_step;
if (k != 0) {
y_step = y_step * Math.pow(10, -k);
if (k > 0)
y_p1 = k;
}
double t = 0.0;
while (t < y_max-0.001*y_step)
t += y_step;
y_max = t;
}
}
y_scale1[0] = 0.0; // 最小値
y_scale1[1] = y_max; // 最大値
y_scale1[2] = y_step; // 刻み幅
// データ
data_x = new double [1][n];
data1_y = new double [1][n];
for (int i1 = 0; i1 < n; i1++)
data_x[0][i1] = (x1.get(i1)).doubleValue();
for (int i1 = 0; i1 < n; i1++)
data1_y[0][i1] = (y1.get(i1)).doubleValue();
// 作図
LineGraph gp1 = new LineGraph(title1, g_title1, x_scale, x_p, y_scale1, y_p1, data_x, data1_y, true, false);
// 分布関数
String title2[]; // グラフ,x軸,及び,y軸のタイトル
String g_title2[]; // 凡例(グラフの内容)
double y_scale2[]; // y軸目盛り
double data2_y[][]; // データ
// グラフ,x軸,及び,y軸のタイトル
title2 = new String [3];
title2[0] = "分布関数(一様分布 区間 [" + x_min + ", " + x_max + "])";
title2[1] = "x";
title2[2] = "F(x)";
// 凡例
g_title2 = new String [1];
g_title2[0] = "分布関数";
// y軸目盛り
y_scale2 = new double[3];
int y_p2 = 1;
y_scale2[0] = 0.0; // 最小値
y_scale2[1] = 1.0; // 最大値
y_scale2[2] = 0.2; // 刻み幅
// データ
data2_y = new double [1][n];
for (int i1 = 0; i1 < n; i1++)
data2_y[0][i1] = (y2.get(i1)).doubleValue();
// 作図
LineGraph gp2 = new LineGraph(title2, g_title2, x_scale, x_p, y_scale2, y_p2, data_x, data2_y, true, false);
}
}
/****************************/
/* 折れ線グラフの描画 */
/* coded by Y.Suganuma */
/****************************/
class LineGraph extends JFrame {
Draw_line pn;
/*********************************************************/
/* コンストラクタ(折れ線グラフ2) */
/* title_i : グラフ,x軸,及び,y軸のタイトル */
/* g_title_i : 凡例 */
/* x_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_x_i : 小数点以下の桁数(x軸) */
/* y_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_y_i : 小数点以下の桁数(y軸) */
/* data_x_i : グラフのデータ(x軸) */
/* data_y_i : グラフのデータ(y軸) */
/* d_t_i : タイトル表示の有無 */
/* d_g_i : 凡例表示の有無 */
/*********************************************************/
LineGraph(String title_i[], String g_title_i[], double x_scale_i[],
int place_x_i, double y_scale_i[], int place_y_i,
double data_x_i[][], double data_y_i[][], boolean d_t_i,
boolean d_g_i)
{
// JFrameクラスのコンストラクタの呼び出し
super("折れ線グラフ(2)");
// Windowサイズと表示位置を設定
int width = 900, height = 600; // Windowの大きさ(初期サイズ)
setSize(width, height);
Toolkit tool = getToolkit();
Dimension d = tool.getScreenSize();
setLocation(d.width / 2 - width / 2, d.height / 2 - height / 2);
// 描画パネル
Container cp = getContentPane();
pn = new Draw_line(title_i, g_title_i, x_scale_i, place_x_i, y_scale_i, place_y_i, data_x_i, data_y_i, d_t_i, d_g_i, this);
cp.add(pn);
// ウィンドウを表示
setVisible(true);
// イベントアダプタ
addWindowListener(new WinEnd());
addComponentListener(new ComponentResize());
}
/**********************/
/* Windowのサイズ変化 */
/**********************/
class ComponentResize extends ComponentAdapter
{
public void componentResized(ComponentEvent e)
{
pn.repaint();
}
}
/************/
/* 終了処理 */
/************/
class WinEnd extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
setVisible(false);
}
}
}
class Draw_line extends JPanel {
String title[]; // グラフのタイトル
String g_title[]; // 凡例(グラフの内容)
String x_title[]; // x軸への表示
double x_scale[]; // y軸目盛り
double y_scale[]; // y軸目盛り
double data_x[][], data_y[][]; // データ
boolean d_t; // タイトル表示の有無
boolean d_g; // 凡例表示の有無
boolean type = true; // 横軸が項目かデータか
boolean ver = true; // 縦か横か
int place_x; // 小数点以下の桁数(x軸)
int place_y; // 小数点以下の桁数(y軸)
int width = 900, height = 600; // Windowの大きさ(初期サイズ)
int bx1, bx2, by1, by2; // 表示切り替えボタンの位置
LineGraph line;
String change = "横 色"; // 表示切り替えボタン
float line_w = 1.0f; // 折れ線グラフ等の線の太さ
boolean line_m = true; // 折れ線グラフ等にマークを付けるか否か
Color cl[] = {Color.black, Color.magenta, Color.blue, Color.orange, Color.cyan,
Color.pink, Color.green, Color.yellow, Color.darkGray, Color.red}; // グラフの色
int n_g; // グラフの数
/*********************************************************/
/* コンストラクタ(折れ線グラフ2) */
/* title_i : グラフ,x軸,及び,y軸のタイトル */
/* g_title_i : 凡例 */
/* x_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_x_i : 小数点以下の桁数(x軸) */
/* y_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_y_i : 小数点以下の桁数(y軸) */
/* data_x_i : グラフのデータ(x軸) */
/* data_y_i : グラフのデータ(y軸) */
/* d_t_i : タイトル表示の有無 */
/* d_g_i : 凡例表示の有無 */
/*********************************************************/
Draw_line(String title_i[], String g_title_i[], double x_scale_i[],
int place_x_i, double y_scale_i[], int place_y_i,
double data_x_i[][], double data_y_i[][], boolean d_t_i,
boolean d_g_i, LineGraph line_i)
{
// 背景色
setBackground(Color.white);
// テーブルデータの保存
title = title_i;
g_title = g_title_i;
x_scale = x_scale_i;
place_x = place_x_i;
y_scale = y_scale_i;
place_y = place_y_i;
data_x = data_x_i;
data_y = data_y_i;
d_t = d_t_i;
d_g = d_g_i;
type = false;
line = line_i;
// イベントアダプタ
addMouseListener(new ClickMouse(this));
}
/********/
/* 描画 */
/********/
public void paintComponent (Graphics g)
{
super.paintComponent(g); // 親クラスの描画(必ず必要)
double r, x1, y1, sp;
int i1, i2, cr, k, k_x, k_y, k1, k2, kx, kx1, ky, ky1, han, len;
int x_l, x_r, y_u, y_d; // 描画領域
int f_size; // フォントサイズ
int n_p; // データの数
String s1;
Font f;
FontMetrics fm;
Graphics2D g2 = (Graphics2D)g;
//
// Windowサイズの取得
//
Insets insets = line.getInsets();
Dimension d = line.getSize();
width = d.width - (insets.left + insets.right);
height = d.height - (insets.top + insets.bottom);
x_l = insets.left + 10;
x_r = d.width - insets.right - 10;
y_u = 20;
y_d = d.height - insets.bottom - insets.top;
//
// グラフタイトルの表示
//
r = 0.05; // タイトル領域の割合
f_size = ((y_d - y_u) < (x_r - x_l)) ? (int)((y_d - y_u) * r) : (int)((x_r - x_l) * r);
if (f_size < 5)
f_size = 5;
if (d_t) {
f = new Font("TimesRoman", Font.BOLD, f_size);
g.setFont(f);
fm = g.getFontMetrics(f);
len = fm.stringWidth(title[0]);
g.drawString(title[0], (x_l+x_r)/2-len/2, y_d-f_size/2);
y_d -= f_size;
}
//
// 表示切り替えボタンの設置
//
f_size = (int)(0.8 * f_size);
if (f_size < 5)
f_size = 5;
f = new Font("TimesRoman", Font.PLAIN, f_size);
fm = g.getFontMetrics(f);
g.setFont(f);
g.setColor(Color.yellow);
len = fm.stringWidth(change);
bx1 = x_r - len - 7 * f_size / 10;
by1 = y_u - f_size / 2;
bx2 = bx1 + len + f_size / 2;
by2 = by1 + 6 * f_size / 5;
g.fill3DRect(bx1, by1, len+f_size/2, 6*f_size/5, true);
g.setColor(Color.black);
g.drawString(change, x_r-len-f_size/2, y_u+f_size/2);
//
// 凡例の表示
//
n_g = g_title.length;
if (d_g) {
han = 0;
for (i1 = 0; i1 < n_g; i1++) {
len = fm.stringWidth(g_title[i1]);
if (len > han)
han = len;
}
han += 15;
r = 0.2; // 凡例領域の割合
k1 = (int)((x_r - x_l) * r);
if (han > k1)
han = k1;
kx = x_r - han;
ky = y_u + 3 * f_size / 2;
k = 0;
g2.setStroke(new BasicStroke(7.0f));
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k]);
g.drawLine(kx, ky, kx+10, ky);
g.setColor(Color.black);
g.drawString(g_title[i1], kx+15, ky+2*f_size/5);
k++;
if (k >= cl.length)
k = 0;
ky += f_size;
}
g2.setStroke(new BasicStroke(1.0f));
x_r -= (han + 10);
}
else
x_r -= (int)(0.03 * (x_r - x_l));
//
// x軸及びy軸のタイトルの表示
//
if (ver) { // 縦
if (title[1].length() > 0 && !title[1].equals("-")) {
len = fm.stringWidth(title[1]);
g.drawString(title[1], (x_l+x_r)/2-len/2, y_d-4*f_size/5);
y_d -= 7 * f_size / 4;
}
else
y_d -= f_size / 2;
if (title[2].length() > 0 && !title[2].equals("-")) {
g.drawString(title[2], x_l, y_u+f_size/2);
y_u += f_size;
}
}
else { // 横
if (title[2].length() > 0 && !title[2].equals("-")) {
len = fm.stringWidth(title[2]);
g.drawString(title[2], (x_l+x_r)/2-len/2, y_d-4*f_size/5);
y_d -= 7 * f_size / 4;
}
else
y_d -= f_size / 2;
if (title[1].length() > 0 && !title[1].equals("-")) {
g.drawString(title[1], x_l, y_u+f_size/2);
y_u += f_size;
}
}
//
// x軸,y軸,及び,各軸の目盛り
//
f_size = (int)(0.8 * f_size);
if (f_size < 5)
f_size = 5;
f = new Font("TimesRoman", Font.PLAIN, f_size);
fm = g.getFontMetrics(f);
y_d -= 3 * f_size / 2;
k_y = (int)((y_scale[1] - y_scale[0]) / (0.99 * y_scale[2]));
k_x = 0;
if (!type)
k_x = (int)((x_scale[1] - x_scale[0]) / (0.99 * x_scale[2]));
g.setFont(f);
DecimalFormat df_x, df_y;
df_x = new DecimalFormat("#");
df_y = new DecimalFormat("#");
if (!type) {
if (place_x != 0) {
s1 = "#.";
for (i1 = 0; i1 < place_x; i1++)
s1 += "0";
df_x = new DecimalFormat(s1);
}
}
if (place_y != 0) {
s1 = "#.";
for (i1 = 0; i1 < place_y; i1++)
s1 += "0";
df_y = new DecimalFormat(s1);
}
// 縦表示
if (ver) {
// y軸
y1 = y_scale[0];
len = 0;
for (i1 = 0; i1 < k_y+1; i1++) {
s1 = df_y.format(y1);
k1 = fm.stringWidth(s1);
if (k1 > len)
len = k1;
y1 += y_scale[2];
}
g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
g.drawLine(x_r, y_u, x_r, y_d);
y1 = y_scale[0];
x1 = y_d;
sp = (double)(y_d - y_u) / k_y;
for (i1 = 0; i1 < k_y+1; i1++) {
ky = (int)Math.round(x1);
s1 = df_y.format(y1);
k1 = fm.stringWidth(s1);
g.drawString(s1, x_l+len-k1, ky+f_size/2);
g.drawLine(x_l+len+5, ky, x_r, ky);
y1 += y_scale[2];
x1 -= sp;
}
x_l += (len + 5);
// x軸
if (type) {
n_p = x_title.length;
sp = (double)(x_r - x_l) / n_p;
x1 = x_l + sp / 2.0;
for (i1 = 0; i1 < n_p; i1++) {
kx = (int)Math.round(x1);
k1 = fm.stringWidth(x_title[i1]);
g.drawString(x_title[i1], kx-k1/2, y_d+6*f_size/5);
g.drawLine(kx, y_d, kx, y_d-5);
x1 += sp;
}
}
else {
x1 = x_scale[0];
y1 = x_l;
sp = (double)(x_r - x_l) / k_x;
for (i1 = 0; i1 < k_x+1; i1++) {
kx = (int)Math.round(y1);
s1 = df_x.format(x1);
k1 = fm.stringWidth(s1);
g.drawString(s1, kx-k1/2, y_d+6*f_size/5);
g.drawLine(kx, y_d, kx, y_u);
x1 += x_scale[2];
y1 += sp;
}
}
}
// 横表示
else {
// y軸
if (type) {
n_p = x_title.length;
len = 0;
for (i1 = 0; i1 < n_p; i1++) {
k1 = fm.stringWidth(x_title[i1]);
if (k1 > len)
len = k1;
}
g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
g.drawLine(x_r, y_u, x_r, y_d);
sp = (double)(y_d - y_u) / n_p;
x1 = y_d - sp / 2.0;
for (i1 = 0; i1 < n_p; i1++) {
ky = (int)Math.round(x1);
k1 = fm.stringWidth(x_title[n_p-1-i1]);
g.drawString(x_title[n_p-1-i1], x_l+len-k1, ky+f_size/2);
g.drawLine(x_l+len+5, ky, x_l+len+10, ky);
x1 -= sp;
}
g.drawLine(x_l+len+5, y_u, x_r, y_u);
g.drawLine(x_l+len+5, y_d, x_r, y_d);
x_l += (len + 5);
}
else {
y1 = x_scale[0];
len = 0;
for (i1 = 0; i1 < k_x+1; i1++) {
s1 = df_x.format(y1);
k1 = fm.stringWidth(s1);
if (k1 > len)
len = k1;
y1 += x_scale[2];
}
g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
g.drawLine(x_r, y_u, x_r, y_d);
y1 = x_scale[0];
x1 = y_d;
sp = (double)(y_d - y_u) / k_x;
for (i1 = 0; i1 < k_x+1; i1++) {
ky = (int)Math.round(x1);
s1 = df_x.format(y1);
k1 = fm.stringWidth(s1);
g.drawString(s1, x_l+len-k1, ky+f_size/2);
g.drawLine(x_l+len+5, ky, x_r, ky);
y1 += x_scale[2];
x1 -= sp;
}
x_l += (len + 5);
}
// x軸
x1 = y_scale[0];
y1 = x_l;
sp = (double)(x_r - x_l) / k_y;
for (i1 = 0; i1 < k_y+1; i1++) {
kx = (int)Math.round(y1);
s1 = df_y.format(x1);
k1 = fm.stringWidth(s1);
g.drawString(s1, kx-k1/2, y_d+6*f_size/5);
g.drawLine(kx, y_d, kx, y_u);
x1 += y_scale[2];
y1 += sp;
}
}
//
// グラフの表示
//
g2.setStroke(new BasicStroke(line_w));
cr = (int)line_w + 6;
// 縦表示
if (ver) {
if (type) {
n_p = x_title.length;
sp = (double)(x_r - x_l) / n_p;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
x1 = x_l + sp / 2.0;
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
kx = (int)Math.round(x1);
ky = y_d - (int)((y_d - y_u) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
x1 += sp;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
else {
n_p = data_x[0].length;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
kx = x_l + (int)((x_r - x_l) * (data_x[i1][i2] - x_scale[0]) / (x_scale[1] - x_scale[0]));
ky = y_d - (int)((y_d - y_u) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
}
// 横表示
else {
if (type) {
n_p = x_title.length;
sp = (double)(y_d - y_u) / n_p;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
y1 = y_d - sp / 2.0;
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
ky = (int)Math.round(y1);
kx = x_l + (int)((x_r - x_l) * (data_y[i1][n_p-1-i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
y1 -= sp;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
else {
n_p = data_x[0].length;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
kx = x_l + (int)((x_r - x_l) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
ky = y_d - (int)((y_d - y_u) * (data_x[i1][i2] - x_scale[0]) / (x_scale[1] - x_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
}
g2.setStroke(new BasicStroke(1.0f));
}
/************************************/
/* マウスがクリックされたときの処理 */
/************************************/
class ClickMouse extends MouseAdapter
{
Draw_line dr;
ClickMouse(Draw_line dr1)
{
dr = dr1;
}
public void mouseClicked(MouseEvent e)
{
int xp = e.getX();
int yp = e.getY();
// 縦表示と横表示の変換
if (xp > bx1 && xp < bx1+(bx2-bx1)/2 && yp > by1 && yp < by2) {
if (ver) {
ver = false;
change = "縦 色";
}
else {
ver = true;
change = "横 色";
}
repaint();
}
// グラフの色,線の太さ等
else if (xp > bx1+(bx2-bx1)/2 && xp < bx2 && yp > by1 && yp < by2) {
Modify md = new Modify(dr.line, dr);
md.setVisible(true);
}
}
}
}
/****************************/
/* 色及び線の太さの変更 */
/* coded by Y.Suganuma */
/****************************/
class Modify extends JDialog implements ActionListener, TextListener {
Draw_line dr; // 折れ線グラフ
JButton bt_dr;
TextField rgb[];
TextField r[];
TextField g[];
TextField b[];
JTextField tx;
JRadioButton r1, r2;
float line_w = 1.0f; // 折れ線グラフ等の線の太さ
boolean line_m = true; // 折れ線グラフ等にマークを付けるか否か
Color cl[]; // グラフの色
int n_g; // グラフの数
int wd; // 線の太さを変更するか
int mk; // マークを変更するか
int n;
JPanel jp[];
// 折れ線グラフ
Modify(Frame host, Draw_line dr1)
{
super(host, "色と線の変更", true);
// 初期設定
dr = dr1;
wd = 1;
mk = 1;
n_g = dr.n_g;
if (n_g > 10)
n_g = 10;
n = n_g + 3;
line_w = dr.line_w;
line_m = dr.line_m;
cl = new Color[n_g];
for (int i1 = 0; i1 < n_g; i1++)
cl[i1] = dr.cl[i1];
set();
// ボタン
Font f = new Font("TimesRoman", Font.BOLD, 20);
bt_dr = new JButton("OK");
bt_dr.setFont(f);
bt_dr.addActionListener(this);
jp[n-1].add(bt_dr);
}
// 設定
void set()
{
setSize(450, 60*(n));
Container cp = getContentPane();
cp.setBackground(Color.white);
cp.setLayout(new GridLayout(n, 1, 5, 5));
jp = new JPanel[n];
for (int i1 = 0; i1 < n; i1++) {
jp[i1] = new JPanel();
cp.add(jp[i1]);
}
Font f = new Font("TimesRoman", Font.BOLD, 20);
// 色の変更
JLabel lb[][] = new JLabel[n_g][3];
rgb = new TextField[n_g];
r = new TextField[n_g];
g = new TextField[n_g];
b = new TextField[n_g];
for (int i1 = 0; i1 < n_g; i1++) {
rgb[i1] = new TextField(3);
rgb[i1].setFont(f);
rgb[i1].setBackground(new Color(cl[i1].getRed(), cl[i1].getGreen(), cl[i1].getBlue()));
jp[i1].add(rgb[i1]);
lb[i1][0] = new JLabel(" 赤");
lb[i1][0].setFont(f);
jp[i1].add(lb[i1][0]);
r[i1] = new TextField(3);
r[i1].setFont(f);
r[i1].setBackground(Color.white);
r[i1].setText(Integer.toString(cl[i1].getRed()));
r[i1].addTextListener(this);
jp[i1].add(r[i1]);
lb[i1][1] = new JLabel("緑");
lb[i1][1].setFont(f);
jp[i1].add(lb[i1][1]);
g[i1] = new TextField(3);
g[i1].setFont(f);
g[i1].setBackground(Color.white);
g[i1].setText(Integer.toString(cl[i1].getGreen()));
g[i1].addTextListener(this);
jp[i1].add(g[i1]);
lb[i1][2] = new JLabel("青");
lb[i1][2].setFont(f);
jp[i1].add(lb[i1][2]);
b[i1] = new TextField(3);
b[i1].setFont(f);
b[i1].setBackground(Color.white);
b[i1].setText(Integer.toString(cl[i1].getBlue()));
b[i1].addTextListener(this);
jp[i1].add(b[i1]);
}
// 線の変更
if (wd > 0) {
JLabel lb1 = new JLabel("線の太さ:");
lb1.setFont(f);
jp[n_g].add(lb1);
tx = new JTextField(2);
tx.setFont(f);
tx.setBackground(Color.white);
tx.setText(Integer.toString((int)line_w));
jp[n_g].add(tx);
}
if (mk > 0) {
JLabel lb2 = new JLabel("マーク:");
lb2.setFont(f);
jp[n-2].add(lb2);
ButtonGroup gp = new ButtonGroup();
r1 = new JRadioButton("付ける");
r1.setFont(f);
gp.add(r1);
jp[n-2].add(r1);
r2 = new JRadioButton("付けない");
r2.setFont(f);
gp.add(r2);
jp[n-2].add(r2);
if (line_m)
r1.doClick();
else
r2.doClick();
}
}
// TextFieldの内容が変更されたときの処理
public void textValueChanged(TextEvent e)
{
for (int i1 = 0; i1 < n_g; i1++) {
if (e.getSource() == r[i1] || e.getSource() == g[i1] || e.getSource() == b[i1]) {
String str = r[i1].getText();
int rc = str.length()>0 ? Integer.parseInt(str) : 0;
str = g[i1].getText();
int gc = str.length()>0 ? Integer.parseInt(str) : 0;
str = b[i1].getText();
int bc = str.length()>0 ? Integer.parseInt(str) : 0;
rgb[i1].setBackground(new Color(rc, gc, bc));
}
}
}
// 値の設定
public void actionPerformed(ActionEvent e)
{
for (int i1 = 0; i1 < n_g; i1++) {
String str = r[i1].getText();
int rc = str.length()>0 ? Integer.parseInt(str) : 0;
str = g[i1].getText();
int gc = str.length()>0 ? Integer.parseInt(str) : 0;
str = b[i1].getText();
int bc = str.length()>0 ? Integer.parseInt(str) : 0;
dr.cl[i1] = new Color(rc, gc, bc);
}
dr.line_w = Integer.parseInt(tx.getText());
if (r1.isSelected())
dr.line_m = true;
else
dr.line_m = false;
dr.repaint();
setVisible(false);
}
}
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>一様分布</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<SCRIPT TYPE="text/javascript">
method = 0;
function main()
{
let a = parseFloat(document.getElementById("a").value);
let b = parseFloat(document.getElementById("b").value);
let z = new Array();
// 1点
if (method == 0) {
let x = parseFloat(document.getElementById("x").value);
let y = uniform(x, a, b, z);
let str = "P(X = " + x + ") = " + z[0] + "\n";
str = str + "P(X < " + x + ") = " + y;
document.getElementById("xx").value = str;
}
// 複数点
else if (method == 1) {
// データ取得
let h = parseFloat(document.getElementById("sp").value);
let x1 = new Array();
let y1 = new Array();
let y2 = new Array();
let mx = 0;
let n = 0;
for (let x = a; x < b+0.5*h; x += h) {
if (x > b-0.5*h && x < b+0.5*h)
x = b;
let y = uniform(x, a, b, z);
x1[n] = x;
y1[n] = z[0];
y2[n] = y;
if (z[0] > mx)
mx = z[0];
n++;
}
let str1 = "密度関数\n";
let str2 = "分布関数\n";
for (let i1 = 0; i1 < n; i1++) {
str1 = str1 + x1[i1] + " " + y1[i1] + "\n";
str2 = str2 + x1[i1] + " " + y2[i1] + "\n";
}
document.getElementById("xx").value = str1;
document.getElementById("yy").value = str2;
// グラフの描画
let gp1 = "2,一様分布(密度関数),x,f(x),1,グラフ1," + a + "," + b;
let sp1 = (b - a) / 5;
let sp2 = Math.floor(1 / (b - a) / 5 * 100);
if (sp2 % 5 > 0)
sp2 = sp2 + 5 - sp2 % 5;
sp2 /= 100;
mx = sp2 * 5;
gp1 = gp1 + "," + sp1 + ",1,0.0," + mx + "," + sp2 + ",2," + n;
for (let i1 = 0; i1 < n; i1++)
gp1 = gp1 + "," + x1[i1];
for (let i1 = 0; i1 < n; i1++)
gp1 = gp1 + "," + y1[i1];
gp1 = gp1 + ",1,0";
let str = "graph_js.htm?gp=" + gp1;
open(str, "density", "width=950, height=700");
let gp2 = "2,一様分布(分布関数),x,F(x),1,グラフ1," + a + "," + b;
sp2 = 0.2;
mx = 1.0;
gp2 = gp2 + "," + sp1 + ",1,0.0," + mx + "," + sp2 + ",1," + n;
for (let i1 = 0; i1 < n; i1++)
gp2 = gp2 + "," + x1[i1];
for (let i1 = 0; i1 < n; i1++)
gp2 = gp2 + "," + y2[i1];
gp2 = gp2 + ",1,0";
str = "graph_js.htm?gp=" + gp2;
open(str, "distribution", "width=950, height=700");
}
// %値
else {
let x = parseFloat(document.getElementById("p").value);
let p = x / 100.0;
let y = b - p * (b - a);
str = x + "%値 = " + y;
document.getElementById("xx").value = str;
}
}
/****************************************/
/* 一様分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* a,b : 区間 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
function uniform(x, a, b, pr)
{
let f = 0.0;
let y;
if (x < a) {
pr[0] = 0.0;
f = 0.0;
}
else if (x > b) {
pr[0] = 0.0;
f = 1.0;
}
else {
y = 1.0 / (b - a);
pr[0] = y;
f = y * (x - a);
}
return f;
}
/********/
/* 目的 */
/********/
function set_m()
{
let sel = document.getElementById("method");
for (let i1 = 0; i1 < 3; i1++) {
if (sel.options[i1].selected) {
method = i1;
break;
}
}
// 1点
if (method == 0) {
document.getElementById("x_t").style.display = "";
document.getElementById("p_t").style.display = "none";
document.getElementById("sp_t").style.display = "none";
document.getElementById("yy").style.display = "none";
}
// 複数点
else if (method == 1) {
document.getElementById("x_t").style.display = "none";
document.getElementById("p_t").style.display = "none";
document.getElementById("sp_t").style.display = "";
document.getElementById("yy").style.display = "";
}
// %値
else {
document.getElementById("x_t").style.display = "none";
document.getElementById("p_t").style.display = "";
document.getElementById("sp_t").style.display = "none";
document.getElementById("yy").style.display = "none";
}
}
</SCRIPT>
</HEAD>
<BODY STYLE="font-size: 130%; text-align:center; background-color: #eeffee;">
<H2 STYLE="text-align:center"><B>一様分布</B></H2>
下限:<INPUT ID="a" STYLE="font-size: 100%" TYPE="text" SIZE="3" VALUE="0">
上限:<INPUT ID="b" STYLE="font-size: 100%" TYPE="text" SIZE="3" VALUE="1">
<SPAN ID="x_t">x:<INPUT ID="x" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="0.5"> </SPAN>
<SPAN ID="p_t" STYLE="display: none">%値:<INPUT ID="p" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="2.5"> </SPAN>
<SPAN ID="sp_t" STYLE="display: none">刻み幅:<INPUT ID="sp" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="0.1"> </SPAN>
目的:<SELECT ID="method" onChange="set_m()" STYLE="font-size:100%">
<OPTION SELECTED>確率(1点)
<OPTION>確率(複数点)
<OPTION>p%値
</SELECT>
<BUTTON STYLE="font-size: 100%; background-color: pink" onClick="main()">OK</BUTTON><BR><BR>
<TEXTAREA ID="xx" COLS="40" ROWS="30" STYLE="font-size: 100%"></TEXTAREA>
<TEXTAREA ID="yy" COLS="40" ROWS="30" STYLE="font-size: 100%; display: none"></TEXTAREA>
</BODY>
</HTML>
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>グラフの表示</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<LINK REL="stylesheet" TYPE="text/css" HREF="../master.css">
<SCRIPT TYPE="text/javascript" SRC="graph.js"></SCRIPT>
<SCRIPT TYPE="text/javascript">
function GetParameter()
{
let result = new Array();
if(1 < window.location.search.length) {
// 最初の1文字 (?記号) を除いた文字列を取得する
let str = window.location.search.substring(1);
// 区切り記号 (&) で文字列を配列に分割する
let param = str.split('&');
for(let i1 = 0; i1 < param.length; i1++ ) {
// パラメータ名とパラメータ値に分割する
let element = param[i1].split('=');
let Name = decodeURIComponent(element[0]);
let Value = decodeURIComponent(element[1]);
// パラメータ名をキーとして連想配列に追加する
result[Name] = Value;
}
}
return result;
}
</SCRIPT>
</HEAD>
<BODY CLASS="white" STYLE="text-align: center">
<DIV ID="cl_line" STYLE="text-align: center; display: none">
<FORM>
<DIV ID="c0">
<INPUT ID="rgb0" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)">
緑<INPUT ID="g0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)">
青<INPUT ID="b0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)">
</DIV>
<DIV ID="c1">
<INPUT ID="rgb1" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)">
緑<INPUT ID="g1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)">
青<INPUT ID="b1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)">
</DIV>
<DIV ID="c2">
<INPUT ID="rgb2" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)">
緑<INPUT ID="g2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)">
青<INPUT ID="b2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)">
</DIV>
<DIV ID="c3">
<INPUT ID="rgb3" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)">
緑<INPUT ID="g3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)">
青<INPUT ID="b3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)">
</DIV>
<DIV ID="c4">
<INPUT ID="rgb4" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)">
緑<INPUT ID="g4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)">
青<INPUT ID="b4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)">
</DIV>
<DIV ID="c5">
<INPUT ID="rgb5" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)">
緑<INPUT ID="g5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)">
青<INPUT ID="b5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)">
</DIV>
<DIV ID="c6">
<INPUT ID="rgb6" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)">
緑<INPUT ID="g6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)">
青<INPUT ID="b6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)">
</DIV>
<DIV ID="c7">
<INPUT ID="rgb7" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)">
緑<INPUT ID="g7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)">
青<INPUT ID="b7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)">
</DIV>
<DIV ID="c8">
<INPUT ID="rgb8" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)">
緑<INPUT ID="g8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)">
青<INPUT ID="b8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)">
</DIV>
<DIV ID="c9">
<INPUT ID="rgb9" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)">
緑<INPUT ID="g9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)">
青<INPUT ID="b9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)">
</DIV>
<DIV ID="line_m">
マーク:<INPUT ID="l_m1" TYPE="radio" NAME="mark" STYLE="font-size: 90%" CHECKED>付ける
<INPUT ID="l_m2" TYPE="radio" NAME="mark" STYLE="font-size: 90%">付けない
</DIV>
<DIV ID="line_w">
線の太さ:<INPUT ID="l_w" TYPE="text" SIZE="3" STYLE="font-size: 90%" VALUE="2">
</DIV>
<DIV>
<SPAN STYLE="background-color: pink; font-size: 100%" onClick="D_Change()">OK</SPAN>
</DIV>
</FORM>
</DIV>
<BR>
<DIV STYLE="text-align: center">
<CANVAS ID="canvas_e" STYLE="background-color: #eeffee;" WIDTH="900" HEIGHT="600" onClick="Click(event)"></CANVAS>
</DIV>
<SCRIPT TYPE="text/javascript">
let result = GetParameter();
graph(result['gp']);
</SCRIPT>
</BODY>
</HTML>
<?php
/****************************/
/* 一様分布の計算 */
/* coded by Y.Suganuma */
/****************************/
/****************************************/
/* 一様分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* a,b : 区間 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
function uniform($x, $a, $b, &$pr)
{
$f = 0.0;
if ($x < $a) {
$pr = 0.0;
$f = 0.0;
}
else if ($x > $b) {
$pr = 0.0;
$f = 1.0;
}
else {
$y = 1.0 / ($b - $a);
$pr = $y;
$f = $y * ($x - $a);
}
return $f;
}
/********/
/* main */
/********/
printf("下限(a)は? ");
fscanf(STDIN, "%lf", $a);
printf("上限(b)は? ");
fscanf(STDIN, "%lf", $b);
printf("目的とする結果は? \n");
printf(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n");
printf(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
fscanf(STDIN, "%d", $sw);
if ($sw == 0) {
printf("グラフ出力?(=1: yes, =0: no) ");
fscanf(STDIN, "%d", $sw);
// 密度関数と分布関数の値
if ($sw == 0) {
printf(" データは? ");
fscanf(STDIN, "%lf", $x);
$f = uniform($x, $a, $b, $pr);
printf("P(X = %f) = %f, P( X < %f) = %f (区間 [%f, %f])\n", $x, $pr, $x, $f, $a, $b);
}
// グラフ出力
else {
printf(" 密度関数のファイル名は? ");
fscanf(STDIN, "%s", $file1);
printf(" 分布関数のファイル名は? ");
fscanf(STDIN, "%s", $file2);
$out1 = fopen($file1,"wb");
$out2 = fopen($file2,"wb");
printf(" 刻み幅は? ");
fscanf(STDIN, "%lf", $h);
for ($x = $a; $x < $b+0.5*$h; $x += $h) {
if ($x > $b-0.5*$h && $x < $b+0.5*$h)
$x = $b;
$f = uniform($x, $a, $b, $pr);
fwrite($out1, $x." ".$pr."\n");
fwrite($out2, $x." ".$f."\n");
}
}
}
// %値
else {
printf("%の値は? ");
fscanf(STDIN, "%lf", $x);
$p = 0.01 * $x;
$f = $b - $p * ($b - $a);
printf("%f%値 = %f (区間 [%f, %f])\n", $x, $f, $a, $b);
}
?>
##########################################
# 一様分布の計算
# coded by Y.Suganuma
##########################################
##########################################
# 一様分布の計算(P(X = x), P(X < x))
# x : データ
# a,b : 区間
# pr : P(X = x)
# return : P(X < x)
##########################################
def uniform(x, a, b, pr)
f = 0.0
if x < a
pr[0] = 0.0
f = 0.0
elsif x > b
pr[0] = 0.0
f = 1.0
else
y = 1.0 / (b - a)
pr[0] = y
f = y * (x - a)
end
return f
end
print("下限(a)は? ")
a = Float(gets())
print("上限(b)は? ")
b = Float(gets())
print("目的とする結果は?\n")
print(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n")
print(" =1 : p%値( P(X > u) = 0.01p となるuの値) ")
sw = Integer(gets())
pr = Array.new(1)
if sw == 0
print("グラフ出力?(=1: yes, =0: no) ")
sw = Integer(gets())
if sw == 0
# 密度関数と分布関数の値
print(" データは? ")
x = Float(gets())
f = uniform(x, a, b, pr)
print("P(X = " + String(x) + ") = " + String(pr[0]) + ", P( X < " + String(x) + ") = " + String(f) + " (区間 [" + String(a) + ", " + String(b) + "])\n")
# グラフ出力
else
print(" 密度関数のファイル名は? ")
file1 = gets().strip()
print(" 分布関数のファイル名は? ")
file2 = gets().strip()
print(" データの上限は? ")
up = Integer(gets())
print(" 刻み幅は? ")
h = Float(gets())
out1 = open(file1, "w")
out2 = open(file2, "w")
x = a
while x < b+0.5*h
if x > b-0.5*h and x < b+0.5*h
x = b
end
f = uniform(x, a, b, pr)
out1.print(String(x) + " " + String(pr[0]) + "\n")
out2.print(String(x) + " " + String(f) + "\n")
x += h
end
out1.close()
out2.close()
end
# %値
else
print("%の値は? ")
x = Float(gets())
p = 0.01 * x
f = b - p * (b - a)
print(String(x) + "%値 = " + String(f) + " (区間 [" + String(a) + ", " + String(b) + "])")
end
# -*- coding: UTF-8 -*-
import numpy as np
import sys
from math import *
##########################################
# 一様分布の計算(P(X = x), P(X < x))
# x : データ
# a,b : 区間
# pr : P(X = x)
# return : P(X < x)
##########################################
def uniform(x, a, b, pr) :
f = 0.0
if x < a :
pr[0] = 0.0
f = 0.0
elif x > b :
pr[0] = 0.0
f = 1.0
else :
y = 1.0 / (b - a)
pr[0] = y
f = y * (x - a)
return f
##########################################
# 一様分布の計算
# coded by Y.Suganuma
##########################################
s = input("下限(a)は? ")
a = float(s)
s = input("上限(b)は? ")
b = float(s)
print("目的とする結果は? ")
print(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)")
s = input(" =1 : p%値( P(X > u) = 0.01p となるuの値) ")
sw = int(s)
pr = np.empty(1, np.float)
if sw == 0 :
s = input("グラフ出力?(=1: yes, =0: no) ")
sw = int(s)
if sw == 0 :
# 密度関数と分布関数の値
s = input(" データは? ")
x = float(s)
f = uniform(x, a, b, pr)
print("P(X = " + str(x) + ") = " + str(pr[0]) + ", P( X < " + str(x) + ") = " + str(f) + " (区間 [" + str(a) + ", " + str(b) + "])")
# グラフ出力
else :
file1 = input(" 密度関数のファイル名は? ")
file2 = input(" 分布関数のファイル名は? ")
s = input(" データの上限は? ")
up = int(s)
s = input(" 刻み幅は? ")
h = float(s)
out1 = open(file1, "w")
out2 = open(file2, "w")
x = a
while x < b+0.5*h :
if x > b-0.5*h and x < b+0.5*h :
x = b
f = uniform(x, a, b, pr)
out1.write(str(x) + " " + str(pr[0]) + "\n")
out2.write(str(x) + " " + str(f) + "\n")
x += h
out1.close()
out2.close()
# %値
else :
s = input("%の値は? ")
x = float(s)
p = 0.01 * x
f = b - p * (b - a)
print(str(x) + "%値 = " + str(f) + " (区間 [" + str(a) + ", " + str(b) + "])")
/****************************/
/* 一様分布の計算 */
/* coded by Y.Suganuma */
/****************************/
using System;
using System.IO;
class Program
{
static void Main()
{
Test1 ts = new Test1();
}
}
class Test1
{
/********/
/* main */
/********/
public Test1()
{
Console.Write("下限(a)? ");
double a = double.Parse(Console.ReadLine());
Console.Write("上限(b)? ");
double b = double.Parse(Console.ReadLine());
Console.WriteLine("目的とする結果は? ");
Console.WriteLine(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)");
Console.Write(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
int sw = int.Parse(Console.ReadLine());
if (sw == 0) {
double pr = 0.0, f;
Console.Write("グラフ出力?(=1: yes, =0: no) ");
sw = int.Parse(Console.ReadLine());
// 密度関数と分布関数の値
if (sw == 0) {
Console.Write(" データは? ");
double x = double.Parse(Console.ReadLine());
f = uniform(x, a, b, ref pr);
Console.WriteLine("P(X = " + x + ") = " + pr + ", P( X < " + x + ") = " + f + " (区間 [" + a + ", " + b + "])");
}
// グラフ出力
else {
Console.Write(" 密度関数のファイル名は? ");
String file1 = Console.ReadLine();
Console.Write(" 分布関数のファイル名は? ");
String file2 = Console.ReadLine();
Console.Write(" 刻み幅は? ");
double h = double.Parse(Console.ReadLine());
StreamWriter out1 = new StreamWriter(file1);
StreamWriter out2 = new StreamWriter(file2);
// データ取得
for (double x = a; x < b+0.5*h; x += h) {
if (x > b-0.5*h && x < b+0.5*h)
x = b;
f = uniform(x, a, b, ref pr);
out1.WriteLine(x + " " + pr);
out2.WriteLine(x + " " + f);
}
out1.Close();
out2.Close();
}
}
// %値
else {
Console.Write("%の値は? ");
double x = double.Parse(Console.ReadLine());
double p = 0.01 * x;
double f = b - p * (b - a);
Console.WriteLine(x + "%値 = " + f + " (区間 [" + a + ", " + b + "])");
}
}
/****************************************/
/* 一様分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* a,b : 区間 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
double uniform(double x, double a, double b, ref double pr)
{
double f = 0.0;
if (x < a)
pr = 0.0;
else if (x > b) {
pr = 0.0;
f = 1.0;
}
else {
double y = 1.0 / (b - a);
pr = y;
f = y * (x - a);
}
return f;
}
}
'**************************'
' 一様分布の計算 '
' coded by Y.Suganuma '
'**************************'
Imports System.IO
Module Test
Sub Main()
Console.Write("下限(a)? ")
Dim a As Double = Double.Parse(Console.ReadLine())
Console.Write("上限(b)? ")
Dim b As Double = Double.Parse(Console.ReadLine())
Console.WriteLine("目的とする結果は? ")
Console.WriteLine(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)")
Console.Write(" =1 : p%値( P(X > u) = 0.01p となるuの値) ")
Dim sw As Integer = Integer.Parse(Console.ReadLine())
If sw = 0
Dim pr As Double = 0.0
Dim f As Double
Console.Write("グラフ出力?(=1: yes, =0: no) ")
sw = Integer.Parse(Console.ReadLine())
' 密度関数と分布関数の値
If sw = 0
Console.Write(" データは? ")
Dim x As Double = Double.Parse(Console.ReadLine())
f = unIform(x, a, b, pr)
Console.WriteLine("P(X = " & x & ") = " & pr & ", P( X < " & x & ") = " & f & " (区間 [" & a & ", " & b & "])")
' グラフ出力
Else
Console.Write(" 密度関数のファイル名は? ")
Dim file1 As String = Console.ReadLine().Trim()
Console.Write(" 分布関数のファイル名は? ")
Dim file2 As String = Console.ReadLine().Trim()
Console.Write(" 刻み幅は? ")
Dim h As Double = Double.Parse(Console.ReadLine())
Dim out1 As StreamWriter = new StreamWriter(file1)
Dim out2 As StreamWriter = new StreamWriter(file2)
' データ取得
Dim x As Double = a
Do While x < b+0.5*h
If x > b-0.5*h and x < b+0.5*h
x = b
End If
f = unIform(x, a, b, pr)
out1.WriteLine(x & " " & pr)
out2.WriteLine(x & " " & f)
x += h
Loop
out1.Close()
out2.Close()
End If
' %値
Else
Console.Write("%の値は? ")
Dim x As Double = Double.Parse(Console.ReadLine())
Dim p As Double = 0.01 * x
Dim f As Double = b - p * (b - a)
Console.WriteLine(x & "%値 = " & f & " (区間 [" & a & ", " & b & "])")
End If
End Sub
'**************************************'
' 一様分布の計算(P(X = x), P(X < x)) '
' x : データ '
' a,b : 区間 '
' pr : P(X = x) '
' return : P(X < x) '
'**************************************'
Function unIform(x As Double, a As Double, b As Double, ByRef pr As Double)
Dim f As Double = 0.0
If x < a
pr = 0.0
ElseIf x > b
pr = 0.0
f = 1.0
Else
Dim y As Double = 1.0 / (b - a)
pr = y
f = y * (x - a)
End If
Return f
End Function
End Module
| 情報学部 | 菅沼ホーム | 目次 | 索引 |