How to get rid of nearly zero numbers in expressions

87 views Asked by At

I tried to do a simplex algorithm in Python and all the tableaus and values were right but, at the last iteration, there were values on the objective function that were off I'm using sympy and numpy. All of the values but 2 are correct and I don't know where did they come from. I even tested the multiplication line and made the add myself, it was supposed to work, but the 5th and 7th numbers are weird.

The results of the old line, the line that should be added to the old line and the actual result:

the results of the old line, the line that should be added to the old line and the actual result

I'm using sympy to use a symbolic value for M. The old line should be added to the "to be added line" and the result should be like [0, 0, 0, 1*M + 1.4, 0.5, 1*M + 0.5, 5.4] but the 5th and 7th elements are weird values that shouldn't be on the line and I don't know how to fix it The M on the last term of the array should be 0.

for index, row in enumerate(self.tableau):
         if pivot_row != index:
            multiplier = -row[entering_column]
            print(f"Index {index}, Multiplcador {multiplier}")
            new_tableau[index] = multiplier * new_tableau[pivot_row] + self.tableau[index]

This is the part of the code that makes the changes above

1

There are 1 answers

0
smichr On

The values you are seeing are very small numbers instead of 0 (note the e-16). Since those are SymPy expressions, if you add the line

new_tableau[index] = [i.n(chop=True) for i in new_tableau[index]]

that should eliminate those "nearly 0" values.