-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_utils.h
More file actions
92 lines (73 loc) · 2.48 KB
/
matrix_utils.h
File metadata and controls
92 lines (73 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef MATRIX_UTILS_H
#define MATRIX_UTILS_H
#include <stdio.h>
#include <math.h>
#include "matrix_utils.h"
// These fcns are meant to make it easier to deal with
// matrices in C/C++. We use col major format since that's
// what underlies Lapack.
// returns +/-1 depending upon sign of x
#define SIGN(x) (((x) > 0) - ((x) < 0))
// Macros to extract matrix index and element.
// Matrix is NxN, i = row idx, j = col idx.
// MATRIX_IDX is where col major format is enforced.
#define MATRIX_IDX(N, I, J) (((N)*(I)) + (J))
#define MATRIX_ELEMENT(A, m, n, i, j) A[ MATRIX_IDX(n, i, j) ]
// Min and max macros for scalars.
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
//===========================================================
// This file holds utility functions for dealing with vectors
// and matrices. The idea is to be able to reuse common matrix
// operations. I will name the utils analogously to their names
// in Matlab.
// Note that C matrices are row-major.
namespace xsf {
namespace mathieu {
//-----------------------------------------------------
void print_matrix(const double* A, int m, int n) {
// prints matrix as 2-dimensional tablei -- this is how we
// usually think of matrices.
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("% 10.4e ", MATRIX_ELEMENT(A, m, n, i, j));
}
printf("\n");
}
}
//-----------------------------------------------------
// Stuff to sort a vector.
// Function to swap two elements
void swap(double* a, double* b) {
double temp = *a;
*a = *b;
*b = temp;
}
// Partition function for quicksort
int partition(double *arr, int low, int high) {
double pivot = arr[high]; // Choose last element as pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
// Quicksort function
void quickSort(double *arr, int low, int high) {
if (low < high) {
// Partition the array and get pivot index
int pivotIndex = partition(arr, low, high);
// Recursively sort elements before and after partition
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
} // namespace mathieu
} // namespace xsf
#endif // #ifndef MATRIX_UTILS_H