#import <Foundation/Foundation.h>

int main()
{
    printf("%d\n", 3 + 5);
    printf("%d\n", 3 - 5);
    printf("%d\n", 3 * 5);
    printf("%d\n", (int)pow(3, 5));
    printf("%d\n", 5 / 3);
    printf("%f\n", 5.0 / 3);
    printf("%f\n", 5 / 3.0);
    printf("%d\n", 5 % 3);

    printf("%3d\n",     3 * 5);
    printf("%23.20f\n", 5 / 3.0);

    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    int i = 3 * 5;
    printf("3 * 5 = %d\n", i);
    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    int i;
    for (i = 1; i < 10; i++)
    {
        printf("%d, ", i);
    }
    printf("\n");

    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    int i;
    for (i = 1; i < 10; i++)
    {
        if (i % 3 == 0)
        {
            printf("%d, ", i);
        }
    }
    printf("\n");

    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    int sum = 0;
    int i;
    for (i = 1; i < 100; i++)
    {
        if (i % 3 == 0)
        {
            sum += i;
        }
    }
    printf("%d\n", sum);

    return 0;
}
#import <Foundation/Foundation.h>

int sn(int a, int lim);

int main (int argc, const char * argv[])
{
    // 3 の倍数の合計
    printf("%d\n", sn(3, 999));
    return 0;
}
// 初項:a, 公差:a で, 上限:lim の数列の総和を返す関数
int sn(int a, int lim)
{
    int n = lim / a;        // 項数:n  =  上限:lim / 公差:a
    int l = n * a;          // 末項:l  =  項数:n   * 公差:a
    return (a + l) * n / 2; // 総和:sn = (初項:a   + 末項:l) * 項数:n / 2
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    // 10000 までの 自然数の和
    // 項目数 n = 10000
    int n = 10000;
    printf("%d\n", (n * (n + 1) / 2) );
    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    // 10000 までの 偶数の和
    // 項目数 n = 5000
    int n = 10000 / 2;
    printf("%d\n", (n * (n + 1)) );
    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    // 10000 までの 奇数の和
    // 項目数 n = 5000
    int n = 10000 / 2;
    printf("%d\n", (int)pow(n, 2) );
    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    // 1000 までの 自然数の2乗の和
    int n = 1000;
    printf("%d\n", (n * (n + 1) * (2 * n + 1) / 6) );
    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    // 100 までの 自然数の3乗の和
    int n = 100;
    printf("%d\n", (int)(pow(n, 2) * pow((n + 1), 2) / 4) );
    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    // 初項 2, 公比 3, 項数 10 の等比数列の和
    int n = 10;
    int a = 2;
    int r = 3;
    printf("%d\n", (int)((a * (pow(r, n) - 1)) / (r - 1)) );
    return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    int a = 5;  // 初項 5
    int d = 3;  // 公差 3
    int n = 10; // 項数 10
    long p = 1; // 積

    int i;
    for (i = 1; i <= n; i++)
    {
        int m = a + (d * (i - 1));
        p *= m;
    }
    printf("%ld\n", p);

    return 0;
}
#import <Foundation/Foundation.h>

long product(int m, int d, int n);

int main (int argc, const char * argv[])
{
    // 初項 5, 公差 3, 項数 10 の数列の積を表示する
    printf("%ld\n", product(5, 3, 10));
    return 0;
}
long product(int m, int d, int n)
{
    if (n == 0)
        return 1;
    else
        return m * product(m + d, d, n - 1);
}
#import <Foundation/Foundation.h>

// 階乗を求める関数
int Fact(int n)
{
    if (n <= 1)
        return 1;
    else
        return n * Fact(n - 1);
}

int main()
{
    // 10の階乗
    printf("%d\n", Fact(10));
    printf("%d\n", 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1);
    return 0;
}
#import <Foundation/Foundation.h>

// 下降階乗冪
int FallingFact(int x, int n)
{
    if (n <= 1)
        return x;
    else
        return x * FallingFact(x - 1, n - 1);
}

int main()
{
    // 10 から 6 までの 総乗
    printf("%d\n", FallingFact(10, 5));
    printf("%d\n", 10 * 9 * 8 * 7 * 6);
    return 0;
}
#import <Foundation/Foundation.h>

// 上昇階乗冪
int RisingFact(int x, int n)
{
    if (n <= 1)
        return x;
    else
        return x * RisingFact(x + 1, n - 1);
}

int main()
{
    // 10 から 14 までの 総乗
    printf("%d\n", RisingFact(10, 5));
    printf("%d\n", 10 * 11 * 12 * 13 * 14);
    return 0;
}
#import <Foundation/Foundation.h>

// 階乗
int Fact(int n)
{
    if (n <= 1)
        return 1;
    else
        return n * Fact(n - 1);
}

// 下降階乗冪
int FallingFact(int x, int n)
{
    if (n <= 1)
        return x;
    else
        return x * FallingFact(x - 1, n - 1);
}

int main()
{
    // 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
    int n = 10;
    int r = 5;
    printf("%d\n", (Fact(n) / Fact(n - r)));
    printf("%d\n", FallingFact(n, r));
    return 0;
}
#import <Foundation/Foundation.h>

int main()
{
    // 重複順列 (異なる 10 個のものから重複を許して 5 個取ってできる順列の総数)
    int n = 10;
    int r = 5;
    printf("%d\n", (int)pow(n, r) );
    return 0;
}
#import <Foundation/Foundation.h>

// 組合せ
int Comb(int n, int r)
{
    if (r == 0 || r == n)
        return 1;
    else if (r == 1)
        return n;
    else
        return Comb(n - 1, r - 1) + Comb(n - 1, r);
}

int main()
{
    // 組合せ (異なる 10 個のものから 5 個取ってできる組合せの総数)
    printf("%d\n", Comb(n, r));
    return 0;
}
#import <Foundation/Foundation.h>

// 組合せ
int Comb(int n, int r)
{
    if (r == 0 || r == n)
        return 1;
    else if (r == 1)
        return n;
    else
        return Comb(n - 1, r - 1) + Comb(n - 1, r);
}

int main()
{
    // 重複組合せ (異なる 10 個のものから重複を許して 5 個とる組合せの総数)
    int n = 10;
    int r = 5;
    printf("%d\n", Comb(n + r - 1, r));

    return 0;
}
#import <Foundation/Foundation.h>

double mySin(double x, int n, bool nega, double numerator, double denominator, double y);

int main()
{
    int degree;
    for (degree = 0; degree <= 360; degree += 15)
    {
        if (degree % 30 == 0 || degree % 45 == 0)
        {
            double radian = degree * M_PI / 180.0;
            // 自作の正弦関数
            double d1     = mySin(radian, 1, false, radian, 1.0, radian);
            // 標準の正弦関数
            double d2     = sin(radian);
            // 標準関数との差異
            printf("%03d : %+13.10f - %+13.10f = %+13.10f\n", degree, d1, d2, d1 - d2);
        }
    }
    return 0;
}

// 自作の正弦関数
double mySin(double x, int n, bool nega, double numerator, double denominator, double y)
{
    int m       = 2 * n;
    denominator = denominator * (m + 1) * m;
    numerator   = numerator   * x * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001)
        return y;
    else
        return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a);
}
#import <Foundation/Foundation.h>

double myCos(double x, int n, bool nega, double numerator, double denominator, double y);

int main()
{
    int degree;
    for (degree = 0; degree <= 360; degree += 15)
    {
        if (degree % 30 == 0 || degree % 45 == 0)
        {
            double radian = degree * M_PI / 180.0;
            // 自作の余弦関数
            double d1     = myCos(radian, 1, false, 1.0, 1.0, 1.0);
            // 標準の余弦関数
            double d2     = cos(radian);
            // 標準関数との差異
            printf("%03d : %+13.10f - %+13.10f = %+13.10f\n", degree, d1, d2, d1 - d2);
        }
    }
    return 0;
}

// 自作の余弦関数
double myCos(double x, int n, bool nega, double numerator, double denominator, double y)
{
    int m       = 2 * n;
    denominator = denominator * m * (m - 1);
    numerator   = numerator   * x * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001)
        return y;
    else
        return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -a);
}
#import <Foundation/Foundation.h>

double myTan(double x, double x2, int n, double t);

int main()
{
    int degree;
    for (degree = -90; degree <= 90; degree += 15)
    {
        if ((degree + 90) % 180 != 0)
        {
            double radian = degree * M_PI / 180.0;
            double x2     = radian * radian;
            // 自作の正接関数
            double d1     = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数
            // 標準の正接関数
            double d2     = tan(radian);
            // 標準関数との差異
            printf("%+03d : %+13.10f - %+13.10f = %+13.10f\n", degree, d1, d2, d1 - d2);
        }
    }
    return 0;
}

// 自作の正接関数
double myTan(double x, double x2, int n, double t)
{
    t = x2 / (n - t);
    n -= 2;
    if (n <= 1)
        return x / (1 - t);
    else
        return myTan(x, x2, n, t);
}
#import <Foundation/Foundation.h>

double myExp(double x, int n, double numerator, double denominator, double y);

int main()
{
    int i;
    for (i = -10; i <= 10; i++)
    {
        double x   = i / 4.0;
        // 標準の指数関数
        double d1  = exp(x);
        // 自作の指数関数
        double d2  = myExp(x, 1, 1.0, 1.0, 1.0);
        // 標準関数との差異
        printf("%+04.2f : %+014.10f - %+014.10f = %+13.10f\n", x, d1, d2, d1 - d2);
    }
    return 0;
}

// 自作の指数関数
double myExp(double x, int n, double numerator, double denominator, double y)
{
    denominator = denominator * n;
    numerator   = numerator   * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (fabs(a) <= 0.00000000001)
        return y;
    else
        return y + myExp(x, ++n, numerator, denominator, a);
}
#import <Foundation/Foundation.h>

double myExp(double x, double x2, int n, double t);

int main()
{
    int i;
    for (i = -10; i <= 10; i++)
    {
        double x  = i / 4.0;
        // 標準の指数関数
        double d1 = exp(x);
        // 自作の指数関数
        double x2 = x * x;
        double d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる
        // 標準関数との差異
        printf("%+04.2f : %+014.10f - %+014.10f = %+13.10f\n", x, d1, d2, d1 - d2);
    }
    return 0;
}

// 自作の指数関数
double myExp(double x, double x2, int n, double t)
{
    t = x2 / (n + t);
    n -= 4;

    if (n < 6)
        return 1 + ((2 * x) / (2 - x + t));
    else
        return myExp(x, x2, n, t);
}
#import <Foundation/Foundation.h>

double myLog(double x2, double numerator, double denominator, double y);

int main()
{
    int i;
    for (i = 1; i <= 20; i++)
    {
        double x  = i / 5.0;
        // 標準の対数関数
        double d1 = log(x);
        // 自作の対数関数
        double x2 = (x - 1) / (x + 1);
        double d2 = 2 * myLog(x2, x2, 1.0, x2);
        // 標準関数との差異
        printf("%+04.2f : %+013.10f - %+013.10f = %+13.10f\n", x, d1, d2, d1 - d2);
    }
    return 0;
}

// 自作の対数関数
double myLog(double x2, double numerator, double denominator, double y)
{
    denominator = denominator + 2;
    numerator   = numerator   * x2 * x2;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (fabs(a) <= 0.00000000001)
        return y;
    else
        return y + myLog(x2, numerator, denominator, a);
}
#import <Foundation/Foundation.h>

double myLog(double x, int n, double t);

int main()
{
    int i;
    for (i = 1; i <= 20; i++)
    {
        double x  = i / 5.0;
        // 標準の対数関数
        double d1 = log(x);
        // 自作の対数関数
        double d2 = myLog(x - 1, 27, 0.0); // 27:必要な精度が得られる十分大きな奇数
        // 標準関数との差異
        printf("%+04.2f : %+013.10f - %+013.10f = %+13.10f\n", x, d1, d2, d1 - d2);
    }
    return 0;
}

// 自作の対数関数
double myLog(double x, int n, double t)
{
    int    n2 = n;
    double x2 = x;
    if (n > 3)
    {
        if (n % 2 == 0)
            n2 = 2;
        x2 = x * (n / 2);
    }
    t = x2 / (n2 + t);

    if (n <= 2)
        return x / (1 + t);
    else
        return myLog(x, --n, t);
}
#import <Foundation/Foundation.h>

double mySinh(double x, int n, double numerator, double denominator, double y);

int main()
{
    int x;
    for (x = -10; x <= 10; x++)
    {
        // 自作の双曲線正弦関数
        double d1 = mySinh(x, 1, x, 1.0, x);
        // 標準の双曲線正弦関数
        double d2 = sinh(x);
        // 標準関数との差異
        printf("%+03d : %+017.10f - %+017.10f = %+13.10f\n", x, d1, d2, d1 - d2);
    }
    return 0;
}

// 自作の双曲線正弦関数
double mySinh(double x, int n, double numerator, double denominator, double y)
{
    int m       = 2 * n;
    denominator = denominator * (m + 1) * m;
    numerator   = numerator   * x * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (fabs(a) <= 0.00000000001)
        return y;
    else
        return y + mySinh(x, ++n, numerator, denominator, a);
}
#import <Foundation/Foundation.h>

double myCosh(double x, int n, double numerator, double denominator, double y);

int main()
{
    int x;
    for (x = -10; x <= 10; x++)
    {
        // 自作の双曲線余弦関数
        double d1 = myCosh(x, 1, 1.0, 1.0, 1.0);
        // 標準の双曲線余弦関数
        double d2 = cosh(x);
        // 標準関数との差異
        printf("%+03d : %+017.10f - %+017.10f = %+13.10f\n", x, d1, d2, d1 - d2);
    }
    return 0;
}

// 自作の双曲線余弦関数
double myCosh(double x, int n, double numerator, double denominator, double y)
{
    int m       = 2 * n;
    denominator = denominator * m * (m - 1);
    numerator   = numerator   * x * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (fabs(a) <= 0.00000000001)
        return y;
    else
        return y + myCosh(x, ++n, numerator, denominator, a);
}
#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return 4 / (1 + x * x);
}

int main()
{
    const double a = 0;
    const double b = 1;

    // 台形則で積分
    int n = 2;
    int i, j;
    for (j = 1; j <= 10; j++)
    {
        double h = (b - a) / n;
        double s = 0;
        double x = a;
        for (i = 1; i <= n - 1; i++)
        {
            x += h;
            s += f(x);
        }
        s = h * ((f(a) + f(b)) / 2 + s);
        n *= 2;

        // 結果を π と比較
        printf("%2d : %13.10f, %13.10f\n", j, s, s - M_PI);
    }

    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return 4 / (1 + x * x);
}

int main()
{
    const double a = 0;
    const double b = 1;

    // 中点則で積分
    int n = 2;
    int i, j;
    for (j = 1; j <= 10; j++)
    {
        double h = (b - a) / n;
        double s = 0;
        double x = a + (h / 2);
        for (i = 1; i <= n; i++)
        {
            s += f(x);
            x += h;
        }
        s *= h;
        n *= 2;

        // 結果を π と比較
        printf("%2d : %13.10f, %13.10f\n", j, s, s - M_PI);
    }

    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return 4 / (1 + x * x);
}

int main()
{
    const double a = 0;
    const double b = 1;

    // Simpson則で積分
    int n = 2;
    int i, j;
    for (j = 1; j <= 5; j++)
    {
        double h = (b - a) / n;
        double s2 = 0;
        double s4 = 0;
        double x = a + h;
        for (i = 1; i <= n / 2; i++)
        {
            s4 += f(x);
            x  += h;
            s2 += f(x);
            x  += h;
        }
        s2 = (s2 - f(b)) * 2 + f(a) + f(b);
        s4 *= 4;
        double s = (s2 + s4) * h / 3;
        n *= 2;

        // 結果を π と比較
        printf("%2d : %13.10f, %13.10f\n", j, s, s - M_PI);
    }

    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return 4 / (1 + x * x);
}

int main()
{
    const double a = 0;
    const double b = 1;

    double t[7][7];

    // 台形則で積分
    int n = 2;
    int i, j;
    for (i = 1; i <= 6; i++)
    {
        double h = (b - a) / n;
        double s = 0;
        double x = a;
        for (j = 1; j <= n - 1; j++)
        {
            x += h;
            s += f(x);
        }
        // 結果を保存
        t[i][1] = h * ((f(a) + f(b)) / 2 + s);
        n *= 2;
    }

    // Richardsonの補外法
    n = 4;
    for (j = 2; j <= 6; j++)
    {
        for (i = j; i <= 6; i++)
        {
            t[i][j] = t[i][j - 1] + (t[i][j - 1] - t[i - 1][j - 1]) / (n - 1);
            if (i == j)
            {
                // 結果を π と比較
                printf("%2d : %13.10f, %13.10f\n", j, t[i][j], t[i][j] - M_PI);
            }
        }
        n *= 4;
    }

    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

// データ点の数
const int N = 7;

// 元の関数
double f(double x);

// Lagrange (ラグランジュ) 補間
double lagrange(double d, double x[], double y[]);

int main()
{
    double x[N], y[N];

    // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
    int i;
    for (i = 0; i < N; i++)
    {
        double d = i * 1.5 - 4.5;
        x[i] = d;
        y[i] = f(d);
    }

    // 0.5刻みで 与えられていない値を補間
    for (i = 0; i <= 18; i++)
    {
        double d  = i * 0.5 - 4.5;
        double d1 = f(d);
        double d2 = lagrange(d, x, y);

        // 元の関数と比較
        printf("%5.2f\t%8.5f\t%8.5f\t%8.5f\n", d, d1, d2, d1 - d2);
    }

   return 0;
}

// 元の関数
double f(double x)
{
    return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2);
}

// Lagrange (ラグランジュ) 補間
double lagrange(double d, double x[], double y[])
{
    int i, j;
    double sum = 0;
    for (i = 0; i < N; i++)
    {
        double prod = y[i];
        for (j = 0; j < N; j++)
        {
            if (j != i)
                prod *= (d - x[j]) / (x[i] - x[j]);
        }
        sum += prod;
    }
    return sum;
}
#import <Foundation/Foundation.h>
#import <math.h>

// データ点の数
const int N = 7;

// 元の関数
double f(double x);

// Neville (ネヴィル) 補間
double neville(double d, double x[], double y[]);

int main()
{
    double x[N], y[N];

    // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
    int i;
    for (i = 0; i < N; i++)
    {
        double d = i * 1.5 - 4.5;
        x[i] = d;
        y[i] = f(d);
    }

    // 0.5刻みで 与えられていない値を補間
    for (i = 0; i <= 18; i++)
    {
        double d  = i * 0.5 - 4.5;
        double d1 = f(d);
        double d2 = neville(d, x, y);

        // 元の関数と比較
        printf("%5.2f\t%8.5f\t%8.5f\t%8.5f\n", d, d1, d2, d1 - d2);
    }

   return 0;
}

// 元の関数
double f(double x)
{
    return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2);
}

// Neville (ネヴィル) 補間
double neville(double d, double x[], double y[])
{
    int i, j;
    double w[N][N];
    for (i = 0; i < N; i++)
        w[0][i] = y[i];

    for (j = 1; j < N; j++)
    {
        for (i = 0; i < N - j; i++)
              w[j][i] = w[j-1][i+1] + (w[j-1][i+1] - w[j-1][i]) * (d - x[i+j]) / (x[i+j] - x[i]);
    }

    return w[N-1][0];
}
#import <Foundation/Foundation.h>
#import <math.h>

// データ点の数
const int N = 7;

// 元の関数
double f(double x);

// Newton (ニュートン) 補間
double newton(double d, double x[], double a[]);

int main()
{
    int i, j;
    double x[N], y[N];

    // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
    for (i = 0; i < N; i++)
    {
        double d = i * 1.5 - 4.5;
        x[i] = d;
        y[i] = f(d);
    }

    // 差分商の表を作る
    double d[N][N];
    for (j = 0; j < N; j++)
        d[0][j] = y[j];

    for (i = 1; i < N; i++)
    {
        for (j = 0; j < N - i; j++)
            d[i][j] = (d[i-1][j+1] - d[i-1][j]) / (x[j+i] - x[j]);
    }

    // n階差分商
    double a[N];
    for (j = 0; j < N; j++)
        a[j] = d[j][0];

    // 0.5刻みで 与えられていない値を補間
    for (i = 0; i <= 18; i++)
    {
        double d  = i * 0.5 - 4.5;
        double d1 = f(d);
        double d2 = newton(d, x, a);

        // 元の関数と比較
        printf("%5.2f\t%8.5f\t%8.5f\t%8.5f\n", d, d1, d2, d1 - d2);
    }

   return 0;
}

// 元の関数
double f(double x)
{
    return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2);
}

// Newton (ニュートン) 補間
double newton(double d, double x[], double a[])
{
    int i, j;
    double sum = a[0];
    for (i = 1; i < N; i++)
    {
        double prod = a[i];
        for (j = 0; j < i; j++)
            prod *= (d - x[j]);
        sum += prod;
    }

    return sum;
}
#import <Foundation/Foundation.h>
#import <math.h>

// データ点の数
const int N   = 7;
const int Nx2 = 14;

// 元の関数
double f(double x);

// 導関数
double fd(double x);

// Hermite (エルミート) 補間
double hermite(double d, double z[], double a[]);

int main()
{
    int i, j;
    double x[N], y[N], yd[N];

    // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
    for (i = 0; i < N; i++)
    {
        double d = i * 1.5 - 4.5;
        x[i]  = d;
        y[i]  = f(d);
        yd[i] = fd(d);
    }

    // 差分商の表を作る
    double z[Nx2];
    double d[Nx2][Nx2];
    for (i = 0; i < Nx2; i++)
    {
        int j   = i / 2;
        z[i]    = x[j];
        d[0][i] = y[j];
    }

    for (i = 1; i < Nx2; i++)
    {
        for (j = 0; j < Nx2 - i; j++)
        {
            if (i == 1 && j % 2 == 0)
                d[i][j] = yd[j / 2];
            else
                d[i][j] = (d[i-1][j+1] - d[i-1][j]) / (z[j+i] - z[j]);
        }
    }

    // n階差分商
    double a[Nx2];
    for (j = 0; j < Nx2; j++)
        a[j] = d[j][0];

    // 0.5刻みで 与えられていない値を補間
    for (i = 0; i <= 18; i++)
    {
        double d  = i * 0.5 - 4.5;
        double d1 = f(d);
        double d2 = hermite(d, z, a);

        // 元の関数と比較
        printf("%5.2f\t%8.5f\t%8.5f\t%8.5f\n", d, d1, d2, d1 - d2);
    }

   return 0;
}

// 元の関数
double f(double x)
{
    return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2);
}
// 導関数
double fd(double x)
{
    return 1 - pow(x,2) / 2 + pow(x,4) / (4 * 3 * 2);
}

// Hermite (エルミート) 補間
double hermite(double d, double z[], double a[])
{
    int i, j;
    double sum = a[0];
    for (i = 1; i < Nx2; i++)
    {
        double prod = a[i];
        for (j = 0; j < i; j++)
            prod *= (d - z[j]);
        sum += prod;
    }

    return sum;
}
#import <Foundation/Foundation.h>
#import <math.h>

// データ点の数
const int N = 7;

// 元の関数
double f(double x);

// Spline (スプライン) 補間
double spline(double d, double x[], double y[], double z[]);

int main()
{
    int i;
    double x[N], y[N];

    // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
    for (i = 0; i < N; i++)
    {
        double d = i * 1.5 - 4.5;
        x[i] = d;
        y[i] = f(d);
    }

    // 3項方程式の係数の表を作る
    double a[N], b[N], c[N], d[N];
    for (i = 1; i < N - 1; i++)
    {
        a[i] =         x[i]   - x[i-1];
        b[i] = 2.0 *  (x[i+1] - x[i-1]);
        c[i] =         x[i+1] - x[i];
        d[i] = 6.0 * ((y[i+1] - y[i]) / (x[i+1] - x[i]) - (y[i] - y[i-1]) / (x[i] - x[i-1]));
    }
    // 3項方程式を解く (ト−マス法)
    double g[N], s[N];
    g[1] = b[1];
    s[1] = d[1];
    for (i = 2; i < N - 1; i++)
    {
        g[i] = b[i] - a[i] * c[i-1] / g[i-1];
        s[i] = d[i] - a[i] * s[i-1] / g[i-1];
    }
    double z[N];
    z[0]   = 0;
    z[N-1] = 0;
    z[N-2] = s[N-2] / g[N-2];
    for (i = N - 3; i >= 1; i--)
    {
        z[i] = (s[i] - c[i] * z[i+1]) / g[i];
    }

    // 0.5刻みで 与えられていない値を補間
    for (i = 0; i <= 18; i++)
    {
        double d  = i * 0.5 - 4.5;
        double d1 = f(d);
        double d2 = spline(d, x, y, z);

        // 元の関数と比較
        printf("%5.2f\t%8.5f\t%8.5f\t%8.5f\n", d, d1, d2, d1 - d2);
    }
   return 0;
}

// 元の関数
double f(double x)
{
    return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2);
}

// Spline (スプライン) 補間
double spline(double d, double x[], double y[], double z[])
{
    // 補間関数値がどの区間にあるか
    int i, k = -1;
    for (i = 1; i < N; i++)
    {
        if (d <= x[i])
        {
            k = i - 1;
            break;
        }
    }
    if (k < 0) k = N - 1;

    double d1 = x[k+1] - d;
    double d2 = d      - x[k];
    double d3 = x[k+1] - x[k];
    return      (z[k] * pow(d1,3) + z[k+1] * pow(d2,3)) / (6.0 * d3)
              + (y[k]   / d3 - z[k]   * d3 / 6.0) * d1
              + (y[k+1] / d3 - z[k+1] * d3 / 6.0) * d2;
}
#import <Foundation/Foundation.h>
#import <math.h>

// 重力加速度
const double g = -9.8;
// 空気抵抗係数
const double k = -0.01;
// 時間間隔(秒)
const double h = 0.01;

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy);
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy);

int main()
{
    // 角度
    double degree = 45;
    double radian = degree * M_PI / 180.0;
    // 初速 250 km/h -> 秒速に変換
    double v = 250 * 1000 / 3600;
    // 水平方向の速度
    double vx[2];
    vx[0] = v * cos(radian);
    // 鉛直方向の速度
    double vy[2];
    vy[0] = v * sin(radian);
    // 経過秒数
    double t = 0.0;
    // 位置
    double x = 0.0;
    double y = 0.0;

    // Euler法
    int i;
    for (i = 1; y >= 0.0; i++)
    {
        // 経過秒数
        t = i * h;

        // 位置
        x += h * vx[0];
        y += h * vy[0];

        printf("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%8.5f\n", t, vx[0], vy[0], x, y);

        // 速度
        vx[1] = vx[0] + h * fx(vx[0], vy[0]);
        vy[1] = vy[0] + h * fy(vx[0], vy[0]);
        vx[0] = vx[1];
        vy[0] = vy[1];
    }
    return 0;
}

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy)
{
    return k * sqrt(vx * vx + vy * vy) * vx;
}
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy)
{
    return g + (k * sqrt(vx * vx + vy * vy) * vy);
}
#import <Foundation/Foundation.h>
#import <math.h>

// 重力加速度
const double g = -9.8;
// 空気抵抗係数
const double k = -0.01;
// 時間間隔(秒)
const double h = 0.01;

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy);
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy);

int main()
{
    // 角度
    double degree = 45;
    double radian = degree * M_PI / 180.0;
    // 初速 250 km/h -> 秒速に変換
    double v = 250 * 1000 / 3600;
    // 水平方向の速度
    double vx[3];
    vx[0] = v * cos(radian);
    // 鉛直方向の速度
    double vy[3];
    vy[0] = v * sin(radian);
    // 経過秒数
    double t = 0.0;
    // 位置
    double x[3];
    x[0] = 0.0;
    double y[3];
    y[0] = 0.0;

    // Heun法
    int i;
    for (i = 1; y[0] >= 0.0; i++)
    {
        // 経過秒数
        t = i * h;

        // 位置・速度
        x[1]  =  x[0] + h *    vx[0];
        y[1]  =  y[0] + h *    vy[0];
        vx[1] = vx[0] + h * fx(vx[0], vy[0]);
        vy[1] = vy[0] + h * fy(vx[0], vy[0]);

        x[2]  =  x[0] + h * (  vx[0]          +    vx[1]        ) / 2;
        y[2]  =  y[0] + h * (  vy[0]          +    vy[1]        ) / 2;
        vx[2] = vx[0] + h * (fx(vx[0], vy[0]) + fx(vx[1], vy[1])) / 2;
        vy[2] = vy[0] + h * (fy(vx[0], vy[0]) + fy(vx[1], vy[1])) / 2;

        x[0]  =  x[2];
        y[0]  =  y[2];
        vx[0] = vx[2];
        vy[0] = vy[2];

        printf("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%8.5f\n", t, vx[0], vy[0], x[0], y[0]);
    }
    return 0;
}

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy)
{
    return k * sqrt(vx * vx + vy * vy) * vx;
}
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy)
{
    return g + (k * sqrt(vx * vx + vy * vy) * vy);
}
#import <Foundation/Foundation.h>
#import <math.h>

// 重力加速度
const double g = -9.8;
// 空気抵抗係数
const double k = -0.01;
// 時間間隔(秒)
const double h = 0.01;

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy);
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy);

int main()
{
    // 角度
    double degree = 45;
    double radian = degree * M_PI / 180.0;
    // 初速 250 km/h -> 秒速に変換
    double v = 250 * 1000 / 3600;
    // 水平方向の速度
    double vx[2];
    vx[0] = v * cos(radian);
    // 鉛直方向の速度
    double vy[2];
    vy[0] = v * sin(radian);
    // 経過秒数
    double t = 0.0;
    // 位置
    double x[2];
    x[0] = 0.0;
    double y[2];
    y[0] = 0.0;

    // 中点法
    int i;
    for (i = 1; y[0] >= 0.0; i++)
    {
        // 経過秒数
        t = i * h;

        // 位置・速度
        vx[1] = h * fx(vx[0], vy[0]);
        vy[1] = h * fy(vx[0], vy[0]);

        double wx = vx[0] + vx[1] / 2;
        double wy = vy[0] + vy[1] / 2;
        vx[0]     = vx[0] + h * fx(wx, wy);
        vy[0]     = vy[0] + h * fy(wx, wy);
        x[0]      =  x[0] + h *    wx;
        y[0]      =  y[0] + h *    wy;

        printf("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%9.5f\n", t, vx[0], vy[0], x[0], y[0]);
    }
    return 0;
}

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy)
{
    return k * sqrt(vx * vx + vy * vy) * vx;
}
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy)
{
    return g + (k * sqrt(vx * vx + vy * vy) * vy);
}
#import <Foundation/Foundation.h>
#import <math.h>

// 重力加速度
const double g = -9.8;
// 空気抵抗係数
const double k = -0.01;
// 時間間隔(秒)
const double h = 0.01;

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy);
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy);

int main()
{
    // 角度
    double degree = 45;
    double radian = degree * M_PI / 180.0;
    // 初速 250 km/h -> 秒速に変換
    double v = 250 * 1000 / 3600;
    // 水平方向の速度
    double vx[5];
    vx[0] = v * cos(radian);
    // 鉛直方向の速度
    double vy[5];
    vy[0] = v * sin(radian);
    // 経過秒数
    double t = 0.0;
    // 位置
    double x[5];
    x[0] = 0.0;
    double y[5];
    y[0] = 0.0;

    // Runge-Kutta法
    int i;
    for (i = 1; y[0] >= 0.0; i++)
    {
        // 経過秒数
        t = i * h;

        // 位置・速度
        x[1]  = h *    vx[0];
        y[1]  = h *    vy[0];
        vx[1] = h * fx(vx[0], vy[0]);
        vy[1] = h * fy(vx[0], vy[0]);

        double wx = vx[0] + vx[1] / 2;
        double wy = vy[0] + vy[1] / 2;
        x[2]  = h *    wx;
        y[2]  = h *    wy;
        vx[2] = h * fx(wx, wy);
        vy[2] = h * fy(wx, wy);

        wx    = vx[0] + vx[2] / 2;
        wy    = vy[0] + vy[2] / 2;
        x[3]  = h *    wx;
        y[3]  = h *    wy;
        vx[3] = h * fx(wx, wy);
        vy[3] = h * fy(wx, wy);

        wx    = vx[0] + vx[3];
        wy    = vy[0] + vy[3];
        x[4]  = h *    wx;
        y[4]  = h *    wy;
        vx[4] = h * fx(wx, wy);
        vy[4] = h * fy(wx, wy);

        x[0]  += ( x[1] +  x[2] * 2 +  x[3] * 2 +  x[4]) / 6;
        y[0]  += ( y[1] +  y[2] * 2 +  y[3] * 2 +  y[4]) / 6;
        vx[0] += (vx[1] + vx[2] * 2 + vx[3] * 2 + vx[4]) / 6;
        vy[0] += (vy[1] + vy[2] * 2 + vy[3] * 2 + vy[4]) / 6;

        printf("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%9.5f\n", t, vx[0], vy[0], x[0], y[0]);
    }
    return 0;
}

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy)
{
    return k * sqrt(vx * vx + vy * vy) * vx;
}
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy)
{
    return g + (k * sqrt(vx * vx + vy * vy) * vy);
}
#import <Foundation/Foundation.h>
#import <math.h>

// 重力加速度
const double g = -9.8;
// 空気抵抗係数
const double k = -0.01;
// 時間間隔(秒)
const double h = 0.01;

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy);
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy);

int main()
{
    // 角度
    double degree = 45;
    double radian = degree * M_PI / 180.0;
    // 初速 250 km/h -> 秒速に変換
    double v = 250 * 1000 / 3600;
    // 水平方向の速度
    double vx[5];
    vx[0] = v * cos(radian);
    // 鉛直方向の速度
    double vy[5];
    vy[0] = v * sin(radian);
    // 経過秒数
    double t = 0.0;
    // 位置
    double x[5];
    x[0] = 0.0;
    double y[5];
    y[0] = 0.0;

    // Runge-Kutta-Gill法
    int i;
    for (i = 1; y[0] >= 0.0; i++)
    {
        // 経過秒数
        t = i * h;

        // 速度
        x[1]  = h *    vx[0];
        y[1]  = h *    vy[0];
        vx[1] = h * fx(vx[0], vy[0]);
        vy[1] = h * fy(vx[0], vy[0]);

        double wx = vx[0] + vx[1] / 2;
        double wy = vy[0] + vy[1] / 2;
        x[2]  = h *    wx;
        y[2]  = h *    wy;
        vx[2] = h * fx(wx, wy);
        vy[2] = h * fy(wx, wy);

        wx    = vx[0] + vx[1] * ((sqrt(2.0) - 1) / 2) + vx[2] * (1 - 1 / sqrt(2.0));
        wy    = vy[0] + vy[1] * ((sqrt(2.0) - 1) / 2) + vy[2] * (1 - 1 / sqrt(2.0));
        x[3]  = h *    wx;
        y[3]  = h *    wy;
        vx[3] = h * fx(wx, wy);
        vy[3] = h * fy(wx, wy);

        wx    = vx[0] - vx[2] / sqrt(2.0) + vx[3] * (1 + 1 / sqrt(2.0));
        wy    = vy[0] - vy[2] / sqrt(2.0) + vy[3] * (1 + 1 / sqrt(2.0));
        x[4]  = h *    wx;
        y[4]  = h *    wy;
        vx[4] = h * fx(wx, wy);
        vy[4] = h * fy(wx, wy);

        x[0]  += ( x[1] +  x[2] * (2 - sqrt(2.0)) +  x[3] * (2 + sqrt(2.0)) +  x[4]) / 6;
        y[0]  += ( y[1] +  y[2] * (2 - sqrt(2.0)) +  y[3] * (2 + sqrt(2.0)) +  y[4]) / 6;
        vx[0] += (vx[1] + vx[2] * (2 - sqrt(2.0)) + vx[3] * (2 + sqrt(2.0)) + vx[4]) / 6;
        vy[0] += (vy[1] + vy[2] * (2 - sqrt(2.0)) + vy[3] * (2 + sqrt(2.0)) + vy[4]) / 6;

        printf("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%9.5f\n", t, vx[0], vy[0], x[0], y[0]);
    }
    return 0;
}

// 空気抵抗による水平方向の減速分
double fx(double vx, double vy)
{
    return k * sqrt(vx * vx + vy * vy) * vx;
}
// 重力と空気抵抗による鉛直方向の減速分
double fy(double vx, double vy)
{
    return g + (k * sqrt(vx * vx + vy * vy) * vy);
}
#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return x * x - 2;
}

double bisection(double a, double b)
{
    double c;
    while (YES)
    {
        // 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2;
        printf("%12.10f\t%13.10f\n", c, c - sqrt(2));

        double fc = f(c);
        if (fabs(fc) < 0.0000000001) break;

        if (fc < 0)
        {
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c;
        }
        else
        {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c;
        }
    }
    return c;
}

int main()
{
    double a = 1;
    double b = 2;
    printf("%12.10f\n", bisection(a, b));
    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return x * x - 2;
}

double falseposition(double a, double b)
{
    double c;
    while (YES)
    {
        // 点 (a,f(a)) と 点 (b,f(b)) を結ぶ直線と x軸の交点
        c = (a * f(b) - b * f(a)) / (f(b) - f(a));
        printf("%12.10f\t%13.10f\n", c, c - sqrt(2));

        double fc = f(c);
        if (fabs(fc) < 0.0000000001) break;

        if (fc < 0)
        {
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c;
        }
        else
        {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c;
        }
    }
    return c;
}

int main()
{
    double a = 1;
    double b = 2;
    printf("%12.10f\n", falseposition(a, b));
    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double g(double x)
{
    return (x / 2) + (1 / x);
}

double iterative(double x0)
{
    double x1;
    while (YES)
    {
        x1 = g(x0);
        printf("%12.10f\t%13.10f\n", x1, x1 - sqrt(2));

        if (fabs(x1 - x0) < 0.0000000001) break;
        x0 = x1;
    }
    return x1;
}

int main()
{
    double x = 1;
    printf("%12.10f\n", iterative(x));
    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double f0(double x)
{
    return x * x - 2;
}
double f1(double x)
{
    return 2 * x;
}

double newton(double x0)
{
    double x1;
    while (YES)
    {
        x1 = x0 - (f0(x0) / f1(x0));
        printf("%12.10f\t%13.10f\n", x1, x1 - sqrt(2));

        if (fabs(x1 - x0) < 0.0000000001) break;
        x0 = x1;
    }
    return x1;
}

int main()
{
    double x = 2;
    printf("%12.10f\n", newton(x));
    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double f0(double x)
{
    return x * x - 2;
}
double f1(double x)
{
    return 2 * x;
}
double f2(double x)
{
    return 2;
}

double bailey(double x0)
{
    double x1;
    while (YES)
    {
        x1 = x0 - (f0(x0) / (f1(x0) - (f0(x0) * f2(x0) / (2 * f1(x0)))));
        printf("%12.10f\t%13.10f\n", x1, x1 - sqrt(2));

        if (fabs(x1 - x0) < 0.0000000001) break;
        x0 = x1;
    }
    return x1;
}

int main()
{
    double x = 2;
    printf("%12.10f\n", bailey(x));
    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return x * x - 2;
}

double secant(double x0, double x1)
{
    double x2;
    while (YES)
    {
        x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));
        printf("%12.10f\t%13.10f\n", x2, x2 - sqrt(2));

        if (fabs(x2 - x1) < 0.0000000001) break;
        x0 = x1;
        x1 = x2;
    }
    return x2;
}

int main()
{
    double x0 = 1;
    double x1 = 2;
    printf("%12.10f\n", secant(x0, x1));
    return 0;
}
#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// ヤコビの反復法
void jacobi(double a[N][N], double b[N], double c[N]);

int main()
{
    double a[4][4] = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}};
    double b[4]    = {20,16,8,17};
    double c[4]    = {0,0,0,0};

    // ヤコビの反復法
    jacobi(a,b,c);

    printf("解\n");
    int i;
    for (i = 0; i < N; i++)
        printf("%14.10f\t", c[i]);
    printf("\n");

    return 0;
}

// ヤコビの反復法
void jacobi(double a[N][N], double b[N], double x0[N])
{
    while (true)
    {
        double x1[N];
        bool finish = true;
        int i, j;
        for (i = 0; i < N; i++)
        {
            x1[i] = 0;
            for (j = 0; j < N; j++)
                if (j != i)
                    x1[i] += a[i][j] * x0[j];

            x1[i] = (b[i] - x1[i]) / a[i][i];
            if (fabs(x1[i] - x0[i]) > 0.0000000001) finish = false;
        }

        for (i = 0; i < N; i++)
        {
            x0[i] = x1[i];
            printf("%14.10f\t", x0[i]);
        }
        printf("\n");
        if (finish) return;
    }
}
#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// ガウス・ザイデル法
void gauss(double a[N][N], double b[N], double c[N]);

int main()
{
    double a[4][4] = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}};
    double b[4]    = {20,16,8,17};
    double c[4]    = {0,0,0,0};

    // ガウス・ザイデル法
    gauss(a,b,c);

    printf("解\n");
    int i;
    for (i = 0; i < N; i++)
        printf("%14.10f\t", c[i]);
    printf("\n");

    return 0;
}

// ガウス・ザイデル法
void gauss(double a[N][N], double b[N], double x0[N])
{
    while (true)
    {
        double x1;
        bool finish = true;
        int i, j;
        for (i = 0; i < N; i++)
        {
            x1 = 0;
            for (j = 0; j < N; j++)
                if (j != i)
                    x1 += a[i][j] * x0[j];

            x1 = (b[i] - x1) / a[i][i];
            if (fabs(x1 - x0[i]) > 0.0000000001) finish = false;
            x0[i] = x1;
        }

        for (i = 0; i < N; i++)
            printf("%14.10f\t", x0[i]);
        printf("\n");
        if (finish) return;
    }
}
#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// ピボット選択
void pivoting(double a[N][N], double b[N]);
// 前進消去
void forward_elimination(double a[N][N], double b[N]);
// 後退代入
void backward_substitution(double a[N][N], double b[N]);
// 状態を確認
void disp_progress(double a[N][N], double b[N]);

// ガウスの消去法
int main()
{
    double a[4][4] = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}};
    double b[4]    = {8,17,20,16};

    // ピボット選択
    pivoting(a,b);
    printf("ピボット選択後\n");
    disp_progress(a,b);

    // 前進消去
    forward_elimination(a,b);
    printf("前進消去後\n");
    disp_progress(a,b);

    // 後退代入
    backward_substitution(a,b);

    printf("解\n");
    int i;
    for (i = 0; i < N; i++)
        printf("%14.10f\t", b[i]);
    printf("\n");

    return 0;
}
// 前進消去
void forward_elimination(double a[N][N], double b[N])
{
    int pivot, row, col;
    for (pivot = 0; pivot < N - 1; pivot++)
    {
        for (row = pivot + 1; row < N; row++)
        {
            double  s   = a[row][pivot] / a[pivot][pivot];
            for (col = pivot; col < N; col++)
                a[row][col] -= a[pivot][col]    * s;
            b[row]          -= b[pivot]         * s;
        }
    }
}
// 後退代入
void backward_substitution(double a[N][N], double b[N])
{
    int row, col;
    for (row = N - 1; row >= 0; row--)
    {
        for (col = N - 1; col > row; col--)
            b[row] -= a[row][col] * b[col];
        b[row] /= a[row][row];
    }
}
// ピボット選択
void pivoting(double a[N][N], double b[N])
{
    int pivot, row, col;
    for(pivot = 0; pivot < N; pivot++)
    {
        // 各列で 一番値が大きい行を 探す
        int     max_row =   pivot;
        double  max_val =   0;
        for (row = pivot; row < N; row++)
        {
            if (fabs(a[row][pivot]) > max_val)
            {
                // 一番値が大きい行
                max_val =   fabs(a[row][pivot]);
                max_row =   row;
            }
        }

        // 一番値が大きい行と入れ替え
        if (max_row != pivot)
        {
            double tmp;
            for (col = 0; col < N; col++)
            {
                tmp             =   a[max_row][col];
                a[max_row][col] =   a[pivot][col];
                a[pivot][col]   =   tmp;
            }
            tmp         =   b[max_row];
            b[max_row]  =   b[pivot];
            b[pivot]    =   tmp;
        }
    }
}
// 状態を確認
void disp_progress(double a[N][N], double b[N])
{
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            printf("%14.10f\t", a[i][j]);
        printf("%14.10f\n", b[i]);
    }
    printf("\n");
}
#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// ピボット選択
void pivoting(double a[N][N], double b[N]);
// 前進消去
void forward_elimination(double a[N][N], double b[N]);
// 後退代入
void backward_substitution(double a[N][N], double b[N]);
// 状態を確認
void disp_progress(double a[N][N], double b[N]);

// ガウス・ジョルダン法
int main()
{
    double a[4][4] = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}};
    double b[4]    = {8,17,20,16};

    // ピボット選択
    pivoting(a,b);
    printf("ピボット選択後\n");
    disp_progress(a,b);

    // 前進消去
    forward_elimination(a,b);
    printf("前進消去後\n");
    disp_progress(a,b);

    // 後退代入
    backward_substitution(a,b);

    printf("解\n");
    int i;
    for (i = 0; i < N; i++)
        printf("%14.10f\t", b[i]);
    printf("\n");

    return 0;
}
// 前進消去
void forward_elimination(double a[N][N], double b[N])
{
    // 対角線上の係数を 1 にする
    int pivot, row, col;
    for (pivot = 0; pivot < N; pivot++)
    {
        double s  = a[pivot][pivot];
        for (col = 0; col < N; col++)
            a[pivot][col] /= s;
         b[pivot] /= s;
    }
    printf("対角線上の係数を 1 にする\n");
    disp_progress(a,b);

    // 対角行列にする
    for (pivot = 0; pivot < N; pivot++)
    {
        for (row = 0; row < N; row++)
        {
            if (row == pivot) continue;

            double  s   = a[row][pivot] / a[pivot][pivot];
            for (col = pivot; col < N; col++)
                a[row][col] -= a[pivot][col]    * s;
            b[row]          -= b[pivot]         * s;
        }
    }
}
// 後退代入
void backward_substitution(double a[N][N], double b[N])
{
    int pivot;
    for (pivot = 0; pivot < N; pivot++)
        b[pivot]  /= a[pivot][pivot];
}
// ピボット選択
void pivoting(double a[N][N], double b[N])
{
    int pivot, row, col;
    for(pivot = 0; pivot < N; pivot++)
    {
        // 各列で 一番値が大きい行を 探す
        int     max_row =   pivot;
        double  max_val =   0;
        for (row = pivot; row < N; row++)
        {
            if (fabs(a[row][pivot]) > max_val)
            {
                // 一番値が大きい行
                max_val =   fabs(a[row][pivot]);
                max_row =   row;
            }
        }

        // 一番値が大きい行と入れ替え
        if (max_row != pivot)
        {
            double tmp;
            for (col = 0; col < N; col++)
            {
                tmp             =   a[max_row][col];
                a[max_row][col] =   a[pivot][col];
                a[pivot][col]   =   tmp;
            }
            tmp         =   b[max_row];
            b[max_row]  =   b[pivot];
            b[pivot]    =   tmp;
        }
    }
}
// 状態を確認
void disp_progress(double a[N][N], double b[N])
{
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            printf("%14.10f\t", a[i][j]);
        printf("%14.10f\n", b[i]);
    }
    printf("\n");
}
#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// ピボット選択
void pivoting(double a[N][N], double b[N]);
// 前進消去
void forward_elimination(double a[N][N], double b[N]);
// LU分解
void decomp(double a[N][N], double b[N], double x[N]);
// 状態を確認
void disp_progress(double a[N][N], double b[N]);

// LU分解
int main()
{
    double a[4][4] = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}};
    double b[4]    = {8,17,20,16};

    // ピボット選択
    pivoting(a,b);
    printf("ピボット選択後\n");
    disp_progress(a,b);

    // LU分解
    double x[4] = {0,0,0,0};
    decomp(a,b,x);

    printf("X\n");
    int i;
    for (i = 0; i < N; i++)
        printf("%14.10f\t", x[i]);
    printf("\n");

    return 0;
}
// LU分解
void decomp(double a[N][N], double b[N], double x[N])
{
    // 前進消去
    forward_elimination(a,b);
    // 下三角行列を確認
    printf("L\n");
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            if (i > j)
                printf("%14.10f\t", a[i][j]);
            else if (i == j)
                printf("%14.10f\t", 1.0);
            else
                printf("%14.10f\t", 0.0);
        printf("\n");
    }
    printf("\n");
    // 上三角行列を確認
    printf("U\n");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            if (i <= j)
                printf("%14.10f\t", a[i][j]);
            else
                printf("%14.10f\t", 0.0);
        printf("\n");
    }
    printf("\n");

    // Ly=b から y を求める (前進代入)
    double y[4] = {0,0,0,0};
    int row, col;
    for (row = 0; row < N; row++)
    {
        for (col = 0; col < row; col++)
            b[row] -= a[row][col] * y[col];
        y[row] = b[row];
    }
    // y の 値 を確認
    printf("Y\n");
    for (i = 0; i < N; i++)
        printf("%14.10f\t", y[i]);
    printf("\n\n");

    // Ux=y から x を求める (後退代入)
    for (row = N - 1; row >= 0; row--)
    {
        for (col = N - 1; col > row; col--)
            y[row] -= a[row][col] * x[col];
        x[row] = y[row] / a[row][row];
    }
}
// 前進消去
void forward_elimination(double a[N][N], double b[N])
{
    int pivot, row, col;
    for (pivot = 0; pivot < N - 1; pivot++)
    {
        for (row = pivot + 1; row < N; row++)
        {
            double s = a[row][pivot] / a[pivot][pivot];
            for (col = pivot; col < N; col++)
                a[row][col] -= a[pivot][col] * s; // これが 上三角行列
            a[row][pivot] = s;                    // これが 下三角行列
            // b[row]    -= b[pivot] * s;         // この値は変更しない
        }
    }
}
// ピボット選択
void pivoting(double a[N][N], double b[N])
{
    int pivot, row, col;
    for(pivot = 0; pivot < N; pivot++)
    {
        // 各列で 一番値が大きい行を 探す
        int     max_row =   pivot;
        double  max_val =   0;
        for (row = pivot; row < N; row++)
        {
            if (fabs(a[row][pivot]) > max_val)
            {
                // 一番値が大きい行
                max_val =   fabs(a[row][pivot]);
                max_row =   row;
            }
        }

        // 一番値が大きい行と入れ替え
        if (max_row != pivot)
        {
            double tmp;
            for (col = 0; col < N; col++)
            {
                tmp             =   a[max_row][col];
                a[max_row][col] =   a[pivot][col];
                a[pivot][col]   =   tmp;
            }
            tmp         =   b[max_row];
            b[max_row]  =   b[pivot];
            b[pivot]    =   tmp;
        }
    }
}
// 状態を確認
void disp_progress(double a[N][N], double b[N])
{
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            printf("%14.10f\t", a[i][j]);
        printf("%14.10f\n", b[i]);
    }
    printf("\n");
}
#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// 前進消去
void forward_elimination(double a[N][N], double b[N]);
// LL^T分解
void decomp(double a[N][N], double b[N], double x[N]);
// 状態を確認
void disp_progress(double a[N][N]);

// コレスキー法
int main()
{
    double a[4][4] = {{5,2,3,4},{2,10,6,7},{3,6,15,9},{4,7,9,20}};
    double b[4]    = {34,68,96,125};
    printf("A\n");
    disp_progress(a);
    printf("\n");

    // LL^T分解
    double x[4]    = {0,0,0,0};
    decomp(a,b,x);

    printf("X\n");
    int i;
    for (i = 0; i < N; i++)
        printf("%14.10f\t", x[i]);
    printf("\n");

    return 0;
}
// LL^T分解
void decomp(double a[N][N], double b[N], double x[N])
{
    // 前進消去
    forward_elimination(a,b);
    printf("L と L^T\n");
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            if (j <= i)
                printf("%14.10f\t", a[i][j]);
            else
                printf("%14.10f\t", a[j][i]);
        printf("\n");
    }
    printf("\n");

    // Ly=b から y を求める (前進代入)
    double y[4] = {0,0,0,0};
    int row, col;
    for (row = 0; row < N; row++)
    {
        for (col = 0; col < row; col++)
            b[row] -= a[row][col] * y[col];
        y[row] = b[row] / a[row][row];
    }

    // y の 値 を確認
    printf("Y\n");
    for (i = 0; i < N; i++)
        printf("%14.10f\t", y[i]);
    printf("\n\n");

    // Ux=y から x を求める (後退代入)
    for (row = N - 1; row >= 0; row--)
    {
        for (col = N - 1; col > row; col--)
            y[row] -= a[col][row] * x[col];
        x[row] = y[row] / a[row][row];
    }
}
// 前進消去
void forward_elimination(double a[N][N], double b[N])
{
    int pivot, row, col;
    for (pivot = 0; pivot < N; pivot++)
    {
        double s = 0;
        for (col = 0; col < pivot; col++)
            s += a[pivot][col] * a[pivot][col];
        // ここで根号の中が負の値になると計算できない!
        a[pivot][pivot] = sqrt(a[pivot][pivot] - s);

        for (row = pivot + 1; row < N; row++)
        {
            s = 0;
            for (col = 0; col < pivot; col++)
                s += a[row][col] * a[pivot][col];
            a[row][pivot] =  (a[row][pivot] - s) / a[pivot][pivot];
        }
    }
}
// 状態を確認
void disp_progress(double a[N][N])
{
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            printf("%14.10f\t", a[i][j]);
        printf("\n");
    }
    printf("\n");
}
#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// 前進消去
void forward_elimination(double a[N][N], double b[N]);
// LDL^T分解
void decomp(double a[N][N], double b[N], double x[N]);
// 状態を確認
void disp_progress(double a[N][N]);

// 修正コレスキー法
int main()
{
    double a[4][4] = {{5,2,3,4},{2,10,6,7},{3,6,15,9},{4,7,9,20}};
    double b[4]    = {34,68,96,125};
    printf("A\n");
    disp_progress(a);
    printf("\n");

    // LDL^T分解
    double x[4]    = {0,0,0,0};
    decomp(a,b,x);

    printf("X\n");
    int i;
    for (i = 0; i < N; i++)
        printf("%14.10f\t", x[i]);
    printf("\n");

    return 0;
}
// LDL^T分解
void decomp(double a[N][N], double b[N], double x[N])
{
    // 前進消去
    forward_elimination(a,b);
    printf("L と D\n");
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            if (j <= i)
                printf("%14.10f\t", a[i][j]);
            else
                printf("%14.10f\t", a[j][i]);
        printf("\n");
    }
    printf("\n");

    // Ly=b から y を求める (前進代入)
    double y[4] = {0,0,0,0};
    int row, col;
    for (row = 0; row < N; row++)
    {
        for (col = 0; col < row; col++)
            b[row] -= a[row][col] * y[col];
        y[row] = b[row];
    }

    // y の 値 を確認
    printf("Y\n");
    for (i = 0; i < N; i++)
        printf("%14.10f\t", y[i]);
    printf("\n\n");

    // DL^Tx=y から x を求める (後退代入)
    for (row = N - 1; row >= 0; row--)
    {
        for (col = N - 1; col > row; col--)
            y[row] -= a[col][row] * a[row][row] * x[col];
        x[row] = y[row] / a[row][row];
    }
}
// 前進消去
void forward_elimination(double a[N][N], double b[N])
{
    int pivot, col, k;
    for (pivot = 0; pivot < N; pivot++)
    {
        double s;

        // pivot < k の場合
        for (col = 0; col < pivot; col++)
        {
            s = a[pivot][col];
            for (k = 0; k < col; k++)
                s -= a[pivot][k] * a[col][k] * a[k][k];
            a[pivot][col] = s / a[col][col];
        }

        // pivot == k の場合
        s = a[pivot][pivot];
        for (k = 0; k < pivot; k++)
            s -= a[pivot][k] * a[pivot][k] * a[k][k];
        a[pivot][pivot] = s;
    }
}
// 状態を確認
void disp_progress(double a[N][N])
{
    int i, j;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
            printf("%14.10f\t", a[i][j]);
        printf("\n");
    }
    printf("\n");
}
inserted by FC2 system