Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ my $builder = Module::Build->new(
license => 'perl',
build_requires => {
'Test::Most' => 0,
'File::Temp' => 0,
'File::Spec' => 0,
},
configure_requires => { 'Module::Build' => 0.38 },
Expand Down
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

* Add save_to_file/new from file (Alberto Simões)

Changes in v2.11 - Jonathan "Duke" Leto
January 2014
--------------------------
Expand Down
2 changes: 2 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Many people deserve recoginition for their help with Math::MatrixReal:
* Mike South <msouth@fulcrum.org> for the new_from_cols() and new_from_rows() functions
that were integrated from Math::MatrixReal::Ext1

* Alberto Simões <ambs@cpan.org> for contributions.

* Everybody involved with CPAN Testers, thanks guys/gals!


Expand Down
36 changes: 35 additions & 1 deletion lib/Math/MatrixReal.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require Exporter;
$VERSION = '2.11';

use overload
'.' => '_concat',
'.' => '_concat',
'neg' => '_negate',
'~' => '_transpose',
'bool' => '_boolean',
Expand Down Expand Up @@ -2813,6 +2813,40 @@ sub _min_column {
}


sub save_to_file {
my ($matrix, $filename) = @_;

open my $fh, ">", $filename or
croak "Math::MatrixReal save_to_file: could not create file '$filename': $!";

# probably faster than creating a full string in memory
my ($rows, $cols) = $matrix->dim();

for my $r (0..$rows-1) {
for my $c (0..$cols-1) {
print $fh $matrix->[0][$r][$c];
print $fh "\t" unless $c == $cols-1;
}
print $fh "\n";
}
close $fh;
}

sub new_from_file {
my ($class, $filename) = @_;
my $m = [];

open my $fh, "<", $filename or
croak "Math::MatrixReal new_from_file: could not open file '$filename': $!";

while (<$fh>) {
chomp;
push @$m, [split /\s+/];
}

return $class->new_from_rows($m);
}


########################################
# #
Expand Down
34 changes: 34 additions & 0 deletions t/save2file.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use Test::More tests => 4;
use File::Spec;
use lib File::Spec->catfile("..","lib");
use Math::MatrixReal;
use File::Temp 'tempfile';

do 'funcs.pl';

my $matrix = Math::MatrixReal->new_from_string(<<"MATRIX");

[ 1 5 9 ]
[ 2 6 10 ]
[ 3 7 11 ]
[ 4 8 12 ]

MATRIX

my ($fh, $filename) = tempfile();

$matrix->save_to_file($filename);

close $fh;

ok -f $filename, "save_to_file: created output file";

my $other = Math::MatrixReal->new_from_file($filename);

isa_ok $other, 'Math::MatrixReal', "new_from_file: created a Math::MatrixReal object";

ok_matrix($matrix, $other, "new_from_file: loaded matrix is the same!");


assert_dies(sub { Math::MatrixReal->new_from_file(File::Temp::mktemp("tmpfileXXXXX")) },
"new_from_file: dies on non-existent file");