user
In linear algebra, Cramer's rule is an explicit formula for the solution of a system of linear equations with as many equations as unknowns, valid whenever the system has a unique solution. It expresses the solution in terms of the determinants of the (square) coefficient matrix and of matrices obtained from it by replacing one column by the vector of right hand sides of the equations.

Given

:   {

    a

    1
    x
    +

    b

    1
    y
    +

    c

    1
    z

    =

    d

    1

    a

    2
    x
    +

    b

    2
    y
    +

    c

    2
    z

    =

    d

    2

    a

    3
    x
    +

    b

    3
    y
    +

    c

    3
    z

    =

    d

    3

    {\displaystyle {\displaystyle \left\{{\begin{matrix}a\_{1}x+b\_{1}y+c\_{1}z&={\color {red}d\_{1}}\\a\_{2}x+b\_{2}y+c\_{2}z&={\color {red}d\_{2}}\\a\_{3}x+b\_{3}y+c\_{3}z&={\color {red}d\_{3}}\end{matrix}}\right.}}

which in matrix format is

:   [

    a

    1


    b

    1


    c

    1

    a

    2


    b

    2


    c

    2

    a

    3


    b

    3


    c

    3
    ]


    [

    x


    y


    z
    ]
    =


    [

    d

    1

    d

    2

    d

    3
    ]
    .
    {\displaystyle {\displaystyle {\begin{bmatrix}a\_{1}&b\_{1}&c\_{1}\\a\_{2}&b\_{2}&c\_{2}\\a\_{3}&b\_{3}&c\_{3}\end{bmatrix}}{\begin{bmatrix}x\\y\\z\end{bmatrix}}={\begin{bmatrix}{\color {red}d\_{1}}\\{\color {red}d\_{2}}\\{\color {red}d\_{3}}\end{bmatrix}}.}}

Then the values of 

x
,
y
{\displaystyle {\displaystyle x,y}}
 and 

z
{\displaystyle {\displaystyle z}}
 can be found as follows:

:   x
    =

    |

    d

    1


    b

    1


    c

    1

    d

    2


    b

    2


    c

    2

    d

    3


    b

    3


    c

    3
    |

    |

    a

    1


    b

    1


    c

    1

    a

    2


    b

    2


    c

    2

    a

    3


    b

    3


    c

    3
    |
    ,

    y
    =

    |

    a

    1

    d

    1


    c

    1

    a

    2

    d

    2


    c

    2

    a

    3

    d

    3


    c

    3
    |

    |

    a

    1


    b

    1


    c

    1

    a

    2


    b

    2


    c

    2

    a

    3


    b

    3


    c

    3
    |
    ,

     and 
    z
    =

    |

    a

    1


    b

    1

    d

    1

    a

    2


    b

    2

    d

    2

    a

    3


    b

    3

    d

    3
    |

    |

    a

    1


    b

    1


    c

    1

    a

    2


    b

    2


    c

    2

    a

    3


    b

    3


    c

    3
    |
    .
    {\displaystyle {\displaystyle x={\frac {\begin{vmatrix}{\color {red}d\_{1}}&b\_{1}&c\_{1}\\{\color {red}d\_{2}}&b\_{2}&c\_{2}\\{\color {red}d\_{3}}&b\_{3}&c\_{3}\end{vmatrix}}{\begin{vmatrix}a\_{1}&b\_{1}&c\_{1}\\a\_{2}&b\_{2}&c\_{2}\\a\_{3}&b\_{3}&c\_{3}\end{vmatrix}}},\quad y={\frac {\begin{vmatrix}a\_{1}&{\color {red}d\_{1}}&c\_{1}\\a\_{2}&{\color {red}d\_{2}}&c\_{2}\\a\_{3}&{\color {red}d\_{3}}&c\_{3}\end{vmatrix}}{\begin{vmatrix}a\_{1}&b\_{1}&c\_{1}\\a\_{2}&b\_{2}&c\_{2}\\a\_{3}&b\_{3}&c\_{3}\end{vmatrix}}},{\text{ and }}z={\frac {\begin{vmatrix}a\_{1}&b\_{1}&{\color {red}d\_{1}}\\a\_{2}&b\_{2}&{\color {red}d\_{2}}\\a\_{3}&b\_{3}&{\color {red}d\_{3}}\end{vmatrix}}{\begin{vmatrix}a\_{1}&b\_{1}&c\_{1}\\a\_{2}&b\_{2}&c\_{2}\\a\_{3}&b\_{3}&c\_{3}\end{vmatrix}}}.}}

Task

Given the following system of equations:

:   {

    2
    w
    −
    x
    +
    5
    y
    +
    z
    =
    −
    3


    3
    w
    +
    2
    x
    +
    2
    y
    −
    6
    z
    =
    −
    32


    w
    +
    3
    x
    +
    3
    y
    −
    z
    =
    −
    47


    5
    w
    −
    2
    x
    −
    3
    y
    +
    3
    z
    =
    49

    {\displaystyle {\displaystyle {\begin{cases}2w-x+5y+z=-3\\3w+2x+2y-6z=-32\\w+3x+3y-z=-47\\5w-2x-3y+3z=49\\\end{cases}}}}

solve for 

w
{\displaystyle {\displaystyle w}}
, 

x
{\displaystyle {\displaystyle x}}
, 

y
{\displaystyle {\displaystyle y}}
 and 

z
{\displaystyle {\displaystyle z}}
, using Cramer's rule.
assistant
(formerly Perl 6)

```
sub det(@matrix) {
    my @a     = @matrix.map: { [|$_] };
    my $sign  = 1;
    my $pivot = 1;
    for ^@a -> \k {
      my @r = (k+1 ..^ @a);
      my $previous-pivot = $pivot;
      if 0 == ($pivot = @a[k;k]) {
        (my \s = @r.first: { @a[$_;k] != 0 }) // return 0;
        (@a[s], @a[k]) = (@a[k], @a[s]);
        my $pivot = @a[k;k];
        $sign = -$sign;
      }
      for @r X @r -> (\i,\j) {
        ((@a[i;j] ×= $pivot) -= @a[i;k]×@a[k;j]) /= $previous-pivot;
      }
    }
    $sign × $pivot
}

sub cramers_rule(@A, @terms) {
    gather for ^@A -> \i {
        my @Ai = @A.map: { [|$_] };
        for ^@terms -> \j {
            @Ai[j;i] = @terms[j];
        }
        take det(@Ai);
    } »/» det(@A);
}

my @matrix = (
    [2, -1,  5,  1],
    [3,  2,  2, -6],
    [1,  3,  3, -1],
    [5, -2, -3,  3],
);

my @free_terms = <-3 -32 -47 49>;
my ($w, $x, $y, $z) = cramers_rule(@matrix, @free_terms);
("w = $w", "x = $x", "y = $y", "z = $z").join("\n").say;

```

Output:

```
w = 2
x = -12
y = -4
z = 1

```