Advertisement
Guest User

python_sparse

a guest
Nov 15th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import random
  2. # Deepcopy is imported in order not to change the matrix a itself
  3. from copy import deepcopy
  4. def sparse_gen(num_row, num_col, spar):
  5.     # Total number of elements
  6.     nelem = num_row * num_col
  7.     # Number of nonzero elements
  8.     n_nonzero = int(nelem * spar)
  9.     # Position of nonzero elements
  10.     poselem = random.sample(range(nelem), n_nonzero)
  11.     # Create an empty dictionary here
  12.     spardict = {};
  13.     # Add entries into the dictionary here  in a for loop
  14.     for i in range(len(poselem)):
  15.         # Create a string from the number in poselem and use it as a key
  16.         key = str(poselem[i])
  17.         # Select -1 or 1  randomly
  18.         spardict[key]  = random.choice([-1,1])
  19.     # Add the size of the matrix with the key value -1
  20.     spardict['-1'] = [num_row, num_col]
  21.     # Return the sparse matrix for reference later on
  22.     return spardict
  23. # Here is the function for addition of two matrices
  24. def sparse_add(a, b):
  25.     # First lets check if the matrix sizes are equal or not
  26.     if a['-1'] != b['-1'] :
  27.         print('Matrix sizes should be equal. Exiting!')
  28.         return
  29.     # Copy matrix a into matrix c the sum a + b = c
  30.     c = deepcopy(a)
  31.     # Delete the size info from  b and retrieve the keys
  32.     del b['-1']
  33.     bkeys = b.keys()
  34.     # Write the for loop for summation iterating over keys of b
  35.     for bkey in iter(bkeys):
  36.         if (bkey in a):
  37.             if ((c[bkey] + b[bkey]) == 0):
  38.                 del c[bkey]
  39.                 continue
  40.             else:
  41.                 c[bkey] += b[bkey]
  42.         else:
  43.                 c[bkey] = b[bkey]
  44.     # Since it gives rise to problems later readd b['-1']
  45.     b['-1'] = a['-1']
  46.     return c
  47. # The function for printing sparse matrices is defined here
  48. def sparse_print(a):
  49.     # Lets retrieve the size information first (rows, columns)
  50.     # Remember the size information is a list
  51.     nrow = a['-1'][0]
  52.     ncol = a['-1'][1]
  53.     # Lets write a for loop to print out the relevant info
  54.     for i in range(nrow * ncol):
  55.         # if the i is in a print that value otherwise print 0
  56.         return_str = ""
  57.         if str(i) in a:
  58.             return_str += "%3d " %  a[str(i)]
  59.         else:
  60.             return_str += "%3d " % 0
  61.         # Print new line if we hit the end of the row
  62.         if ((i + 1) % nrow) == 0:
  63.             return_str += "\n"
  64.     return_str += "\n\n"# Two new line characters to avoid confusion
  65.     return return_str
  66. # The function for converting sparse matrices to full is defined here
  67. def mat2sparse(a):
  68.     spardict = {}
  69.     num_row = len(a)
  70.     num_col = len(a[0])
  71.     nelem = num_row * num_col
  72.     # Input the size information
  73.     spardict['-1'] = [num_row, num_col]
  74.     for i in range(num_row):
  75.         for j in range(num_col):
  76.             if a[i][j] != 0:
  77.                 # Calculate the position of the key
  78.                 key = str(i*num_col + j)
  79.                 spardict[key] = a[i][j]
  80.     return spardict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement