#!/apps/perl/bin/perl -w
use lib '/net/uranus/usr1/projects/testing';
my $infile =$ARGV[0];
open (IN, "$infile") or die $!;
$outfile= "check.val";
open (OUT, "> $outfile") || die $!;


$count=0;
while (<IN>){
@mo=split(/\s+/,$_);
push @da,$mo[0];
push @db,$mo[1];
push @dc,$mo[2];
$count++;
$total =$count
}
close(IN);
$max=4;
$dif=$total-$max;
print OUT " dif $dif max $max total $total \n";
print OUT "total = $total \n";
$num=0;
while ($num ne $total){
print OUT "$da[$num], $db[$num], $dc[$num]\n";
$num++;
}
close(OUT);

$dset=0;
while($dset ne $dif){
$inc=0;
$max=4;
$inc=$inc+$dset;
$max=$max+$dset;
while ($inc ne $max){ 
my $w=0;
while ($w le 1){
my $x=0;
while ($x le 1){
$a[$dset][$inc-$dset][$w][$x]=($da[$inc]**$x)*($db[$inc]**$w);
$x++;
}
$w++;
}
$inc++;
}
$dset++;
}
$dsetm=0;
$outfile2= "marry.val";
open (OUTB, "> $outfile2") || die $!;
while($dsetm ne $dif){
$mcnt=0;
$maxm=4;
while ($mcnt ne $maxm){
$b[$dsetm][$mcnt]=NULL;
my $w=0;
while ($w le 1){
my $x=0;
while ($x le 1){
$b[$dsetm][$mcnt]=join ",",$b[$dsetm][$mcnt],$a[$dsetm][$mcnt][$w][$x];
print OUTB "$maxm $dsetm $mcnt $w $x \n";
$x++;
}
$w++;
}
$mcnt++;
}
$dsetm++;
}
print OUTB "$b[0][0]\n";
print OUTB "$dsetm $mcnt\n";
print OUTB "$b[$dsetm-1][$mcnt-1]\n";
$dsetm=0;
while($dsetm ne $dif){
$mcnt=0;
$maxm=4;
while ($mcnt ne $maxm){
$b[$dsetm][$mcnt]=~s/NULL\,/\[/;
$b[$dsetm][$mcnt]=~s/$/\]/;
#print OUTB "$dsetm $mcnt\n";
$mcnt++;
}
$dsetm++;
}
print OUTB "$b[$dsetm-1][$mcnt-1]\n";
close(OUTB); 

$outfile3= "ma.val";
open (OUTC, "> $outfile3") || die $!;
$dm=0;
while($dm ne $dif){
$mm=0;
$maxmm=4;
$m[$dm]=NULL;
while ($mm ne $maxmm){
$m[$dm]= join ",",$m[$dm],$b[$dm][$mm];
#print OUTC "$maxmm $dm $mm\n";
$mm++;
}
$dm++;
}
$dma=0;
while($dma ne $dif){
$m[$dma]=~s/NULL\,//;
$dma++;
}
print OUTC "$m[0]\n";
print OUTC "$dma\n";
print OUTC "$m[$dma-1]\n";
close(OUTC); 


$outfile4= "matcheck.val";
open (OUTG, "> $outfile4") || die $!;
$dmtf=0;
while($dmtf ne $dif){
$mmtf=0;
$maxmmtf =4;
$mmtf=$mmtf+$dmtf;
$maxmmtf =$maxmmtf+$dmtf;
$mtfall[$dmtf]=NULL;
while ($mmtf ne $maxmmtf){
$mtfall[$dmtf]=join",",$mtfall[$dmtf],$dc[$mmtf];
#print OUTG "$maxmmtf $dmtf $mmtf\n";
$mmtf++;
}
$dmtf++;
}
print OUTG "$mtfall[0]\n";
print OUTG "$dmtf $mmtf\n";
print OUTG "$mtfall[$dmtf-1]\n";
$dmtfs=0;
while($dmtfs ne $dif){
$mtfall[$dmtfs]=~s/NULL\,/\[/;
$mtfall[$dmtfs]=~s/$/\]/;

$dmtfs++;
}
print OUTG "$mtfall[0]\n";
print OUTG "$dmtfs\n";
print OUTG "$mtfall[$dmtfs-1]\n";
close(OUTG); 


use Math::Matrix;
$outfile7= "mtesting.val";
open (OUTH, "> $outfile7") || die $!;
print OUTH "$m[0] \n";
print OUTH "$mtfall[0] \n";
$mcof=$m[0];
$mdelcr=$mtfall[0];
print OUTH "$mcof \n";
print OUTH "$mdelcr \n";
$cof = new Math::Matrix ($mcof);
$delcr = new Math::Matrix ($mdelcr);
$cof->print("cof\n");
print OUTH "cof",$cof->print ,"\n";
$delcr->print("delcr\n");
print OUTH "det ->",$cof-> determinant,"\n";
$crsys = $cof->concat($delcr->transpose);
$crsys->print("Equation system\n");
$scr = $crsys->solve;
$scr->print("Solutions s\n");

