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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from visma.functions.constant import Constant
from visma.functions.operator import Multiply
from visma.functions.operator import Minus
from visma.functions.operator import Plus
from visma.functions.constant import Zero
from visma.functions.structure import Expression
import numpy as np
from visma.functions.operator import Binary


class Matrix(object):
    """Class for matrix type

    The elements in the matrix are function tokens.

    Example:
        [              1            ,            2xy^2            ;
                      4xy           ,             x+y             ]
        is tokenized to
        [[         [Constant]        ,          [Variable]        ],
         [         [Variable]        , [Variable, Binary, Variable]]]
        and stored in matrix.value.
    """

    def __init__(self, value=None, coefficient=None, power=None, dim=None, scope=None):
        self.scope = None
        if value is not None:
            self.value = value
        else:
            self.value = []
        if coefficient is not None:
            self.coefficient = coefficient
        else:
            self.coefficient = 1
        if power is not None:
            self.power = power
        else:
            self.power = 1
        if dim is not None:
            self.dim = dim
        else:
            self.dim = [0, 0]

    def convertMatrixToString(self, Latex=False):
        from visma.io.parser import tokensToString
        MatrixString = ''
        self.dim[0] = len(self.value)
        self.dim[1] = len(self.value[0])
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                if not Latex:
                    MatrixString += tokensToString(self.value[i][j]) + '\t'
                else:
                    MatrixString += tokensToString(self.value[i][j]) + '    '
            MatrixString += '\n'
        return MatrixString

    def __add__(self, other):
        """Adds two matrices
         Arguments:
            self {visma.matrix.structure.Matrix} -- matrix token
            other {visma.matrix.structure.Matrix} -- matrix token
         Returns:
            matSum {visma.matrix.structure.Matrix} -- sum matrix token
         Note:
            Make dimCheck before calling addMatrix
        """
        matSum = Matrix()
        matSum.empty(self.dim)
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                matSum.value[i][j].extend(self.value[i][j])
                matSum.value[i][j].append(Binary('+'))
                matSum.value[i][j].extend(other.value[i][j])
        from visma.matrix.operations import simplifyMatrix
        matSum = simplifyMatrix(matSum)
        return matSum

    def __sub__(self, other):
        """Subtracts two matrices
         Arguments:
            self {visma.matrix.structure.Matrix} -- matrix token
            other {visma.matrix.structure.Matrix} -- matrix token
         Returns:
            matSub {visma.matrix.structure.Matrix} -- subtracted matrix token
         Note:
            Make dimCheck before calling subMatrix
        """
        matSub = Matrix()
        matSub.empty(self.dim)
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                matSub.value[i][j].extend(self.value[i][j])
                matSub.value[i][j].append(Binary('-'))
                matSub.value[i][j].extend(other.value[i][j])
        from visma.matrix.operations import simplifyMatrix
        matSub = simplifyMatrix(matSub)
        return matSub

    def __mul__(self, other):
        """Multiplies two matrices
         Arguments:
            self {visma.matrix.structure.Matrix} -- matrix token
            other {visma.matrix.structure.Matrix} -- matrix token
         Returns:
            matPro {visma.matrix.structure.Matrix} -- product matrix token
         Note:
            Make mulitplyCheck before calling multiplyMatrix
            Not commutative
        """
        matPro = Matrix()
        matPro.empty([self.dim[0], other.dim[1]])
        for i in range(self.dim[0]):
            for j in range(other.dim[1]):
                for k in range(self.dim[1]):
                    if matPro.value[i][j] != []:
                        matPro.value[i][j].append(Binary('+'))
                    if len(self.value[i][k]) != 1:
                        matPro.value[i][j].append(Expression(self.value[i][k]))
                    else:
                        matPro.value[i][j].extend(self.value[i][k])
                    matPro.value[i][j].append(Binary('*'))
                    if len(other.value[k][j]) != 1:
                        matPro.value[i][j].append(Expression(other.value[k][j]))
                    else:
                        matPro.value[i][j].extend(other.value[k][j])
        from visma.matrix.operations import simplifyMatrix
        matPro = simplifyMatrix(matPro)
        return matPro

    def __str__(self):
        represent = "["
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                for tok in self.value[i][j]:
                    represent += tok.__str__()
                represent += ","
            represent = represent[:-1] + ";"
        represent = represent[:-1] + "]"
        return represent

    def empty(self, dim=None):
        """Empties the matrix into a matrix of dimension dim

        Keyword Arguments:
            dim {list} -- dimension of matrix (default: {None})
        """
        if dim is not None:
            self.dim = dim
        self.value = [[[] for _ in range(self.dim[1])] for _ in range(self.dim[0])]

    def prop(self, scope=None, value=None, coeff=None, power=None, operand=None, operator=None):
        if scope is not None:
            self.scope = scope
        if value is not None:
            self.value = value
        if coeff is not None:
            self.coefficient = coeff
        if power is not None:
            self.power = power

    def isSquare(self):
        """Checks if matrix is square

        Returns:
            bool -- if square matrix or not
        """
        if self.dim[0] == self.dim[1]:
            self.__class__ = SquareMat
            return True
        else:
            return False

    def isIdentity(self):
        """Checks if matrix is identity

        Returns:
            bool -- if identity matrix or not
        """
        if self.isDiagonal():
            for i in range(0, self.dim[0]):
                if self.value[i][i][0].value != 1:
                    return False
            self.__class__ = IdenMat
            return True
        else:
            return False

    def isDiagonal(self):
        """Checks if matrix is diagonal

        Returns:
            bool -- if diagonal matrix or not
        """
        if self.isSquare():
            for i in range(0, self.dim[0]):
                for j in range(0, self.dim[1]):
                    if i != j and (self.value[i][j][0].value != 0 or len(self.value[i][j]) > 1):
                        return False
            self.__class__ = DiagMat
            return True
        else:
            return False

    def dimension(self):
        """Gets the dimension of the matrix

        dim[0] -- number of rows
        dim[1] -- number of columns
        """
        self.dim[0] = len(self.value)
        self.dim[1] = len(self.value[0])

    def transposeMat(self):
        """Returns Transpose of Matrix

        Returns:
            matRes {visma.matrix.structure.Matrix} -- result matrix token
        """
        matRes = Matrix()
        matRes.empty([self.dim[1], self.dim[0]])
        for i in range(self.dim[0]):
            for j in range(self.dim[1]):
                matRes.value[j][i] = self.value[i][j]
        return matRes


class SquareMat(Matrix):
    """Class for Square matrix

    Square matrix is a matrix with equal dimensions.

    Extends:
        Matrix
    """

    def determinant(self, mat=None):
        """Calculates square matrices' determinant

        Returns:
            list of tokens forming the determinant
        """
        from visma.simplify.simplify import simplify

        if mat is None:
            self.dimension()
            mat = np.array(self.value)
        if(mat.shape[0] > 2):
            ans = []
            for i in range(mat.shape[0]):
                mat1 = SquareMat()
                mat1.value = np.concatenate((mat[1:, :i], mat[1:, i+1:]), axis=1).tolist()
                a, _, _, _, _ = simplify(mat1.determinant())
                if(a[0].value != 0 and a != []):
                    a, _, _, _, _ = simplify(a + [Multiply()] + mat[0][i].tolist())
                    if(i % 2 == 0):
                        if(ans != []):
                            ans, _, _, _, _ = simplify(ans + [Plus()] + a)
                        else:
                            ans = a
                    else:
                        ans, _, _, _, _ = simplify(ans + [Minus()] + a)
        elif(mat.shape[0] == 2):
            a = Multiply()
            b = Minus()
            mat = mat.tolist()
            a1, _, _, _, _ = simplify(mat[0][0] + [a] + mat[1][1])
            a2, _, _, _, _ = simplify(mat[0][1] + [a] + mat[1][0])
            ans, _, _, _, _ = simplify([a1[0], b, a2[0]])
            if (isinstance(ans[0], Minus) or isinstance(ans[0], Plus)) and ans[0].value not in ['+', '-']:
                ans[0] = Constant(ans[0].value)
        else:
            ans, _, _, _, _ = simplify(mat[0][0])
        if not ans:
            ans = Zero()
        return ans

    def traceMat(self):
        """Returns the trace of a square matrix (sum of diagonal elements)

        Arguments:
            mat {visma.matrix.structure.Matrix} -- matrix token

        Returns:
            trace {visma.matrix.structure.Matrix} -- string token
        """
        from visma.simplify.simplify import simplify
        trace = []
        self.dim[0] = len(self.value)
        self.dim[1] = len(self.value[0])
        for i in range(self.dim[0]):
            trace.extend(self.value[i][i])
            trace.append(Binary('+'))
        trace.pop()
        trace, _, _, _, _ = simplify(trace)
        return trace

    def inverse(self):
        """Calculates the inverse of the matrix using Gauss-Jordan Elimination

        Arguments:
            matrix {visma.matrix.structure.Matrix} -- matrix token

        Returns:
            inv {visma.matrix.structure.Matrix} -- result matrix token
        """
        from visma.simplify.simplify import simplify
        from visma.io.tokenize import tokenizer
        from visma.io.parser import tokensToString

        if tokensToString(self.determinant()) == "0":
            return -1
        self.dim[0] = len(self.value)
        self.dim[1] = len(self.value[0])
        n = self.dim[0]
        mat = Matrix()
        mat.empty([n, 2*n])
        for i in range(0, n):
            for j in range(0, 2*n):
                if j < n:
                    mat.value[i][j] = self.value[i][j]
                else:
                    mat.value[i][j] = []

        for i in range(0, n):
            for j in range(n, 2*n):
                if j == (i + n):
                    mat.value[i][j].extend(tokenizer('1'))
                else:
                    mat.value[i][j].extend(tokenizer("0"))

        for i in range(n-1, 0, -1):
            if mat.value[i-1][0][0].value < mat.value[i][0][0].value:
                for j in range(0, 2*n):
                    temp = mat.value[i][j]
                    mat.value[i][j] = mat.value[i-1][j]
                    mat.value[i-1][j] = temp

        for i in range(0, n):
            for j in range(0, n):
                if j != i:
                    temp = []
                    if len(mat.value[j][i]) != 1:
                        temp.append(Expression(mat.value[j][i]))
                    else:
                        temp.extend(mat.value[j][i])
                    temp.append(Binary('/'))
                    if len(mat.value[i][i]) != 1:
                        temp.append(Expression(mat.value[i][i]))
                    else:
                        temp.extend(mat.value[i][i])
                    temp, _, _, _, _ = simplify(temp)

                    for k in range(0, 2*n):
                        t = []
                        if mat.value[i][k][0].value != 0:
                            if len(mat.value[i][k]) != 1:
                                t.append(Expression(mat.value[i][k]))
                            else:
                                t.extend(mat.value[i][k])
                            t.append(Binary('*'))
                            if len(temp) != 1:
                                t.append(Expression(temp))
                            else:
                                t.extend(temp)
                            t, _, _, _, _ = simplify(t)
                            mat.value[j][k].append(Binary('-'))
                            if len(t) != 1:
                                mat.value[j][k].append(Expression(t))
                            else:
                                mat.value[j][k].extend(t)
                            mat.value[j][k], _, _, _, _ = simplify(mat.value[j][k])

        for i in range(0, n):
            temp = []
            temp.extend(mat.value[i][i])
            for j in range(0, 2*n):
                if mat.value[i][j][0].value != 0:
                    mat.value[i][j].append(Binary('/'))
                    mat.value[i][j].extend(temp)
                    mat.value[i][j], _, _, _, _ = simplify(mat.value[i][j])

        inv = SquareMat()
        inv.empty([n, n])
        for i in range(0, n):
            for j in range(n, 2*n):
                inv.value[i][j-n] = mat.value[i][j]
        return inv

    def cofactor(self):
        """Calculates cofactors matrix of the Square Matrix

        Returns:
            An object of type SquareMat
        """
        mat1 = SquareMat()
        mat1.value = []
        for i in range(self.dim[0]):
            if(i % 2 == 0):
                coeff = -1
            else:
                coeff = 1
            mat1.value.append([])
            for j in range(self.dim[1]):
                coeff *= -1
                mat = SquareMat()
                temp = np.array(self.value)
                mat.value = np.concatenate((np.concatenate((temp[:i, :j], temp[i+1:, :j])), np.concatenate((temp[:i, j+1:], temp[i+1:, j+1:]))), axis=1).tolist()
                val = mat.determinant()[0]
                val.value *= coeff
                mat1.value[i].append([val])
        mat1.dimension()
        return mat1


class DiagMat(SquareMat):
    """Class for Diagonal matrix

    Diagonal matrix is a square matrix with all elements as 0 except for the diagonal elements.

    Extends:
        SquareMat
    """

    def __init__(self, dim, diagElem):
        """
        dim {list} -- dimension of matrix
        diagElem {list} -- list of tokens list
        """
        super().__init__()
        self.dim = dim
        for i in range(0, dim[0]):
            row = []
            for j in range(0, dim[1]):
                if i == j:
                    row.append(diagElem[i])
                else:
                    row.append([Constant(0)])
            self.value.append(row)


class IdenMat(DiagMat):
    """Class for identity matrix

    Identity matrix is a diagonal matrix with all elements as 0 except for the diagonal elements which are 1.

    Extends:
        DiagMat
    """

    def __init__(self, dim):
        super().__init__(dim, [[Constant(1)]]*dim[0])
        for i in range(0, dim[0]):
            self.value[i][i] = Constant(1)