Skip to content

Commit

Permalink
gh-218: add pretty printing for mat & vec
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Aug 30, 2023
1 parent 5d747e2 commit deed61f
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 23 deletions.
39 changes: 16 additions & 23 deletions python/example.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import pyspla
from pyspla import *

a = pyspla.Array.from_list(dtype=pyspla.INT, values=[-1, 0, 1])
a = Array.from_list(dtype=INT, values=[-1, 0, 1])
print(a)

b = pyspla.Array.generate(dtype=pyspla.INT, shape=10, dist=(-10, 10))
b = Array.generate(dtype=INT, shape=10, dist=(-10, 10))
print(b)

s = pyspla.Scalar(value=10, dtype=pyspla.INT)
s = Scalar(value=10, dtype=INT)
print(s)

v = pyspla.Vector.from_lists([10, 20, 30], [-10, 100, 20], shape=100, dtype=pyspla.FLOAT)
print(v.to_list())

u = pyspla.Vector.generate(shape=100, dtype=pyspla.FLOAT, density=0.3)
print(u.to_list())
m = Vector.from_lists([0, 2, 5], [-1, 1, 1], shape=10, dtype=INT)
t = Vector(shape=10, dtype=INT)
t.assign(m, Scalar(INT, 10), INT.SECOND, INT.GEZERO)
print(t.to_list())

r = v.eadd(pyspla.FLOAT.MULT, u)
print(r.to_list())
M = Matrix.generate((4, 4), INT, density=0.3, dist=[0, 10])
print(M)

m = pyspla.Vector.from_lists([0, 2, 5], [-1, 1, 1], shape=10, dtype=pyspla.INT)
t = pyspla.Vector(shape=10, dtype=pyspla.INT)
t.assign(m, pyspla.Scalar(pyspla.INT, 10), pyspla.INT.SECOND, pyspla.INT.GEZERO)
print(t.to_list())
M = Matrix.from_lists([1, 2, 3], [1, 2, 3], [-1, 5, 10], (4, 4), INT)
print(M)

G = pyspla.Matrix.generate((10, 10), pyspla.INT, density=0.1, dist=[0, 10])
print(G.to_lists())
print(G.reduce(pyspla.INT.PLUS))
v = Vector.generate(shape=4, dtype=INT, density=0.5, dist=[1, 10])
print(v)

M = pyspla.Matrix.from_lists([1, 2, 3], [1, 2, 3], [-1, 5, 10], (10, 10), pyspla.INT)
M.set_format(pyspla.FormatMatrix.ACC_CSR)
print(M.get(1, 0))
print(M.get(1, 1))
print(M.to_list())
v = Vector.from_lists([0, 1, 3], [-1, 7, 5], 4, INT)
print(v)
73 changes: 73 additions & 0 deletions python/pyspla/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,71 @@ def to_list(self):
I, J, V = self.to_lists()
return list(zip(I, J, V))

def to_string(self, format_string="{:>%s}", width=2, precision=2, skip_value=0, cell_sep=""):
"""
Generate from a vector a pretty string for a display.
>>> M = Matrix.from_lists([1, 2, 3], [1, 2, 3], [-1, 5, 10], (4, 4), INT)
>>> print(M)
'
0 1 2 3
0| . . . .| 0
1| .-1 . .| 1
2| . . 5 .| 2
3| . . .10| 3
0 1 2 3
'
:param format_string: str.
How to format single value.
:param width: int.
Integral part length.
:param precision: int.
Fractional part length.
:param skip_value: any.
Value to skip and not display
:param cell_sep: str.
How to separate values in a row.
:return: Pretty string with vector content.
"""

format_string = format_string % width
header = format_string.format("") + " " + "".join(format_string.format(i) for i in range(self.n_cols))

result = header + "\n"
for row in range(self.n_rows):
result += format_string.format(row) + "|"
for col in range(self.n_cols):
value = self.get(row, col)
value = value if value != skip_value else "."
result += cell_sep + self.dtype.format_value(value, width, precision)
result += "| " + str(row) + "\n"
result += header

return result

@classmethod
def from_lists(cls, I: list, J: list, V: list, shape, dtype=INT):
"""
Build matrix from a list of sorted keys and associated values to store in matrix.
List with keys `I` and `J` must index entries from range [0, shape-1] and all keys must be sorted.
>>> M = Matrix.from_lists([1, 2, 3], [1, 2, 3], [-1, 5, 10], (4, 4), INT)
>>> print(M)
'
0 1 2 3
0| . . . .| 0
1| .-1 . .| 1
2| . . 5 .| 2
3| . . .10| 3
0 1 2 3
'
:param I: list[UINT].
List with integral keys of entries.
Expand Down Expand Up @@ -315,6 +374,17 @@ def generate(cls, shape, dtype=INT, density=0.1, seed=None, dist=(0, 1)):
Creates new matrix of desired type and shape and fills its content
with random values, generated using specified distribution.
>>> M = Matrix.generate((4, 4), INT, density=0.3, dist=[0, 10])
>>> print(M)
'
0 1 2 3
0| . 4 . 5| 0
1| . 7 . .| 1
2| . . . .| 2
3| . . . 2| 3
0 1 2 3
'
:param shape: tuple.
Size of the matrix (number of values).
Expand Down Expand Up @@ -386,6 +456,9 @@ def reduce(self, op_reduce, out=None, init=None, desc=None):

return out

def __str__(self):
return self.to_string()

def __iter__(self):
I, J, V = self.to_lists()
return zip(I, J, V)
Expand Down
62 changes: 62 additions & 0 deletions python/pyspla/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,62 @@ def to_list(self):
I, V = self.to_lists()
return list(zip(I, V))

def to_string(self, format_string="{:>%s}", width=2, precision=2, skip_value=0):
"""
Generate from a vector a pretty string for a display.
>>> v = Vector.from_lists([0, 1, 3], [-1, 7, 5], 4, INT)
>>> print(v)
'
0|-1
1| 7
2| .
3| 5
'
:param format_string: str.
How to format single value.
:param width: int.
Integral part length.
:param precision: int.
Fractional part length.
:param skip_value: any.
Value to skip and not display
:return: Pretty string with vector content.
"""

format_string = format_string % width
result = ""

for row in range(self.n_rows):
value = self.get(row)
value = value if value != skip_value else "."
result += format_string.format(row) + "|"
result += format_string.format(self.dtype.format_value(value, width, precision)).rstrip()
if row < self.n_rows - 1:
result += "\n"

return result

@classmethod
def from_lists(cls, I: list, V: list, shape, dtype=INT):
"""
Build vector from a list of sorted keys and associated values to store in vector.
List with keys `keys` must index entries from range [0, shape-1] and all keys must be sorted.
>>> v = Vector.from_lists([0, 1, 3], [-1, 7, 5], 4, INT)
>>> print(v)
'
0|-1
1| 7
2| .
3| 5
'
:param I: list[UINT].
List with integral keys of entries.
Expand Down Expand Up @@ -277,6 +327,15 @@ def generate(cls, shape, dtype=INT, density=0.1, seed=None, dist=(0, 1)):
Creates new vector of desired type and shape and fills its content
with random values, generated using specified distribution.
>>> v = Vector.generate(shape=4, dtype=INT, density=0.5, dist=[1,10])
>>> print(v)
'
0| .
1| .
2| 5
3| .
'
:param shape: int.
Size of the vector.
Expand Down Expand Up @@ -411,6 +470,9 @@ def reduce(self, op_reduce, out=None, init=None, desc=None):

return out

def __str__(self):
return self.to_string()

def __iter__(self):
I, V = self.to_lists()
return zip(I, V)
Expand Down

0 comments on commit deed61f

Please sign in to comment.