first init
This commit is contained in:
commit
862ec6eba2
20
LICENSE
Normal file
20
LICENSE
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
simple matrix operations c header only library
|
||||||
|
Copyright © 2023 tonitch
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the "Software"),
|
||||||
|
to deal in the Software without restriction, including without limitation
|
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included
|
||||||
|
in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
||||||
|
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
13
README.md
Normal file
13
README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# GMath
|
||||||
|
a header only simple matrix operation in c
|
||||||
|
|
||||||
|
## Quick start
|
||||||
|
put the `gmath.h` in your project directory and add these lines in your project file
|
||||||
|
```c
|
||||||
|
#define GMATH_IMPLEMENTATION
|
||||||
|
#include "gmath.h"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contribution
|
||||||
|
|
||||||
|
The project can be found and issue can be oppened at : https://git.herisson.ovh/tonitch/gmath.h
|
5
build.sh
Executable file
5
build.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -xe
|
||||||
|
|
||||||
|
gcc -g -Wall -Wextra -o main main.c
|
116
gmath.h
Normal file
116
gmath.h
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
#ifndef GMATH_H
|
||||||
|
#define GMATH_H
|
||||||
|
|
||||||
|
#define GMAT_AT(m, r, c) ((m).mat[(r) * (m).width + (c)])
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float *mat;
|
||||||
|
short width;
|
||||||
|
short height;
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
|
Matrix GMatrix_malloc(short width, short height);
|
||||||
|
void GMatrix_fill(Matrix mat, float var);
|
||||||
|
|
||||||
|
Matrix GMatrix_unit(short size);
|
||||||
|
void GMatrix_print(const Matrix mat);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return 0 on success
|
||||||
|
*/
|
||||||
|
void GMatrix_sum(Matrix dest, const Matrix mat);
|
||||||
|
void GMatrix_sub(Matrix dest, const Matrix mat);
|
||||||
|
void GMatrix_scal(Matrix dest, int val);
|
||||||
|
void GMatrix_prod(Matrix dest, const Matrix mat1, const Matrix mat2);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef GMATH_IMPLEMENTATION
|
||||||
|
|
||||||
|
#ifndef GMATH_MALLOC
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define GMATH_MALLOC malloc
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef GMATH_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define GMATH_ASSERT(n) assert((n))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Matrix GMatrix_malloc(short width, short height){
|
||||||
|
float* mat = GMATH_MALLOC(sizeof(float)*width*height);
|
||||||
|
Matrix ret = {.mat=mat, .width=width, .height=height};
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
void GMatrix_fill(Matrix mat, float var){
|
||||||
|
for(int i = 0; i < mat.width * mat.height; i++){
|
||||||
|
mat.mat[i] = var;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix GMatrix_unit(short size){
|
||||||
|
Matrix ret = GMatrix_malloc(size, size);
|
||||||
|
GMatrix_fill(ret, 0);
|
||||||
|
for (int i = 0; i < size; ++i) {
|
||||||
|
GMAT_AT(ret, i, i) = 1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GMatrix_print(const Matrix mat){
|
||||||
|
for (int i = 0; i < mat.height; ++i) {
|
||||||
|
for (int j = 0; j < mat.width; ++j) {
|
||||||
|
printf("%f\t",GMAT_AT(mat, i, j));
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return 0 on su
|
||||||
|
*/
|
||||||
|
void GMatrix_sum(Matrix dest, const Matrix mat){
|
||||||
|
GMATH_ASSERT(dest.width == mat.width);
|
||||||
|
GMATH_ASSERT(dest.height == mat.height);
|
||||||
|
for(int row = 0; row < dest.height; row++){
|
||||||
|
for(int column = 0; column < dest.width; column++){
|
||||||
|
GMAT_AT(dest, row, column) += GMAT_AT(mat, row, column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void GMatrix_sub(Matrix dest, const Matrix mat){
|
||||||
|
GMATH_ASSERT(dest.width == mat.width);
|
||||||
|
GMATH_ASSERT(dest.height == mat.height);
|
||||||
|
for(int row = 0; row < dest.height; row++){
|
||||||
|
for(int column = 0; column < dest.width; column++){
|
||||||
|
GMAT_AT(dest, row, column) -= GMAT_AT(mat, row, column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void GMatrix_scal(Matrix dest, int val){
|
||||||
|
for(int row = 0; row < dest.height; row++){
|
||||||
|
for(int column = 0; column < dest.width; column++){
|
||||||
|
GMAT_AT(dest, row, column) *= val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GMatrix_prod(Matrix dest, const Matrix mat1, const Matrix mat2){
|
||||||
|
GMATH_ASSERT(mat1.width == mat2.height);
|
||||||
|
GMATH_ASSERT(dest.height = mat1.height);
|
||||||
|
GMATH_ASSERT(dest.width = mat2.width);
|
||||||
|
|
||||||
|
for(int row = 0; row < dest.height; row++){
|
||||||
|
for(int column = 0; column < dest.width; column++){
|
||||||
|
GMAT_AT(dest, row, column) = 0;
|
||||||
|
for(int i=0; i < mat1.width; i++){
|
||||||
|
GMAT_AT(dest, row, column) += GMAT_AT(mat1, row, i) * GMAT_AT(mat2, column, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
50
main.c
Normal file
50
main.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#define GMATH_IMPLEMENTATION
|
||||||
|
#include "gmath.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
float matrix_shape[] = {
|
||||||
|
1, 2, 3,
|
||||||
|
4, 5, 6,
|
||||||
|
7, 8, 9,
|
||||||
|
};
|
||||||
|
|
||||||
|
float matrix_shape2[] = {
|
||||||
|
9, 8, 7,
|
||||||
|
6, 5, 4,
|
||||||
|
3, 2, 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
float matrix_shape3[] = {
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
1,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Matrix mat[3] = { */
|
||||||
|
/* {matrix_shape, 3, 3}, */
|
||||||
|
/* {matrix_shape2, 3, 3}, */
|
||||||
|
/* GMatrix_malloc(3,3), */
|
||||||
|
/* }; */
|
||||||
|
|
||||||
|
Matrix mat[3] = {
|
||||||
|
GMatrix_unit(4),
|
||||||
|
{matrix_shape3, 1, 4},
|
||||||
|
GMatrix_malloc(1, 4),
|
||||||
|
};
|
||||||
|
|
||||||
|
GMAT_AT(mat[0], 0, 3) = 1;
|
||||||
|
GMAT_AT(mat[0], 1, 3) = 1;
|
||||||
|
GMAT_AT(mat[0], 2, 3) = 1;
|
||||||
|
|
||||||
|
|
||||||
|
GMatrix_print(mat[0]);
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
GMatrix_print(mat[1]);
|
||||||
|
|
||||||
|
GMatrix_prod(mat[2], mat[0], mat[1]);
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
GMatrix_print(mat[2]);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user