Arrays and Memory¶
Overview¶
These are utilities for plain C arrays of numbers: swapping, finding the
largest entry, scaled addition (axpy, \(\underline{y} \leftarrow a
\underline{x} + \underline{y}\)), linear interpolation, and comparing two arrays
within a tolerance (allclose, useful in tests). They are in
ksl/array.h, which ksl/ksl.h includes.
Two-dimensional arrays. ksl/memory.h, which must be included
separately, allocates matrices whose entries can be written A[i][j]. Each
array is an array of row pointers into one contiguous block of
rows * columns values stored row by row:
double** A = ksl_array2D_alloc(3, 4); /* 3 rows, 4 columns, zeroed */
A[1][2] = 5.0;
/* A[0] is the whole matrix in row major order, so it can be passed to
the _rmo linear algebra functions: A[0][1 * 4 + 2] == 5.0 */
ksl_array2D_free(A);
Free each array with the free function for its type, which releases both
the row pointers and the data block.
Array utilities¶
swap two doubles |
|
swap two floats |
|
swap two integers |
|
Swap the entries of two strided arrays. |
|
Return the index of the entry with the largest absolute value in an array. |
|
y += a * x for double precision arrays with unit stride |
|
y += a * x for single precision arrays with unit stride |
|
used to determine if two double arrays are close to each other within an error tolerance of 1e-6 |
|
used to determine if two single precision arrays are close to each other within an error tolerance of 1e-6 |
|
used to determine if two int arrays are equal to each other |
|
linear interpolation |
|
linear interpolation |
-
static inline void ksl_swap(double *a, double *b)¶
swap two doubles
-
static inline void ksl_swapf(float *a, float *b)¶
swap two floats
-
static inline void ksl_swapi(int *a, int *b)¶
swap two integers
-
static inline void ksl_swapArray(const int n, double *a, const int a_stride, double *b, const int b_stride)¶
Swap the entries of two strided arrays.
Swaps a[i * a_stride] with b[i * b_stride] for i = 0 to n-1, so it can swap rows or columns of a matrix.
- Parameters:
n – [in] number of entries to swap
a – [in,out] first array
a_stride – [in] distance between consecutive entries of a
b – [in,out] second array
b_stride – [in] distance between consecutive entries of b
-
static inline int ksl_maxIndex(const int n, const double *a)¶
Return the index of the entry with the largest absolute value in an array.
- Parameters:
n – [in] number of entries, at least 1
a – [in] array of n entries
- Returns:
index of the first entry with the largest absolute value
-
static inline void ksl_axpy(const int n, const double a, const double *x, double *y)¶
y += a * x for double precision arrays with unit stride
-
static inline void ksl_axpyf(const int n, const float a, const float *x, float *y)¶
y += a * x for single precision arrays with unit stride
-
bool ksl_allclose(const int count, const double *a1, const double *a2)¶
used to determine if two double arrays are close to each other within an error tolerance of 1e-6
- Parameters:
count – [in] number of entries in array
a1 – [in] first double array
a2 – [in] second double array
- Returns:
true if all pairs are close, false at the first pair not close
-
bool ksl_allclosef(const int count, const float *a1, const float *a2)¶
used to determine if two single precision arrays are close to each other within an error tolerance of 1e-6
- Parameters:
count – [in] number of entries in array
a1 – [in] first float array
a2 – [in] second float array
- Returns:
true if all pairs are close, false at the first pair not close
-
static inline bool ksl_allclosei(const int count, const int *a1, const int *a2)¶
used to determine if two int arrays are equal to each other
- Parameters:
count – [in] number of entries in array
a1 – [in] first int array
a2 – [in] second int array
- Returns:
true if all pairs are close, false at the first pair not close
-
static inline void ksl_arraylerp(const int count, const double p, const double *y1i, const double *y2i, double *yo)¶
linear interpolation
- Parameters:
count – [in] number of entries to interpolate
p – [in] interpolant value, ranges from 0 to 1
y1i – [in] value of function at p = 0
y2i – [in] value of function at p = 1
yo – [out] interpolated data returned in yo
-
static inline void ksl_arraylerpf(const int count, const float p, const float *y1i, const float *y2i, float *yo)¶
linear interpolation
- Parameters:
count – [in] number of entries to interpolate
p – [in] interpolant value, ranges from 0 to 1
y1i – [in] value of function at p = 0
y2i – [in] value of function at p = 1
yo – [out] interpolated data returned in yo
Two-dimensional arrays¶
The alloc functions return an array of row pointers, so that elements can
be accessed as A[i][j]. Free each array with the matching free
function.
Allocates memory for a two dimensional double array. |
|
Frees memory for a two dimensional double array. |
|
Allocates memory for a two dimensional single precision array. |
|
Frees memory for a two dimensional float array. |
|
Allocates memory for a two dimensional integer array. |
|
Free memory for a two dimensional integer array. |
|
Allocates memory for a two dimensional integer array. |
|
Free memory for a one dimensional unsigned integer array. |
|
Allocates memory for a two dimensional char array. |
|
Frees memory for a two dimensional double array. |
|
Allocates memory for a two dimensional array of booleans. |
|
Frees memory for a two dimensional array of booleans. |
-
double **ksl_array2D_alloc(int rows, int columns)¶
Allocates memory for a two dimensional double array.
- Parameters:
rows – [in] number of rows to allocate
columns – [in] number of columns to allocate
- Returns:
two dimensional double array
-
void ksl_array2D_free(double **x)¶
Frees memory for a two dimensional double array.
-
float **ksl_array2Df_alloc(int rows, int columns)¶
Allocates memory for a two dimensional single precision array.
- Parameters:
rows – [in] number of rows to allocate
columns – [in] number of columns to allocate
- Returns:
two dimensional double array
-
void ksl_array2Df_free(float **x)¶
Frees memory for a two dimensional float array.
-
int **ksl_array2Di_alloc(int rows, int columns)¶
Allocates memory for a two dimensional integer array.
- Parameters:
rows – [in] number of rows to allocate
columns – [in] number of columns to allocate
- Returns:
two dimensional integer array
-
void ksl_array2Di_free(int **x)¶
Free memory for a two dimensional integer array.
-
unsigned int **ksl_array2Dui_alloc(int rows, int columns)¶
Allocates memory for a two dimensional integer array.
- Parameters:
rows – [in] number of rows to allocate
columns – [in] number of columns to allocate
- Returns:
two dimensional integer array
-
void ksl_array2Dui_free(unsigned int **x)¶
Free memory for a one dimensional unsigned integer array.
-
char **ksl_array2Dc_alloc(int rows, int columns)¶
Allocates memory for a two dimensional char array.
- Parameters:
rows – [in] number of rows to allocate
columns – [in] number of columns to allocate
- Returns:
two dimensional char array
-
void ksl_array2Dc_free(char **x)¶
Frees memory for a two dimensional double array.
-
bool **ksl_array2Db_alloc(int rows, int columns)¶
Allocates memory for a two dimensional array of booleans.
- Parameters:
rows – [in] number of rows to allocate
columns – [in] number of columns to allocate
- Returns:
two dimensional char array
-
void ksl_array2Db_free(bool **x)¶
Frees memory for a two dimensional array of booleans.
One-dimensional arrays¶
Free memory for a one dimensional double array. |
|
Free memory for a one dimensional float array. |
|
Free memory for a one dimensional integer array. |
|
Free memory for a one dimensional unsigned integer array. |
|
Free memory for a one dimensional boolean array. |
|
Free memory for a one dimensional boolean array. |
-
void ksl_array_free(double *x)¶
Free memory for a one dimensional double array.
-
void ksl_arrayf_free(float *x)¶
Free memory for a one dimensional float array.
-
void ksl_arrayi_free(int *x)¶
Free memory for a one dimensional integer array.
-
void ksl_arrayui_free(unsigned int *x)¶
Free memory for a one dimensional unsigned integer array.
-
void ksl_arrayc_free(char *x)¶
Free memory for a one dimensional boolean array.
-
void ksl_arrayb_free(bool *x)¶
Free memory for a one dimensional boolean array.