From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

#!perl
use strict;
use Test::More tests => 36;
################################################################
note('cumprod() on a 4-by-5 matrix');
{
my $x = Math::Matrix -> new([[ 3, -1, 5, 2, 8 ],
[ 4, 0, 2, -3, 1 ],
[ 2, 6, -5, 1, -2 ],
[ 0, -3, 4, 2, 3 ]]);
my $y = $x -> cumprod();
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3, -1, 5, 2, 8 ],
[ 12, 0, 10, -6, 8 ],
[ 24, 0, -50, -6, -16 ],
[ 0, 0, -200, -12, -48 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3, -1, 5, 2, 8 ],
[ 4, 0, 2, -3, 1 ],
[ 2, 6, -5, 1, -2 ],
[ 0, -3, 4, 2, 3 ]], '$x is unmodified');
}
note('cumprod(1) on a 4-by-5 matrix');
{
my $x = Math::Matrix -> new([[ 3, -1, 5, 2, 8 ],
[ 4, 0, 2, -3, 1 ],
[ 2, 6, -5, 1, -2 ],
[ 0, -3, 4, 2, 3 ]]);
my $y = $x -> cumprod(1);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3, -1, 5, 2, 8 ],
[ 12, 0, 10, -6, 8 ],
[ 24, 0, -50, -6, -16 ],
[ 0, 0, -200, -12, -48 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3, -1, 5, 2, 8 ],
[ 4, 0, 2, -3, 1 ],
[ 2, 6, -5, 1, -2 ],
[ 0, -3, 4, 2, 3 ]], '$x is unmodified');
}
note('cumprod(2) on a 4-by-5 matrix');
{
my $x = Math::Matrix -> new([[ 3, -1, 5, 2, 8 ],
[ 4, 0, 2, -3, 1 ],
[ 2, 6, -5, 1, -2 ],
[ 0, -3, 4, 2, 3 ]]);
my $y = $x -> cumprod(2);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3, -3, -15, -30, -240 ],
[ 4, 0, 0, 0, 0 ],
[ 2, 12, -60, -60, 120 ],
[ 0, 0, 0, 0, 0 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3, -1, 5, 2, 8 ],
[ 4, 0, 2, -3, 1 ],
[ 2, 6, -5, 1, -2 ],
[ 0, -3, 4, 2, 3 ]], '$x is unmodified');
}
################################################################
note('cumprod() on a 4-by-1 matrix');
{
my $x = Math::Matrix -> new([[ 3 ],
[ 4 ],
[ 2 ],
[ 0 ]]);
my $y = $x -> cumprod();
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3 ],
[ 12 ],
[ 24 ],
[ 0 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3 ],
[ 4 ],
[ 2 ],
[ 0 ]], '$x is unmodified');
}
note('cumprod(1) on a 4-by-1 matrix');
{
my $x = Math::Matrix -> new([[ 3 ],
[ 4 ],
[ 2 ],
[ 0 ]]);
my $y = $x -> cumprod(1);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3 ],
[ 12 ],
[ 24 ],
[ 0 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3 ],
[ 4 ],
[ 2 ],
[ 0 ]], '$x is unmodified');
}
note('cumprod(2) on a 4-by-1 matrix');
{
my $x = Math::Matrix -> new([[ 3 ],
[ 4 ],
[ 2 ],
[ 0 ]]);
my $y = $x -> cumprod(2);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3 ],
[ 4 ],
[ 2 ],
[ 0 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3 ],
[ 4 ],
[ 2 ],
[ 0 ]], '$x is unmodified');
}
################################################################
note('cumprod() on a 1-by-5 matrix');
{
my $x = Math::Matrix -> new([[ 3, -1, 5, 2, 8 ]]);
my $y = $x -> cumprod();
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3, -3, -15, -30, -240 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3, -1, 5, 2, 8 ]], '$x is unmodified');
}
note('cumprod(1) on a 1-by-5 matrix');
{
my $x = Math::Matrix -> new([[ 3, -1, 5, 2, 8 ]]);
my $y = $x -> cumprod(1);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3, -1, 5, 2, 8 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3, -1, 5, 2, 8 ]], '$x is unmodified');
}
note('cumprod(2) on a 1-by-5 matrix');
{
my $x = Math::Matrix -> new([[ 3, -1, 5, 2, 8 ]]);
my $y = $x -> cumprod(2);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[ 3, -3, -15, -30, -240 ]], '$y has the right values');
# Verify that modifying $y does not modify $x.
my ($nrowy, $ncoly) = $y -> size();
for my $i (0 .. $nrowy - 1) {
for my $j (0 .. $ncoly - 1) {
$y -> [$i][$j] += 100;
}
}
is_deeply([ @$x ], [[ 3, -1, 5, 2, 8 ]], '$x is unmodified');
}
################################################################
note('cumprod() on an empty matrix');
{
my $x = Math::Matrix -> new([]);
my $y = $x -> cumprod();
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [], '$y has the right values');
is_deeply([ @$x ], [], '$x is unmodified');
}
note('cumprod(1) on an empty matrix');
{
my $x = Math::Matrix -> new([]);
my $y = $x -> cumprod(1);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [], '$y has the right values');
is_deeply([ @$x ], [], '$x is unmodified');
}
note('cumprod(2) on an empty matrix');
{
my $x = Math::Matrix -> new([]);
my $y = $x -> cumprod(2);
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [], '$y has the right values');
is_deeply([ @$x ], [], '$x is unmodified');
}