home > さまざまな言語で数値計算 > 連立一次方程式 >

さまざまな言語で数値計算

Only Do What Only You Can Do

ヤコビの反復法

ヤコビの反復法を使って, 連立一次方程式の解を求める .

例として, 連立一次方程式

を考える.
この方程式を上から順に対角線上の変数について解くと

となる.
$x,y,z,u$ に適当な値を入れて右辺を計算し,
得られた値を新たな $x,y,z,u$ として計算を繰り返す.
漸化式で書くと

VBScript

Option Explicit

Private Const N = 4

Private a: a = Array(Array(9,2,1,1),Array(2,8,-2,1),Array(-1,-2,7,-2),Array(1,-1,-2,6))
Private b: b = Array(20,16,8,17)
Private c: c = Array(0,0,0,0)

'ヤコビの反復法
jacobi a,b,c

WScript.StdOut.WriteLine "X"
disp_vector c

'ヤコビの反復法
Private Sub jacobi(ByVal a, ByVal b, ByRef x0)
    Do While(True)
        Dim x1: x1 = Array(0,0,0,0)
        Dim finish: finish = True
        Dim i, j
        For i = 0 To (N - 1)
            x1(i) = 0
            For j = 0 To (N - 1)
                If i <> j Then
                    x1(i) = x1(i) + a(i)(j) * x0(j)
                End If
            Next
            x1(i) = (b(i) - x1(i)) / a(i)(i)
            If (Abs(x1(i) - x0(i)) > 0.0000000001) Then
                finish = False
            End If
        Next
        For i = 0 To (N - 1)
            x0(i) = x1(i)
        Next
        If (finish) Then
            Exit Sub
        End If

        disp_vector x0
    Loop
End Sub

'1次元配列を表示
Private Sub disp_vector(ByVal row())
    Dim col
    For Each col In row
        WScript.StdOut.Write Right(Space(14) & FormatNumber(col, 10, -1, 0, 0), 14) & vbTab
    Next
    WScript.StdOut.WriteLine
End Sub
Z:\>cscript //nologo Z:\1001.vbs
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

JScript

var N = 4

var a = [[9,2,1,1],[2,8,-2,1],[-1,-2,7,-2],[1,-1,-2,6]]
var b = [20,16,8,17]
var c = [0,0,0,0]

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

WScript.StdOut.WriteLine("X")
disp_vector(c)

// ヤコビの反復法
function jacobi(a, b, x0)
{
    while (true)
    {
        x1 = []
        var finish = true
        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 (Math.abs(x1[i] - x0[i]) > 0.0000000001) finish = false
        }
        for (i = 0; i < N; i++)
            x0[i] = x1[i]
        if (finish) return

        disp_vector(x0)
    }
}

// 1次元配列を表示
function disp_vector(row)
{
    for (var i = 0; i < N; i++)
        WScript.StdOut.Write(("              "    + row[i].toFixed(10)).slice(-14) + "\t")
    WScript.StdOut.WriteLine()
}
Z:\>cscript //nologo Z:\1001.js
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

PowerShell

set-variable -name N -value 3 -option constant

# 1次元配列を表示
function disp_vector($row)
{
    foreach ($col in $row)
    {
        Write-Host ([String]::Format("{0,14:F10}", $col)) -nonewline
    }
    Write-Host
}
# ヤコビの反復法
function jacobi($a, $b, $x0)
{
    while ($true)
    {
        $x1 = 0.0, 0.0, 0.0, 0.0
        $finish = $true
        foreach ($i in 0..$N)
        {
            $x1[$i] = 0
            foreach ($j in 0..$N)
            {
                if ($j -ne $i)
                {
                    $x1[$i] += $a[$i][$j] * $x0[$j]
                }
            }
            $x1[$i] = ($b[$i] - $x1[$i]) / $a[$i][$i]
            if ([Math]::Abs($x1[$i] - $x0[$i]) -gt 0.0000000001)
            {
                $finish = $false
            }
        }
        foreach ($i in 0..$N)
        {
            $x0[$i] = $x1[$i]
        }
        if ($finish)
        {
            return
        }
        disp_vector  $x0
    }
}

$a = (9.0,2.0,1.0,1.0), (2.0,8.0,-2.0,1.0), (-1.0,-2.0,7.0,-2.0), (1.0,-1.0,-2.0,6.0)
$b = 20.0, 16.0, 8.0, 17.0
$c =  0.0,  0.0, 0.0,  0.0

# ヤコビの反復法
jacobi $a $b $c

Write-Host "X"
disp_vector  $c
Z:\>powershell -file Z:\1001.ps1
  2.2222222222  2.0000000000  1.1428571429  2.8333333333
  1.3359788360  1.3759920635  2.8412698413  3.1772486772
  1.2477219283  1.9791666667  2.6346371882  3.7870921517
  1.0688819252  1.8733422960  2.9686056521  3.8334531858
  1.0501396189  1.9957492835  2.9260675555  3.9569452792
  1.0139431776  1.9743638243  2.9936469635  3.9662907960
  1.0101482880  1.9991395970  2.9850360597  3.9912857623
  1.0028221093  1.9948112226  2.9987141438  3.9931772381
  1.0020540192  1.9998258539  2.9969712901  3.9982362335
  1.0005711965  1.9989497885  2.9997397420  3.9986190691
  1.0004157346  1.9999647527  2.9993869874  3.9996430127
  1.0001156105  1.9997874366  2.9999473236  3.9997204988
  1.0000841449  1.9999928659  2.9998759259  3.9999277456
  1.0000233996  1.9999569771  2.9999893383  3.9999434288
  1.0000170310  1.9999985561  2.9999748873  3.9999853757
  1.0000047361  1.9999912921  2.9999978421  3.9999885500
  1.0000034471  1.9999997077  2.9999949172  3.9999970400
  1.0000009586  1.9999982375  2.9999995632  3.9999976825
  1.0000006977  1.9999999408  2.9999989712  3.9999994009
  1.0000001940  1.9999996433  2.9999999116  3.9999995309
  1.0000001412  1.9999999880  2.9999997918  3.9999998787
  1.0000000393  1.9999999278  2.9999999821  3.9999999051
  1.0000000286  1.9999999976  2.9999999579  3.9999999755
  1.0000000079  1.9999999854  2.9999999964  3.9999999808
  1.0000000058  1.9999999995  2.9999999915  3.9999999950
  1.0000000016  1.9999999970  2.9999999993  3.9999999961
  1.0000000012  1.9999999999  2.9999999983  3.9999999990
  1.0000000003  1.9999999994  2.9999999999  3.9999999992
  1.0000000002  2.0000000000  2.9999999997  3.9999999998
  1.0000000001  1.9999999999  3.0000000000  3.9999999998
  1.0000000000  2.0000000000  2.9999999999  4.0000000000
X
  1.0000000000  2.0000000000  3.0000000000  4.0000000000

Perl

use constant N => 3;

my @a = ([9,2,1,1],[2,8,-2,1],[-1,-2,7,-2],[1,-1,-2,6]);
my @b = (20,16,8,17);
my @c = (0,0,0,0);

# ヤコビの反復法
jacobi(\@a, \@b, \@c);

print "X\n";
disp_vector(\@c);

# ヤコビの反復法
sub jacobi
{
    my ($a, $b, $x0) = @_;

    while (1)
    {
        my @x1 = ();
        my $finish = 1;
        for $i (0..N)
        {
            $x1[$i] = 0;
            for $j (0..N)
            {
                if ($j != $i)
                {
                    $x1[$i] += $$a[$i][$j] * $$x0[$j];
                }
            }
            $x1[$i] = ($$b[$i] - $x1[$i]) / $$a[$i][$i];
            $finish = 0 if (abs($x1[$i] - $$x0[$i]) > 0.0000000001)
        }

        for $i (0..N)
        {
            $$x0[$i] = $x1[$i];
        }
        return if ($finish);

        disp_vector($x0);
    }
}
# 1次元配列を表示
sub disp_vector
{
    my ($row) = @_;
    foreach $col (@$row)
    {
        printf("%14.10f\t", $col);
    }
    print "\n";
}
Z:\>perl Z:\1001.pl
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

PHP

<?php
define("N", 3);

$a = [[9,2,1,1],[2,8,-2,1],[-1,-2,7,-2],[1,-1,-2,6]];
$b = [20,16,8,17];
$c = [0,0,0,0];

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

print "X\n";
disp_vector($c);

# ヤコビの反復法
function jacobi($a, $b, &$x0)
{
    while (true)
    {
        $x1 = array();
        $finish = true;
        foreach (range(0, N) as $i)
        {
            $x1[$i] = 0;
            foreach (range(0, N) as $j)
            {
                if ($j != $i)
                    $x1[$i] += $a[$i][$j] * $x0[$j];
            }
            $x1[$i] = ($b[$i] - $x1[$i]) / $a[$i][$i];
            if (abs($x1[$i] - $x0[$i]) > 0.0000000001) $finish = false;
        }

        foreach (range(0, N) as $i)
        {
            $x0[$i] = $x1[$i];
        }
        if ($finish) return;

        disp_vector($x0);
    }
}
# 1次元配列を表示
function disp_vector($row)
{
    foreach ($row as $col)
        printf("%14.10f\t", $col);
    print "\n";
}
?>
Z:\>php Z:\1001.php
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Python

# coding: Shift_JIS

N = 4

# 1次元配列を表示
def disp_vector(row):
    for col in row:
        print "%14.10f\t" % col,
    print ""

# ヤコビの反復法
def jacobi(a, b, x0):
    while True:
        x1 = range(N)
        finish = True
        for i in range(0, N, 1):
            x1[i] = 0
            for j in range(0, N, 1):
                if (j != i):
                    x1[i] += a[i][j] * x0[j]

            x1[i] = (b[i] - x1[i]) / a[i][i]
            if (abs(x1[i] - x0[i]) > 0.0000000001):
                finish = False

        for i in range(0, N, 1):
            x0[i] = x1[i]
        if (finish):
            return

        disp_vector(x0)

a = [[ 9.0,  2.0, 1.0,  1.0], [2.0, 8.0, -2.0, 1.0], [-1.0, -2.0, 7.0, -2.0], [1.0, -1.0, -2.0, 6.0]]
b =  [20.0, 16.0, 8.0, 17.0]
c =  [ 0.0,  0.0, 0.0,  0.0]

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

print "X"
disp_vector(c)
Z:\>python Z:\1001.py
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Ruby

# coding: Shift_JIS

N = 3

# 1次元配列を表示
def disp_vector(row)
    row.each do |col|
        printf("%14.10f\t", col)
    end
    puts ""
end

# ヤコビの反復法
def jacobi(a, b, x0)
    while true
        x1 = []
        finish = true
        (0..N).each do |i|
            x1[i] = 0
            (0..N).each do |j|
                if (j != i)
                    x1[i] += a[i][j] * x0[j]
                end
            end
            x1[i] = (b[i] - x1[i]) / a[i][i]
            if ((x1[i] - x0[i]).abs > 0.0000000001)
                finish = false
            end
        end
        (0..N).each do |i|
            x0[i] = x1[i]
        end

        return if finish

        disp_vector(x0)
    end
end

a = [[ 9.0,  2.0, 1.0,  1.0], [2.0, 8.0, -2.0, 1.0], [-1.0, -2.0, 7.0, -2.0], [1.0, -1.0, -2.0, 6.0]]
b =  [20.0, 16.0, 8.0, 17.0]
c =  [ 0.0,  0.0, 0.0,  0.0]

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

puts "X"
disp_vector(c)
Z:\>ruby Z:\1001.rb
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Groovy

N = 3

def a = [[ 9.0,  2.0, 1.0,  1.0], [2.0, 8.0, -2.0, 1.0], [-1.0, -2.0, 7.0, -2.0], [1.0, -1.0, -2.0, 6.0]]  as double[][]
def b =  [20.0, 16.0, 8.0, 17.0] as double[]
def c =  [ 0.0,  0.0, 0.0,  0.0] as double[]

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

println("X")
disp_vector(c)

// ヤコビの反復法
def jacobi(a, b, x0) {
    def x1 =  [ 0.0,  0.0, 0.0,  0.0] as double[]

    while (true) {
        def finish = true
        for (i in 0..N ) {
            x1[i] = 0
            for (j in 0..N ) {
                if (j != i)
                    x1[i] += a[i][j] * x0[j]
            }
            x1[i] = (b[i] - x1[i]) / a[i][i]
            if (Math.abs(x1[i] - x0[i]) > 0.0000000001)
                finish = false
        }
        for (i in 0..N )
            x0[i] = x1[i]
        if (finish) return

        disp_vector(x0)
    }
}
// 1次元配列を表示
def disp_vector(row) {
    for (col in row)
        printf ("%14.10f\t" , col)
    println()
}
Z:\>groovy Groovy1001.groovy
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Pascal

program Pas1001(arg);
{$MODE delphi}

uses
    SysUtils, Math;

const
    N = 3;

type
    TwoDimArray = array [0..N,0..N] of Double;

// 1次元配列を表示
procedure disp_vector(row:array of Double);
var
    i:Integer;
begin
    for i := Low(row) to High(row) do
        write(format('%14.10f'#9, [row[i]]));
    writeln();
end;

// ヤコビの反復法
procedure jacobi(a:TwoDimArray; b:array of Double; var x0:array of Double);
var
    x1:array [0..N] of Double = (0.0, 0.0, 0.0, 0.0);
    finish:Boolean;
    i, j:Integer;
begin
    while (true) do
    begin
        finish := true;
        for i := Low(x0) to High(x0) do
        begin
            x1[i] := 0.0;
            for j := Low(x0) to High(x0) do
                if j <> i then
                    x1[i] := x1[i] + a[i,j] * x0[j];

            x1[i] := (b[i] - x1[i]) / a[i,i];
            if (Abs(x1[i] - x0[i]) > 0.0000000001) then finish := false;
        end;
        for i := Low(x0) to High(x0) do
            x0[i] := x1[i];
        if finish then exit;

        disp_vector(x0);
    end;
end;

var
    a:TwoDimArray = (( 9.0,  2.0, 1.0,  1.0), (2.0, 8.0, -2.0, 1.0), (-1.0, -2.0, 7.0, -2.0), (1.0, -1.0, -2.0, 6.0));
    b:array [0..N] of Double = (20.0, 16.0, 8.0, 17.0);
    c:array [0..N] of Double = ( 0.0,  0.0, 0.0,  0.0);
begin
    // ヤコビの反復法
    jacobi(a,b,c);

    writeln('X');
    disp_vector(c);
end.
Z:\>fpc -v0 -l- Pas1001.pp

Z:\>Pas1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Ada

with TEXT_IO, Ada.Long_Float_Text_IO;
use  TEXT_IO, Ada.Long_Float_Text_IO;

procedure Ada1001 is

    N:Constant Integer := 3;

    type Long_Float_Array       is array (0..N)       of Long_Float;
    type Long_Float_TwoDimArray is array (0..N, 0..N) of Long_Float;

    a:Long_Float_TwoDimArray := (( 9.0,  2.0, 1.0,  1.0), (2.0, 8.0, -2.0, 1.0), (-1.0, -2.0, 7.0, -2.0), (1.0, -1.0, -2.0, 6.0));
    b:Long_Float_Array := (20.0, 16.0, 8.0, 17.0);
    c:Long_Float_Array := ( 0.0,  0.0, 0.0,  0.0);

    -- 1次元配列を表示
    procedure disp_vector(row:Long_Float_Array) is
    begin
        for i in row'Range loop
            Put(row(i), Fore=>3, Aft=>10, Exp=>0);
            Put(Ascii.HT);
        end loop;
        New_Line;
    end disp_vector;

    -- ヤコビの反復法
    procedure jacobi(a:Long_Float_TwoDimArray; b:Long_Float_Array; x0:in out Long_Float_Array) is
        x1:Long_Float_Array := ( 0.0,  0.0, 0.0,  0.0);
        finish:Boolean;
    begin
        while true loop
            finish := true;
            for i in x0'Range loop
                x1(i) := 0.0;
                for j in x0'Range loop
                    if j /= i then
                        x1(i) := x1(i) + a(i,j) * x0(j);
                    end if;
                end loop;

                x1(i) := (b(i) - x1(i)) / a(i,i);
                if (Abs(x1(i) - x0(i)) > 0.0000000001) then
                    finish := false;
                end if;
            end loop;
            for i in x0'Range loop
                x0(i) := x1(i);
            end loop;
            if finish then
                exit;
            end if;

            disp_vector(x0);
        end loop;
    end jacobi;
begin
    -- ヤコビの反復法
    jacobi(a,b,c);

    Put_Line("X");
    disp_vector(c);
end Ada1001;
xxxxxx@yyyyyy /Z
$ gnatmake Ada1001.adb

xxxxxx@yyyyyy /Z
$ Ada1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

VB.NET

Option Explicit

Module VB1001
    Private Const N As Integer = 3

    Public Sub Main()
        Dim a(,) As Double = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}}
        Dim b()  As Double = {20,16,8,17}
        Dim c()  As Double = {0,0,0,0}

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

        Console.WriteLine("X")
        disp_vector(c)
    End Sub

    '1次元配列の表示
    Private Sub disp_vector(ByVal row() As Double)
        For Each col As Double In row
            Console.Write(String.Format("{0,14:F10}{1}", col, vbTab))
        Next
        Console.WriteLine()
    End Sub

    'ヤコビの反復法
    Private Sub jacobi(ByVal a(,) As Double, ByVal b() As Double, ByVal x0() As Double)
        Do While(True)
            Dim x1() As Double = {0,0,0,0}
            Dim finish As Boolean = True
            For i As Integer = 0 To N
                x1(i) = 0
                For j As Integer = 0 To N
                    If j <> i Then
                        x1(i) += a(i,j) * x0(j)
                    End If
                Next

                x1(i) = (b(i) - x1(i)) / a(i,i)
                If (Math.Abs(x1(i) - x0(i)) > 0.0000000001) Then finish = False
            Next
            For i As Integer = 0 To N
                x0(i) = x1(i)
            Next
            If finish Then Return

            disp_vector(x0)
        Loop
    End Sub
End Module
Z:\>vbc -nologo VB1001.vb

Z:\>VB1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

C#

using System;

public class CS1001
{
    private const int N = 4;

    public static void Main()
    {
        double[,] a = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}};
        double[]  b = {20,16,8,17};
        double[]  c = {0,0,0,0};

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

        Console.WriteLine("X");
        disp_vector(c);
    }

    // ヤコビの反復法
    private static void jacobi(double[,] a, double[] b, double[] x0)
    {
        while (true)
        {
            double[] x1 = new double[N];
            bool finish = true;
            for (int i = 0; i < N; i++)
            {
                x1[i] = 0;
                for (int 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 (Math.Abs(x1[i] - x0[i]) > 0.0000000001) finish = false;
            }
            for (int i = 0; i < N; i++)
                x0[i] = x1[i];
            if (finish) return;

            disp_vector(x0);
        }
    }
    // 1次元配列を表示
    private static void disp_vector(double[] row)
    {
        foreach (double col in row)
            Console.Write(string.Format("{0,14:F10}\t", col));
        Console.WriteLine();
    }
}
Z:\>csc -nologo CS1001.cs

Z:\>CS1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Java

import java.lang.*;

public class Java1001 {

    private static final int N = 4;

    public static void main(String []args) {
        double[][] a = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}};
        double[]   b = {20,16,8,17};
        double[]   c = {0,0,0,0};

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

        System.out.println("X");
        disp_vector(c);
    }

    // ヤコビの反復法
    private static void jacobi(double[][] a, double[] b, double[] x0) {
        while (true) {
            double[] x1 = new double[N];
            boolean finish = true;
            for (int i = 0; i < N; i++) {
                x1[i] = 0;
                for (int 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 (Math.abs(x1[i] - x0[i]) > 0.0000000001) finish = false;
            }
            for (int i = 0; i < N; i++)
                x0[i] = x1[i];
            if (finish) return;

            disp_vector(x0);
        }
    }
    // 1次元配列を表示
    private static void disp_vector(double[] row) {
        for (double col: row)
            System.out.print(String.format("%14.10f\t", col));
        System.out.println();
    }
}
Z:\>javac Java1001.java

Z:\>java Java1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

C++

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

const int N = 4;

// ヤコビの反復法
void jacobi(double a[N][N], double b[N], double c[N]);
// 1次元配列を表示
void disp_vector(double row[N]);

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

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

    cout << "X" << endl;
    disp_vector(c);

    return 0;
}

// ヤコビの反復法
void jacobi(double a[N][N], double b[N], double x0[N])
{
    while (true)
    {
        double x1[N];
        bool finish = true;
        for (int i = 0; i < N; i++)
        {
            x1[i] = 0;
            for (int 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 (int i = 0; i < N; i++)
            x0[i] = x1[i];
        if (finish) return;

        disp_vector(x0);
    }
}

// 1次元配列を表示
void disp_vector(double row[N])
{
    for (int i = 0; i < N; i++)
        cout << setw(14) << fixed << setprecision(10) << row[i] << "\t";
    cout << endl;
}
Z:\>bcc32 -q CP1001.cpp
cp1001.cpp:

Z:\>CP1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Objective-C

#import <Foundation/Foundation.h>
#import <math.h>

const int N = 4;

// ヤコビの反復法
void jacobi(double a[N][N], double b[N], double c[N]);
// 1次元配列を表示
void disp_vector(double row[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("X\n");
    disp_vector(c);

    return 0;
}

// ヤコビの反復法
void jacobi(double a[N][N], double b[N], double x0[N])
{
    while (YES)
    {
        double x1[N];
        BOOL finish = YES;
        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 = NO;
        }
        for (i = 0; i < N; i++)
        {
            x0[i] = x1[i];
        }
        if (finish) return;

        disp_vector(x0);
    }
}
// 1次元配列を表示
void disp_vector(double row[N])
{
    int i;
    for (i = 0; i < N; i++)
    {
        printf("%14.10f\t", row[i]);
    }
    printf("\n");
}
xxxxxx@yyyyyy /Z
$ gcc -o OC1001 OC1001.m -lobjc -lgnustep-base -I $INCLUDE -L $LIB $CFLAGS

xxxxxx@yyyyyy /Z
$ OC1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

D

import std.stdio;
import std.math;

const int N = 4;

void main(string[] args)
{
    double[N][N] a = [[9,2,1,1],[2,8,-2,1],[-1,-2,7,-2],[1,-1,-2,6]];
    double[N]    b = [20,16,8,17];
    double[N]    c = [0,0,0,0];

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

    writefln("X");
    disp_vector(c);
}
// ヤコビの反復法
void jacobi(double[N][N] a, double[N] b, ref double[N] x0)
{
    while (true)
    {
        double[N] x1;
        bool finish = true;
        foreach(i; 0..N)
        {
            x1[i] = b[i];
            foreach(j; 0..N)
                if (j != i)
                    x1[i] -= a[i][j] * x0[j];

            x1[i] /= a[i][i];
            if (fabs(x1[i] - x0[i]) > 0.0000000001) finish = false;
        }
        foreach(i; 0..N)
            x0[i] = x1[i];
        if (finish) return;

        disp_vector(x0);
    }
}
// 1次元配列を表示
void disp_vector(double[] row)
{
    foreach(col; row)
        writef("%14.10f\t", col);
    writefln("");
}
Z:\>dmd D1001.d

Z:\>D1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Go

package main

import "fmt"
import "math"

const N = 4

func main() {
    var a [N][N]float64 = [N][N]float64{{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}}
    var b = []float64{20,16,8,17}
    var c = []float64{0,0,0,0}

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

    fmt.Println("X")
    disp_vector(c)
}
// ヤコビの反復法
func jacobi(a[N][N]float64, b[]float64, x0[]float64) {
    for {
        var x1 = []float64{0,0,0,0}
        var finish = true
        for i := 0; i < N; i++ {
            x1[i] = b[i]
            for j := 0; j < N; j++ {
                if (j != i) {
                    x1[i] -= a[i][j] * x0[j]
                }
            }
            x1[i] /= a[i][i]
            if (math.Fabs(x1[i] - x0[i]) > 0.0000000001) {
                finish = false
            }
        }
        for i := 0; i < N; i++ {
            x0[i] = x1[i]
        }
        if (finish) {
            return
        }

        disp_vector(x0)
    }
}
// 1次元配列を表示
func disp_vector(row[]float64) {
    for _, col := range row {
        fmt.Printf("%14.10f\t", col)
    }
    fmt.Println("")
}
Z:\>8g GO1001.go

Z:\>8l -o GO1001.exe GO1001.8

Z:\>GO1001
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Scala

object Scala1001 {
    val N = 3

    def main(args: Array[String]) {
        var a:Array[Array[Double]] = Array(Array(9,2,1,1),Array(2,8,-2,1),Array(-1,-2,7,-2),Array(1,-1,-2,6))
        var b:Array[Double] = Array(20,16,8,17)
        var c:Array[Double] = Array(0,0,0,0)

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

        println("X")
        disp_vector(c)
    }
    // ヤコビの反復法
    def jacobi(a:Array[Array[Double]], b:Array[Double], x0:Array[Double]) = {
        var finish:Boolean = false
        while (!finish) {
            var x1:Array[Double] = Array(0,0,0,0)
            finish = true

            for (i <- 0 to N) {
                x1(i) = 0
                for (j <- 0 to N)
                    if (j != i)
                        x1(i) += a(i)(j) * x0(j)

                x1(i) = (b(i) - x1(i)) / a(i)(i)
                if (Math.abs(x1(i) - x0(i)) > 0.0000000001)
                    finish = false
            }
            for (i <- 0 to N)
                x0(i) = x1(i)

            if (!finish)
                disp_vector(x0)
        }
    }

    // 1次元配列を表示
    def disp_vector(row:Array[Double]) = {
        row.foreach { col =>
            print("%14.10f\t".format(col))
        }
        println()
    }
}
Z:\>scala Scala1001.scala
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

F#

module Fs1001

open System

let N = 3

// 1次元配列を表示
let disp_vector (row:float[]) =
    row
    |> Array.iter (fun x -> printf "%14.10f" x)
    printfn ""

// ヤコビの反復法
let jacobi (a:float[][]) (b:float[]) (x0:float[]) =
    let mutable finish:bool = false
    while not finish do
        let x1:float[] = [| 0.0;0.0;0.0;0.0 |]
        finish <- true

        for i in [0..N] do
            x1.[i] <- 0.0
            for j in [0..N] do
               if j <> i then
                   x1.[i] <- x1.[i] + a.[i].[j] * x0.[j]

            x1.[i] <- (b.[i] - x1.[i]) / a.[i].[i]
            if Math.Abs(x1.[i] - x0.[i]) > 0.0000000001 then
                finish <- false

        for i in [0..N] do
            x0.[i] <- x1.[i]

        if not finish then
            disp_vector x0

let a:float[][] = [| [| 9.0;2.0;1.0;1.0 |]; [|2.0;8.0;-2.0;1.0 |]; [|-1.0;-2.0;7.0;-2.0 |]; [|1.0;-1.0;-2.0;6.0 |] |]
let b:float[] = [| 20.0;16.0;8.0;17.0 |]
let c:float[] = [| 0.0;0.0;0.0;0.0 |]

// ヤコビの反復法
jacobi a b c

printfn "X"
disp_vector c

exit 0
Z:\>fsi  --nologo --quiet Fs1001.fs
  2.2222222222  2.0000000000  1.1428571429  2.8333333333
  1.3359788360  1.3759920635  2.8412698413  3.1772486772
  1.2477219283  1.9791666667  2.6346371882  3.7870921517
  1.0688819252  1.8733422960  2.9686056521  3.8334531858
  1.0501396189  1.9957492835  2.9260675555  3.9569452792
  1.0139431776  1.9743638243  2.9936469635  3.9662907960
  1.0101482880  1.9991395970  2.9850360597  3.9912857623
  1.0028221093  1.9948112226  2.9987141438  3.9931772381
  1.0020540192  1.9998258539  2.9969712901  3.9982362335
  1.0005711965  1.9989497885  2.9997397420  3.9986190691
  1.0004157346  1.9999647527  2.9993869874  3.9996430127
  1.0001156105  1.9997874366  2.9999473236  3.9997204988
  1.0000841449  1.9999928659  2.9998759259  3.9999277456
  1.0000233996  1.9999569771  2.9999893383  3.9999434288
  1.0000170310  1.9999985561  2.9999748873  3.9999853757
  1.0000047361  1.9999912921  2.9999978421  3.9999885500
  1.0000034471  1.9999997077  2.9999949172  3.9999970400
  1.0000009586  1.9999982375  2.9999995632  3.9999976825
  1.0000006977  1.9999999408  2.9999989712  3.9999994009
  1.0000001940  1.9999996433  2.9999999116  3.9999995309
  1.0000001412  1.9999999880  2.9999997918  3.9999998787
  1.0000000393  1.9999999278  2.9999999821  3.9999999051
  1.0000000286  1.9999999976  2.9999999579  3.9999999755
  1.0000000079  1.9999999854  2.9999999964  3.9999999808
  1.0000000058  1.9999999995  2.9999999915  3.9999999950
  1.0000000016  1.9999999970  2.9999999993  3.9999999961
  1.0000000012  1.9999999999  2.9999999983  3.9999999990
  1.0000000003  1.9999999994  2.9999999999  3.9999999992
  1.0000000002  2.0000000000  2.9999999997  3.9999999998
  1.0000000001  1.9999999999  3.0000000000  3.9999999998
  1.0000000000  2.0000000000  2.9999999999  4.0000000000
X
  1.0000000000  2.0000000000  3.0000000000  4.0000000000

Clojure

(def N 4)

(def a [[9.0 2.0 1.0 1.0] [2.0 8.0 -2.0 1.0] [-1.0 -2.0 7.0 -2.0] [1.0 -1.0 -2.0 6.0]])
(def b  [20.0 16.0 8.0 17.0])
(def x0 [0.0 0.0 0.0 0.0])

;1次元配列を表示
(defn disp_vector [row]
    (doseq [col row]
        (print (format "%14.10f\t" col)))
    (println))

;2次元配列を表示
(defn disp_matrix [matrix]
    (doseq [row matrix]
        (doseq [col row]
            (print (format "%14.10f\t" col)))
        (println)))

;ヤコビの反復法
(defn row_loop [row a b x0 x1]
    (def a1 (map (fn [a b] [a b]) (nth a row) x0))
    (def a2 (concat (take row a1) (drop (+ row 1) a1)))
    (def a3 (map (fn [x] (* (first x) (second x))) a2))
    (def s  (apply + a3))
    (def x  (/ (- (nth b row) s) (nth (nth a row) row)))
    (def xs (cons x x1))

    (if (>= row (- N 1))
        (reverse xs)
        (row_loop (inc row) a b x0 xs)))

(defn jacobi [a b x0 xs]
    (def x1 (row_loop 0 a b x0 []))

    (def cnt (count
        (filter (fn [x] (>= x 0.0000000001))
            (map (fn [x] (. Math abs (- (first x) (second x))))
                (map (fn [a b] [a b]) x0 x1)))))

    (if (< cnt 1)
        (vector (reverse xs) x1)
        (jacobi a b x1 (cons x1 xs))))

;ヤコビの反復法
(def xs (jacobi a b x0 []))

(disp_matrix (first xs))
(println "X")
(disp_vector (second xs))
Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main Clj1001.clj
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000

Haskell

import Text.Printf
import Control.Monad

n = 4::Int

-- 2次元配列を表示
disp_matrix::[[Double]]->IO()
disp_matrix matrix = do
    forM_ matrix $ \row -> do
        forM_ row $ \elem -> do
            printf "%14.10f\t" elem
        putStrLn ""

-- 1次元配列を表示
disp_vector::[Double]->IO()
disp_vector vector = do
    forM_ vector $ \elem -> do
        printf "%14.10f\t" elem
    putStrLn ""

-- ヤコビの反復法
row_loop::Int->[[Double]]->[Double]->[Double]->[Double]->[Double]
row_loop row a b x0 x1 =
    let
        a1 = zip (a!!row) x0
        a2 = take row a1 ++ drop (row + 1) a1
        a3 = map (\(x, y) -> x * y) $ a2
        s  = sum a3
        x  = ((b!!row) - s) / a!!row!!row
        xs = x:x1
    in
        if row >= (n - 1)
            then
                reverse xs
            else
                (row_loop (row+1) a b x0 xs)

jacobi::[[Double]]->[Double]->[Double]->[[Double]]->([[Double]],[Double])
jacobi a b x0 xs =
    let
        x1  = (row_loop 0 a b x0 [])
        cnt = length $ filter (>= 0.0000000001) $ map (\(x,y) -> abs(x - y)) $ zip x0 x1
    in
        if cnt < 1
            then
                (reverse xs, x1)
            else
                (jacobi a b x1 (x1:xs))

main = do
    let a  = [[9,2,1,1],[2,8,-2,1],[-1,-2,7,-2],[1,-1,-2,6::Double]]
    let b  = [20,16,8,17::Double]
    let x0 = [0,0,0,0::Double]

    -- ヤコビの反復法
    let (xs, x1) = jacobi a b x0 []
    disp_matrix xs

    putStrLn "X"
    disp_vector x1
Z:\>runghc Hs1001.hs
  2.2222222222    2.0000000000    1.1428571429    2.8333333333
  1.3359788360    1.3759920635    2.8412698413    3.1772486772
  1.2477219283    1.9791666667    2.6346371882    3.7870921517
  1.0688819252    1.8733422960    2.9686056521    3.8334531858
  1.0501396189    1.9957492835    2.9260675555    3.9569452792
  1.0139431776    1.9743638243    2.9936469635    3.9662907960
  1.0101482880    1.9991395970    2.9850360597    3.9912857623
  1.0028221093    1.9948112226    2.9987141438    3.9931772381
  1.0020540192    1.9998258539    2.9969712901    3.9982362335
  1.0005711965    1.9989497885    2.9997397420    3.9986190691
  1.0004157346    1.9999647527    2.9993869874    3.9996430127
  1.0001156105    1.9997874366    2.9999473236    3.9997204988
  1.0000841449    1.9999928659    2.9998759259    3.9999277456
  1.0000233996    1.9999569771    2.9999893383    3.9999434288
  1.0000170310    1.9999985561    2.9999748873    3.9999853757
  1.0000047361    1.9999912921    2.9999978421    3.9999885500
  1.0000034471    1.9999997077    2.9999949172    3.9999970400
  1.0000009586    1.9999982375    2.9999995632    3.9999976825
  1.0000006977    1.9999999408    2.9999989712    3.9999994009
  1.0000001940    1.9999996433    2.9999999116    3.9999995309
  1.0000001412    1.9999999880    2.9999997918    3.9999998787
  1.0000000393    1.9999999278    2.9999999821    3.9999999051
  1.0000000286    1.9999999976    2.9999999579    3.9999999755
  1.0000000079    1.9999999854    2.9999999964    3.9999999808
  1.0000000058    1.9999999995    2.9999999915    3.9999999950
  1.0000000016    1.9999999970    2.9999999993    3.9999999961
  1.0000000012    1.9999999999    2.9999999983    3.9999999990
  1.0000000003    1.9999999994    2.9999999999    3.9999999992
  1.0000000002    2.0000000000    2.9999999997    3.9999999998
  1.0000000001    1.9999999999    3.0000000000    3.9999999998
  1.0000000000    2.0000000000    2.9999999999    4.0000000000
X
  1.0000000000    2.0000000000    3.0000000000    4.0000000000
inserted by FC2 system