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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | //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:
1 2 3 4 5 6 7 8 | $ 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 Program to Implement Jarvis Algorithm
Jackson – JsonMappingException (No serializer found for class)
Spring Security Form Login
Set Interface trong Java
Extra Login Fields with Spring Security
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Spring Cloud AWS – S3
A Guide to Java 9 Modularity
Ép kiểu trong Java (Type casting)
So sánh HashMap và Hashtable trong Java
Java Program to Implement the RSA Algorithm
How to Get All Spring-Managed Beans?
Java Program to Implement the MD5 Algorithm
Python Program to Transpose a Matrix
Spring Boot - Batch Service
Reading an HTTP Response Body as a String in Java
Java Program to Perform Left Rotation on a Binary Search Tree
New Stream Collectors in Java 9
Java Program to Implement Adjacency Matrix
Introduction to Apache Commons Text
Spring REST API + OAuth2 + Angular
Convert String to int or Integer in Java
Java Program to Check for balanced parenthesis by using Stacks
Introduction to Thread Pools in Java
Java Program to Implement DelayQueue API
Tìm hiểu về Web Service
Java Program to Implement SimpeBindings API
Java Program to Implement Hash Tables with Linear Probing
Lớp TreeMap trong Java
Java InputStream to String
Java Program to Implement Miller Rabin Primality Test Algorithm
Java Program to Implement Levenshtein Distance Computing Algorithm