@@ -78,6 +78,14 @@ def __mul__(self, other):
78
78
return Math .ComplexNumber (self .real * other .real - self .imaginary * other .imaginary , self .real * other .imaginary + self .imaginary * other .real )
79
79
return Math .ComplexNumber (self .real * other .real - self .imaginary * other .imaginary , self .real * other .imaginary + self .imaginary * other .real , self .complex_letter )
80
80
81
+ def __pow__ (self , power ):
82
+ if power == 0 :
83
+ return Math .ComplexNumber (1 , 0 )
84
+ result = Math .ComplexNumber (self .real , self .imaginary )
85
+ for i in range (power - 1 ):
86
+ result *= self
87
+ return result
88
+
81
89
def __str__ (self ):
82
90
if self .imaginary == 0 :
83
91
return f"{ self .real } "
@@ -255,15 +263,15 @@ def __mul__(self, other: Union['Math.Matrix', int]) -> 'Math.Matrix':
255
263
def __rmul__ (self , other : int ) -> 'Math.Matrix' :
256
264
# int * matrix fix
257
265
return self .__mul__ (other )
258
-
266
+
259
267
def __str__ (self ):
260
268
# Spaghetti code goes hard <3
261
269
262
270
__temp = []
263
271
for i in range (0 , self .height ):
264
272
__temp .append ([])
265
273
266
- # Iterate through the collumns
274
+ # Iterate through the columns
267
275
for i in range (0 , self .width ):
268
276
269
277
# Get longest number in characters used (- included etc.) per column
@@ -336,15 +344,15 @@ def __pow__(self, power: int) -> 'Math.Matrix':
336
344
for i in range (power ):
337
345
result *= self
338
346
return result
339
-
347
+
340
348
def transpose (self ) -> 'Math.Matrix' :
341
349
result = []
342
350
for i in range (self .width ):
343
351
result .append ([])
344
352
for j in range (self .height ):
345
353
result [i ].append (self .matrix [j ][i ])
346
354
return Math .Matrix (result )
347
-
355
+
348
356
def diagonal (self ) -> list :
349
357
if self .width != self .height :
350
358
raise ValueError ("Matrix is not a square matrix" )
@@ -353,7 +361,7 @@ def diagonal(self) -> list:
353
361
for i in range (self .width ):
354
362
result .append (self .matrix [i ][i ])
355
363
return result
356
-
364
+
357
365
def secondary_diagonal (self ) -> list :
358
366
if self .width != self .height :
359
367
raise ValueError ("Matrix is not a square matrix" )
@@ -362,7 +370,7 @@ def secondary_diagonal(self) -> list:
362
370
for i in range (self .width ):
363
371
result .append (self .matrix [i ][self .width - i - 1 ])
364
372
return result
365
-
373
+
366
374
def inferior_triangular_matrix (self , offset = 0 ):
367
375
if self .width != self .height :
368
376
raise ValueError ("Matrix is not a square matrix" )
@@ -375,12 +383,12 @@ def inferior_triangular_matrix(self, offset=0):
375
383
for j in range (self .height ):
376
384
line .append (0 )
377
385
result .append (line )
378
-
386
+
379
387
for i in range (offset , self .height ):
380
388
for j in range (i + 1 - offset ):
381
389
result [i ][j ] = self .matrix [i ][j ]
382
390
return Math .Matrix (result )
383
-
391
+
384
392
def superior_triangular_matrix (self , offset = 0 ):
385
393
if self .width != self .height :
386
394
raise ValueError ("Matrix is not a square matrix" )
@@ -393,12 +401,11 @@ def superior_triangular_matrix(self, offset=0):
393
401
for j in range (self .height ):
394
402
line .append (0 )
395
403
result .append (line )
396
-
404
+
397
405
for i in range (self .height ):
398
406
for j in range (i + offset , self .width ):
399
407
result [i ][j ] = self .matrix [i ][j ]
400
408
return Math .Matrix (result )
401
-
402
409
403
410
@staticmethod
404
411
def unit_matrix (size : int ) -> 'Math.Matrix' :
@@ -445,7 +452,7 @@ def transpose_of(matrix: Union[list, 'Math.Matrix']) -> 'Math.Matrix':
445
452
for j in range (matrix .height ):
446
453
result [i ].append (matrix .matrix [j ][i ])
447
454
return Math .Matrix (result )
448
-
455
+
449
456
@staticmethod
450
457
def inferior_triangular_matrix_of (matrix : Union [list , 'Math.Matrix' ], offset = 0 ) -> 'Math.Matrix' :
451
458
if not isinstance (matrix , Math .Matrix ):
@@ -462,12 +469,12 @@ def inferior_triangular_matrix_of(matrix: Union[list, 'Math.Matrix'], offset=0)
462
469
for j in range (matrix .height ):
463
470
line .append (0 )
464
471
result .append (line )
465
-
472
+
466
473
for i in range (offset , matrix .height ):
467
474
for j in range (i + 1 - offset ):
468
475
result [i ][j ] = matrix .matrix [i ][j ]
469
476
return Math .Matrix (result )
470
-
477
+
471
478
@staticmethod
472
479
def superior_triangular_matrix_of (matrix : Union [list , 'Math.Matrix' ], offset = 0 ) -> 'Math.Matrix' :
473
480
if not isinstance (matrix , Math .Matrix ):
@@ -484,7 +491,7 @@ def superior_triangular_matrix_of(matrix: Union[list, 'Math.Matrix'], offset=0)
484
491
for j in range (matrix .height ):
485
492
line .append (0 )
486
493
result .append (line )
487
-
494
+
488
495
for i in range (matrix .height ):
489
496
for j in range (i + offset , matrix .width ):
490
497
result [i ][j ] = matrix .matrix [i ][j ]
@@ -496,5 +503,6 @@ def from_terminal(height, width) -> 'Math.Matrix':
496
503
for i in range (height ):
497
504
matrix .append ([])
498
505
for j in range (width ):
499
- matrix [i ].append (int (input (f"Enter the value for [{ i } ][{ j } ]: " )))
506
+ matrix [i ].append (
507
+ int (input (f"Enter the value for [{ i } ][{ j } ]: " )))
500
508
return Math .Matrix (matrix )
0 commit comments