About Matrix class

In this page we explain a square matrix of 3 rd order for example.

The abbreviation of type name defines as follows:

  f :float,  d:double

For example,

float data type : Matrx33f

double data type: Matrix33d

 

The abbreviation type: 

kvs::Matrix33<float> A;

is same as follows:

kvs::Matrix33<float> A;

 

Member functions

Constructor

There are four types of constructor.

Following example explains the constructor case of "kvs::Matrix33f"
( Notie that the initial value sets to zero in case of the default constructor )

// Default constructor

kvs::Matrix33f A;

 

// assign all component

kvs::Matrix33f B( 1.0, 2.0, 0.0,

                 2.0, 2.0, 1.0,

                 0.0, 1.0, 3.0 );

 

// Three vector

kvs::Vector3f v0( 1.0, 1.0, 1.0 );

kvs::Vector3f v1( 2.0, 2.0, 1.0 );

kvs::Vector3f v2( 1.0, 2.0, 4.0 );

kvs::Matrix33f C( v0, v1, v2 );

 

// Array

float elem[9] = { 1.0, 2.0, 3.0,

                 4.0, 5.0, 6.0,

                 7.0, 8.0, 9.0 };

kvs::Matrix33f D( elem );

Value substitution: Operator[ ],   zero( ),   identity( ),   set( )

Operator [] : same as arrays

zero(): all element of matrix sets to ZERO

identity(): set as identity matrix

set( T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22),

set( T a0[3], T a1[3], T a2[3]),

set(T elem[9] ):

//  Operator []

A[0][0] = 1.0; A[0][1] = 1.0; A[0][2] = 3.0;

A[1][0] = 4.0; A[1][1] = 1.0; A[1][2] = 0.0;

A[2][0] = 2.0; A[2][1] = 0.0; A[2][2] = 2.0;

 

// Zero Matrix

A.zero();

 

// dentity matrix

A.identity();

 

// method set(): all components

A.set( 1.0, 2.0, 2.0,

        2.0, 2.0, 1.0,

        0.0, 1.0, 3.0 );

 

// method set(): Three vector

A.set( v0, v1, v2 );

 

// method set(): Array

A.set( elem );

Getting value: Operator [ ]

For example:

float a11 = a[1][1];

std::cout << a[0][0] << " " << a[0][1] << " " << a[0][2] << std::endl;

Standard output: print( ),  output operator<<

A.print( );

std::cout << A ;

Above example is same output:

1 2 3

4 5 6

7 8 9

 

Transposed matrix: transpose( ),   transposed( )

transpose( ): superscription (normalize own)

transposed( ): transposed matrix returns (the own value does not change)

// Superscription

A.transpose( );

 

// Return the transposed matrix

kvs::Matrix33f E = B.transposed( );

Inverse matrix: invert( ),   inverted( )

invert( )( ): superscription (normalize own)

inverted( ): inverse matrix returns (the own value does not change)

// Superscription

C.invert( );

 

// Return the inverse matrix

const kvs::Matrix33f F = B.inverted( );

Trace: trace( ),   determinant: determinant( )

For example:

// Trace

float tr = A.trace();

 

// Determinamt

float det = A.determinant();

The type of return value is the same of vector (Here is float).

 

Four arithmetic operations: +,  -,  *,  /

Vector Addition and Subtraction are defined as

Operator + (+=) and Operator - ( -= ), respectively.

C = A + B;

A+=C;

A -= B;

std::cout << -A << std::endl;

Operator * ( *= ) multiply matrix by scalar value or vector or matrix.

// Matrix by matrix multiplication

C = A*B

A*=B;

 

// Scalar by matrix multiplication

int i = 10;

C = i*B;

C = B*i;

C *= i;        

 

// Vector by matrix multiplication

A.set( 1, 1, 1, 1, 1, 1, 2, 2, 2);

v0.set( 1, 1, 1);

kvs::Vector3f x = v0*A;      // raw vector by matryx

kvs::Vector3f y = A*v0;      // column vector by matrix

Operator / (/= ) divides the each element by scalar value.

D = C/i;

C /= i;

Relational operator: ==, !=

Relational operator compares each component.

For exampe:

// Relational operator

if( A == B ) {

      std::cout << "A == B" << std::endl;

}

else if( A != B ) {

      std::cout << "A != B" << std::endl; }

Sample program: MatrixTest.tgz

 

Compile and execute

Let's try to compile and execute above program.

When compiling a KVS program, it is easy to use the automatic Makefile-generation tool kvsmake supplied by KVS.

Here, the option “-G” defines the name of executable file same as the name of current directory.

# kvsmake -G       <-- Generate Makefile

# kvsmake           <-- Compile

And then,

# ./MatrixTest

Last Updated at Nov. 1, 2015 

 

TOP