📜  查找由阵列旋转生成的矩阵的行列式

📅  最后修改于: 2021-04-27 05:50:38             🧑  作者: Mango

给定三个元素的数组。任务是通过使用数组的所有三个旋转作为矩阵的行来构造3 * 3阶矩阵,并找到所得矩阵的行列式。

例子

Input : arr[] = {1, 2, 3}
Output : 18

Input : arr[] = {1, 1, 1}
Output : 0

方法:根据问题陈述,使用给定的数组构造3 * 3矩阵。如果a1,a2,a3是数组元素,则对应的矩阵将是:

{{a1, a2, a3}, 
 {a3, a1, a2},
 {a2, a3, a1}}

任务是计算上述矩阵的行列式。

可以使用适当的方法来计算行列式,但另一方面,如果扩展所得矩阵以进行计算,则结果将为a1 3 + a2 3 + a3 3 – 3 * a1 * a2 * a3 。因此,不是通过适当的扩展来计算行列式,而是使用上面生成的公式。

因此,上述矩阵的行列式为:

a13 + a23 + a33 - (3*a1*a2*a3)

下面是上述方法的实现:

C++
// C++ program for finding determinant of generated matrix
  
#include 
#define N 3
using namespace std;
  
// Function to calculate determinant
int calcDeterminant(int arr[])
{
    int determinant = 0;
  
    for (int i = 0; i < N; i++) {
        determinant += pow(arr[i], 3);
    }
  
    determinant -= 3 * arr[0] * arr[1] * arr[2];
  
    return determinant;
}
  
// Driver code
int main()
{
    int arr[] = { 4, 5, 3 };
    cout << calcDeterminant(arr);
    return 0;
}


Java
// Java program for finding determinant
// of generated matrix 
import java.util.*;
import java.lang.*;
  
class GFG
{
static int N = 3;
  
// Function to calculate determinant 
static double calcDeterminant(int arr[]) 
{ 
    double determinant = 0; 
  
    for (int i = 0; i < N; i++) 
    { 
        determinant += Math.pow(arr[i], 3); 
    } 
  
    determinant -= 3 * arr[0] * 
                    arr[1] * arr[2]; 
  
    return determinant; 
} 
  
// Driver code
static public void main (String args[])
{
    int []arr = { 4, 5, 3 }; 
    System.out.println(calcDeterminant(arr)); 
}
}
  
// This code is contributed 
// by Akanksha Rai


Python3
# Python3 program for finding determinant of generated matrix
  
# Function to calculate determinant
def calcDeterminant(arr,n):
    determinant =0
  
    for i in range(n):
        determinant+= pow(arr[i],3)
  
    determinant -= 3*arr[0]*arr[1]*arr[2]
  
    return determinant
  
# Driver code
arr = [4,5,3]
n = len(arr)
print(calcDeterminant(arr,n))
  
# This code is contributed by Shrikant13


C#
// C# program for finding determinant
// of generated matrix 
using System;
  
class GFG
{
static int N = 3;
  
// Function to calculate determinant 
static double calcDeterminant(int []arr) 
{ 
    double determinant = 0; 
  
    for (int i = 0; i < N; i++) 
    { 
        determinant += Math.Pow(arr[i], 3); 
    } 
  
    determinant -= 3 * arr[0] * 
                       arr[1] * arr[2]; 
  
    return determinant; 
} 
  
// Driver code
static public void Main ()
{
    int []arr = { 4, 5, 3 }; 
    Console.WriteLine(calcDeterminant(arr)); 
}
}
  
// This code is contributed by akt_mit


PHP


输出:
36