Facial Recognition
UsePCAtoperformfacialrecognition
 All Classes Files Functions Variables Typedefs Macros
Functions
linalg.c File Reference

File containing common linear algebra functions. More...

#include "linalg.h"
#include "tiff_util.h"
Include dependency graph for linalg.c:

Functions

vectorvector_create (size_t size)
 Creates a vector this function will malloc the exact space for the required dimensions. More...
 
matrixmatrix_create (size_t row, size_t col)
 Creates a matrix this function will malloc the exact space for the required dimensions. More...
 
matrixvec_to_mat (vector *vec, int orientation)
 Converts vector into matrix this function will "cast" the vector into a matrix by using the fact that they both have the same size. when calling this function, calling free() on the matrix will free the vector and vice versa. More...
 
vectormat_to_vec (matrix *mat)
 Converts matrix into vector this function will "cast" the matrix into a vector by using the fact that they both have the same size. when calling this function, calling free() on the vector will free the matrix and vice versa. the input matrix can only be a matrix with dimensions 1 X N (a row matrix) More...
 
void matrix_reshape (matrix *mat, size_t row, size_t col)
 Reshapes the matrix this function will reshape the matrix in constant time. More...
 
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 same size. More...
 
vectorvecscalar_multiply (const vector *vec, const double scalar)
 Performs scalar multiplication of a vector this function will malloc for the user a vector*. More...
 
vectorvecscalar_divide (const vector *vec, const double scalar)
 Performs scalar division of a vector this function will malloc for the user a vector*. More...
 
vectorvecmat_multiply (const vector *vec, const matrix *mat)
 Performs vector matrix multiplication this function will malloc for the user a vector* if the vector is of size M and the matrix size M by N, the resulting column vector will be of size N. More...
 
vectormatvec_multiply (const matrix *mat, const vector *vec)
 Performs matrix vector multiplication this function will malloc for the user a vector* if the matrix is of size M by N and the vector N, the resulting row vector will be of size M. More...
 
void mat_print (const matrix *mat)
 prints the matrix this function will not modify the matrix and print to stdout More...
 
void vec_print (const vector *vec)
 prints the vector this function will not modify the vector and print to stdout More...
 
matrixmatmat_multiply (const matrix *left, const matrix *right)
 Performs standard matrix multiplication this function will malloc for the user a matrix*. More...
 
matrixmatmat_addition (const matrix *left, const matrix *right)
 Performs standard matrix addition this function will malloc for the user a matrix*. More...
 
matrixmatmat_subtraction (const matrix *left, const matrix *right)
 Performs standard matrix subtration this function will malloc for the user a matrix*. More...
 
matrixmatscalar_multiply (const matrix *mat, const double scalar)
 Performs scalar multiplication of a matrix this function will malloc for the user a matrix*. More...
 
matrixmatscalar_divide (const matrix *mat, const double scalar)
 Performs scalar division of a matrix this function will malloc for the user a matrix*. More...
 
matrixmat_transpose (const matrix *mat)
 Performs matrix transpose this function will malloc for the user a matrix*. More...
 
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. More...
 
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 and rot_num it does not malloc but rather requires the caller to malloc space for it. More...
 
void mat_identity (int n, double a[])
 modifies a matrix to be the identity matrix of size n More...
 
void diag_vector (int n, double a[], double v[])
 gets the diagonal entries More...
 
double frobenius_norm (int n, int k, double a[], double x[], double lambda[])
 computes the Frobenius norm in a right eigensystem More...
 
matrixcovmat (matrix *mat)
 computes the variance covariance matrix More...
 
vectorcompute_average (matrix *images, int num_images)
 computes the average matrix of all the *vector images More...
 

Detailed Description

File containing common linear algebra functions.

Minhyuk Park

Date
7 Nov 2017

Function Documentation

vector* compute_average ( matrix images,
int  num_images 
)

computes the average matrix of all the *vector images

Returns
vector* the average image
Parameters
imagesmatrix* a matrix where each column represents an image
num_imagesint number of images in the vector

References MAT, _matrix::row, _vector::size, VEC, and vector_create().

407  {
408  /*
409  matrix* ones_row = matrix_create(1, num_images);
410  for(int i =0; i <num_images; i++) {
411  MAT(ones_row, 0,i) = 1;
412  }
413  matrix* row_images = mat_transpose(images);
414  matrix* row_sum = matmat_multiply(ones_row, row_images);
415  vector* vector_row_sum = mat_to_vec(row_sum);
416  vector* img_avg = vecscalar_divide(vector_row_sum, num_images);
417  free(vector_row_sum);
418  free(row_images);
419  free(ones_row);
420  */
421  vector* img_avg = vector_create(images->row);
422 
423  for(size_t i = 0; i < img_avg->size; i ++) {
424  uint32 avg_pixel = 0;
425 
426  uint32 r = 0;
427  uint32 g = 0;
428  uint32 b = 0;
429  uint32 a = 0;
430  for(int j = 0; j < num_images; j ++) {
431  uint32 current_pixel = MAT(images, i, j);
432 
433  r += ((current_pixel & 0xff) * (current_pixel & 0xff));
434  g += (((current_pixel >> 8) & 0xff) * ((current_pixel >> 8) & 0xff));
435  b += (((current_pixel >> 16) & 0xff) * ((current_pixel >> 16) & 0xff));
436  a += (((current_pixel >> 24) & 0xff) * ((current_pixel >> 24) & 0xff));
437  }
438 
439  r /= num_images;
440  g /= num_images;
441  b /= num_images;
442  a /= num_images;
443 
444  r = sqrt(r);
445  g = sqrt(g);
446  b = sqrt(b);
447  a = sqrt(a);
448  /*
449  for(int j = 0; j < num_images; j ++) {
450  uint32 current_pixel = MAT(images, i, j);
451 
452  r += ((current_pixel & 0xff));
453  g += (((current_pixel >> 8) & 0xff));
454  b += (((current_pixel >> 16) & 0xff));
455  a += (((current_pixel >> 24) & 0xff));
456  }
457 
458  r /= num_images;
459  g /= num_images;
460  b /= num_images;
461  a /= num_images;
462  */
463 
464 
465  avg_pixel |= r;
466  avg_pixel |= (g << 8);
467  avg_pixel |= (b << 16);
468  avg_pixel |= (a << 24);
469 
470  VEC(img_avg, i) = avg_pixel;
471  }
472 
473 
474 
475 
476  return img_avg;
477 }
vector * vector_create(size_t size)
Creates a vector this function will malloc the exact space for the required dimensions.
Definition: linalg.c:11
#define VEC(v, x)
macro for element access in a vector struct
Definition: linalg.h:19
size_t row
the number of rows in the matrix
Definition: linalg.h:42
size_t size
the number of elements in the vector
Definition: linalg.h:29
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
matrix* covmat ( matrix mat)

computes the variance covariance matrix

Returns
matrix* the output variance-covariance matrix this function will malloc a new matrix
Parameters
mata matrix* representing the input matrix of deviation scores of size n by k

References mat_transpose(), matmat_multiply(), matscalar_divide(), and _matrix::row.

397  {
398  size_t n = mat->row;
399  matrix* mat_T = mat_transpose(mat);
400  matrix* x_T_x = matmat_multiply(mat_T, mat);
401  matrix* retmat = matscalar_divide(x_T_x, n);
402  free(x_T_x);
403  free(mat_T);
404  return retmat;
405 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
matrix * mat_transpose(const matrix *mat)
Performs matrix transpose this function will malloc for the user a matrix*.
Definition: linalg.c:181
size_t row
the number of rows in the matrix
Definition: linalg.h:42
matrix * matmat_multiply(const matrix *left, const matrix *right)
Performs standard matrix multiplication this function will malloc for the user a matrix*.
Definition: linalg.c:129
matrix * matscalar_divide(const matrix *mat, const double scalar)
Performs scalar division of a matrix this function will malloc for the user a matrix*.
Definition: linalg.c:176
void diag_vector ( int  n,
double  a[],
double  v[] 
)

gets the diagonal entries

Parameters
nint the dimension
a[]double[] input the matrix, N by N
v[]double[] output the diagonal entries, N

Referenced by eigen().

363  {
364  for(int i = 0; i < n; i ++) {
365  v[i] = a[i + i * n];
366  }
367  return;
368 }
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 same size.

Returns
a double representing the inner product
Parameters
leftconst double* the first vector
rightconst double* the second vector
lengthint the size of the vectors

Referenced by matvec_multiply().

57  {
58  double retval = 0.0;
59  for(int i = 0; i < length; i ++) {
60  retval += (left[i] * right[i]);
61  }
62  return retval;
63 }
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 and rot_num it does not malloc but rather requires the caller to malloc space for it.

Parameters
Nint the dimiension of the input matrix a, which is a N by N matrix
a[]double[] the input matrix which has to be square, real, and symmetric
it_maxint maximum number of iterations to stop at
v[]double[]output matrix of eigenvectors, which is a N by N matrix
d[]double[] output matrix of eigenvalues, in descending order
it_numint* output total number of iterations
rot_numint* output total number of rotations

References diag_vector(), and mat_identity().

212  {
213  mat_identity(n, v); // create the identity matrix using what the caller allocated for us
214  diag_vector(n, a, d); // get the diagonal values of a and store it in caller allocated vector d
215 
216  double* bw = (double*) malloc(n * sizeof(double));
217  double* zw = (double*) malloc(n * sizeof(double));
218 
219  for(int i = 0; i < n; i ++) {
220  bw[i] = d[i];
221  zw[i] = 0.0;
222  }
223  *it_num = 0;
224  *rot_num = 0;
225 
226  double thresh = 0.0;
227  double gapq = 0.0;
228  while(*it_num < it_max) {
229  *it_num += 1;
230  thresh = 0.0;
231  // set up the convergence threshold
232  // based on the size of strict upper triangle
233  for(int i = 0; i < n; i ++) {
234  for(int j = 0; j < i; j ++) {
235  thresh += (a[j + i * n] * a[j + i * n]);
236  }
237  }
238  // PASS 1
239  thresh = sqrt(thresh) / (double)(4 * n);
240  if(thresh == 0.0) {
241  break;
242  }
243  // PASS 2
244  for(int p = 0; p < n; p ++) {
245  for(int q = p + 1; q < n; q ++) {
246  gapq = 10.0 * fabs(a[p + q * n]);
247  double termp = gapq + fabs(d[p]);
248  double termq = gapq + fabs(d[q]);
249  if(4 < *it_num && termp == fabs(d[p]) && termq == fabs(d[q])) {
250  a[p + q * n] = 0.0;
251  // apply rotation otherwise
252  // PASS 3
253  } else if(thresh <= fabs(a[p + q * n])) {
254  double h = d[q] - d[p];
255  double term = fabs(h) + gapq;
256  double t = 0.0;
257  if(term == fabs(h)) {
258  t = a[p + q * n] / h;
259  } else {
260  double theta = 0.5 * h / a[p + q * n];
261  t = 1.0 / (fabs(theta) + sqrt(1.0 + theta * theta));
262  if(theta < 0.0) {
263  t = -t;
264  }
265  }
266  // PASS 4
267  double c = 1.0 / sqrt(1.0 + t * t);
268  double s = t * c;
269  double tau = s / (1.0 + c);
270  h = t * a[p + q * n];
271 
272  zw[p] -= h;
273  zw[q] += h;
274  d[p] -= h;
275  d[q] += h;
276  a[p + q * n] = 0.0;
277  // PASS 5
278  // rotate
279  double g = 0.0;
280  for(int j = 0; j < p; j ++) {
281  g = a[j + p * n];
282  h = a[j + q * n];
283  a[j + p * n] = g - s * (h + g * tau);
284  a[j + q * n] = h + s * (g - h * tau);
285  }
286  for(int j = p + 1; j < q; j ++) {
287  g = a[p + j * n];
288  h = a[j + q * n];
289  a[p + j * n] = g - s * (h + g * tau);
290  a[j + q * n] = h + s * (g - h * tau);
291  }
292  for(int j = q + 1; j < n; j ++) {
293  g = a[p + j * n];
294  h = a[q + j * n];
295  a[p + j * n] = g - s * (h + g * tau);
296  a[q + j * n] = h + s * (g - h * tau);
297  }
298  // PASS 6
299  // store to eigenvector matrix v
300  for(int j = 0; j < n; j ++) {
301  g = v[j + p * n];
302  h = v[j + q * n];
303  v[j + p * n] = g - s * (h + g * tau);
304  v[j + q * n] = h + s * (g - h * tau);
305  }
306  *rot_num += 1;
307  }
308  }
309  }
310  for(int i = 0; i < n; i ++) {
311  bw[i] += zw[i];
312  d[i] = bw[i];
313  zw[i] = 0.0;
314  }
315  }
316  // PASS 7
317  // restore upper triangle
318  for(int i = 0; i < n; i ++) {
319  for(int j = 0; j < i; j ++) {
320  a[j + i * n] = a [i + j * n];
321  }
322  }
323  for(int k = 0; k < n - 1; k ++) {
324  int m = k;
325  for(int l = k + 1; l < n; l ++) {
326  if(d[l] < d[m]) {
327  m = l;
328  }
329  }
330  if(m != k) {
331  double t = d[m];
332  d[m] = d[k];
333  d[k] = t;
334  for(int i = 0; i < n; i ++) {
335  double w = v[i + m * n];
336  v[i + m * n] = v[i + k * n];
337  v[i + k * n] = w;
338  }
339  }
340  }
341  free(bw);
342  free(zw);
343  return;
344 }
void diag_vector(int n, double a[], double v[])
gets the diagonal entries
Definition: linalg.c:363
void mat_identity(int n, double a[])
modifies a matrix to be the identity matrix of size n
Definition: linalg.c:347
double frobenius_norm ( int  n,
int  k,
double  a[],
double  x[],
double  lambda[] 
)

computes the Frobenius norm in a right eigensystem

Returns
double the frobenius norm of A * X - X * lambda
Parameters
nint the dimension of the matrix
kint the number of eigen vectors
a[]double[] input matrix of size n by n
x[]double[] input vector of eigenvectors of size k
lamdba[]double[] input vector of eigen values
371  {
372  double* c = (double*) malloc(n * k * sizeof(double));
373  for(int i = 0; i < k; i ++) {
374  for(int j = 0; j < n; j ++) {
375  c[j + i * n] = 0.0;
376  for(int l = 0; l < n; l ++) {
377  c[j + i * n] = c[j + i * n] + a[j + l * n] * x[l + i * n];
378  }
379  }
380  }
381  for(int i = 0; i < k; i ++) {
382  for(int j = 0; j < n; j ++) {
383  c[j + i * n] = c[j + i * n] - lambda[i] * x[j + i * n];
384  }
385  }
386  double retval = 0.0;
387  for(int i = 0; i < k; i ++) {
388  for(int j = 0; j < n; j ++) {
389  retval += pow(c[j + i * n], 2);
390  }
391  }
392  retval = sqrt(retval);
393  free(c);
394  return retval;
395 }
void mat_identity ( int  n,
double  a[] 
)

modifies a matrix to be the identity matrix of size n

Parameters
nint the dimension
a[]double output identity matrix

Referenced by eigen().

347  {
348  int k = 0;
349  for (int j = 0; j < n; j ++ ) {
350  for (int i = 0; i < n; i ++ ) {
351  if ( i == j ) {
352  a[k] = 1.0;
353  } else {
354  a[k] = 0.0;
355  }
356  k += 1;
357  }
358  }
359  return;
360 }
void mat_print ( const matrix mat)

prints the matrix this function will not modify the matrix and print to stdout

Parameters
matconst matrix* the matrix to be printed

References _matrix::col, MAT, and _matrix::row.

107  {
108  for(size_t i = 0; i < mat->row; i ++) {
109  for(size_t j = 0; j < mat->col; j ++) {
110  if(j == mat->col - 1) {
111  printf("%f ", MAT(mat, i, j));
112  } else {
113  printf("%f, ", MAT(mat, i, j));
114  }
115  }
116  printf("\n");
117  }
118 }
size_t row
the number of rows in the matrix
Definition: linalg.h:42
size_t col
the number of columns in the matrix
Definition: linalg.h:44
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
vector* mat_to_vec ( matrix mat)

Converts matrix into vector this function will "cast" the matrix into a vector by using the fact that they both have the same size. when calling this function, calling free() on the vector will free the matrix and vice versa. the input matrix can only be a matrix with dimensions 1 X N (a row matrix)

Returns
vector* the new pointer for input matrix*
Parameters
matmatrix* to be converted in terms of the newly formed vector

References _matrix::col, and _matrix::row.

43  {
44  assert(mat->row = 1);
45  mat->row = mat->col;
46  mat->col = 1;
47  return (vector*)mat;
48 }
size_t row
the number of rows in the matrix
Definition: linalg.h:42
size_t col
the number of columns in the matrix
Definition: linalg.h:44
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
matrix* mat_transpose ( const matrix mat)

Performs matrix transpose this function will malloc for the user a matrix*.

Returns
matrix* the newly transposed and malloced matrix
Parameters
matconst matrix* the matrix to be transposed

References _matrix::col, MAT, matrix_create(), and _matrix::row.

Referenced by covmat().

181  {
182  matrix* retval = matrix_create(mat->col, mat->row);
183  for (size_t i = 0; i < mat->row; i ++) {
184  for (size_t j = 0; j < mat->col; j ++) {
185  MAT(retval, j, i) = MAT(mat, i, j);
186  }
187  }
188  return retval;
189 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
size_t row
the number of rows in the matrix
Definition: linalg.h:42
matrix * matrix_create(size_t row, size_t col)
Creates a matrix this function will malloc the exact space for the required dimensions.
Definition: linalg.c:22
size_t col
the number of columns in the matrix
Definition: linalg.h:44
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
matrix* matmat_addition ( const matrix left,
const matrix right 
)

Performs standard matrix addition this function will malloc for the user a matrix*.

Returns
matrix* newly malloced result of left plus right
Parameters
leftconst matrix* the matrix to be added to
rightconst matrix* the matrix to be added

References _matrix::col, MAT, matrix_create(), and _matrix::row.

143  {
144  matrix* retval = matrix_create(left->row, right->col);
145  for (size_t i = 0; i < left->row; i ++) {
146  for (size_t j = 0; j < right->col; j ++) {
147  MAT(retval, i, j) = MAT(left, i, j) + MAT(right, i, j);
148  }
149  }
150  return retval;
151 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
size_t row
the number of rows in the matrix
Definition: linalg.h:42
matrix * matrix_create(size_t row, size_t col)
Creates a matrix this function will malloc the exact space for the required dimensions.
Definition: linalg.c:22
size_t col
the number of columns in the matrix
Definition: linalg.h:44
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
matrix* matmat_multiply ( const matrix left,
const matrix right 
)

Performs standard matrix multiplication this function will malloc for the user a matrix*.

Returns
matrix* newly malloced result of left times right
Parameters
leftconst matrix* the matrix to be multiplied to
rightconst matrix* the matrix to be multiplied

References _matrix::col, MAT, matrix_create(), and _matrix::row.

Referenced by covmat().

129  {
130  matrix* retval = matrix_create(left->row, right->col);
131  for(size_t i = 0; i < left->row; i ++) {
132  for(size_t j = 0; j < right->col; j ++) {
133  MAT(retval,i,j) = 0;
134  for(size_t k = 0; k < left->col; k ++) {
135  MAT(retval, i, j) += MAT(left, i, k) * MAT(right, k, j);
136  }
137  }
138  }
139  return retval;
140 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
size_t row
the number of rows in the matrix
Definition: linalg.h:42
matrix * matrix_create(size_t row, size_t col)
Creates a matrix this function will malloc the exact space for the required dimensions.
Definition: linalg.c:22
size_t col
the number of columns in the matrix
Definition: linalg.h:44
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
matrix* matmat_subtraction ( const matrix left,
const matrix right 
)

Performs standard matrix subtration this function will malloc for the user a matrix*.

Returns
matrix* newly malloced result of left minus right
Parameters
leftconst matrix* the matrix to be subtracted from
rightconst matrix* the matrix to be subtrated

References _matrix::col, MAT, matrix_create(), and _matrix::row.

154  {
155  matrix* retval = matrix_create(left->row, right->col);
156  for (size_t i = 0; i < left->row; i ++) {
157  for (size_t j = 0; j < right->col; j ++) {
158  MAT(retval, i, j) = MAT(left, i, j) - MAT(right, i, j);
159  }
160  }
161  return retval;
162 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
size_t row
the number of rows in the matrix
Definition: linalg.h:42
matrix * matrix_create(size_t row, size_t col)
Creates a matrix this function will malloc the exact space for the required dimensions.
Definition: linalg.c:22
size_t col
the number of columns in the matrix
Definition: linalg.h:44
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
matrix* matrix_create ( size_t  row,
size_t  col 
)

Creates a matrix this function will malloc the exact space for the required dimensions.

Returns
matrix* the newly malloced matrix
Parameters
rowsize_t the desired number of rows in the matrix
colsize_t the desired number of columns in the matrix

References _matrix::col, and _matrix::row.

Referenced by mat_transpose(), matmat_addition(), matmat_multiply(), matmat_subtraction(), and matscalar_multiply().

22  {
23  if(row <= 0 || col <= 0) {
24  return NULL;
25  }
26  matrix* retval = malloc((row * col * sizeof(double)) + (2 * sizeof(size_t)));
27  retval->row = row;
28  retval->col = col;
29  return retval;
30 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
size_t row
the number of rows in the matrix
Definition: linalg.h:42
size_t col
the number of columns in the matrix
Definition: linalg.h:44
void matrix_reshape ( matrix mat,
size_t  row,
size_t  col 
)

Reshapes the matrix this function will reshape the matrix in constant time.

Parameters
matmatrix* to be reshaped
rowsize_t the new row
colsize_t the new column

References _matrix::col, and _matrix::row.

51  {
52  mat->row = row;
53  mat->col = col;
54 }
size_t row
the number of rows in the matrix
Definition: linalg.h:42
size_t col
the number of columns in the matrix
Definition: linalg.h:44
matrix* matscalar_divide ( const matrix mat,
const double  scalar 
)

Performs scalar division of a matrix this function will malloc for the user a matrix*.

Returns
matrix* that is the result of element wise division of the matrix by the scalar
Parameters
matconst matrix* the input matrix
scalarconst double the input scalar

References matscalar_multiply().

Referenced by covmat().

176  {
177  return matscalar_multiply(mat, 1/scalar);
178 }
matrix * matscalar_multiply(const matrix *mat, const double scalar)
Performs scalar multiplication of a matrix this function will malloc for the user a matrix*...
Definition: linalg.c:165
matrix* matscalar_multiply ( const matrix mat,
const double  scalar 
)

Performs scalar multiplication of a matrix this function will malloc for the user a matrix*.

Returns
matrix* that is the result of element wise multiplication of the matrix by the scalar
Parameters
matconst matrix* the input matrix
scalarconst double the input scalar

References _matrix::col, MAT, matrix_create(), and _matrix::row.

Referenced by matscalar_divide().

165  {
166  matrix* retval = matrix_create(mat->row, mat->col);
167  for (size_t i = 0; i < mat->row; i++) {
168  for (size_t j = 0; j < mat->col; j++) {
169  MAT(retval,i,j) = MAT(mat,i,j) * scalar;
170  }
171  }
172  return retval;
173 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
size_t row
the number of rows in the matrix
Definition: linalg.h:42
matrix * matrix_create(size_t row, size_t col)
Creates a matrix this function will malloc the exact space for the required dimensions.
Definition: linalg.c:22
size_t col
the number of columns in the matrix
Definition: linalg.h:44
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
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 is of size M by N and the vector N, the resulting row vector will be of size M.

Returns
vector* newly malloced result of matrix vector multiplication
Parameters
matconst matrix* the input matrix
vecconst vector* the input vector

References _matrix::col, _vector::data, _matrix::data, dot_product(), _vector::size, VEC, and vector_create().

95  {
96  if(vec == NULL || mat == NULL) {
97  return NULL;
98  }
99  vector* retval = vector_create(vec->size);
100  for(size_t i = 0; i < retval->size; i++) {
101  VEC(retval,i) = dot_product(((double*)(mat->data) + (mat->col * i)), (double*)vec->data, vec->size);
102  }
103  return retval;
104 }
double data[]
the elements of the vector
Definition: linalg.h:33
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...
Definition: linalg.c:57
vector * vector_create(size_t size)
Creates a vector this function will malloc the exact space for the required dimensions.
Definition: linalg.c:11
#define VEC(v, x)
macro for element access in a vector struct
Definition: linalg.h:19
size_t col
the number of columns in the matrix
Definition: linalg.h:44
double data[]
row wise expansion of the elements in the matrix
Definition: linalg.h:46
size_t size
the number of elements in the vector
Definition: linalg.h:29
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
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.

Parameters
vec_avector** the vector to be realloced
vec_bvector* the vector to be appended and freed thereafter

References _vector::data, _vector::size, and vector_create().

Referenced by tiff_stream_to_vec().

193 {
194  void* saved_vec_a = *vec_a;
195  vector* buffer = vector_create(vec_b->size);
196  memcpy(buffer, vec_b, sizeof(double) * vec_b->size + (2 * sizeof(size_t)));
197  size_t newSize = (*vec_a)->size + buffer->size;
198  void* dest = *vec_a;
199  void* arrv = NULL;
200  arrv = realloc(dest, sizeof(double) * newSize + (2 * sizeof(size_t)));
201  *vec_a = arrv;
202  memcpy((*vec_a)->data + (*vec_a)->size, buffer->data, buffer->size *sizeof(double));
203  (*vec_a)->size = newSize;
204  free(buffer);
205  if(saved_vec_a != vec_b)
206  {
207  free(vec_b);
208  }
209 }
double data[]
the elements of the vector
Definition: linalg.h:33
vector * vector_create(size_t size)
Creates a vector this function will malloc the exact space for the required dimensions.
Definition: linalg.c:11
size_t size
the number of elements in the vector
Definition: linalg.h:29
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
void vec_print ( const vector vec)

prints the vector this function will not modify the vector and print to stdout

Parameters
vecconst vector* the vector to be printed

References _vector::data, and _vector::size.

121  {
122  for(size_t i = 0; i < vec->size; i ++) {
123  printf("%f ", vec->data[i]);
124  }
125  printf("\n");
126 }
double data[]
the elements of the vector
Definition: linalg.h:33
size_t size
the number of elements in the vector
Definition: linalg.h:29
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 they both have the same size. when calling this function, calling free() on the matrix will free the vector and vice versa.

Returns
matrix* the new pointer for input vector*
Parameters
vecvector* to be converted
orientationint 1 is Row-wise and 0 is Column-wise in terms of the newly formed matrix

References _vector::padding, and _vector::size.

33  {
34  if(orientation == 1) {
35  vec->padding = vec->size;
36  vec->size = 1;
37  } else {
38  vec->padding = 1;
39  }
40  return (matrix*)vec;
41 }
represents a matrix between matrix and vector they are able to be casted into one another ...
Definition: linalg.h:40
size_t padding
unused value but important as padding
Definition: linalg.h:31
size_t size
the number of elements in the vector
Definition: linalg.h:29
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 is of size M and the matrix size M by N, the resulting column vector will be of size N.

Returns
vector* newly malloced result of vector matrix multiplication
Parameters
vecconst vector* the input vector
matconst matrix* the input matrix

References _matrix::col, _vector::data, MAT, _matrix::row, _vector::size, VEC, and vector_create().

80  {
81  if(vec == NULL || mat == NULL) {
82  return NULL;
83  }
84  vector* retval = vector_create(vec->size);
85  memset(retval->data, 0, sizeof(double) * vec->size);
86  for(size_t j = 0; j < mat->col; j ++) {
87  for(size_t i = 0; i < mat->row; i ++) {
88  VEC(retval, j) += (VEC(vec, i) * MAT(mat, i, j));
89  }
90  }
91  return retval;
92 }
double data[]
the elements of the vector
Definition: linalg.h:33
vector * vector_create(size_t size)
Creates a vector this function will malloc the exact space for the required dimensions.
Definition: linalg.c:11
#define VEC(v, x)
macro for element access in a vector struct
Definition: linalg.h:19
size_t row
the number of rows in the matrix
Definition: linalg.h:42
size_t col
the number of columns in the matrix
Definition: linalg.h:44
size_t size
the number of elements in the vector
Definition: linalg.h:29
#define MAT(m, x, y)
macro for element access in a matrix struct
Definition: linalg.h:17
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
vector* vecscalar_divide ( const vector vec,
const double  scalar 
)

Performs scalar division of a vector this function will malloc for the user a vector*.

Returns
vector* that is the result of element wise division of the vector by the scalar
Parameters
vecconst vector* the input vector
scalarconst double the input scalar

References vecscalar_multiply().

74  {
75  return vecscalar_multiply(vec, 1 / scalar);
76 }
vector * vecscalar_multiply(const vector *vec, const double scalar)
Performs scalar multiplication of a vector this function will malloc for the user a vector*...
Definition: linalg.c:66
vector* vecscalar_multiply ( const vector vec,
const double  scalar 
)

Performs scalar multiplication of a vector this function will malloc for the user a vector*.

Returns
vector* that is the result of element wise multiplication of the vector by the scalar
Parameters
vecconst vector* the input vector
scalarconst double the input scalar

References _vector::size, VEC, and vector_create().

Referenced by vecscalar_divide().

66  {
67  vector* retval = vector_create(vec->size);
68  for (size_t i = 0; i < retval->size; i ++) {
69  VEC(retval, i) = VEC(vec, i) * scalar;
70  }
71  return retval;
72 }
vector * vector_create(size_t size)
Creates a vector this function will malloc the exact space for the required dimensions.
Definition: linalg.c:11
#define VEC(v, x)
macro for element access in a vector struct
Definition: linalg.h:19
size_t size
the number of elements in the vector
Definition: linalg.h:29
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
vector* vector_create ( size_t  size)

Creates a vector this function will malloc the exact space for the required dimensions.

Returns
vector* the newly malloced vector
Parameters
sizesize_t the desired number of elements in the vecrtor

References _vector::padding, and _vector::size.

Referenced by compute_average(), matvec_multiply(), tiff_to_vec(), vec_append(), vecmat_multiply(), and vecscalar_multiply().

11  {
12  if(size <= 0) {
13  return NULL;
14  }
15  vector* retval = malloc((size * sizeof(double)) + (2 * sizeof(size_t)));
16  retval->size = size;
17  retval->padding = 0;
18  return retval;
19 }
size_t padding
unused value but important as padding
Definition: linalg.h:31
size_t size
the number of elements in the vector
Definition: linalg.h:29
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27