Skip to content

Instantly share code, notes, and snippets.

@Coldsp33d
Last active Jul 2, 2016
Embed
What would you like to do?
Pretty print in NumPy format, any 1D or 2D C matrix (as long as it is stored in 1D form) by specifying dimensions.
#include <stdio.h>
typedef float fpoint_t;
void print(fpoint_t* m, int row_size, int col_size)
{
int i, j;
printf(" array([");
if(row_size == 1 || col_size == 1)
{
int len = row_size == 1? col_size : row_size;
if(col_size == 1)
{
for(i = 0; i < len; i++)
{
if(i == len - 1)
printf("%.2e", m[i]);
else
{
if(m[i] >= 0) printf(" %5.2e, ", m[i]);
else printf("%5.2e, ", m[i]);
if((i + 1) % 6 == 0)
printf("\n\t");
}
}
}
else
{
for(i = 0; i < len; i++)
{
if(i == 0)
printf(" %.2e\n", m[i]);
else if(i == len - 1)
printf("\t %.2e", m[i]);
else
{
if(m[i] >= 0) printf("\t %5.2e,\n", m[i]);
else printf("\t %5.2e,\n", m[i]);
}
}
}
printf(" ])\n");
return;
}
if (row_size > 10)
{
for(i = 0; i < 3; i++)
{
if(i == 0)
printf("[ ");
else
printf("\t[ ");
if(col_size > 10)
{
for(j = 0; j < 3; j++)
{
if(j < 2)
{
if(m[i * col_size + j] >= 0) printf(" %5.4e,\t", m[i * col_size + j]);
else printf("%5.4e,\t", m[i * col_size + j]);
}
else
{
if(m[i * col_size + j] >= 0) printf(" %5.4e, ", m[i * col_size + j]);
else printf("%5.4e, ", m[i * col_size + j]);
}
}
printf("..., ");
if(m[i * col_size + col_size - 3] >= 0)
printf(" %5.4e,\t\n\t", m[i * col_size + col_size - 3]);
else
printf("%5.4e,\t\n\t", m[i * col_size + col_size - 3]);
if(m[i * col_size + col_size - 2] >= 0)
printf(" %5.4e,\t", m[i * col_size + col_size - 2]);
else
printf(" %5.4e,\t", m[i * col_size + i]);
if(m[i * col_size + col_size - 1] >= 0)
printf(" %5.4e", m[i * col_size + col_size - 1]);
else
printf("%5.4e", m[i * col_size + col_size - 1]);
}
else
{
for(j = 0; j < col_size; j++)
{
if(j != col_size - 1)
printf("%.6g, ", m[i * col_size + j]);
else
printf("%.6g", m[i * col_size + j]);
}
}
printf(" ],\n");
}
printf("\t...,\n");
for(i = row_size - 3; i < row_size; i++)
{
printf("\t[ ");
if(col_size > 10)
{
for(j = 0; j < 3; j++)
{
if(j < 2)
{
if(m[i * col_size + j] >= 0) printf(" %5.4e,\t", m[i * col_size + j]);
else printf("%5.4e,\t", m[i * col_size + j]);
}
else
{
if(m[i * col_size + j] >= 0) printf(" %5.4e, ", m[i * col_size + j]);
else printf("%5.4e, ", m[i * col_size + j]);
}
}
printf("..., ");
if(m[i * col_size + col_size - 3] >= 0)
printf(" %5.4e,\t\n\t", m[i * col_size + col_size - 3]);
else
printf("%5.4e,\t\n\t", m[i * col_size + col_size - 3]);
if(m[i * col_size + col_size - 2] >= 0)
printf(" %5.4e,\t", m[i * col_size + col_size - 2]);
else
printf(" %5.4e,\t", m[i * col_size + i]);
if(m[i * col_size + col_size - 1] >= 0)
printf(" %5.4e", m[i * col_size + col_size - 1]);
else
printf("%5.4e", m[i * col_size + col_size - 1]);
}
else
{
for(j = 0; j < col_size; j++)
{
if(j != col_size - 1)
printf("%.6g, ", m[i * col_size + j]);
else
printf("%.6g", m[i * col_size + j]);
}
}
if(i == row_size - 1)
printf(" ]])\n");
else
printf(" ],\n");
}
}
else
{
for(i = 0; i < row_size; i++)
{
if(i == 0)
printf("[ ");
else
printf("\t[ ");
if(col_size > 10)
{
for(j = 0; j < 3; j++)
{
if(j < 2)
{
if(m[i * col_size + j] >= 0) printf(" %5.4e,\t", m[i * col_size + j]);
else printf("%5.4e,\t", m[i * col_size + j]);
}
else
{
if(m[i * col_size + j] >= 0) printf(" %5.4e, ", m[i * col_size + j]);
else printf("%5.4e, ", m[i * col_size + j]);
}
}
printf("..., ");
if(m[i * col_size + col_size - 3] >= 0)
printf(" %5.4e,\t\n", m[i * col_size + col_size - 3]);
else
printf("%5.4e,\t\n", m[i * col_size + col_size - 3]);
if(m[i * col_size + col_size - 2] >= 0)
printf("\t %5.4e,\t", m[i * col_size + col_size - 2]);
else
printf("\t %5.4e,\t", m[i * col_size + i]);
if(m[i * col_size + col_size - 1] >= 0)
printf(" %5.4e", m[i * col_size + col_size - 1]);
else
printf("%5.4e", m[i * col_size + col_size - 1]);
}
else
{
for(j = 0; j < col_size; j++)
{
if(j != col_size - 1)
printf("%.6g, ", m[i * col_size + j]);
else
printf("%.6g", m[i * col_size + j]);
}
}
if(i == row_size - 1)
printf(" ]])\n");
else
printf(" ],\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment