# Java Program to Find Basis and Dimension of a Matrix

This is the java program to find whether the vectors entered by users form the basis for the given dimension. The result for the same can be obtained by checking whether the determinant of the matrix formed by vectors is zero or not. If the determinant is non zero its forms the basis for the given dimension, not otherwise.

Here is the source code of the Java Program to Find Basis and Dimension of a Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `//This is a sample program to find the basis and dimension of a vectors`
2. `import java.util.Scanner;`
3. ` `
4. `public class Basis_Dimension_Matrix `
5. `{`
6. `    public static double determinant(double A[][],int N)`
7. `    {`
8. `        double det=0;`
9. `        if(N == 1)`
10. `        {`
11. `            det = A[0][0];`
12. `        }`
13. `        else if (N == 2)`
14. `        {`
15. `            det = A[0][0]*A[1][1] - A[1][0]*A[0][1];`
16. `        }`
17. `        else`
18. `        {`
19. `            det=0;`
20. `            for(int j1=0;j1<N;j1++)`
21. `            {`
22. `                double[][] m = new double[N-1][];`
23. `                for(int k=0;k<(N-1);k++)`
24. `                {`
25. `                    m[k] = new double[N-1];`
26. `                }`
27. `                for(int i=1;i<N;i++)`
28. `                {`
29. `                    int j2=0;`
30. `                    for(int j=0;j<N;j++)`
31. `                    {`
32. `                        if(j == j1)`
33. `                            continue;`
34. `                        m[i-1][j2] = A[i][j];`
35. `                        j2++;`
36. `                    }`
37. `                }`
38. `                det += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);`
39. `            }`
40. `        }`
41. `        return det;`
42. `    }`
43. ` `
44. `    public static void main(String args[])`
45. `    {`
46. `        Scanner sc = new Scanner(System.in);`
47. `        System.out.println("Enter the number of vectors:");`
48. `        int n = sc.nextInt();`
49. `        double [][]mat = new double[n][n];`
50. `        System.out.println("Enter the vectors one by one:");`
51. `        for(int i=0; i<n; i++)`
52. `        {`
53. `            for(int j=0; j<n; j++)`
54. `            {`
55. `                mat[j][i] = sc.nextDouble();`
56. `            }`
57. `        }`
58. `        double det = determinant(mat, n);`
59. `        if(det != 0)`
60. `            System.out.println("The vectors froms the basis of R"+n+" as the determinant is non-zero");`
61. `        else`
62. `            System.out.println("The vectors doesn't form the basis of R"+n+" as the determinant is zero");`
63. `        sc.close();`
64. `    }`
65. `}`

Output:

```\$ javac Basis_Dimension_Matrix.java
\$ java Basis_Dimension_Matrix
Enter the number of vectors:
2
Enter the vectors one by one:
1 1
-1 2
The vectors froms the basis of R2 as the determinant is non-zero```

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!