Skip to content

Commit

Permalink
fixed: nml_mat_concath and nml_mat_concath_va functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nomemory committed Jan 10, 2021
1 parent 953b933 commit 9090777
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
33 changes: 33 additions & 0 deletions examples/concatenate_matrices.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdio.h>

#include "lib/nml.h"

int main(int argc, char *argv[]) {
nml_mat *I = nml_mat_id(3);
nml_mat *Ix2 = nml_mat_scalarmult(I, 2.0);
// Concatenate using a pointer
nml_mat **ms = malloc(sizeof(*ms) * 2);
ms[0] = I;
ms[1] = Ix2;
nml_mat *concats1 = nml_mat_concath(2, ms);
//Concatenates using variable arguments list
nml_mat *concats2 = nml_mat_concath_va(3, I, Ix2, I);

printf("I=\n");
nml_mat_print(I);
printf("Ix2=\n");
nml_mat_print(Ix2);
printf("concats1=\n");
nml_mat_print(concats1);
printf("concats2=\n");
nml_mat_print(concats2);

free(ms);
nml_mat_free(I);
nml_mat_free(Ix2);
nml_mat_free(concats1);
nml_mat_free(concats2);

return 0;
}
54 changes: 24 additions & 30 deletions nml.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,71 +447,65 @@ int nml_mat_swapcols_r(nml_mat *m, unsigned int col1, unsigned int col2) {
return 1;
}

nml_mat *nml_mat_concath(unsigned int mnun, nml_mat **matrices) {
return NULL;
}

// Concatenates a variable number of matrices into one
// The concentation is done horizontally this means the matrices need to have
// the same number of rows, while the number of columns is allowed to
// be variable
nml_mat *nml_mat_concath_va(unsigned int mnum, ...) {
nml_mat *nml_mat_concath(unsigned int mnum, nml_mat **marr) {
if (0==mnum) {
return NULL;
}
va_list argp;
va_start(argp, mnum);
nml_mat * fm = va_arg(argp, nml_mat*);
if (1==mnum) {
// We just return the one matrix supplied as the first param
// no need for additional logic
va_end(argp);
return nml_mat_cp(fm);
return nml_mat_cp(marr[0]);
}
// We compute to see if the matrices have the same number of rows
// We fillup the array containing the matrices from the varags
// We calculate the total number of columns to know how to allocate memory
// for the resulting matrix]
int i,j,k,jk,offset;
int i,j,k,offset;
unsigned int lrow, ncols;
nml_mat **marr;
marr = malloc(sizeof(*marr) * mnum);
marr[0] = fm;
lrow = fm->num_rows;
ncols = fm->num_cols;
lrow = marr[0]->num_rows;
ncols = marr[0]->num_cols;
for(k = 1; k < mnum; k++) {
marr[k] = va_arg(argp, nml_mat*);
if (lrow!=marr[k]->num_rows) {
NML_FERROR(CANNOT_CONCATENATE_H, lrow, marr[k]->num_rows);
free(marr);
return NULL;
}
ncols+=marr[k]->num_cols;
}
va_end(argp);

// At this point we know how the resulting matrix looks like,
// we allocate memory for it accordingly
nml_mat *r = nml_mat_new(lrow, ncols);
nml_mat_print(r);
for(i = 0; i < r->num_rows; i++) {
k = 0;
offset = 0;
for(j = 0; j < r->num_cols; j++) {
jk = j - offset;
// If the column index of marr[k] overflows
// We jump to the next matrix in the array
if (jk >= marr[k]->num_cols) {
offset = marr[k]->num_cols;
if (j-offset == marr[k]->num_cols) {
offset += marr[k]->num_cols;
k++;
}
r->data[i][j] = marr[k]->data[i][jk];
r->data[i][j] = marr[k]->data[i][j - offset];
}
}
free(marr);
return r;
}

// Concatenates a variable number of matrices into one
// The concentation is done horizontally this means the matrices need to have
// the same number of rows, while the number of columns is allowed to
// be variable
nml_mat *nml_mat_concath_va(unsigned int mnum, ...) {
nml_mat **marr = malloc(sizeof(*marr) * mnum);
va_list argp;
va_start(argp, mnum);
int k;
for(k = 0; k < mnum; k++) {
marr[k] = va_arg(argp, nml_mat*);
}
va_end(argp);
return nml_mat_concath(mnum, marr);
}

// Concatenates a variable number of matrices into one.
// The concentation is done vertically this means the matrices need to have
// the same number of columns, while the number of rows is allowed to
Expand Down

0 comments on commit 9090777

Please sign in to comment.