#!/usr/bin/perl
use warnings;
use Math::Matrix;
#say you have equations
# 1x 2y 3z 4w = 17
# 5x 6y 7z 8w = 18
# 9x 10y 11z 12w = 19
# 13x 14y 15z 16w = 20
$a = new Math::Matrix ([1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]);
$x = new Math::Matrix ([17,18,19,20]);
print "det-> ",$a->determinant,"\n";
$a->print("A\n");
$E = $a->concat($x->transpose);
$E->print("Equation system\n");
$s = $E->solve;
$s->print("Solutions s\n");
$a->multiply($s)->print("A*s\n");
####
OUTPUT
det-> 0
A
1.00000 2.00000 3.00000 4.00000
5.00000 6.00000 7.00000 8.00000
9.00000 10.00000 11.00000 12.00000
13.00000 14.00000 15.00000 16.00000
Equation system
1.00000 2.00000 3.00000 4.00000 17.00000
5.00000 6.00000 7.00000 8.00000 18.00000
9.00000 10.00000 11.00000 12.00000 19.00000
13.00000 14.00000 15.00000 16.00000 20.00000
Solutions s
-16.50000
16.75000
0.00000
0.00000
A*s
17.00000
18.00000
19.00000
20.00000
##
##
#!/usr/bin/perl
use warnings;
use strict;
use Math::MatrixReal;
my ($a1, $b1, $c1, $d1, $e1,
$a2, $b2, $c2, $d2, $e2,
$a3, $b3, $c3, $d3, $e3,
$a4, $b4, $c4, $d4, $e4) =
(1, 2, 3, 4, 17,
5, 6, 7, 8, 18,
9, 10, 11, 12, 19,
13, 14, 15, 16, 20);
my $A = Math::MatrixReal->new_from_string(<new_from_string(< ",$A->det(),"\n";
my $LR = $A->decompose_LR();
my ($dim, $solution, $B) = $LR->solve_LR($b);
#print "dim->$dim\n\nsolution->\n$solution\n\nbase->\n$B\n\n";
print "dim->$dim\n\nsolution->\n$solution\n\nbase->\n$B\n\n";
my $out = $A * $solution;
print $out,"\n";
##
##
OUTPUT:
det-> 0
dim->2
solution->
[ -5.333333333333E+00 ]
[ 0.000000000000E+00 ]
[ 0.000000000000E+00 ]
[ 5.583333333333E+00 ]
[ 1.700000000000E+01 ]
[ 1.800000000000E+01 ]
[ 1.900000000000E+01 ]
[ 2.000000000000E+01 ]
##
##
#!/usr/bin/perl
use warnings;
use Math::Matrix;
$a = new Math::Matrix ([1, -2, 3, 4],
[4, -5, 6, 7],
[7, 8, -9, 10],
[8, 9, -9, -11],
);
$x = new Math::Matrix ([17,18,19,20]);
print "det-> ",$a->determinant,"\n";
$a->print("A\n");
$E = $a->concat($x->transpose);
$E->print("Equation system\n");
$s = $E->solve;
$s->print("Solutions s\n");
$a->multiply($s)->print("A*s\n");
##
##
OUTPUT:
det-> -1104
A
1.00000 -2.00000 3.00000 4.00000
4.00000 -5.00000 6.00000 7.00000
7.00000 8.00000 -9.00000 10.00000
8.00000 9.00000 -9.00000 -11.00000
Equation system
1.00000 -2.00000 3.00000 4.00000 17.00000
4.00000 -5.00000 6.00000 7.00000 18.00000
7.00000 8.00000 -9.00000 10.00000 19.00000
8.00000 9.00000 -9.00000 -11.00000 20.00000
Solutions s
1.45652
18.03261
16.02899
0.88043
A*s
17.00000
18.00000
19.00000
20.00000
##
##
#!/usr/bin/perl
use warnings;
use strict;
use Math::MatrixReal;
my ($a1, $b1, $c1, $d1, $e1,
$a2, $b2, $c2, $d2, $e2,
$a3, $b3, $c3, $d3, $e3,
$a4, $b4, $c4, $d4, $e4) =
(1, -2, 3, 4, 17,
4, -5, 6, 7, 18,
7, 8, -9, 10, 19,
8, 9, -9, -11, 20);
my $A = Math::MatrixReal->new_from_string(<new_from_string(< ",$A->det(),"\n";
my $LR = $A->decompose_LR();
my ($dim, $solution, $B) = $LR->solve_LR($b);
#print "dim->$dim\n\nsolution->\n$solution\n\nbase->\n$B\n\n";
print "dim->$dim\n\nsolution->\n\n$solution\n\n";
my $out = $A * $solution;
print $out,"\n";
##
##
det-> -1104
dim->0
solution->
[ 1.456521739130E+00 ]
[ 1.803260869565E+01 ]
[ 1.602898550725E+01 ]
[ 8.804347826087E-01 ]
[ 1.700000000000E+01 ]
[ 1.800000000000E+01 ]
[ 1.900000000000E+01 ]
[ 2.000000000000E+01 ]
##
##
[1 0 0 0],
[0 1 0 0],
[0 0 1 0],
[0 0 0 1]
##
##
#!/usr/bin/perl
use warnings;
use Math::MatrixReal;
$a = Math::MatrixReal->new_from_rows ([
[1, -2, 3, 4],
[4, -5, 6, 7],
[7, 8, -9, 10],
[8, 9, -9, -11],
]);
print $a,"\n";
my $b = Math::MatrixReal->new_from_cols ([
[17,18,19,20]
]);
print $b,"\n";
print $a->det(),"\n";
$inva = $a->inverse();
print $inva,"\n";
$c = $inva * $b ;
print "solution->\n$c\n";
##
##
OUTPUT:
[ 1.000000000000E+00 -2.000000000000E+00 3.000000000000E+00 4.000000000000E+00 ]
[ 4.000000000000E+00 -5.000000000000E+00 6.000000000000E+00 7.000000000000E+00 ]
[ 7.000000000000E+00 8.000000000000E+00 -9.000000000000E+00 1.000000000000E+01 ]
[ 8.000000000000E+00 9.000000000000E+00 -9.000000000000E+00 -1.100000000000E+01 ]
[ 1.700000000000E+01 ]
[ 1.800000000000E+01 ]
[ 1.900000000000E+01 ]
[ 2.000000000000E+01 ]
-1104
[ -1.684782608696E-01 1.739130434783E-01 5.434782608696E-03 5.434782608696E-02 ]
[ 1.595108695652E+00 -6.304347826087E-01 -3.532608695652E-02 1.467391304348E-01 ]
[ 1.362318840580E+00 -4.492753623188E-01 -8.695652173913E-02 1.304347826087E-01 ]
[ 6.793478260870E-02 -2.173913043478E-02 4.619565217391E-02 -3.804347826087E-02 ]
solution->
[ 1.456521739130E+00 ]
[ 1.803260869565E+01 ]
[ 1.602898550725E+01 ]
[ 8.804347826087E-01 ]