BluePink BluePink
XHost
Servere virtuale de la 20 eur / luna. Servere dedicate de la 100 eur / luna - servicii de administrare si monitorizare incluse. Colocare servere si echipamente de la 75 eur / luna. Pentru detalii accesati site-ul BluePink.

Computer Science Life Just another WordPress weblog

5Mar/100

Steganography

Today, reading all kind off things after hours, I've found an interesting subject : Steganography. What this term really means : well, it represents a technique of hiding something by changing it's form or by encapsulated it in something else. This way of covering something is used since long ago, from the times of ancient Greeks ( where they used head tattooed slaves to send messages ).But ,steganography have become more known with the introduction of digital photos. Why, because photos permit hiding information very well without any suspicion. So called Digital steganography can be used where cryptography isn't possible or legal. We can easily send coded messages into photos to our Chinese fellows, without any problem. Sadly , digital steganography has a bad part. I can be ( and it was ) used by terrorist to send key instructions. Anyway, here is a site where you can play with this thing : http://mozaiq.org/

Filed under: Uncategorized No Comments
4Mar/100

First post

Welcome to my blog. In my first post I will talk have a programing topic, more exactly a Divide and conquer problem : Matrix multiplication using this programming technique. We will show the simplest way, using  a particular case of matrices, square matrices with dimension being an even number (in order to keep the thing clear and concise ).

The idea is the follow : split each matrix in 4 square matrices till the dimension of this matrices is 2 . In this case, we will apply the known matrix multiplication.  Our function will have as arguments the input matrices, their dimension , the left upper corner coordinates ( line and column ) and the current dimension.

When the algorithm reaches matrices with dimension equal to 2 , proper elements in the matrix result are computed.

Here is the algorithm, written in C :

  1.  
  2. #include  <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int **d = NULL ;        // global result matrix
  6. /*
  7.  *  reading  function
  8.  * file_name file input name
  9.  * n   dimension of the matrix
  10.  * a, b   operand matrices
  11.  */
  12. void read(char* file_name, int *n, int ***a, int ***b) {
  13.  int i, j;
  14.  FILE *fd = fopen(file_name, "r");
  15.  
  16.  // dimension of the matrix
  17.  fscanf(fd, "%i", n);
  18.  
  19.  // elements of  A
  20.  *a = (int**)malloc((*n) * sizeof(int*));
  21.  for (i = 0; i < *n; i++) {
  22.   (*a)[i] = (int*)malloc((*n) * sizeof(int));
  23.   for (j = 0; j < *n; j++) {
  24.    fscanf(fd, "%i", &((*a)[i][j]));
  25.   }
  26.  }
  27.  
  28.  // elements of B
  29.  *b = (int**)malloc((*n) * sizeof(int*));
  30.  for (i = 0; i < *n; i++) {
  31.   (*b)[i] = (int*)malloc((*n) * sizeof(int));
  32.   for (j = 0; j < *n; j++) {
  33.    fscanf(fd, "%i", &((*b)[i][j]));
  34.   }
  35.  }
  36.  
  37.  fclose(fd);
  38. }
  39.  
  40. /*
  41.  * iterative algorithm for multiplication
  42.  * n  dimension
  43.  * a, b  operands
  44.  * c result  
  45.  */
  46. void matrix_multiply(int n, int **a, int **b, int ***c) {
  47.  int i, j, k;
  48.  
  49.  *c = (int**)malloc((n) * sizeof(int*));
  50.  // inmultire matrice
  51.  for (i = 0; i < n; i++) {
  52.   (*c)[i] = (int*)malloc((n) * sizeof(int));
  53.   for (j = 0; j < n; j++) {
  54.    (*c)[i][j] = 0;
  55.    for (k = 0; k < n; k++) {
  56.     (*c)[i][j] += a[i][k] * b[k][j];
  57.    }
  58.   }
  59.  }
  60. }
  61.  
  62.  
  63. /*
  64.  * compare matrices
  65.  * n dimension
  66.  * a,b matrices
  67.  * return 1/0  different/equal
  68.  */
  69. int matrix_cmp(int n, int **a, int **b) {
  70.  int i, j;
  71.  
  72.  if ( (a == NULL) || (b == NULL) ) {
  73.   return 1;
  74.  }
  75.  
  76.  for (i = 0; i < n; i++) {
  77.   for (j = 0; j < n; j++) {
  78.    if (a[i][j] != b[i][j]) {
  79.     return 1;
  80.    }
  81.   }
  82.  }
  83.  
  84.  return 0;
  85. }
  86.  
  87. /*
  88.  * free memory
  89.  * n dimension
  90.  * a matrix
  91.  */
  92. void free_matrix(int n, int **a) {
  93.  int i;
  94.  for (i = 0; i < n; i++) {
  95.   free(a[i]);
  96.  }
  97.  free(a);
  98. }
  99.  
  100.  
  101. void print_matrix(int n, int **a)
  102. {
  103.  for(int i=0; i<n; i++)
  104.        {
  105.          for (int j=0; j<n; j++)
  106.          {
  107.    printf("%i ",a[i][j]);
  108.   }
  109.  printf("\n");
  110.        }
  111. }
  112.  
  113. void DI_mm(int n, int **a, int **b, int lia, int cia, int lib, int cib, int dim)
  114. {
  115.  int i;
  116.  
  117.  if ( dim == 2 )
  118.  {
  119.   d[lia][cib] += a[lia][cia] * b[lib][cib] + a[lia][cia + 1] * b[lib + 1][cib] ;
  120.   d[lia][cib + 1] += a[lia][cia] * b[lib][cib + 1] + a[lia][cia + 1] * b[lib + 1][cib + 1] ;
  121.   d[lia + 1][cib] += a[lia + 1][cia] * b[lib][cib] + a[lia + 1][cia + 1] * b[lib + 1][cib] ;
  122.   d[lia + 1][cib + 1] += a[lia + 1][cia] * b[lib][cib + 1] + a[lia + 1][cia + 1] * b[lib + 1][cib + 1] ;
  123.  }
  124.  
  125.  else
  126.  {
  127.   dim = dim / 2 ;
  128.   DI_mm(n,a,b,lia,cia,lib,cib,dim); // 1
  129.   DI_mm(n,a,b,lia,cia + dim,lib + dim,cib,dim); // 2
  130.   DI_mm(n,a,b,lia,cia,lib,cib + dim,dim); // 3
  131.   DI_mm(n,a,b,lia,cia + dim,lib + dim,cib + dim,dim);  // 4
  132.   DI_mm(n,a,b,lia + dim,cia,lib,cib,dim);  // 5
  133.   DI_mm(n,a,b,lia + dim,cia + dim,lib + dim,cib,dim); // 6
  134.   DI_mm(n,a,b,lia + dim,cia,lib,cib + dim,dim); // 7
  135.   DI_mm(n,a,b,lia + dim,cia + dim,lib + dim,cib + dim,dim);  // 8
  136.  }
  137.  
  138.  
  139. }
  140.  
  141. int main(int argc, char** argv) {
  142.  
  143.  int n; // dimensiune matrice
  144.  int **a, **b, **c; // matrices
  145.  char *in_file; // in file
  146.  
  147.  if (argc < 2) {
  148.   fprintf(stderr,"Usage %s in_file\n", argv[0]);
  149.   exit(1);
  150.  }
  151.  
  152.  in_file = argv[1];
  153.  
  154.  // read matrices and their dimension
  155.  read(in_file, &n, &a, &b);
  156.  
  157.  // iterative matrix multiply (c = a * b)
  158.  matrix_multiply(n, a, b, &c);
  159.  
  160.  // matrix multiplication using DivideAndConquer (d = a * b)
  161.  d = (int**)malloc((n) * sizeof(int*));
  162.  
  163.  for (int i = 0; i < n; i++)
  164.  {
  165.  d[i] = (int*)malloc((n) * sizeof(int));
  166.  }
  167.  
  168.  for (int i = 0; i < n ;i++)
  169.     for(int j = 0; j < n ;j++)
  170.      d[i][j]  = 0;
  171.  
  172.  printf("a = \n");
  173.  print_matrix(n,a);
  174.  printf("\nb = \n");
  175.   print_matrix(n,b);
  176.  printf("\nc = \n");
  177.  print_matrix(n,c);
  178.  
  179.  DI_mm(n,a,b,0,0,0,0,n);
  180.  printf("\nd = \n");
  181.  print_matrix(n,d);
  182.  
  183.  // verificare rezultat
  184.  if (matrix_cmp(n, c, d)) {
  185.   printf("Wrong answer\n");
  186.  }
  187.  else {
  188.   printf("Corect\n");
  189.  }
  190.  
  191.  // free memory
  192.  free_matrix(n, a);
  193.  free_matrix(n, b);
  194.  free_matrix(n, c);
  195.  // TODO free_matrix(n, d);
  196.  
  197.  return 0;
  198. }