Neural Network
Generic Neural Network for Classification
linalg.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <math.h>
13 #include <time.h>
14 #include <assert.h>
15 #include <iostream>
16 
18 #define MAT(m, x, y) (m->data[(x * m->col) + y])
19 #define VEC(v, x) (v->data[x])
21 
28 typedef struct _vector {
30  size_t size;
32  size_t padding;
34  double data[];
35  inline friend std::ostream& operator<< (std::ostream &out, const _vector & vector) {
36  for(size_t i = 0; i < vector.size; i ++) {
37  out << (vector.data)[i];
38  out << " ";
39  }
40  out << std::endl;
41  return out;
42  }
43 } vector;
44 
49 typedef struct _matrix {
51  size_t row;
53  size_t col;
55  double data[];
56  inline friend std::ostream& operator<< (std::ostream &out, const _matrix & matrix) {
57  for(size_t i = 0; i < matrix.row; i ++) {
58  for(size_t j = 0; j < matrix.col; j ++) {
59  out << (matrix.data)[(i * matrix.col) + j];
60  out << " ";
61  }
62  out << std::endl;
63  }
64  out << std::endl;
65  return out;
66  }
67 } matrix;
68 
69 
76 vector* vector_create(size_t size);
77 
85 matrix* matrix_create(size_t row, size_t col);
86 
98 matrix* vec_to_mat(vector* vec, int orientation);
99 
111 vector* mat_to_vec(matrix* mat);
112 
120 void matrix_reshape(matrix* mat, size_t row, size_t col);
121 
131 double dot_product(const double* left, const double* right, int length);
132 
141 vector* vecscalar_multiply(const vector* vec, const double scalar);
142 
151  vector* vecscalar_divide(const vector* vec, const double scalar);
152 
153 
163  vector* vecmat_multiply(const vector* vec, const matrix* mat);
164 
174 vector* matvec_multiply(const matrix* mat, const vector* vec);
175 
181 void mat_print(const matrix* mat);
182 
188 void vec_print (const vector* vec);
189 
197 matrix* matmat_multiply(const matrix* left, const matrix* right);
198 
206 matrix* matmat_addition(const matrix* left, const matrix* right);
207 
215 matrix* matmat_subtraction(const matrix* left, const matrix* right);
216 
225 matrix* matscalar_multiply(const matrix* mat, const double scalar);
226 
235 matrix* matscalar_divide(const matrix* mat, const double scalar);
236 
243 matrix* mat_transpose(const matrix* mat);
244 
252 void vec_append(vector** vec_a, vector* vec_b);
253 
266 void eigen(int N, double a[], int it_max, double v[], double d[], int* it_num, int* rot_num);
267 
273 void mat_identity(int n, double a[]);
274 
281 void diag_vector(int n, double a[], double v[]);
282 
292 double frobenius_norm(int n, int k, double a[], double x[], double lambda[]);
293 
300 matrix* covmat(matrix* mat);
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:49
double data[]
the elements of the vector
Definition: linalg.h:34
void vec_print(const vector *vec)
prints the vector this function will not modify the vector and print to stdout
double dot_product(const double *left, const double *right, int length)
Performs dot product on two double* this function will malloc for the user a double arrays must be of...
vector * vecscalar_divide(const vector *vec, const double scalar)
Performs scalar division of a vector this function will malloc for the user a vector*.
matrix * matmat_multiply(const matrix *left, const matrix *right)
Performs standard matrix multiplication this function will malloc for the user a matrix*.
matrix * matrix_create(size_t row, size_t col)
Creates a matrix this function will malloc the exact space for the required dimensions.
struct _matrix matrix
represents a matrix between matrix and vector they are able to be casted into one another ...
size_t row
the number of rows in the matrix
Definition: linalg.h:51
matrix * covmat(matrix *mat)
computes the variance covariance matrix
struct _vector vector
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
matrix * vec_to_mat(vector *vec, int orientation)
Converts vector into matrix this function will "cast" the vector into a matrix by using the fact that...
void vec_append(vector **vec_a, vector *vec_b)
Appends vector b to vector a this function will realloc for the user vector a and free vector b...
void eigen(int N, double a[], int it_max, double v[], double d[], int *it_num, int *rot_num)
Performs Jacobi eigenvalue iteration this function will required the user to pass in non-null it_num ...
matrix * mat_transpose(const matrix *mat)
Performs matrix transpose this function will malloc for the user a matrix*.
void matrix_reshape(matrix *mat, size_t row, size_t col)
Reshapes the matrix this function will reshape the matrix in constant time.
vector * mat_to_vec(matrix *mat)
Converts matrix into vector this function will "cast" the matrix into a vector by using the fact that...
vector * vecscalar_multiply(const vector *vec, const double scalar)
Performs scalar multiplication of a vector this function will malloc for the user a vector*...
void diag_vector(int n, double a[], double v[])
gets the diagonal entries
matrix * matmat_subtraction(const matrix *left, const matrix *right)
Performs standard matrix subtration this function will malloc for the user a matrix*.
vector * vector_create(size_t size)
Creates a vector this function will malloc the exact space for the required dimensions.
void mat_identity(int n, double a[])
modifies a matrix to be the identity matrix of size n
size_t col
the number of columns in the matrix
Definition: linalg.h:53
matrix * matscalar_multiply(const matrix *mat, const double scalar)
Performs scalar multiplication of a matrix this function will malloc for the user a matrix*...
matrix * matscalar_divide(const matrix *mat, const double scalar)
Performs scalar division of a matrix this function will malloc for the user a matrix*.
vector * matvec_multiply(const matrix *mat, const vector *vec)
Performs matrix vector multiplication this function will malloc for the user a vector* if the matrix ...
size_t padding
unused value but important as padding
Definition: linalg.h:32
void mat_print(const matrix *mat)
prints the matrix this function will not modify the matrix and print to stdout
vector * vecmat_multiply(const vector *vec, const matrix *mat)
Performs vector matrix multiplication this function will malloc for the user a vector* if the vector ...
matrix * matmat_addition(const matrix *left, const matrix *right)
Performs standard matrix addition this function will malloc for the user a matrix*.
double frobenius_norm(int n, int k, double a[], double x[], double lambda[])
computes the Frobenius norm in a right eigensystem
double data[]
row wise expansion of the elements in the matrix
Definition: linalg.h:55
size_t size
the number of elements in the vector
Definition: linalg.h:30
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:28