Skip to content

Commit

Permalink
Cleaned up some warnings and added a SHOW_MATRIX macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
henryfo committed Feb 8, 2020
1 parent f76ca96 commit 23f248e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
IDIR =./inc
CC=gcc
CFLAGS=-I$(IDIR) -std=c99 -pedantic-errors
CFLAGS=-I$(IDIR) -std=c99 -pedantic-errors -fsanitize=address -fno-omit-frame-pointer -fsanitize=leak -fsanitize=undefined -fsanitize=bounds -fsanitize=null -Wall -Wextra -Wpedantic

ODIR =./obj
LDIR =./lib
Expand Down
54 changes: 30 additions & 24 deletions src/polyfit.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <stdbool.h> // bool
#include "polyfit.h"

// Define SHOW_MATRIX to display intermediate matrix values:
// #define SHOW_MATRIX 1

// Structure of a matrix.
typedef struct matrix_s
Expand All @@ -21,14 +23,26 @@ typedef struct matrix_s
// MACRO to access a value with a matrix.
#define MATRIX_VALUE_PTR( pA, row, col ) (&(((pA)->pContents)[ (row * (pA)->cols) + col]))

#ifdef SHOW_MATRIX
#define showMatrix( x ) do {\
printf( " @%d: " #x " =\n", __LINE__ ); \
reallyShowMatrix( x ); \
printf( "\n" ); \
} while( 0 )
#else // SHOW_MATRIX
#define showMatrix( x )
#endif // SHOW_MATRIX


//------------------------------------------------
// Private Function Prototypes
//------------------------------------------------

static matrix_t * createMatrix( int rows, int cols );
static void destroyMatrix( matrix_t *pMat );
static void showMatrix( matrix_t *pMat );
#ifdef SHOW_MATRIX
static void reallyShowMatrix( matrix_t *pMat );
#endif // SHOW_MATRIX
static matrix_t * createTranspose( matrix_t *pMat );
static matrix_t * createProduct( matrix_t *pLeft, matrix_t *pRight );

Expand Down Expand Up @@ -66,7 +80,6 @@ static matrix_t * createProduct( matrix_t *pLeft, matrix_t *pRight );
int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCount, double *coefficientResults )
{
int rVal = 0;
int i;
int degree = coefficientCount - 1;

// Check that the input pointers aren't null.
Expand Down Expand Up @@ -105,8 +118,7 @@ int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCo
}
}

// printf( "matA = \n");
// showMatrix( pMatA );
showMatrix( pMatA );

// Make the b matrix
matrix_t *pMatB = createMatrix( pointCount, 1);
Expand All @@ -120,18 +132,14 @@ int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCo
*(MATRIX_VALUE_PTR(pMatB, r, 0)) = yValues[r];
}

// printf( "matB = \n");
// showMatrix( pMatB );

// Make the transpose of matrix A
matrix_t * pMatAT = createTranspose( pMatA );
if( NULL == pMatAT )
{
return -3;
}

// printf( "matAT = \n");
// showMatrix( pMatAT );
showMatrix( pMatAT );

// Make the product of matrices AT and A:
matrix_t *pMatATA = createProduct( pMatAT, pMatA );
Expand All @@ -140,17 +148,16 @@ int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCo
return -3;
}

// printf( "(matAT * matA) =\n");
// showMatrix( pMatATA );
showMatrix( pMatATA );

// Make the product of matrices AT and b:
matrix_t *pMatATB = createProduct( pMatAT, pMatB );
if( NULL == pMatATB )
{
return -3;
}
// printf( "(matAT * matB) = \n");
// showMatrix( pMatATB );

showMatrix( pMatATB );

// Now we need to solve the system of linear equations,
// (AT)Ax = (AT)b for "x", the coefficients of the polynomial.
Expand All @@ -163,7 +170,7 @@ int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCo
if( 0.0 == prVal )
{
// printf( "Unable to solve equations, pr = %d, c = %d.\n", pr, c );
// showMatrix( pMatATA );
showMatrix( pMatATA );
rVal = -4;
break;
}
Expand All @@ -178,13 +185,13 @@ int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCo
*MATRIX_VALUE_PTR(pMatATA, r, c2) -= *MATRIX_VALUE_PTR(pMatATA, pr, c2) * factor;
// printf( "c = %d, pr = %d, r = %d, c2=%d, targetRowVal = %f, prVal = %f, factor = %f.\n",
// c, pr, r, c2, targetRowVal, prVal, factor );
// printf( "reduced matATA =\n");

// showMatrix( pMatATA );

}
*MATRIX_VALUE_PTR(pMatATB, r, 0) -= *MATRIX_VALUE_PTR(pMatATB, pr, 0) * factor;
// printf( "reduced matATb =\n");
// showMatrix( pMatATB );

showMatrix( pMatATB );
}
}
}
Expand All @@ -197,11 +204,9 @@ int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCo
*MATRIX_VALUE_PTR(pMatATB, pr, 0) /= prVal;
}

// printf( "reduced matATA =\n");
// showMatrix( pMatATA );
showMatrix( pMatATA );

// printf( "reduced matATb =\n");
// showMatrix( pMatATB );
showMatrix( pMatATB );

for( int i = 0; i < coefficientCount; i++)
{
Expand Down Expand Up @@ -254,12 +259,12 @@ void showPoly( int coeffCount, double *coefficients )
// Private function definitions
//=========================================================


#ifdef SHOW_MATRIX
//--------------------------------------------------------
// showmat()
// reallyShowMatrix()
// Printf the contents of a matrix
//--------------------------------------------------------
static void showMatrix( matrix_t *pMat )
static void reallyShowMatrix( matrix_t *pMat )
{
for( int r = 0; r < pMat->rows; r++ )
{
Expand All @@ -270,6 +275,7 @@ static void showMatrix( matrix_t *pMat )
printf( "\n" );
}
}
#endif // SHOW_MATRIX

//--------------------------------------------------------
// createTranspose()
Expand Down
2 changes: 1 addition & 1 deletion src/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ double y3[] = { 0, 1, 1, 0};
int pc3 = (int) (sizeof(x3) / sizeof(x3[0])); // pointCount
double cr3[] = {0, 0, 0}; // coefficientResults
int cc3 = (int) (sizeof(cr3) / sizeof(cr3[0])); // coefficientCount
char *er3 = "this one should fail"; // expected result
char *er3 = "this one should fail returning -4"; // expected result

// ---------------- TEST 4 DATA ------------------------
// test MLS regeression example from https://www.mathsisfun.com/data/least-squares-regression.html
Expand Down

0 comments on commit 23f248e

Please sign in to comment.