Update: Thanks to dave_the_m, ikegami and hossman I have found the problems and fixed them in my code. The warnings on $a and $b were not the issue. Using ikegami's suggestion removed those warnings.
The problem was that the abs function returned a scalar and not an object. This is the intended result for that function. After the first pass through reduce $a was a scalar and couldn't call abs().
Greetings! I'm getting some warnings and errors with a module function. I've searched around but haven't found any node or web page with a solution. Hopefully some eyes from a distance can see where I've gone wrong.
I'm using build 518 of ActiveState's Perl on Windows XP. I've included the smallest sample that recreates the problem in the read more section below. The messages I'm receiving are:
Name "Math::Interval::a" used only once: possible typo at C:\SRC\ivpm. +pl line 39. Name "Math::Interval::b" used only once: possible typo at C:\SRC\ivpm. +pl line 39. Can't call method "abs" on an undefined value at C:\SRC\ivpm.pl line 3 +9.
I've used Data::Dumper to examine the value of $row_ref and it is an array of three Math::Interval objects like I expect. Also, I'm able to call abs() for any value in the matrix. (Ex. $x->[1][1]->abs()).
Any idea where I've gone wrong? Also any idea why I'm getting the warnings for $a and $b from using reduce? Any code improvements are welcome. TIA!
#!/usr/bin/perl package Math::Interval; use warnings; use strict; use List::Util qw( max min reduce ); sub _eps_for { my ($num, $epsilon) = (shift) x 2; # copy arg to both vars $epsilon /= 2.0 while $num + $epsilon / 2.0 != $num; return $epsilon; } sub _interval { my ($min, $max) = ( min(@_), max(@_) ); return bless [$min - _eps_for($min), $max + _eps_for($max)], __PAC +KAGE__; } sub lb { my ($x) = @_; return $x->[0]; } sub ub { my ($x) = @_; return $x->[1]; } sub abs { my ($x) = @_; return max( abs($x->lb()), abs($x->ub()) ); } sub matrix_norm { my ($mat_ref) = @_; my @row_sums; foreach my $row_ref (@$mat_ref) { my $sum = reduce { $a->abs() + $b->abs() } @$row_ref; push @row_sums, $sum; } return max @row_sums; } package main; # getting the absolute value of a single interval works my $a = Math::Interval::_interval(0.333333, 0.333334); print 'abs([0.333333; 0.333334] is ', $a->abs(), "\n"; # getting the matrix norm for a matrix of intervals doesn't my $x = [ [ Math::Interval::_interval(3), Math::Interval::_interval(2), Math::Interval::_interval(3) ], [ Math::Interval::_interval(5), Math::Interval::_interval(9), Math::Interval::_interval(8) ], ]; print 'mat norm ', Math::Interval::matrix_norm($x), "\n";
Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.
In reply to Can't call method "abs" on an undefined value. by HollyKing
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |