matrix generators - DrakenWan/Matrix GitHub Wiki

Non-member matrix generators

There are some useful non-member functions available in matrix library that help you generate certain kind of matrices that might have special names too. Many of these matrix generator functions only work with double precision numbers.

Below are usage examples for some of these. It is usually recommended to use template parameters along with function calls to determine the data type but in cases where functions only generate double precision matrices they are not needed.

Identity matrix

//create a 3x3 integer identity matrix
matrix<int> I = eye<int>(3);
I.display("Identity(3x3):-");

Outputs

Identity(3x3):-
 1 0 0
 0 1 0
 0 0 1

Diagonal matrix

diagonal creates a diagonal matrix with a specifica value in its principal diagonal.

//create a 4x4 diagonal matrix with double values
matrix<double> D = diagonal<double>(4, 3.5);
D.display("D(4x4):-");

Outputs

D(4x4):-
 3.5   0   0   0
   0 3.5   0   0
   0   0 3.5   0
   0   0   0 3.5

The diag function takes into input an nx1 vector (matrix) and creates an nxn matrix and inflates its principal (or other) diagonals with values from the vector.

// create a 3x1 column vector
matrix<double> B(3,1);

//inflate with integer values starting from 1
B.iota(1); 
//iota function is useful in initializing arithmetic matrices with integer values starting from the given input.

// create an nxn diagonal matrix
matrix<double> C = diag(B);

C.display("C:-");

Outputs

C:-
 1 0 0
 0 2 0
 0 0 3

matrix<double> C = diag(B); in this line of code, the type of the matrix that diag generates is inferred from the input vector's types. However, you can try to explicitly call the type in template parameter like so diag<int>(B) and although B is double matrix the copy constructor will do the conversion between numerical types (and there might be some loss of precision or information).

If you want to put values into the super- or subdiagonal, you can do like so:-

// creates a 4x4 matrix and fills its subdiagonal
matrix<double> D = diag(B,-1);
D.display("D:-");

Outputs

D:-
 0 0 0 0
 1 0 0 0
 0 2 0 0
 0 0 3 0

Triangular matrices

There are two utility functions to generate upper or lower triangular matrices. There are different identifiers for same functioanlities. Every function generates random values from gaussian distribution and populates the triangular matrix. These only produce double precision matrices. Also std::complex types are not supported yet.

Upper triangular

Three different function calls can be made for UTM generation.

// create 3x3 lower triangular matrix
upper_triangle_matrix(3).display("UTM(1):-");
utm(3).display("UTM(2):-");
triu(3).display("UTM(3):-");

Outputs

UTM(1):-
 -0.1451 -0.9964 -2.5098
       0 -1.5392 -0.0697
       0       0 -1.0688

UTM(2):-
 -0.6411 -0.9003  0.9932
       0  0.8671  -0.038
       0       0  0.8029

UTM(3):-
 -1.2511 -1.3852  0.4838
       0  0.7861 -0.9476
       0       0  0.5074

Lower triangular

Three different function calls CAN be made for LTM generation.

lower_triangle_matrix(3).display("LTM(1):-");
ltm(3).display("LTM(2):-");
tril(3).display("LTM(3):-");

Outputs

LTM(1):-
  0.3307       0       0
  1.8901 -0.8195       0
  0.7801 -0.2831 -0.1935

LTM(2):-
  0.3949       0       0
  0.8216  1.6085       0
 -0.7253  0.2498 -0.3327

LTM(3):-
 -0.2572       0       0
  0.0977  0.3371       0
  0.9666  2.2267  0.8034
⚠️ **GitHub.com Fallback** ⚠️