Skip to content

Commit

Permalink
make [xyz]{lin,log}vals work with dim-length of one
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Sep 10, 2024
1 parent c7e4f79 commit 871d7e6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Basic/Core/Basic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ etc. see L<zeroes|PDL::Core/zeroes>.
=for ref
X axis values between endpoints (see L</xvals>).
Works with dim-length of one as of 2.093.
=for usage
Expand Down Expand Up @@ -188,6 +189,7 @@ See L</xlinvals> for more information.
=for ref
X axis values logarithmically spaced between endpoints (see L</xvals>).
Works with dim-length of one as of 2.093.
=for usage
Expand Down Expand Up @@ -241,12 +243,12 @@ sub PDL::zvals {
sub _dimcheck {
my ($pdl, $whichdim, $name) = @_;
my $dim = $pdl->getdim($whichdim);
barf "Must have at least two elements in dimension for $name" if $dim <= 1;
barf "Must have at least one element in dimension for $name" if $dim < 1;
$dim;
}
sub _linvals {
my ($pdl, $v1, $v2, $dim, $method) = @_;
$pdl->$method * (($v2 - $v1) / ($dim-1)) + $v1;
$pdl->$method * (($v2 - $v1) / ($dim > 1 ? ($dim-1) : 1)) + $v1;
}
sub PDL::xlinvals {
_linvals(@_[0..2], _dimcheck($_[0], 0, 'xlinvals'), 'xvals');
Expand All @@ -262,7 +264,7 @@ sub _logvals {
my ($pdl, $min, $max, $dim, $method) = @_;
barf "min and max must be positive" if $min <= 0 || $max <= 0;
my ($lmin,$lmax) = map log($_), $min, $max;
exp($pdl->$method * (($lmax - $lmin) / ($dim-1)) + $lmin);
exp($pdl->$method * (($lmax - $lmin) / ($dim > 1 ? ($dim-1) : 1)) + $lmin);
}
sub PDL::xlogvals {
_logvals(@_[0..2], _dimcheck($_[0], 0, 'xlogvals'), 'xvals');
Expand Down
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- [xyz]{lin,log}vals now works with dim-length of one

2.092 2024-09-07
- add Type::howbig
- restore ABI (https://github.com/moocow-the-bovine/PDL-VectorValued/issues/10 https://github.com/PDLPorters/PDL-Stats/issues/33) - thanks @sebastic for report
Expand Down
8 changes: 8 additions & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ ok(tapprox($z->mv(2,0)->uniqvec->flat,pdl(0..25)/5-3),"zlinvals values"); #12
$a1->inplace->xvals;
my $got = $a1->slice('(10),(0),(0)');
ok tapprox($got, 10), 'inplace xvals works' or diag "got:$got";
eval { zeroes(1)->xlinvals(5,10) };
is $@, '', 'can have dim-length of one *linvals';
eval { zeroes(0)->xlinvals(5,10) };
like $@, qr/at least one/, 'cannot have zero-length dim *linvals';
}

{
Expand All @@ -59,6 +63,10 @@ ok(all($x->shape==$zl->shape),"zlogvals shape"); #15
ok(tapprox($xl->uniqvec->flat->log10,pdl(2..12)),"xlogvals values"); #16
ok(tapprox($yl->mv(1,0)->uniqvec->flat->log10,pdl(-3..2)),"ylogvals values"); #17
ok(tapprox($zl->mv(2,0)->uniqvec->flat->log10,pdl(-10..-3)),"zlogvals values");#18
eval { zeroes(1)->xlogvals(5,10) };
is $@, '', 'can have one-element *logvals';
eval { zeroes(0)->xlogvals(5,10) };
like $@, qr/at least one/, 'cannot have zero-element *logvals';
}
#test axisvals
my $z = axisvals(zeroes(3,4,5,6),3);
Expand Down

0 comments on commit 871d7e6

Please sign in to comment.