Python Forum
math,numpy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: math,numpy (/thread-33329.html)



math,numpy - pyStudent - Apr-16-2021

I need write a function which generate a random matrix (bigger than 2x2). And calculate all the 2x2 part determinant of the generated matrix and generate a new matrix with that values. Confused


that's my code:
arr = np.random.randint(1,10,(3,3))
matrix = np.zeros((3,3))
matrix[0][0] = int(np.linalg.det(arr[0:2,0:2]))
matrix[0][1] = int(np.linalg.det(arr[0:2,1:3]))
matrix[1][0] = int(np.linalg.det(arr[1:3,0:2]))
matrix[1][1] = int(np.linalg.det(arr[1:3,1:3]))
how can i edit this to work for exaple on a 4x4 or on a 16x16 matrix?


RE: math,numpy - Gribouillis - Apr-21-2021

I suggest
b = a[:-1,:-1] * a[1:,1:] - a[:-1,1:] * a[1:,:-1]
For example
>>> import numpy as np
>>> a = np.random.randint(1, 10,(5,6))
>>> b = a[:-1,:-1] * a[1:,1:] - a[:-1,1:] * a[1:,:-1]
>>> print(a)
[[4 1 8 9 4 4]
 [3 6 7 8 9 1]
 [9 4 8 6 5 7]
 [9 7 1 8 4 1]
 [1 4 9 5 4 7]]
>>> print(b)
[[ 21 -41   1  49 -32]
 [-42  20 -22 -14  58]
 [ 27 -52  58 -16 -23]
 [ 29  59 -67  12  24]]



RE: math,numpy - pyStudent - Apr-26-2021

Thanks for your solution. Big Grin It works perfectly.