added lina_saveMatrixToStream definition

This commit is contained in:
Raffaele
2022-03-20 23:52:40 +01:00
parent ad674c03ba
commit 251523a949
2 changed files with 53 additions and 4 deletions
+52 -3
View File
@@ -569,13 +569,13 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
/* Function: lina_saveMatrixToStream /* Function: lina_saveMatrixToStream
** **
** Save to the stream [fp] a matrix encoding it as an ** Save to the stream [fp] a matrix [A] encoding it as an
** ASCII sequence in the form: ** ASCII sequence in the form:
** **
** [a b c .. , d e f .. , ..] ** [a b c .. , d e f .. , ..]
** **
** For instance, the 4x4 identity matrix will ** For instance, the 4x4 identity matrix will
** be ancoded as: ** be encoded as:
** **
** [1 0 0 0, 0 1 0 0, 0 0 1 0, 0 0 0 1] ** [1 0 0 0, 0 1 0 0, 0 0 1 0, 0 0 0 1]
** **
@@ -595,4 +595,53 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
** **
** - If the stream [fp] is NULL, then [stdout] is used. ** - If the stream [fp] is NULL, then [stdout] is used.
*/ */
int lina_saveMatrixToStream(FILE *fp, int *width, int *height, char **error); int lina_saveMatrixToStream(FILE *fp, double *A, int width, int height, char **error)
{
assert(A != NULL);
char *dummy;
if (error == NULL)
error = &dummy;
else
*error = NULL;
if (width < 1)
{
// ERROR: The provided width is less than one.
*error = "The provided width is less than one";
return -1;
}
if (height < 1)
{
// ERROR: The provided height is less than one.
*error = "The provided height is less than one";
return -1;
}
if (fp == NULL)
fp = stdout;
putc('[',fp);
for (int i = 0; i < height-1; i++)
{
for (int j = 0; j < width-1; j++)
fprintf(fp, "%f ", A[i*width + j]);
fprintf(fp, "%f, ", A[i*width + width-1]);
}
for (int j = 0; j < width-1; j++)
fprintf(fp, "%f ", A[(height-1)*width + j]);
fprintf(fp, "%f", A[(height-1)*width + width-1]);
putc(']',fp);
return 0;
}
+1 -1
View File
@@ -4,4 +4,4 @@ void lina_add(double *A, double *B, double *C, int m, int n);
void lina_scale(double *A, double *B, double k, int m, int n); void lina_scale(double *A, double *B, double k, int m, int n);
void lina_transpose(double *A, double *B, int m, int n); void lina_transpose(double *A, double *B, int m, int n);
double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **error); double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **error);
int lina_saveMatrixToStream(FILE *fp, int *width, int *height, char **error); int lina_saveMatrixToStream(FILE *fp, double *A, int width, int height, char **error);