Skip to content

ghmoon90/commonmath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

commonmath

developing common algebra for c++ langugae

1. Introduction

The numcpp is legacy code for developing c++ class for linear algebra library.

2. Relaese Note

  • 2023.2.1. inital version
  • 2023.2.10. get, set function
  • 2023.2.12. clear function
  • 2023.2.13. add, substract, product, vec_dot, vec_cross function

3. Contribution

GunHee Moon

Graduated Inha Univ in 2012
Get M.S. degree at KAIST in 2014
Get Ph.D. degree at KAIST in 2019
Working at Hyundai Motor Company from 2019 as a GNC engineer

4. Purpose

to practice basic algebra function to develope basis math function set for matrix calculation

5. Dev. Env. Setting

#tool chain If you are using windows OS, any following option is available, cygwin, mingW, or any gcc compiler (minGW, Cygwin), msys or MS visual studio compiler (VS comunity)

If you are using linux, (Ubuntu or CentOS) just use gcc compiler of the latest version.

6. User Manual

6.1. matrix declearing

    cmf::matrix a;
    double arr[9] = {1, 2, 3, 4, 5,6,7,8,9};
    a.set(arr, 3,3 );
    a.print_mat();

    // swap element
    a.set(3.9, 2,2);
    a.print_mat();

6.2. matrix add, substract

    cmf::matrix a,b,c;
    double arr[9] = {1, 2, 3, 4, 5,6,7,8,9};
    double arr2[9] = {2, 4,2, 5,3,6,3,2,5};
    a.set(arr, 3,3 );
    b.set(arr2,3,3);
    add(c,a,b); // c = a+b
    a.print_mat();
    b.print_mat();
    c.print_mat();

    substrct(c,a,b); // c = a-b
    c.print_mat();

6.3 matrix product

    cmf::matrix a,b,c;
    double arr[6] = {1, 2, 4, 5,6,9};
    double arr2[6] = {2, 4,6,3,2,5};
    a.set(arr, 2,3 );
    b.set(arr2,3,2);
    
    printf("a matrix \n");
    a.print_mat();
    printf("b matrix \n");
    b.print_mat();

    matprod(c,a,b); // c = ab

    printf("c = ab \n");
    c.print_mat();

    matprod(c,b,a); // c = ba
    printf("c = ba \n");
    c.print_mat();

    return 1;

6.3 vector dot

    cmf::matrix a,b;
    double arr[6] = {1, 2, 4, 5,6,9};
    double arr2[6] = {2, 4,6,3,2,5};
    a.set(arr, 6,1 );
    b.set(arr2,6,1);
    double c = 0.0;
    printf("%f\n",c);
    c = vdot(a,b); // c = ab
    printf("a dot b = %f\n",c);

    c = vdot(b,a); // c = ba
    printf("b dot a = %f\n",c);

6.6 Determinant

    cmf::matrix A, B, C;
    double arr[6] = {1, 2, 4, 5, 6, 4};
    double arr2[4] = {2, 4,2,5};
    double arr3[9] = {1,2,4, 5,6,1, 2,4,2};
    A.set(arr, 3,2 );
    B.set(arr2,2,2); 
    C.set(arr3,3,3);       
    printf("A matrix \n");
    A.print_mat();
    
    printf("B matrix \n");
    B.print_mat();
    
    printf("C matrix \n");
    C.print_mat();

    printf("Determinant A : %f \n",Det(A));

    printf("Determinant B : %f \n",Det(B));

    printf("Determinant C : %f \n",Det(C));

6.4 matrix LU decomposition

``` c++ 
    matrix a,b,c;
    
```

6.5 matrix Chrolsky decomposition

``` c++ 
    matrix a,b,c;
    
```

6.6 inverse

``` c++ 
    
```

6.6 matrix operator *

    // matrix product
    cmf::matrix a,b,c;
    double arr[4] = {1, 2, 4, 5};
    double arr2[4] = {2, 4,2,5};
    a.set(arr, 2,2 );
    b.set(arr2,2,2);
    
    printf("a matrix \n");
    a.print_mat();
    printf("b matrix \n");
    b.print_mat();

    c = a*b;

    printf("c = ab \n");
    c.print_mat();

    c = b * a;       
    printf("c = ba \n");
    c.print_mat();


    // double * matrix 
    c = 3 * a ;
    print ("c = 3.0 * a \n");
    c.print_mat();

    // matrix * double 
    c = a * 3;
    print ("c = a * 3.0  \n");
    c.print_mat();

    

6.6 matrix operator +

    cmf::matrix a,b,c;
    double arr[6] = {1, 2, 4, 5, 6, 4};
    double arr2[4] = {2, 4,2,5};
    a.set(arr, 3,2 );
    b.set(arr2,2,2);        
    printf("a matrix \n");
    a.print_mat();
    
    printf("b matrix \n");
    b.print_mat();
    
    printf("Determinant A : %f \n",Det(a));

    printf("Determinant B : %f \n",Det(b));

6.6 matrix operator +

    cmf::matrix a,b,c;
    double arr[4] = {1, 2, 4, 5};
    double arr2[4] = {2, 4,2,5};
    a.set(arr, 2,2 );
    b.set(arr2,2,2);        
    printf("a matrix \n");
    a.print_mat();
    printf("b matrix \n");
    b.print_mat();
    c = a;
    printf("c = a");
    c.print_mat();

    c = a +1 ;
    printf("c = a+1");
    c.print_mat();
    
    c = 101 + a ;
    printf("c = 101+a");
    c.print_mat();

6.6 matrix operator == and !=

    cmf::matrix a,b,c;
    double arr[4] = {1, 2, 4, 5};
    double arr2[4] = {2, 4,2,5};
    a.set(arr, 2,2 );
    b.set(arr2,2,2);
    
    printf("a matrix \n");
    a.print_mat();
    printf("b matrix \n");
    b.print_mat();

    c = a ;
    printf("c =a \n"); 
    c.print_mat(); 


    printf("c == a\n");
    if(c==a)
    {
        printf("Same as\n");

    }else
    {
        printf("Not Same\n");
    }

    printf("c == b\n");
    if(c==b)
    {
        printf("Same as\n");

    }else
    {
        printf("Not Same\n");
    }

    printf("c != b\n");
    if(c!=b)
    {
        printf("Not same\n");

    }else
    {
        printf("Same as\n");
    }

run excutible

use following excutible for simple calcuation. and Test.

major function

  1. setting matrix
  2. empty matrix and one matrix
  3. matrix add
  4. matrix substract
  5. matrix multiply
  6. cross product
  7. dot product
  8. LU decomposition
  9. matrix inversion

dd

let's say matrix A in mxn and B in mxn are given as follows, respectively
A= [a_ij] i in m, j in n
B= [b_ij] i in m, j in n
A+B = [a_ij + b_ij]

mat A, B , C;
A = B + C ;

let's say matrix A in lxm and B in mxn are given as follows, respectively
A= [a_ij] i in l, j in m
B= [b_jk] i in m, j in n

then

A*B = [ Sum (a_ij * b_jk) from j=1 to m ], i in l, k in n

Thus



let's say matrix A and B are 3x1 with three components as follows, respectively

A = [a1, a2, a3] B = [b1, b2, b3]

Then the output should be

C = AxB = [a2b3 - a3b2 , -a1b3+a3b1, a1b2-a2b1]

mat A, B , C;
A = B + C ;

Function Call

cmf::matrix() constructor

cmf::~matrix() destructor

cmf::matrix::matrix(double *elm, int nrow, int ncol)

no type variable name description
1 double * elm matrix element data array
2 int nrow number of rows
3 int ncol number of columns

cmf::add(matrix &dst_mat, matrix &A, matrix &B)

no type variable name description
1 matrix & dst_mat output reference
2 matrix & A A matrix
3 matrix & B B matrix
dst_mat = A+ B

cmf::substract(matrix &dst_mat, matrix &A, matrix &B)

no type variable name description
1 matrix & dst_mat output reference
2 matrix & A A matrix
3 matrix & B B matrix
dst_mat = A- B

double cmf::vdot(matrix &A, matrix &B)

no type variable name description
0 double vector_dot output
1 matrix & A 1 x m vector A
2 matrix & B 1 x m vector B
o = A dot B

int cmf::vprod(matrix &dst_mat, matrix &A, matrix &B)

no type variable name description
1 matrix & dst_mat output reference
2 matrix & A A matrix
3 matrix & B B matrix

mathmatical representative
dst_mat = A x B

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages