--- loncom/homework/default_homework.lcpm 2001/05/31 22:36:18 1.25 +++ loncom/homework/default_homework.lcpm 2001/06/13 20:00:07 1.27 @@ -135,16 +135,16 @@ sub web { return @_[1]; } else { if ( $external::target eq "web") { - return @_[0]; - } else { return @_[2]; + } else { + return @_[0]; } } } sub html { if ( $external::target eq "web" ) { - return @_[0]; + return shift; } } @@ -164,6 +164,140 @@ sub random { return $result; } +sub random_normal { + my ($item_cnt,$seed,$av,$std_dev) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_normal($item_cnt,$av,$std_dev); + return @retArray; +} + +sub random_beta { + my ($item_cnt,$seed,$aa,$bb) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_beta($item_cnt,$aa,$bb); + return @retArray; +} + +sub random_gamma { + my ($item_cnt,$seed,$a,$r) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_gamma($item_cnt,$a,$r); + return @retArray; +} + +sub random_exponential { + my ($item_cnt,$seed,$av) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_exponential($item_cnt,$av); + return @retArray; +} + +sub random_poisson { + my ($item_cnt,$seed,$mu) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_poisson($item_cnt,$mu); + return @retArray; +} + +sub random_chi { + my ($item_cnt,$seed,$df) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_chi_square($item_cnt,$df); + return @retArray; +} + +sub random_noncentral_chi { + my ($item_cnt,$seed,$df,$nonc) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc); + return @retArray; +} + +sub random_f { + my ($item_cnt,$seed,$dfn,$dfd) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_f($item_cnt,$dfn,$dfd); + return @retArray; +} + +sub random_noncentral_f { + my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc); + return @retArray; +} + +sub random_multivariate_normal { + my ($item_cnt,$seed,@mean) = @_; + return "Number of deviates must be greater than 0" if $item_cnt <= 0; + my (@covar,@retArray); + my $ind = 0; + while ($ind<$item_cnt) { + push @covar, pop (@mean); + $ind++; + } + &random_set_seed_from_phrase($seed); + @retArray=&math_random_multivariate_normal($item_cnt,@mean,@covar); + return @retArray; +} + +sub random_multinomial { + my ($item_cnt,$seed,@p) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_multinomial($item_cnt,@p); + return @retArray; +} + +sub random_permutation { + my ($seed,@inArray) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_permutation(@inArray); + return @retArray; +} + +sub random_uniform { + my ($item_cnt,$seed,$low,$high) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_uniform($item_cnt,$low,$high); + return @retArray; +} + +sub random_uniform_integer { + my ($item_cnt,$seed,$low,$high) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_uniform_integer($item_cnt,$low,$high); + return @retArray; +} + +sub random_binomial { + my ($item_cnt,$seed,$nt,$p) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_binomial($item_cnt,$nt,$p); + return @retArray; +} + +sub random_negative_binomial { + my ($item_cnt,$seed,$ne,$p) = @_; + my @retArray; + &random_set_seed_from_phrase($seed); + @retArray=&math_random_negative_binomial($item_cnt,$ne,$p); + return @retArray; +} + sub abs { abs(shift) } sub sin { sin(shift) } sub cos { cos(shift) } @@ -213,8 +347,9 @@ sub roundto { sub to_string { my ($input,$n) = @_; - return sprintf('%'.$n,$input) if $n ne ""; return sprintf($input) if $n eq ""; + $n = '.'.$n if $n !~ /^\./; + return sprintf('%'.$n,$input) if $n ne ""; } sub sub_string { @@ -223,8 +358,9 @@ sub sub_string { } sub pow {return $_[0] ** $_[1]; } -sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); } -sub floor {return int($_[0]); } +sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (int($_[0])+ 1) : int($_[0])); } +sub floor {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? int($_[0]) : (int($_[0])-1)); } +#sub floor {return int($_[0]); } sub format { my ($value,$fmt)=@_; @@ -232,21 +368,57 @@ sub format { } sub map { - my $num = $#_/2; - my $i; - my @used; - my @permutation; - for ($i=0; $i < $num;) { - $rand = &random(1,$num,1); - if ( $used[$rand] == '0' ) { - $i++; - $used[$rand]='1'; - $permutation[$i]=$rand; + 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]]; + $ctr++; + } + } else { + while ($ctr < $tot) { + $$dest[$ctr] = $$source[$idx[$ctr]]; + $ctr++; + } + } +} + +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]]; + $ctr++; + } + } else { + while ($ctr < $tot) { + $$dest[$ctr] = $$source[$r_idx[$ctr]]; + $ctr++; + } } - } - for ( $i=1 ; $i<$num+1 ; $i++ ) { - ${$_[$permutation[$i]]}=$_[$i+$num] - } } sub capa_id { return } @@ -256,7 +428,7 @@ sub problem { return } sub name{ my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename'); $fullname = "" if $fullname eq ", "; - $fullname =~ s/\%2d/-/; + $fullname =~ s/\%2d/-/g; return $fullname; }