Skip to content

Commit 373b8fb

Browse files
authored
Merge pull request #6 from Krekep/master
Python examples
2 parents 1b50e4f + e59f8e0 commit 373b8fb

16 files changed

+1057
-0
lines changed

docs/examples/python_examples.md

Lines changed: 630 additions & 0 deletions
Large diffs are not rendered by default.

python/examples/add_example.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Example of element-wise addition of two matrices
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
shape = (3, 3) # Matrix shape
12+
a = cb.Matrix.empty(shape=shape)
13+
a[0, 0] = True
14+
a[2, 0] = True
15+
16+
shape = (3, 3) # Matrix shape
17+
b = cb.Matrix.empty(shape=shape)
18+
b[1, 1] = True
19+
b[1, 2] = True
20+
21+
#
22+
# Matrix element-wise addition
23+
#
24+
25+
result = a.ewiseadd(b) # result = a + b
26+
27+
print("Matrix element-wise addition:")
28+
print(result, sep='\n') # Matrix output

python/examples/create_example.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

python/examples/data/input_matrix.mtx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# pycubool sparse boolean matrix
2+
3 3 4
3+
0 0
4+
0 1
5+
2 1
6+
2 2
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# pycubool sparse boolean matrix
2+
3 3 4
3+
0 0
4+
1 0
5+
1 1
6+
1 2

python/examples/dup_example.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Example of matrix duplication
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
shape = (3, 3) # Matrix shape
12+
a = cb.Matrix.empty(shape=shape)
13+
a[1, 0] = True
14+
a[1, 1] = True
15+
a[1, 2] = True
16+
a[0, 1] = True
17+
18+
#
19+
# Matrix duplicate operation
20+
#
21+
22+
result = a.dup()
23+
24+
print("Result of matrix duplication operation:")
25+
print(result, sep='\n') # Matrix output

python/examples/export_example.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
An example of writing a matrix to a file in .mtx format
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
a = cb.Matrix.empty(shape=(3, 3)) # Creating an empty matrix of a given shape
12+
a[1, 0] = True
13+
a[1, 1] = True
14+
a[1, 2] = True
15+
a[0, 0] = True
16+
17+
#
18+
# Export matrix to file
19+
#
20+
21+
path = "data/output_matrix.mtx" # relative path to target matrix
22+
cb.export_matrix_to_mtx(path, a) # write matrix to file
23+
24+
#
25+
# Import this matrix to check the correctness of the writing
26+
#
27+
28+
result = cb.import_matrix_from_mtx(path) # read matrix from file
29+
30+
print("Result matrix:")
31+
print(result, sep='\n') # Matrix output

python/examples/import_example.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
An example of reading a matrix from a file in .mtx format
3+
"""
4+
5+
import pycubool as cb
6+
7+
8+
#
9+
# Import matrix from file
10+
#
11+
12+
path = "data/input_matrix.mtx" # relative path to target matrix
13+
a = cb.import_matrix_from_mtx(path) # read matrix from file
14+
15+
print("Result of import matrix from file:")
16+
print(a, sep='\n') # Matrix output

python/examples/iteration_example.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Example of iterating over matrix cells
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
shape = (3, 3) # Matrix shape
12+
a = cb.Matrix.empty(shape=shape)
13+
a[1, 0] = True
14+
a[1, 1] = True
15+
a[1, 2] = True
16+
a[0, 1] = True
17+
18+
19+
#
20+
# Iterating over matrix elements
21+
#
22+
23+
print("Filled cell indices (row, column):")
24+
for i, j in a:
25+
print(f"({i}, {j})", end=" ")

python/examples/kronecker_example.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Example of kronecker product of two matrices
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
a = cb.Matrix.empty(shape=(2, 2)) # Creating an empty matrix of a given shape
12+
a[0, 0] = True
13+
a[1, 0] = True
14+
15+
b = cb.Matrix.empty(shape=(2, 2)) # Creating an empty matrix of a given shape
16+
b[1, 1] = True
17+
b[1, 0] = True
18+
19+
#
20+
# Matrix kronecker product
21+
#
22+
23+
result = a.kronecker(b) # result = a x b
24+
25+
print("Matrix kronecker product:")
26+
print(result, sep='\n') # Matrix output

python/examples/log_example.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Example of start the logger
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Example of starting the logger
9+
#
10+
11+
path = "my-example-log.textlog" # Set file name in current directory to logged messages
12+
cb.setup_logger(path)
13+
14+
#
15+
# Matrix initialization
16+
#
17+
18+
a = cb.Matrix.empty(shape=(3, 3)) # Creating an empty matrix of a given shape
19+
a[1, 0] = True
20+
a[1, 1] = True
21+
a[1, 2] = True
22+
a[0, 0] = True
23+
24+
a.set_marker("a-log-example-matrix") # Set debug marker for "a" matrix
25+
26+
b = cb.Matrix.empty(shape=(3, 3)) # Creating an empty matrix of a given shape
27+
b[0, 1] = True
28+
b[0, 2] = True
29+
30+
#
31+
# Simple matrix multiplication
32+
#
33+
34+
result = a.mxm(b) # result = a * b
35+
36+
print("Simple matrix multiplication:")
37+
print(result, sep='\n') # Matrix output
38+
39+
#
40+
# Matrix multiplication with accumulate
41+
#
42+
43+
result = a.mxm(b, out=a, accumulate=True) # result = a + a * b
44+
45+
print("Matrix multiplication with accumulation:")
46+
print(result, sep='\n') # Matrix output
47+

python/examples/mxm_example.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Examples of matrix multiplication of two matrices
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
a = cb.Matrix.empty(shape=(3, 3)) # Creating an empty matrix of a given shape
12+
a[1, 0] = True
13+
a[1, 1] = True
14+
a[1, 2] = True
15+
a[0, 0] = True
16+
17+
b = cb.Matrix.empty(shape=(3, 3)) # Creating an empty matrix of a given shape
18+
b[0, 1] = True
19+
b[0, 2] = True
20+
21+
#
22+
# Simple matrix multiplication
23+
#
24+
25+
result = a.mxm(b) # result = a * b
26+
27+
print("Simple matrix multiplication:")
28+
print(result, sep='\n') # Matrix output
29+
30+
#
31+
# Matrix multiplication with accumulate
32+
#
33+
34+
result = a.mxm(b, out=a, accumulate=True) # result = a + a * b
35+
36+
print("Matrix multiplication with accumulation:")
37+
print(result, sep='\n') # Matrix output

python/examples/reduce_example.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
An example of matrix reduction
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
a = cb.Matrix.empty(shape=(3, 3)) # Creating an empty matrix of a given shape
12+
a[1, 0] = True
13+
a[1, 1] = True
14+
a[1, 2] = True
15+
a[0, 1] = True
16+
17+
#
18+
# Matrix reduce operation
19+
#
20+
21+
result = a.reduce()
22+
23+
print("Result of matrix reduce operation:")
24+
print(result, sep='\n') # Matrix output

python/examples/submatrix_example.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Examples of extracting sub-matrices
3+
"""
4+
5+
import pycubool as cb
6+
7+
#
8+
# Matrix initialization
9+
#
10+
11+
shape = (3, 3) # Matrix shape
12+
a = cb.Matrix.empty(shape=shape)
13+
a[1, 0] = True
14+
a[1, 1] = True
15+
a[1, 2] = True
16+
a[0, 1] = True
17+
18+
#
19+
# Cut of a 2x2 (third param) matrix
20+
# below and to the right of the specified index (first and second params)
21+
# of the original matrix
22+
#
23+
24+
result = a.extract_matrix(1, 1, (2, 2))
25+
26+
print("First result of extract sub-matrix operation:")
27+
print(result, sep='\n') # Matrix output
28+
29+
30+
#
31+
# Create duplicate of original matrix by extract a matrix with 3x3 shape
32+
#
33+
34+
result = a.extract_matrix(0, 0, (3, 3))
35+
36+
print("Second result of extract sub-matrix operation:")
37+
print(result, sep='\n') # Matrix output

0 commit comments

Comments
 (0)