In this program, you will learn to find the basis and dimension of a matrix in c++.

**Basis**

A set of vectors **S = {v1, … , vk}** span a vector space V if there are enough of the right vectors in S, while they are linearly independent if there are no redundancies. We now combine the two concepts.

Let V be a vector space and S = {v1, v2, … , vk} be a subset of V. Then S is a basis for V if the following two statements are true.

- S spans V.
- S is a linearly independent set of vectors in V.

**Example:**

Let V = R^{n} and let S = {e1, e2, … ,en} where e_{i} has ith component equal to 1 and the rest 0. For example

e_{2} = (0,1,0,0,…,0)

Then S is a basis for V called the standard basis.

**Dimension**

Here we call this size the dimension of the vector space and define it as the number of vectors that are needed to form a basis. Tow show that the dimensions are well defined, we need the following theorem.

**Example: Find Basis and Dimension of Matrix**

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 |
#include<conio.h> #include<iostream> #include<math.h> using namespace std; double d = 0; double det(int n, double mat[10][10]); double det(int n, double mat[10][10]) { double submat[10][10]; if (n == 2) return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1])); else { for (int c = 0; c < n; c++) { int subi = 0; //submatrix's i value for (int i = 1; i < n; i++) { int subj = 0; for (int j = 0; j < n; j++) { if (j == c) continue; submat[subi][subj] = mat[i][j]; subj++; } subi++; } d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat)); } } return d; } int main(int argc, char **argv) { cout << "Enter the number of vectors:\n"; int n; cin >> n; double mat[10][10]; cout << "Enter the vectors one by one:\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> mat[j][i]; } } d = det(n, mat); if (d != 0) cout << "The vectors forms the basis of R" << n << " as the determinant is non-zero"; else cout << "The vectors doesn't form the basis of R" << n << " as the determinant is zero"; } |

When you run the program, the output will be following:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Enter the number of vectors: 3 Enter the vectors one by one: 1 2 3 3 4 5 1 2 3 The vectors doesn't form the basis of R3 as the determinant is zero Enter the number of vectors: 4 Enter the vectors one by one: 2 3 5 8 1 6 2 9 3 4 2 7 2 5 3 9 The vectors forms the basis of R4 as the determinant is non-zero |