Pure-Tech

导航

Perl科学计算(1.2)

Perl科学计算(1.2)

  1 #分布函数
  2 my @ex8=(1,9,4,9,10,12,13,15,12,13);
  3 my %hash=&frequency(\@ex8);
  4 print join(",",values %hash);
  5 print join(",",keys %hash);
  6 
  7 sub frequency{
  8     my($vec)=@_;
  9     my %hash;
 10     foreach(@$vec){
 11         ++$hash{$_};
 12     }
 13     return %hash;
 14 }
 15 
 16 #排序函数
 17 my @ex9=(1,9,4,9,10,12,13,15,12,13);
 18 print  join(',',&sortf("numeric","asc",\@ex9));
 19 
 20 sub sortf{
 21     my($type,$direction,$vec)=@_;
 22     my @vec_sort;
 23     if(($type eq "numeric") & ($direction eq "asc")){
 24         @vec_sort=sort{$a<=>$b}@$vec;
 25     }
 26     if(($type eq "numeric") & ($direction eq "desc")){
 27         @vec_sort=sort{$b<=>$a}@$vec;
 28     }
 29     if(($type eq "string") & ($direction eq "asc")){
 30         @vec_sort=sort{$a cmp $b}@$vec;
 31     }
 32     if(($type eq "string") & ($direction eq "desc")){
 33         @vec_sort=sort{$b cmp $a}@$vec;
 34     }
 35     return @vec_sort;
 36 }
 37 
 38 #众数
 39 my @ex10=(1,9,4,9,10,12,13,15,12,13);
 40 print join(',',&mode(\@ex10));
 41 
 42 sub mode{
 43     my($vec)=@_;
 44     my %hash=&frequency($vec);
 45     my @result;
 46     foreach(sort{ $hash{$b}<=>$hash{$a} }  keys %hash){
 47         last if @result && $hash{$_} != $hash{$result[0]};
 48         push(@result,$_);
 49     }
 50             return @result; 
 51 }
 52 
 53 #中位数
 54 my @ex111=(1,9,4,5);
 55 print &median(\@ex11);
 56 my @ex112=(1,9,4,5,5);
 57 
 58 sub median{
 59     my($vec)=@_;
 60     my @vec1=&sortf('numeric','asc',$vec);
 61     my $length=scalar @$vec;
 62     my $result;
 63     if($length % 2){
 64         $result=$vec1[($length-1)/2];
 65     }else{
 66         $result=($vec1[$length/2]+$vec1[$length/2-1])/2;
 67         }
 68     return $result;
 69 }
 70 
 71 #四分之一分位数
 72 
 73  my @ex12=( 7, 15, 36, 39, 40, 41);
 74 print join(',',&quantile(\@ex12));
 75 
 76 sub quantile{
 77     my($vec)=@_;
 78     my @vec1=&sortf('numeric','asc',$vec);
 79     my $length=scalar @$vec+1;
 80     my($upper,$lower);
 81     if($length %4 ){
 82         my $temp1=int($length/4);
 83         my $temp2=int(3*$length/4);
 84         $upper=$vec1[$temp1-1]+($vec1[$temp1]-$vec1[$temp1-1])*($length/4-$temp1);
 85         $lower=$vec1[$temp2-1]+($vec1[$temp2]-$vec1[$temp2-1])*(3*$length/4-$temp2);
 86         }else{
 87             $upper=$vec1[$length/4-1];
 88             $lower=$vec1[3*$length/4-1];
 89         }
 90         my @result=($upper,$lower);
 91         return @result; 
 92     }
 93 
 94 #最大值
 95 my @ex13=( 7, 15, 36, 39, 40, 41);
 96 print &max(\@ex13);
 97 
 98 sub max{
 99     my($vec)=@_;
100     my @result=&sortf('numeric','desc',$vec);
101     return $result[0];
102 }
103 
104 #最小值
105 my @ex14=( 7, 15, 36, 39, 40, 41);
106 print &min(\@ex14);
107 
108 sub min{
109     my($vec)=@_;
110     my @result=&sortf('numeric','asc',$vec);
111     return $result[0];
112 };
113 #标准化,共2种方法
114 my @ex15=( 7, 15, 36, 39, 40, 41);
115 print join(',',&scale(\@ex15,2));
116 
117 sub scale{
118         my($vec,$m)=@_;
119         my @result;
120         if($m eq 1){
121             if(&max($vec) eq &min($vec)){
122                 die "could not scale as range method!";
123             }else{
124                 @result=map(($_-&min($vec))/(&max($vec)-&min($vec)),@$vec);
125                 }
126            return @result;
127            }
128            if($m eq  2){
129                if(&var($vec) eq 0){
130                 die "could not scale as centered method!";
131                }else{    
132                     @result=map(($_-&mean($vec))/&sd($vec),@$vec);           
133                    }
134                return @result;
135               }
136 }
137   

 

posted on 2014-12-03 14:31  Pure-Tech  阅读(611)  评论(0编辑  收藏  举报