📄 paritychecknode.c
字号:
/******************************************************** * * Function node for usage on parity check function nodes. * Special implementation of update. * Assumes that all neighbours are of size 2! * *********************************************************/#include "ParityCheckNode.h"#include "VariableNode.h"#include "ParityCheck.h"#include <iostream.h>#include <math.h>ParityCheckNode::ParityCheckNode(const char *name, int id, int numNeighbours) : FunctionNode(name, id, numNeighbours, NULL) {}double ParityCheckNode::LogModMinusQuan(double a) { double tmp; if(a<-3.0) tmp=0.0; else if(a<-0.68) tmp=-0.25*(a+3.0); else if(a<-0.27) tmp=-2*(a+0.97); else if(a<0.0) tmp=-8*(a+0.445); else if(a<0.15) tmp=16*a-4.0; else if(a<0.4) tmp=4*a-2.2; else if(a<1.3) tmp=2*a-1.4; else tmp=a-0.1; return(tmp);}double ParityCheckNode::LogModMinus(double sum, double a) { double tmp=LogModMinusQuan(sum+a)-LogModMinusQuan(a-sum)-sum; return(tmp);}double ParityCheckNode::jacobiFunction(double a) { double tmp; if(a>=0.0) tmp=log(1.0+exp(-a)); else tmp=log(1.0+exp(a)); return(tmp);}double ParityCheckNode::LogModPlus(double a, double b) { double tmp, jia, jian; jia=a+b; tmp=(jia>0.0)?jia:0.0; //tmp+=jacobiFunction(jia); tmp-=(a>b)?a:b; jian=a-b; //tmp-=jacobiFunction(jian); return(tmp);}/*void ParityCheckNode::updateAll() { double sum=(*(VariableNode*)neighbours[0]).getOutMessage(portNumbers[0])[0]; for(int i=1;i< numNeighbours; i++){ sum=LogModPlus(sum, ((*(VariableNode*)neighbours[i]).getOutMessage(portNumbers[i]))[0]); } for(i=0;i< numNeighbours; i++){ outMessages[i][0]=LogModMinus(sum, ((*(VariableNode*)neighbours[i]).getOutMessage(portNumbers[i]))[0]); }}*/void ParityCheckNode::updateAll() { double *forwardMessage=new double[numNeighbours]; double *backwardMessage = new double[numNeighbours]; forwardMessage[0]=(*(VariableNode*)neighbours[0]).getOutMessage(portNumbers[0])[0]; for (int i = 1; i < numNeighbours; i++){ forwardMessage[i]=LogModPlus(forwardMessage[i-1], ((*(VariableNode*)neighbours[i]).getOutMessage(portNumbers[i]))[0]); } //backward in the internal factor graph backwardMessage[numNeighbours-1]=((*(VariableNode*)neighbours[numNeighbours-1]).getOutMessage(portNumbers[numNeighbours-1]))[0]; for (i = numNeighbours-2; i >= 0; i--){ backwardMessage[i]=LogModPlus(backwardMessage[i+1], ((*(VariableNode*)neighbours[i]).getOutMessage(portNumbers[i]))[0]); } //update the messages to the neighbours of this parity check node outMessages[0][0] = backwardMessage[1]; for (i = 1; i < numNeighbours-1; i++){ outMessages[i][0]=LogModPlus(forwardMessage[i-1], backwardMessage[i+1]); } outMessages[numNeighbours-1][0] = forwardMessage[numNeighbours-2]; delete [] backwardMessage; delete [] forwardMessage;} void ParityCheckNode::update(int port){ //the usage of class ParityCheckNode is suitable only for //function nodes where always all ports are updated at once, //not only the message at one single port updateAll();}void ParityCheckNode::reset(void) { for (int n = 0; n < numNeighbours; n++) { int size = (*(VariableNode*)(neighbours[n])).getSize(); for (int i = 0; i < size; i++) { outMessages[n][i] = 0.0; //1.0/size; } }}void ParityCheckNode::draw(void) { printf("NODE: %20s, %6d\n", name, id); printf("---------------------------------------------\n"); printf("PORT NEIGHBOUR OUTMESSAGE\n"); for (int n = 0; n < numNeighbours; n++) { printf("%3d %20s%15f\n", n, (*neighbours[n]).getName(), outMessages[n][0]); int size = (*(VariableNode*)(neighbours[n])).getSize(); for (int i = 1; i < size; i++) { printf("%45f\n", outMessages[n][i]); } } printf("\n");}void ParityCheckNode::draw(FILE *file) { fprintf(file, "NODE: %20s, %6d\n", name, id); fprintf(file, "---------------------------------------------\n"); fprintf(file, "PORT NEIGHBOUR OUTMESSAGE\n"); for (int n = 0; n < numNeighbours; n++) { fprintf(file, "%3d %20s%15f\n", n, (*neighbours[n]).getName(), outMessages[n][0]); int size = (*(VariableNode*)(neighbours[n])).getSize(); for (int i = 1; i < size; i++) { fprintf(file, "%45f\n", outMessages[n][i]); } } fprintf(file, "\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -