Formal Power Series
hi @davide_zorzenon @alsultanm . I just tried to take inspiration from the max_plus file to do start with methods in the Class FormalPowerSerier but I was not sure about many issues so I want to share some of those:
class FormalPowerSeries:
def __init__(self, matrix):
if not isinstance(matrix, Matrix):
raise ValueError("Input must be an instance of Matrix")
self.matrix = matrix
def Add(self, other):
if not isinstance(other, FormalPowerSeries):
raise ValueError("Input must be an instance of FormalPowerSeries")
if self.matrix.height != other.matrix.height or self.matrix.width != other.matrix.width:
raise ValueError("Matrices must be of the same size")
result_elements = []
for i in range(self.matrix.height):
row = []
for j in range(self.matrix.width):
# Concatenate the string elements of the corresponding lists
combined_elements = self.matrix.elements[i][j][0] + '+' +other.matrix.elements[i][j][0]
row.append(combined_elements)
result_elements.append(row)
return FormalPowerSeries(Matrix(result_elements))
# I did here some example it works however I didn't have to use the class Powerseries to use the addition method
a_matrix = Matrix([[['(a+b)*'], ['b'], ['c']], [['b'], ['v'], ['d']], [['e'], ['s'], ['p']]])
b_matrix = Matrix([[['g'], ['b'], ['h']], [['b'], ['f'], ['d']], [['e'], ['s'], ['p']]])
a = FormalPowerSeries(a_matrix)
b = FormalPowerSeries(b_matrix)
sum_series = a.Add(b)
print(sum_series.matrix)
# here is a remind how the Powerseries class looks :
class PowerSeries:
def __init__(self, series):
self.series = series
def __str__(self):
return self.series
def __add__(self, other):
if not isinstance(other, PowerSeries):
return NotImplemented
return self.series + '+' + other.series
def __mul__(self, other):
if not isinstance(other, PowerSeries):
return NotImplemented
return PowerSeries(self.series + other.series)
def kleene_star(self):
return '(' + self.series + ')*'
# @davide_zorzenon pleas let me know what You Think so I can go on or change