一般离散无记忆信道容量的逐步迭代算法—C++实现

参考教材:《信息论(第四版)——基础理论与应用》

教材给的算法描述让我怀疑作者有没有亲自编程尝试过。

注意到  x^(lny) = y.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e3;
const double eps = 1e-6;

double p[N];
double p_next[N];
double a[N];
int r;
int s;
double P[N][N];
double C;
double C_new;

int main()
{
     //freopen("input.txt", "r", stdin);
     cin>>r>>s;
     for(int i=1; i<=r; i++)
     {
          p[i] = 1.0 / r;
     }
     for(int i=1; i<=r; i++)
     {
          for(int j=1; j<=s; j++)
          {
               cin>>P[i][j];
          }
     }
     int n=1;
//     for(int i=1; i<=r; i++)
//     {
//          cout<<p[i]<<' ';
//     }
//     cout<<endl;
//     for(int i=1; i<=r; i++)
//     {
//          for(int j=1; j<=s; j++)
//          {
//               //cout<<P[i][j]<<' ';
//               printf("%.8f\t", P[i][j]);
//          }
//          cout<<endl;
//     }

     while(n++)
     {
          for(int i=1; i<=r; i++)
          {
               double temp = 1;
               for(int j=1; j<=s; j++)
               {
                    double temp2 = 0;
                    for(int k=1; k<=r; k++)
                    {
                         temp2 = temp2 + p[k] *P[k][j];
                    }
                    temp2 = P[i][j] / temp2;
                    temp2 = pow(temp2, P[i][j]);
                    temp = temp * temp2;
               }
               a[i] = temp;
          }
          C = 0;
          C_new = 0;
          for(int i=1; i<=r; i++)
          {
               C = C + p[i] * a[i];
               C_new = max(C_new, a[i]);
          }
          C = log( C );
          C_new = log(C_new);
          if( fabs(C - C_new) >eps )
          {
               double sum = 0;
               for(int i=1; i<=r; i++)
               {
                    sum = sum + a[i] * p[i];
               }
               for(int i=1; i<=r; i++)
               {
                    p_next[i] = p[i] * a[i] / sum;
                    p[i] = p_next[i];
               }
          }
          else
          {
               break;
          }
     }
     cout<<n-1<<endl;
     for(int i=1; i<=r; i++)
     {
          printf("%12.8f",p[i]);
     }
     cout<<endl;
     printf("%.6f\n", C_new);

     return 0;
}





 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值