Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A lightweight, high-performance linear algebra library written in C11. Designed

- **Dense Matrix Operations**: Full support for matrix creation, destruction, copying, addition, multiplication, scaling, transposition, inversion, and determinants
- **Dense Vector Operations**: First-class 1-D vector type with arithmetic, dot product, L2 norm, normalization, distance, angle, and Hadamard product
- **Matrix-Vector Operations**: Efficient matrix-vector multiplication, vector-matrix multiplication, and fused affine transforms (Mx + b) for ML workloads
- **C11 Standard Compliance**: Strict adherence to C11 specification with portable code
- **Row-Major Memory Layout**: Contiguous memory storage optimized for sequential access patterns
- **Explicit Memory Management**: Safe allocation and deallocation APIs to prevent leaks
Expand Down Expand Up @@ -206,6 +207,14 @@ int vec_set(Vector *v, size_t index, double value);
void vec_print(const char *label, const Vector v);
```

#### Matrix-Vector Operations

```c
int mat_vec_mul(const Matrix m, const Vector v, Vector *result);
int vec_mat_mul(const Vector v, const Matrix m, Vector *result);
int mat_vec_add(const Matrix m, const Vector v, const Vector *b, Vector *result);
```

For detailed documentation, see the Doxygen comments in `include/linal.h`.

## Use Cases
Expand Down
6 changes: 0 additions & 6 deletions docs/sphinx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ The following packages are required:
- `python-sphinx` - For generating HTML documentation
- `python-breathe` - For integrating Doxygen XML with Sphinx

On Arch Linux, install with:

```bash
sudo pacman -S doxygen python-sphinx python-breathe
```

## Building Documentation

### Quick Start
Expand Down
47 changes: 47 additions & 0 deletions include/linal.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,53 @@ int vec_hadamard(const Vector a, const Vector b, Vector *__restrict__ result);
*/
int vec_abs(const Vector v, Vector *__restrict__ result);

/**
* @brief Multiply a matrix by a vector: result = M * v.
*
* Computes the matrix-vector product:
* @f$ C_i = \sum_j A_{ij} v_j @f$
*
* Requires A.cols == v.size and result.size == A.rows.
*
* @param m Input matrix (m x n)
* @param v Input vector (n)
* @param result Output vector (m)
* @return 0 on success, -1 on dimension mismatch or invalid pointers
*/
int mat_vec_mul(const Matrix m, const Vector v, Vector *__restrict__ result);

/**
* @brief Multiply a vector by a matrix: result = v^T * M.
*
* Computes the vector-matrix product:
* @f$ C_j = \sum_i v_i A_{ij} @f$
*
* Requires v.size == A.rows and result.size == A.cols.
*
* @param v Input vector (m)
* @param m Input matrix (m x n)
* @param result Output vector (n)
* @return 0 on success, -1 on dimension mismatch or invalid pointers
*/
int vec_mat_mul(const Vector v, const Matrix m, Vector *__restrict__ result);

/**
* @brief Fused matrix-vector multiplication and addition: result = M * v + b.
*
* Computes:
* @f$ C_i = (\sum_j A_{ij} v_j) + b_i @f$
*
* Requires A.cols == v.size, A.rows == b.size, and result.size == A.rows.
*
* @param m Input matrix (m x n)
* @param v Input vector (n)
* @param b Input bias vector (m)
* @param result Output vector (m)
* @return 0 on success, -1 on dimension mismatch or invalid pointers
*/
int mat_vec_add(const Matrix m, const Vector v, const Vector *b,
Vector *__restrict__ result);

#ifdef __cplusplus
}
#endif
Expand Down
121 changes: 121 additions & 0 deletions src/linal_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,124 @@ vec_abs(const Vector v, Vector *__restrict__ result)

return 0;
}

/**
* @brief Multiply a matrix by a vector: result = M * v.
*
* Computes the matrix-vector product:
* @f$ C_i = \sum_j A_{ij} v_j @f$
*
* Requires A.cols == v.size and result.size == A.rows.
*
* @param m Input matrix (m x n)
* @param v Input vector (n)
* @param result Output vector (m)
* @return 0 on success, -1 on dimension mismatch or invalid pointers
*/
int
mat_vec_mul(const Matrix m, const Vector v, Vector *__restrict__ result)
{
if (!m.data || !v.data || !result || !result->data || m.cols != v.size
|| m.rows != result->size) {
vec_error("mat_vec_mul", "null data or dimension mismatch");
return -1;
}

if (result->data == m.data || result->data == v.data) {
vec_error("mat_vec_mul", "result aliases input operand");
return -1;
}

for (size_t i = 0; i < m.rows; i++) {
double sum = 0.0;
for (size_t j = 0; j < m.cols; j++) {
sum += m.data[i * m.cols + j] * v.data[j];
}
result->data[i] = sum;
}

return 0;
}

/**
* @brief Multiply a vector by a matrix: result = v^T * M.
*
* Computes the vector-matrix product:
* @f$ C_j = \sum_i v_i A_{ij} @f$
*
* Requires v.size == A.rows and result.size == A.cols.
*
* @param v Input vector (m)
* @param m Input matrix (m x n)
* @param result Output vector (n)
* @return 0 on success, -1 on dimension mismatch or invalid pointers
*/
int
vec_mat_mul(const Vector v, const Matrix m, Vector *__restrict__ result)
{
if (!v.data || !m.data || !result || !result->data || v.size != m.rows
|| m.cols != result->size) {
vec_error("vec_mat_mul", "null data or dimension mismatch");
return -1;
}

if (result->data == v.data || result->data == m.data) {
vec_error("vec_mat_mul", "result aliases input operand");
return -1;
}

/* Initialize result to zero as we are accumulating */
memset(result->data, 0, result->size * sizeof(double));

for (size_t i = 0; i < v.size; i++) {
double factor = v.data[i];
const double *m_row = m.data + i * m.cols;
for (size_t j = 0; j < m.cols; j++) {
result->data[j] += factor * m_row[j];
}
}

return 0;
}

/**
* @brief Fused matrix-vector multiplication and addition: result = M * v + b.
*
* Computes:
* @f$ C_i = (\sum_j A_{ij} v_j) + b_i @f$
*
* Requires A.cols == v.size, A.rows == b.size, and result.size == A.rows.
*
* @param m Input matrix (m x n)
* @param v Input vector (n)
* @param b Input bias vector (m)
* @param result Output vector (m)
* @return 0 on success, -1 on dimension mismatch or invalid pointers
*/
int
mat_vec_add(const Matrix m, const Vector v, const Vector *b,
Vector *__restrict__ result)
{
if (!m.data || !v.data || !b || !b->data || !result || !result->data
|| m.cols != v.size || m.rows != result->size
|| m.rows != b->size) {
vec_error("mat_vec_add", "null data or dimension mismatch");
return -1;
}

if (result->data == m.data || result->data == v.data
|| result->data == b->data) {
vec_error("mat_vec_add", "result aliases input operand");
return -1;
}

for (size_t i = 0; i < m.rows; i++) {
double sum = 0.0;
for (size_t j = 0; j < m.cols; j++) {
sum += m.data[i * m.cols + j] * v.data[j];
}
result->data[i] = sum + b->data[i];
}

return 0;
}
8 changes: 8 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,11 @@ test_vec_workloads = executable(
include_directories: [public_headers, generated_headers],
)
test('vec_workloads', test_vec_workloads, timeout: 120)

test_mat_vec = executable(
'test_mat_vec',
files('test_mat_vec.c'),
dependencies: [linal_dep],
include_directories: [public_headers, generated_headers],
)
test('mat_vec', test_mat_vec)
Loading
Loading