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.

//This is a sample program to find the basis and dimension of a vectors
import java.util.Scanner;
 
public class Basis_Dimension_Matrix 
{
    public static double determinant(double A[][],int N)
    {
        double det=0;
        if(N == 1)
        {
            det = A[0][0];
        }
        else if (N == 2)
        {
            det = A[0][0]*A[1][1] - A[1][0]*A[0][1];
        }
        else
        {
            det=0;
            for(int j1=0;j1<N;j1++)
            {
                double[][] m = new double[N-1][];
                for(int k=0;k<(N-1);k++)
                {
                    m[k] = new double[N-1];
                }
                for(int i=1;i<N;i++)
                {
                    int j2=0;
                    for(int j=0;j<N;j++)
                    {
                        if(j == j1)
                            continue;
                        m[i-1][j2] = A[i][j];
                        j2++;
                    }
                }
                det += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);
            }
        }
        return det;
    }
 
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of vectors:");
        int n = sc.nextInt();
        double [][]mat = new double[n][n];
        System.out.println("Enter the vectors one by one:");
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                mat[j][i] = sc.nextDouble();
            }
        }
        double det = determinant(mat, n);
        if(det != 0)
            System.out.println("The vectors froms the basis of R"+n+" as the determinant is non-zero");
        else
            System.out.println("The vectors doesn't form the basis of R"+n+" as the determinant is zero");
        sc.close();
    }
}

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

Related posts:

Java equals() and hashCode() Contracts
Using the Map.Entry Java Class
Converting String to Stream of chars
Java Program to Implement RenderingHints API
Spring Data JPA and Null Parameters
Send an email using the SMTP protocol
Why String is Immutable in Java?
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
The Registration Process With Spring Security
Java – Convert File to InputStream
Java Program to Implement Kosaraju Algorithm
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Hướng dẫn Java Design Pattern – Bridge
Hướng dẫn sử dụng Lớp FilePermission trong java
Exploring the Spring 5 WebFlux URL Matching
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Implement Splay Tree
Properties with Spring and Spring Boot
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java Program to Implement Bresenham Line Algorithm
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Examine the internal DNS cache
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Spring MVC Async vs Spring WebFlux
Immutable ArrayList in Java
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Implement Randomized Binary Search Tree
Anonymous Classes in Java
Java Program to Implement Graph Coloring Algorithm
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Java Program to Implement the Program Used in grep/egrep/fgrep