|
| 1 | +""" |
| 2 | +Examples of different ways to create matrices |
| 3 | +""" |
| 4 | + |
| 5 | +import pycubool as cb |
| 6 | + |
| 7 | +# |
| 8 | +# Creation an empty matrix of a known form |
| 9 | +# |
| 10 | + |
| 11 | +shape = (3, 3) # Matrix shape |
| 12 | +a = cb.Matrix.empty(shape=shape) # Creating matrix |
| 13 | + |
| 14 | +# |
| 15 | +# Filling values by indices |
| 16 | +# |
| 17 | + |
| 18 | +a[1, 0] = True # Set "True" value to (1, 0) cell |
| 19 | +a[1, 1] = True # Set "True" value to (1, 1) cell |
| 20 | +a[1, 2] = True # Set "True" value to (1, 2) cell |
| 21 | +a[0, 0] = True # Set "True" value to (0, 0) cell |
| 22 | + |
| 23 | +print("First example:") |
| 24 | +print(a, sep='\n') # Matrix output |
| 25 | + |
| 26 | +# |
| 27 | +# Creation an empty matrix of known shape and filling with given arrays of indices |
| 28 | +# |
| 29 | + |
| 30 | +shape = (3, 3) # Matrix shape |
| 31 | +a = cb.Matrix.empty(shape=shape) # Creating matrix |
| 32 | +rows = [0, 1, 1, 1] # Row indices of values |
| 33 | +cols = [0, 0, 1, 2] # Column indices of values |
| 34 | +a.build(rows, cols) # Filling matrix |
| 35 | + |
| 36 | +print("Second example:") |
| 37 | +print(a, sep='\n') # Matrix output |
| 38 | + |
| 39 | +# |
| 40 | +# Creating a matrix via shape and arrays of significant element indices |
| 41 | +# |
| 42 | + |
| 43 | +shape = (3, 3) # Matrix shape |
| 44 | +rows = [0, 1, 1, 1] # Row indices of values |
| 45 | +cols = [0, 0, 1, 2] # Column indices of values |
| 46 | +a = cb.Matrix.from_lists(shape=shape, rows=rows, cols=cols) |
| 47 | + |
| 48 | +print("Third example:") |
| 49 | +print(a, sep='\n') # Matrix output |
| 50 | + |
| 51 | + |
| 52 | +# |
| 53 | +# Generate random matrix by determining the shape and density - the percentage of non-zeros elements |
| 54 | +# |
| 55 | + |
| 56 | +shape = (4, 4) # Matrix shape |
| 57 | +density = 0.5 # Matrix filling density |
| 58 | +a = cb.Matrix.generate(shape=shape, density=density) |
| 59 | + |
| 60 | +print("Fourth example:") |
| 61 | +print(a, sep='\n') # Matrix output |
0 commit comments