Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C code to python
#1
#include<stdio.h>
#include<stdlib.h>

#define K 2
#define N 8
#define G 64
int sigA1=0,sigA2=0,sigB1=0,sigB2=0,tauA=0,tauB=0;
typedef struct Node 
{
        int data;
        struct Node *next;
        struct Node *prev;
}input_node,stateA,stateB,pi,bufferA,bufferB,wtA,wtB;

	input_node *input_start,*input_temp;
	stateA *stateA_start,*stateA_temp;
	stateB *stateB_start,*stateB_temp;
	pi *pi_start,*pi_temp;
	bufferA *bufferA_start,*bufferA_temp;
	bufferB *bufferB_start,*bufferB_temp;
	wtA *wtA_start,*wtA_temp;
	wtB *wtB_start,*wtB_temp;


void initialize_pi(pi *pointer)
{
int i=0;
for(i=0;i<G;i++)
	{
	 pointer->data=i;
        							/* Allocate memory for the new node and put data in it.*/
        pointer->next = (pi *)malloc(sizeof(pi));
        (pointer->next)->prev = pointer;
        pointer = pointer->next;			//one node extra
        //pointer->data = i;
        //pointer->next = NULL; 
	}
}

int init_stateA(stateA *pointer,int seed1)
{
int i,j,data=0;
for(i=0;i<G;i++)
	{  j=rand();
	   if(j%2==0) data=0;
           else data=1;  //printf("%d",state[i]);
	pointer->data = data;
        /* Allocate memory for the new node and put data in it.*/
        pointer->next = (stateA *)malloc(sizeof(stateA));
        (pointer->next)->prev = pointer;
        pointer = pointer->next;
       // pointer->next = NULL; 
	}
return rand();
} 

int init_stateB(stateB *pointer,int seed1)
{
int i,j,data=0;
for(i=0;i<G;i++)
	{  j=rand();
	   if(j%2==0) data=0;
           else data=1;  
	pointer->data = data;
        /* Allocate memory for the new node and put data in it.*/
        pointer->next = (stateB *)malloc(sizeof(stateB));
        (pointer->next)->prev = pointer;
        pointer = pointer->next;
        
        //pointer->next = NULL; 
	}
return rand();
} 

void init_bufferA(bufferA *pointer)
{
int i=0,data=0;
for(i=0;i<G;i++)
	{pointer->data = data;
	 
        /* Allocate memory for the new node and put data in it.*/
        pointer->next = (bufferA *)malloc(sizeof(bufferA));
        (pointer->next)->prev = pointer;
        pointer = pointer->next;
        
        //pointer->next = NULL; 
	}
}

void init_bufferB(bufferB *pointer)
{
int i=0,data=0;
for(i=0;i<G;i++)
	{pointer->data = data;
	
        /* Allocate memory for the new node and put data in it.*/
        pointer->next = (bufferB *)malloc(sizeof(bufferB));
        (pointer->next)->prev = pointer;
        pointer = pointer->next;
        
        //pointer->next = NULL; 
	}
}

int shuffel_pi(pi *pointer1,pi *pointer2,int seed)
{
int i,j,temp,i1=0,i2=0,idata=0,jdata=0;
for(i=G-1;i>0;i--)
	{ pointer1=pi_start;
	  pointer2=pi_start;
	  //while(pointer1 -> prev !=NULL)
	  //{ pointer1 = pointer1 -> prev;
	  //}
	  //while(pointer2 -> prev !=NULL)
	  //{ pointer2 = pointer2 -> prev;
	  //}
	  j=rand() % i;
	  for(i2=0;i2<i;i2++)
	   {  pointer1 = pointer1 -> next;
	   }
	   idata=pointer1 -> data;
	   	    
	  for(i1=0;i1<j;i1++)
	   {  pointer2 = pointer2 -> next;
	   }
	   jdata=pointer2 -> data;
	   	 
	   pointer1 -> data = jdata;
	   pointer2 -> data = idata;
	}
return rand();
}


int init_input(input_node *pointer,int seed)
{
int i,j,rand1=0,data=0;
for(i=0;i<K*N;i++)
	{  rand1=rand();
	   if(rand1%2==0) data=1;
           else data=0;  
	  pointer->data = data; 
        /* Allocate memory for the new node and put data in it.*/
        pointer->next = (stateB *)malloc(sizeof(stateB));
        (pointer->next)->prev = pointer;
        pointer = pointer->next;
        
        //pointer->next = NULL; 
	}
return rand();
} 


void create_wts(wtA *aptr,wtB *bptr)
{
int i=0,data1=0;
for(i=0;i<K*N;i++)
	{aptr->data = data1;
	 bptr->data = data1;
        /* Allocate memory for the new node and put data in it.*/
        aptr->next = (wtA *)malloc(sizeof(wtA));
	bptr->next = (wtB *)malloc(sizeof(wtB));
        (aptr->next)->prev = aptr;
	(bptr->next)->prev = bptr;
        aptr = aptr->next;
        bptr = bptr->next;
        //pointer->next = NULL; 
	}
}


init_weightA(wtA *wptr,stateA *sptr, pi *pptr)
{
int i,j,value;
//wptr=wtA_start;
//sptr=stateA_start;
//pptr=pi_start;
for(i=0;i<K*N;i++)
     {  
	value= pptr -> data;
	for(j=0;j<value;j++)
		{ sptr = sptr -> next;
		}
	wptr -> data = sptr -> data;
	wptr = wptr -> next;
	pptr = pptr -> next;                                                          //flag=pi[i];
        sptr=stateA_start;                                                                              //iitemp[i]=state[pi[i]]; 
     }
  
}


init_weightB(wtB *wptr,stateB *sptr, pi *pptr)
{
int i,j,value;
//wptr=wtA_start;
//sptr=stateA_start;
//pptr=pi_start;
for(i=0;i<K*N;i++)
     {  value= pptr -> data;
	
	
	for(j=0;j<value;j++)
		{ sptr = sptr -> next;
		}
	wptr -> data = sptr -> data;
	wptr = wptr -> next;
	pptr = pptr -> next;                                                          //flag=pi[i];
        sptr=stateB_start;                                                                              //iitemp[i]=state[pi[i]]; 
     }
  
}

void comp_sigmaA(input_node * iptr, wtA *wptr)
{
int i,j,hid[K],flag=0,d1=0,d2=0,d3=0;
for(i=0;i<N;i++)
  {  	d1=0,d2=0;
	d1 = iptr -> data;
	d2 = wptr -> data;
	iptr = iptr -> next;
	wptr = wptr -> next;	
	d3+= d1 ^ d2;
  }
     	if(d3> N/2) sigA1=1;
     	else sigA1=0;
d3=0;
for(i=0;i<N;i++)
  {  	d1=0,d2=0;
	d1 = iptr -> data;
	d2 = wptr -> data;
	iptr = iptr -> next;
	wptr = wptr -> next;	
	d3+= d1 ^ d2;
  }
     	if(d3> N/2) sigA2=1;
     	else sigA2=0;
//printf("\nsigA1=%d,%d,%d,%d\n",sigA1,sigA2,sigB1,sigB2);
}


void comp_sigmaB(input_node * iptr, wtB *wptr)
{
int i,j,d1=0,d2=0,d3=0;
for(i=0;i<N;i++)
  {  	d1=0,d2=0;
	d1 = iptr -> data;
	d2 = wptr -> data;
	iptr = iptr -> next;
	wptr = wptr -> next;	
	d3+= d1 ^ d2;
  }
     	if(d3> N/2) sigB1=1;
     	else sigB1=0;
d3=0;
for(i=0;i<N;i++)
  {  	d1=0,d2=0;
	d1 = iptr -> data;
	d2 = wptr -> data;
	iptr = iptr -> next;
	wptr = wptr -> next;	
	d3+= d1 ^ d2;
  }
     	if(d3> N/2) sigB2=1;
     	else sigB2=0;
//printf("\nsigA1=%d,%d,%d,%d\n",sigA1,sigA2,sigB1,sigB2);
}

void comp_tau()
{
tauA = sigA1 ^ sigA2;
tauB = sigB1 ^ sigB2;
}

void store_buffer(bufferA *ap,bufferB *bp, int inner)
{
int i;
for(i=0;i<=inner;i++)
{ ap = ap->next;
  bp = bp->next;
} 
ap->data = sigA1;
bp->data = sigB1;
}

copy_buffer(stateA *sap,stateB *sbp,bufferA *ap,bufferB *bp)
{
int i;
for(i=0;i<G;i++)
	{
	sap -> data = ap -> data;
	sbp -> data = bp -> data;
	sap = sap -> next;
	sbp = sbp -> next;
	ap = ap -> next;
	bp = bp -> next;
	}
}

void reset_buf(bufferA *ap,bufferB *bp)
{int i,v=0;
for(i=0;i<G;i++)
	{ap -> data = v;
	 bp -> data = v;
	 ap = ap -> next;
	 bp = bp -> next;
	}
}




void print(stateA *pointer)
{
int i=0;       
	/* if(pointer==NULL)
        {
                return;
        }
        printf("%d ",pointer->data);
        print(pointer->next);*/
	for(i=0;i<G;i++)
	{
	printf("%d",pointer->data);
	pointer=pointer->next;
	}
	
}




void main()
{
int i,j,seed1=23532,seed2=76544,counter=0,inner=0,outer=0;
srandom(seed1+time(NULL));
        /* start always points to the first node of the linked list.
           temp is used to point to the last node of the linked list.*/
       // input_node *input_start,*input_temp;
	//sigma *sigma_start,*sigma_temp;
	//stateA *stateA_start,*stateA_temp;
	//stateB *stateB_start,*stateB_temp;
	//pi *pi_start,*pi_temp;
	//bufferA *bufferA_start,*bufferA_temp;
	//bufferB *bufferB_start,*bufferB_temp;
        
	input_start = (input_node *)malloc(sizeof(input_node)); 
        input_temp = input_start;
        input_temp -> next = NULL;
        input_temp -> prev = NULL;
	input_start -> prev = NULL;
	
	stateA_start = (stateA *)malloc(sizeof(stateA)); 
        stateA_temp = stateA_start;
        stateA_temp -> next = NULL;
        stateA_temp -> prev = NULL;
	stateA_start -> prev = NULL;

	pi_start = (pi *)malloc(sizeof(pi)); 
        pi_temp = pi_start;
        pi_temp -> next = NULL;
        pi_temp -> prev = NULL;
	pi_start -> prev = NULL;

	stateB_start = (stateB *)malloc(sizeof(stateB)); 
        stateB_temp = stateB_start;
        stateB_temp -> next = NULL;
        stateB_temp -> prev = NULL;
	stateB_start -> prev = NULL;
	
	bufferA_start = (bufferA *)malloc(sizeof(bufferA)); 
        bufferA_temp = bufferA_start;
        bufferA_temp -> next = NULL;
        bufferA_temp -> prev = NULL;
	bufferA_start -> prev = NULL;

	bufferB_start = (bufferB *)malloc(sizeof(bufferB)); 
        bufferB_temp = bufferB_start;
        bufferB_temp -> next = NULL;
        bufferB_temp -> prev = NULL;
	bufferB_start -> prev = NULL;

	wtA_start = (wtA *)malloc(sizeof(wtA)); 
        wtA_temp = wtA_start;
        wtA_temp -> next = NULL;
        wtA_temp -> prev = NULL;
	wtA_start -> prev = NULL;

	wtB_start = (wtB *)malloc(sizeof(wtB)); 
        wtB_temp = wtB_start;
        wtB_temp -> next = NULL;
        wtB_temp -> prev = NULL;
	wtB_start -> prev = NULL;

	initialize_pi(pi_start);   
	seed2=init_stateA(stateA_start,seed1); 
	seed1=init_stateB(stateB_start,seed2);
//printf("The list is ");  print(pi_start);

	init_bufferA(bufferA_start);
	init_bufferB(bufferB_start);
	create_wts(wtA_start,wtB_start);


	 while(counter<G)
	{printf("%d ",counter);
	seed2=shuffel_pi(pi_start,pi_start,seed2+counter);
	seed1=init_input(input_start,seed2);
	 
	init_weightA(wtA_start,stateA_start,pi_start);  	//print(wtA_start);
	init_weightB(wtB_start,stateB_start,pi_start);      	//print(wtB_start);
	
	comp_sigmaA(input_start,wtA_start);
	comp_sigmaB(input_start,wtB_start);
	
	comp_tau();
	if(tauA==tauB)
		{counter++;
		 store_buffer(bufferA_start,bufferB_start,inner);
		 inner++;
			if(inner==G)
			{copy_buffer(stateA_start,stateB_start,bufferA_start,bufferB_start);
	   	 	reset_buf(bufferA_start,bufferB_start); 
               		outer++; 
                 	inner=0;
			}
		}
	else if(tauA!=tauB) 	{counter=0;}
	}
printf("\nstate vector A=\n");
print(stateA_start);
printf("\nstate vector B=\n");
print(stateB_start);

              		 
}
Reply
#2
What have you tried and what is your question?
Just posting some [400+ lines of] C code is not how it works
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
so basically i am trying to implement Permutation Parity Machine(PPM). I am trying to replicate same in python and see their efficiency
Reply
#4
(Jan-08-2020, 08:17 AM)jhapradeep88 Wrote: I am trying to replicate same in python and see their efficiency
again, show what you have done so far (i.e. post your python code in python tags)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
import random
a= int(raw_input("enter the binary no"))
t='{0:050b}'.format(a)
print (t)
b=len(t)
print (b)

#for i in range(0, 10):
c = random.randint(0, 50)
print c
u='{0:050b}'.format(c)
print u
z=(c^a)
r='{0:050b}'.format(z)
print r
in this i have tried to take one i/p from user, convert to binary amd generating second random binary no. keeping in mind that both have same bits.
After that i have done XOR of the 2 i/p
**pls check-----permutation parity machine section (https://en.wikipedia.org/wiki/Neural_cryptography)

i need help in coding as how the 2 machines get same output after synchronization
Reply
#6
Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:

Python Code

import example
result = example.do_something()
C Code

static PyObject * example(PyObject *self)
{
// do something
return Py_BuildValue("i", result);
}
If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.

You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.

If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?

If you write a *.exe then you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python" or a "Python program with some C".

Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?

Python reads *.py files and compiles to *.pyc bytecode files when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided." (from the Python glossary) So if you haven't edited your *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.
If you are interested and search python institute in Noida then join CETPA.
Reply
#7
(Jan-08-2020, 08:17 AM)jhapradeep88 Wrote: I am trying to replicate same in python and see their efficiency
(Jan-08-2020, 10:22 AM)ananyagupta Wrote: Yes, you can write C code that can be imported into Python as a module.
ananyagupta, I don't think they want C extension modules, as then they can't compare the "efficiency".
Reply
#8
Hi Ananya
I think their efficiency will be same as the core code remains same.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020