changed curly bracket code style
This commit is contained in:
@@ -1,4 +1,2 @@
|
||||
# Lina
|
||||
Lina (***Lin**ear **A**lgebra*) is a C library that implements common linear algebra operations.
|
||||
|
||||
Note that this is still a work in progress.
|
||||
# Lina, the nice-to-read linear algebra toolkit!
|
||||
Lina (***Lin**ear **A**lgebra*) is a C library that implements common linear algebra operations with the aim to be nice to read!
|
||||
+46
-86
@@ -29,34 +29,26 @@
|
||||
** - This function can never fail.
|
||||
*/
|
||||
void lina_dot(double *A, double *B, double *C, int m, int n, int l)
|
||||
{
|
||||
lina_dot2(A, B, C, 0, 0, 0, m, n, l);
|
||||
}
|
||||
|
||||
void lina_dot2(double *A, double *B, double *C,
|
||||
int As, int Bs, int Cs,
|
||||
int m, int n, int l)
|
||||
{
|
||||
assert(m > 0 && n > 0 && l > 0);
|
||||
assert(As >= 0 && Bs >= 0 && Cs >= 0);
|
||||
assert(A != NULL && B != NULL && C != NULL);
|
||||
assert(A != C && B != C);
|
||||
|
||||
// Iteration over A's rows
|
||||
for(int i = 0; i < m; i++)
|
||||
{
|
||||
for(int i = 0; i < m; i++) {
|
||||
|
||||
// Iteration over B's columns
|
||||
for(int k = 0; k < l; k++)
|
||||
{
|
||||
double pos = 0;
|
||||
for(int k = 0; k < l; k++) {
|
||||
|
||||
double sum = 0;
|
||||
|
||||
// Iteration over the single B column
|
||||
// for executing the product of sum
|
||||
|
||||
for(int j=0; j < n; j++)
|
||||
pos += A[i*(n + As) + j] * B[j*(l + Bs) + k];
|
||||
sum += A[i * n + j] * B[j * l + k];
|
||||
|
||||
C[i*(l + Cs) + k] = pos;
|
||||
C[i * l + k] = sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,8 +122,7 @@ void lina_transpose(double *A, double *B, int m, int n)
|
||||
assert(m > 0 && n > 0);
|
||||
assert(A != NULL && B != NULL);
|
||||
|
||||
if(m == 1 || n == 1)
|
||||
{
|
||||
if(m == 1 || n == 1) {
|
||||
// For a matrix with height or width of 1
|
||||
// row-major and column-major order coincide,
|
||||
// so the stransposition doesn't change the
|
||||
@@ -140,9 +131,9 @@ void lina_transpose(double *A, double *B, int m, int n)
|
||||
|
||||
if(A != B) // Does the copy or the branch cost more?
|
||||
memcpy(B, A, sizeof(A[0]) * m * n);
|
||||
}
|
||||
else if(m == n)
|
||||
{
|
||||
|
||||
} else if(m == n) {
|
||||
|
||||
// Iterate over the upper triangular portion of
|
||||
// the matrix and switch each element with the
|
||||
// corresponding one in the lower triangular portion.
|
||||
@@ -153,15 +144,13 @@ void lina_transpose(double *A, double *B, int m, int n)
|
||||
// is avoided.
|
||||
|
||||
for(int i = 0; i < n; i += 1)
|
||||
for(int j = 0; j < i+1; j += 1)
|
||||
{
|
||||
for(int j = 0; j < i+1; j += 1) {
|
||||
double temp = A[i*n + j];
|
||||
B[i*n + j] = A[j*n + i];
|
||||
B[j*n + i] = temp;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
} else {
|
||||
// Not only the matrix needs to be transposed
|
||||
// assuming the destination matrix is the same
|
||||
// as the source matrix, but the memory representation
|
||||
@@ -182,8 +171,7 @@ void lina_transpose(double *A, double *B, int m, int n)
|
||||
double item = A[1];
|
||||
int next = m;
|
||||
|
||||
while(next != 1)
|
||||
{
|
||||
while(next != 1) {
|
||||
double temp = A[next];
|
||||
B[next] = item;
|
||||
item = temp;
|
||||
@@ -241,12 +229,9 @@ static int scanValue(FILE *fp, char *buffer, int max_length, char first, char *f
|
||||
// Scan the integer portion of
|
||||
// the numeric value and copy it
|
||||
// into the buffer.
|
||||
do
|
||||
{
|
||||
if(n == max_length)
|
||||
{
|
||||
// ERROR: Internal buffer is too small to hold
|
||||
// the representation of this item.
|
||||
do {
|
||||
|
||||
if(n == max_length) {
|
||||
*error = "Internal buffer is too small to hold "
|
||||
"the representation of a numeric value";
|
||||
return 0;
|
||||
@@ -255,8 +240,8 @@ static int scanValue(FILE *fp, char *buffer, int max_length, char first, char *f
|
||||
buffer[n++] = c;
|
||||
|
||||
c = getc(fp);
|
||||
}
|
||||
while(c != EOF && isdigit(c));
|
||||
|
||||
} while(c != EOF && isdigit(c));
|
||||
|
||||
// Did the integer part end with
|
||||
// a dot?
|
||||
@@ -265,10 +250,8 @@ static int scanValue(FILE *fp, char *buffer, int max_length, char first, char *f
|
||||
// Now scan and copy the decimal
|
||||
// part of the numeric value if
|
||||
// a dot was found.
|
||||
if(dot)
|
||||
{
|
||||
if(n == max_length)
|
||||
{
|
||||
if(dot) {
|
||||
if(n == max_length) {
|
||||
// ERROR: Internal buffer is too small to hold
|
||||
// the representation of this item.
|
||||
// (The dot doesn't fit.)
|
||||
@@ -281,18 +264,15 @@ static int scanValue(FILE *fp, char *buffer, int max_length, char first, char *f
|
||||
|
||||
c = getc(fp);
|
||||
|
||||
if(!isdigit(c))
|
||||
{
|
||||
if(!isdigit(c)) {
|
||||
// ERROR: Got something other than a
|
||||
// digit after the dot.
|
||||
*error = "Got something other than a digit after the dot.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
if(n == max_length)
|
||||
{
|
||||
do {
|
||||
if(n == max_length) {
|
||||
// ERROR: Internal buffer is too small
|
||||
// to hold the representation of
|
||||
// this item.
|
||||
@@ -304,8 +284,7 @@ static int scanValue(FILE *fp, char *buffer, int max_length, char first, char *f
|
||||
buffer[n++] = c;
|
||||
|
||||
c = getc(fp);
|
||||
}
|
||||
while(c != EOF && isdigit(c));
|
||||
} while(c != EOF && isdigit(c));
|
||||
}
|
||||
|
||||
buffer[n] = '\0';
|
||||
@@ -377,16 +356,14 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
while(c != EOF && isspace(c))
|
||||
c = getc(fp);
|
||||
|
||||
if(c == EOF)
|
||||
{
|
||||
if(c == EOF) {
|
||||
// ERROR: Stream ended before a matrix was
|
||||
// found.
|
||||
*error = "Stream ended before a matrix was found";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(c != '[')
|
||||
{
|
||||
if(c != '[') {
|
||||
// ERROR: Was expected a '[' as the first
|
||||
// character of a matrix, but got
|
||||
// something else instead.
|
||||
@@ -401,8 +378,7 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
while(c != EOF && isspace(c))
|
||||
c = getc(fp);
|
||||
|
||||
if(c == EOF)
|
||||
{
|
||||
if(c == EOF) {
|
||||
// ERROR: Stream ended where a numeric value
|
||||
// was expected.
|
||||
*error = "Stream ended where a numeric value "
|
||||
@@ -412,8 +388,7 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
|
||||
double *matrix = malloc(sizeof(matrix[0]) * 64);
|
||||
|
||||
if(matrix == NULL)
|
||||
{
|
||||
if(matrix == NULL) {
|
||||
// ERROR: Insufficient memory.
|
||||
*error = "Insufficient memory";
|
||||
return NULL;
|
||||
@@ -423,10 +398,8 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
w = -1, i = 0, j = 0;
|
||||
|
||||
if(c != ']')
|
||||
while(1)
|
||||
{
|
||||
if(!isdigit(c))
|
||||
{
|
||||
while(1) {
|
||||
if(!isdigit(c)) {
|
||||
// ERROR: Got something other than a digit
|
||||
// where a numeric value was expected.
|
||||
*error = "Got something other than a numeric "
|
||||
@@ -451,14 +424,12 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
assert(res == 1 || res == -1);
|
||||
|
||||
// Make sure the matrix has enough space.
|
||||
if(size == capacity)
|
||||
{
|
||||
if(size == capacity) {
|
||||
int new_capacity = capacity * 2;
|
||||
|
||||
double *temp = realloc(matrix, sizeof(double) * new_capacity);
|
||||
|
||||
if(temp == NULL)
|
||||
{
|
||||
if(temp == NULL) {
|
||||
// ERROR: Insufficient memory.
|
||||
*error = "Insufficient memory";
|
||||
free(matrix);
|
||||
@@ -478,8 +449,7 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
else
|
||||
casted = strtod(buffer, NULL);
|
||||
|
||||
if(errno)
|
||||
{
|
||||
if(errno) {
|
||||
// ERROR: Failed to convert a numeric value
|
||||
// from it's string form to a numeric
|
||||
// variable.
|
||||
@@ -495,23 +465,20 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
while(c != EOF && isspace(c))
|
||||
c = getc(fp);
|
||||
|
||||
if(c == ']' || c == ',')
|
||||
{
|
||||
if(c == ']' || c == ',') {
|
||||
// The matrix's row just ended.
|
||||
|
||||
if(w == -1)
|
||||
// This was the first row.
|
||||
w = i;
|
||||
else
|
||||
{
|
||||
else {
|
||||
// This wasn't the first row,
|
||||
// so it's possible that it's
|
||||
// length is different from the
|
||||
// previous ones.
|
||||
assert(w > -1);
|
||||
|
||||
if(i != w)
|
||||
{
|
||||
if(i != w) {
|
||||
// ERROR: The j-th row has the wrong
|
||||
// number of elements.
|
||||
if(i < w)
|
||||
@@ -535,8 +502,7 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
c = getc(fp);
|
||||
}
|
||||
|
||||
if(c == EOF)
|
||||
{
|
||||
if(c == EOF) {
|
||||
// ERROR: Stream ended inside a matrix, where
|
||||
// either ',', ']' or a numeric value was
|
||||
// expected.
|
||||
@@ -546,8 +512,7 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
}
|
||||
}
|
||||
|
||||
if(size == 0)
|
||||
{
|
||||
if(size == 0) {
|
||||
free(matrix);
|
||||
*error = "Empty matrix";
|
||||
return NULL;
|
||||
@@ -559,8 +524,8 @@ double *lina_loadMatrixFromStream(FILE *fp, int *width, int *height, char **erro
|
||||
// build the matrix.
|
||||
int fragm_threshold = 30; // (It's a percentage)
|
||||
|
||||
if(100.0 * size/capacity < fragm_threshold)
|
||||
{
|
||||
if(100.0 * size/capacity < fragm_threshold) {
|
||||
|
||||
int new_capacity = (size == 0) ? 1 : size;
|
||||
|
||||
double *temp = realloc(matrix, new_capacity * sizeof(double));
|
||||
@@ -613,14 +578,12 @@ int lina_saveMatrixToStream(FILE *fp, double *A, int width, int height, char **e
|
||||
else
|
||||
*error = NULL;
|
||||
|
||||
if (width < 1)
|
||||
{
|
||||
if (width < 1) {
|
||||
*error = "The provided width is less than one";
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (height < 1)
|
||||
{
|
||||
if (height < 1) {
|
||||
*error = "The provided height is less than one";
|
||||
return -1;
|
||||
}
|
||||
@@ -630,13 +593,11 @@ int lina_saveMatrixToStream(FILE *fp, double *A, int width, int height, char **e
|
||||
|
||||
putc('[',fp);
|
||||
|
||||
for (int i = 0; i < height-1; i++)
|
||||
{
|
||||
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++)
|
||||
@@ -666,8 +627,7 @@ void lina_conv(double *A, double *B, double *C,
|
||||
|
||||
// Iterate over each pixel of the result matrix..
|
||||
for(int j = 0; j < Ch; j += 1)
|
||||
for(int i = 0; i < Cw; i += 1)
|
||||
{
|
||||
for(int i = 0; i < Cw; i += 1) {
|
||||
// ..and calculate it's value as
|
||||
// the scalar product between the
|
||||
// mask B and a portion of A.
|
||||
|
||||
Reference in New Issue
Block a user