--- loncom/homework/default_homework.lcpm 2001/06/13 20:00:07 1.27 +++ loncom/homework/default_homework.lcpm 2001/06/14 21:29:14 1.28 @@ -371,23 +371,33 @@ sub map { my ($phrase,$dest,$source)=@_; my @seed = &random_seed_from_phrase($phrase); &random_set_seed(@seed); - my $num = scalar(@$source); my $destct = scalar(@$dest); -# print "Warning - Number of source elements and destination elements does not match.\n" if $destct != $num; - my @idx = &math_random_permuted_index($num); - my $ctr = 0; - my $tot = $num; - $tot = $destct if $destct < $num; - if (ref($$dest[0])) { - while ($ctr < $tot) { - ${$$dest[$ctr]} = $$source[$idx[$ctr]]; + if (!$source) { + my @output; + my @idx = &math_random_permuted_index($destct); + my $ctr = 0; + while ($ctr < $destct) { + $output[$ctr] = $$dest[$idx[$ctr]]; $ctr++; - } + } + return @output; } else { - while ($ctr < $tot) { - $$dest[$ctr] = $$source[$idx[$ctr]]; - $ctr++; - } + my $num = scalar(@$source); + my @idx = &math_random_permuted_index($num); + my $ctr = 0; + my $tot = $num; + $tot = $destct if $destct < $num; + if (ref($$dest[0])) { + while ($ctr < $tot) { + ${$$dest[$ctr]} = $$source[$idx[$ctr]]; + $ctr++; + } + } else { + while ($ctr < $tot) { + $$dest[$ctr] = $$source[$idx[$ctr]]; + $ctr++; + } + } } } @@ -395,29 +405,45 @@ sub rmap { my ($phrase,$dest,$source)=@_; my @seed = &random_seed_from_phrase($phrase); &random_set_seed(@seed); - my $num = scalar(@$source); my $destct = scalar(@$dest); -# print "Warning - Number of source elements and destination elements does not match.\n" if $destct != $num; - my @idx = &math_random_permuted_index($num); - my $ctr = 0; - my $tot = $num; - $tot = $destct if $destct < $num; - my @r_idx; - while ($ctr < $tot) { - $r_idx[$idx[$ctr]] = $ctr; - $ctr++; - } - $ctr = 0; - if (ref($$dest[0])) { - while ($ctr < $tot) { - ${$$dest[$ctr]} = $$source[$r_idx[$ctr]]; + if (!$source) { + my @idx = &math_random_permuted_index($destct); + my $ctr = 0; + my @r_idx; + while ($ctr < $destct) { + $r_idx[$idx[$ctr]] = $ctr; + $ctr++; + } + my @output; + $ctr = 0; + while ($ctr < $destct) { + $output[$ctr] = $$dest[$r_idx[$ctr]]; $ctr++; - } + } + return @output; } else { + my $num = scalar(@$source); + my @idx = &math_random_permuted_index($num); + my $ctr = 0; + my $tot = $num; + $tot = $destct if $destct < $num; + my @r_idx; while ($ctr < $tot) { - $$dest[$ctr] = $$source[$r_idx[$ctr]]; + $r_idx[$idx[$ctr]] = $ctr; $ctr++; - } + } + $ctr = 0; + if (ref($$dest[0])) { + while ($ctr < $tot) { + ${$$dest[$ctr]} = $$source[$r_idx[$ctr]]; + $ctr++; + } + } else { + while ($ctr < $tot) { + $$dest[$ctr] = $$source[$r_idx[$ctr]]; + $ctr++; + } + } } } @@ -495,7 +521,7 @@ sub array_moments { $output[0]=$N; if ($N <= 1) { $output[1]=$input[0]; - $output[1] = "Input array not defined" if ($N == 0); + $output[1]="Input array not defined" if ($N == 0); $output[2]="variance undefined for N<=1"; $output[3]="skewness undefined for N<=1"; $output[4]="kurtosis undefined for N<=1";