GithubHelp home page GithubHelp logo

cmatrix's Introduction

cmatrix

A library for calculating vectors and two-dimensional matrices.

C99 Standard.

REFERENCE

variable etc
typedef struct mat {int *arr--array, int r--row, int c-columm}
functions parm desc
void print (mat*) Print content of matrix.
mat *add (mat*, mat*) Add two matrix and return matrix pointer.
mat *sub (mat*, mat*) Sub two matrix and return matrix pointer.
mat *mul (mat*, mat*) Multiply two matrix and return matrix poitner.
mat *mul_s (double n, mat*) Scalar product.
mat *mat_init (int r, int c) Initialize matrix to zero.
mat *mat_init_f (char*) Initialize matrix using data read from file and return matrix pointer.
mat *mat_init_a (double*arr, int r, int c) Initialize matrix using given 1d-array, row, columm and return matrix pointer.
mat *mat_init_r (int r, int c, long int seed) Initialize matrix using random number that based on gaussian-distribution.
int reshape (mat *,int r,int c) Change the form of the matrix according to the given values of r and c. If success return 1, else return 0.
int save (char *,mat *) Save matrix to file if success return 1, else return 0.
void resize (mat *,int r,int c) Change (force) the form of the matrix according to the given values of r and c.
mat *mat_copy (mat *src) Copy matrix src to dst and return dst
void mat_free (int argc, ...) Release memory of matrix pointers in ...

FILEFORMAT

In the first line, enter values for rows and columns,

and separate each number with a space to create a matrix.

2 4
3 421 5 2
55 12 3 4

INSTRUCTIONS

FILL ZERO

#include "matrix.h"
int main(){
	mat *matrix = mat_init(3,4);
	print(matrix);

	/*OUTPUT
	0.000,      0.000,      0.000,      0.000,
   	0.000,      0.000,      0.000,      0.000,
    	0.000,      0.000,      0.000,      0.000,
	*/

	mat_free(1,matrix);
}

FROM FILE

/*my_file.txt
  2 4
  3 421 5 2
  55 12 3 4
*/

#include "matrix.h"
int main(){
	mat *matrix = mat_init_f("my_file.txt");
	print(matrix);
	
	/*OUTPUT
	3.000,    421.000,      5.000,      2.000,
    	55.000,     12.000,      3.000,      4.000,
	*/

	mat_free(1,matrix);
}

FROM 1D-ARRAY

#inclue "matrix.h"

double array[] = {2.4, 5.1, 22.2, 3.6, 1.0, 2.0, 4.0, 2.0, 1.0};
int main(){
	mat *matrix = mat_init_a(array,3,3);
	/*!!The number of elements in the array and the r*c value must match!!*/
	print(matrix);
	
	/*OUTPUT
	3.000,      4.000,      2.000,
    	1.000,      2.000,      5.000,
    	2.000,      1.000,      2.000,
	*/

	mat_free(1,matrix);

}

FILL RANDOM NUMBER

#include <time.h> //This required to srand(time(NULL))

#include "matrix.h"
int main(){
	mat *matrix = mat_init_r(4,2,time(NULL));
	print(matrix);

	/*OUTPUT
	1.642,     -1.327,
     	0.738,      0.884,
    	-0.313,      0.324,
    	-0.658,      0.873,
	*/

	mat_free(1,matrix);
}

BUILD

gcc main.c matrix.c -lm

# If u prefer the library than raw build
gcc -fPIC -c matrix.c
gcc -shared -o libmatrix.so matrix.o

#Then

gcc main.c -lmatrix -lm
./a.out

SIMPLE EXAMPLE

#include <stdio.h>
#include <time.h>
#include <matrix.h> //libmatrix.so

int main(){
	mat *m1 = mat_init_r(3,10,time(NULL));
	mat *m2 = mat_init_r(10,7,time(NULL));
	mat *result = mul(m1,m2);
	print(result);
	/*OUTPUT
	-3.672,     -3.639,      1.784,      0.039,      6.257,      3.690,     -2.566,
    	-2.228,      2.148,     -3.768,     -1.955,      0.138,      0.263,     -1.658,
     	2.780,     -2.793,      2.390,      4.821,      1.379,     -5.925,      7.295,
	*/

	mat_free(3,m1,m2,result);
}

cmatrix's People

Contributors

arching3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.