The code below may give your some ideas. I don't have the time, and mostly don't feel like implementing the algorithm, but I did some of the routines you implemented the way I currently code in Perl. Also, this could be homework ;-)

Note that if I were to write real code I'd

  1. use the Math::MatrixReal module or better still PDL;
  2. bother to check the regex for checking real numbers carefully, this was just hastily thrown together, so use with care;
  3. use some proper documentation tool such as POD or robodoc

The code is below, comments/flames/suggestions are welcome. Just my 2 cents, -gjb-

#!perl use strict; use warnings; # declaration of matrix $A and $B my $A; my $B; # declartion of format string for nice looking output my $format = "%11.4e"; # read the equations { my $rows = 0; while (<DATA>) { chomp($_); # strip leading and trailing whitespace s/^\s*(.+?)\s*$/$1/; # split equation into left hand side and right hand side my ($lhs, $rhs) = split(/\s*=\s*/, $_); # check whether there's something left and right of the # equality operator die("lhs is not defined in $_") unless length($lhs) > 0; die("rhs is not defined in $_") unless length($rhs) > 0; # split the lhs into it's elements my @numbers = split(/\s+/, $lhs); # check whether all lhs and the rhs are real numbers check_number_format(@numbers, $rhs); # assign row of matrix $A $A->[$rows] = [@numbers]; # assign element of vector $B $B->[$rows++] = $rhs; } check_matrix_valid($A, $format); } print "matrix A:\n", matrix_to_string($A, $format), "\n\n"; print "vector B:\n", matrix_to_string($B, $format), "\n\n"; print "equations:\n", equations_to_string($A, $B, $format), "\n\n"; # ------------------------------------------------------------ # function takes a matrix and a format string and checks the # matrix to see if each row has the same number of elements; # the format string is provided to get a nicely formatted # error message sub check_matrix_valid { my ($M, $fmt) = @_; # take the number of columns equal to the number of elements # of the first row my $columns = scalar(@{$M->[0]}); # check the number of elements of each row; foreach (1..$#{@$M}) { die("matrix is not filled completely:\n" . matrix_to_string($M, $fmt) . "\n ") unless $columns == scalar(@{$M->[$_]}); } # check whether the matrix is square die("matrix is not square:\n" . matrix_to_string($M)) unless scalar(@$M) == $columns; } # ------------------------------------------------------------ # function takes a matrix, a vector and a format string and # them to a string representing the set of equations using # the format string for a nice layout sub equations_to_string { my ($A, $B, $fmt) = @_; # for code reuse, matrix_to_string is used, but the rows # must now be pulled apart first my @rowStr = split(/\n/, matrix_to_string($A, $fmt)); my @rowRHStr = split(/\n/, matrix_to_string($B, $fmt)); # construct each equation separately $rowStr[$_] .= ' = ' . $rowRHStr[$_] foreach (0..$#rowStr); # put all equations together again (or rows if you wish) return join("\n", @rowStr); } # ------------------------------------------------------------ # function takes a matrix or a vector and a format string and # converts the matrix to a string using the specified format # to get a nice layout sub matrix_to_string { my ($M, $fmt) = @_; return join("\n", # if the element of $M is a reference, it's a # matrix, otherwise it's a vector; # if it's a matrix, join the row elements after # applying the format; # if it's a vector, just format the element map((ref($_) ? join("\t", map(sprintf($fmt, $_), @$_)) : sprintf($fmt, $_)), @$M)); } # ------------------------------------------------------------ # for each argument of the function, check whether it's a # valid real number and die if not sub check_number_format { foreach my $number (@_) { die("$number is not a valid numerical format") unless $number =~ /^ # optional leading sign (?: \+|- )? # number is either nn, nn.nn or .nn (?: \d+ (?: \.\d+ )? | \.\d+ ) # optional exponential (?: (?: e|E ) (?: \+|- )? \d+ )? $/x; } } __DATA__ .3 4.5 -2e-5 = +7.17 0.8e+03 0.3 -8 = 0 17 2 -3 = 5

In reply to Re: Gaussian Elimination Algorithm by gjb
in thread Gaussian Elimination Algorithm by grexman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.