Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- typedef long long ll;
- typedef vector<vector<int>> matrix;
- const int maxn = 1e5 + 10;
- const int INF = 2e9;
- matrix multiply_by_k(matrix A, int k) {
- matrix res = A;
- int n = (int) A.size(), m = (int) A[0].size();
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- res[i][j] *= k;
- }
- }
- return res;
- }
- matrix add_two_matrices(matrix A, matrix B) {
- int n_a = (int) A.size(), m_a = (int) A[0].size();
- int n_b = (int) B.size(), m_b = (int) B[0].size();
- if(n_a == n_b and m_a == m_b) {
- matrix res = A;
- for(int i = 0; i < n_a; i++) {
- for(int j = 0; j < m_a; j++) {
- res[i][j] = A[i][j] + B[i][j];
- }
- }
- return res;
- }
- cout << "Dimenziite na matricite treba da se isti za da mozhe da gi sobereme istite" << endl;
- return {{0}};
- }
- matrix subtract_two_matrices(matrix A, matrix B) {
- int n_a = (int) A.size(), m_a = (int) A[0].size();
- int n_b = (int) B.size(), m_b = (int) B[0].size();
- if(n_a != n_b or m_a != m_b) {
- cout << "Dimenziite na matricite treba da se isti za da mozhe da gi sobereme istite" << endl;
- return {{0}};
- }
- matrix res = A;
- for(int i = 0; i < n_a; i++) {
- for(int j = 0; j < m_a; j++) {
- res[i][j] = A[i][j] - B[i][j];
- }
- }
- return res;
- }
- matrix multiply_two_matrices(matrix A, matrix B) {
- int n_a = (int) A.size(), m_a = (int) A[0].size();
- int n_b = (int) B.size(), m_b = (int) B[0].size();
- if(m_a != n_b) {
- cout << "Matricite ne mozhat da se mnozhat" << endl;
- return {{0}};
- }
- matrix res(n_a, vector<int>(m_b, 0));
- for(int i = 0; i < n_a; i++) {
- for(int j = 0; j < m_b; j++) {
- int c = 0;
- for(int k = 0; k < n_a; k++) {
- c += A[i][k] * B[k][j];
- }
- res[i][j] = c;
- }
- }
- return res;
- }
- int determinant_of_matrix(matrix A) {
- int sz = (int) A.size();
- if(sz == 2) {
- return (A[0][0] * A[1][1]) - (A[0][1] * A[1][0]);
- }
- int determinant = 0;
- int sign = 1;
- for(int i = 0; i < sz; i++) {
- int n = 0, m = 0;
- matrix tmp(sz - 1, vector<int>(sz - 1, 0));
- for(int j = 0; j < sz; j++) {
- for(int k = 0; k < sz; k++) {
- if(j != 0 and k != i) {
- tmp[n][m] = A[j][k];
- if(m < sz - 2) {
- m++;
- }
- else {
- m = 0;
- n++;
- }
- }
- }
- }
- determinant = determinant + sign*(A[0][i]) * determinant_of_matrix(tmp);
- sign *= -1;
- }
- return determinant;
- }
- void print(matrix A) {
- int n = (int) A.size(), m = (int) A[0].size();
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cout << A[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- int main() {
- matrix A = {
- {1, 3, 5, 9},
- {1, 3, 1, 7},
- {4, 3, 9, 7},
- {5, 2, 0, 9}
- };
- matrix B = {
- {1, 2, 3},
- {4, 5, 6},
- };
- cout << determinant_of_matrix(A) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement