summaryrefslogtreecommitdiffstats
path: root/lib/ts.c
diff options
context:
space:
mode:
authorEthan Werner <mail@ethanwerner.com>2026-08-09 15:02:21 +0400
committerEthan Werner <mail@ethanwerner.com>2026-08-09 15:02:21 +0400
commit3d92926a902017e7cf1f5738e99ff62da596ce12 (patch)
tree2b55422c26a48759d56fa91f06df24449e9dfa88 /lib/ts.c
parent07e18043145f3d91c8ea7ec3ad41b3c905585277 (diff)
Diffstat (limited to 'lib/ts.c')
-rwxr-xr-xlib/ts.c91
1 files changed, 44 insertions, 47 deletions
diff --git a/lib/ts.c b/lib/ts.c
index 68e2f41..e035625 100755
--- a/lib/ts.c
+++ b/lib/ts.c
@@ -7,18 +7,19 @@
fp_t * ts_fill( fp_t *x, uint_t n, fp_t x0, fp_t x1 )
{
- fp_t step = ( x1 - x0 ) / ( n - 1 );
+ if ( !x || n == 0 ) return x;
- if( x0 == 0.0 && x1 == 0.0 )
+ fp_t step = n > 1 ? ( x1 - x0 ) / ( fp_t )( n - 1 ) : 0.0;
+
+ if( fabs( x0 ) < TS_FILL_ZERO_THRESHOLD && fabs( x1 ) < TS_FILL_ZERO_THRESHOLD )
{
- memset(x, 0, sizeof( fp_t ) * n );
+ memset( x, 0, sizeof( fp_t ) * n );
}
-
else
{
for( uint_t i = 0; i < n; i++ )
{
- x[i] = x0 + ( i * step );
+ x[i] = x0 + ( (fp_t)i * step );
}
}
@@ -26,42 +27,48 @@ fp_t * ts_fill( fp_t *x, uint_t n, fp_t x0, fp_t x1 )
}
-void ts_print( fp_t *x, uint_t n )
+void ts_print( fp_t const *x, uint_t n, FILE *stream )
{
- fputc('[', stdout);
+ if ( !x || n == 0 ) return;
+
+ FILE *out = stream ? stream : stdout;
- uint_t i;
- for( i = 0; i < n - 1; i++ )
- {
- fprintf(stdout, "%f,", x[i]);
- }
+ fputc( '[', out );
+
+ for ( uint_t i = 0; i < n - 1; i++ )
+ {
+ fprintf( out, "%f,", x[i] );
+ }
- fprintf(stdout, "%f]", x[i]);
+ fprintf( out, "%f]", x[n - 1] );
}
-void ts_pretty_print( fp_t const * const *x, uint_t m, uint_t n )
+void ts_pretty_print( fp_t const * const *x, uint_t m, uint_t n, FILE *stream )
{
- if( m > 1 )
- {
- for( uint_t i = 0; i < n; i++ )
- {
- uint_t j = 0;
- for( ; j < m; j++ )
- {
- fprintf( stdout, "%f ", x[i][j] );
- }
+ if ( !x || m == 0 || n == 0 ) return;
- fputs( "\n", stdout );
- }
- }
- else
- {
- for( uint_t i = 0; i < n; i++ )
- {
- fprintf( stdout, "%f\n", x[0][i] );
- }
- }
+ FILE *out = stream ? stream : stdout;
+
+ if( m > 1 )
+ {
+ for( uint_t i = 0; i < n; i++ )
+ {
+ for( uint_t j = 0; j < m; j++ )
+ {
+ fprintf( out, "%f ", x[j][i] );
+ }
+
+ fputs( "\n", out );
+ }
+ }
+ else
+ {
+ for( uint_t i = 0; i < n; i++ )
+ {
+ fprintf( out, "%f\n", x[0][i] );
+ }
+ }
}
@@ -91,29 +98,19 @@ fp_t _ts_triangle_number( fp_t x )
fp_t _ts_sum_to_covariance( fp_t sum_xy, fp_t sum_x, fp_t sum_y, uint_t k )
{
+ if ( k == 0 ) return 0.0;
return ( sum_xy - ( sum_x * sum_y / k ) ) / k;
}
fp_t _ts_sum_to_variance( fp_t sum_x2, fp_t sum_x, uint_t k )
{
- return _ts_sum_to_covariance( sum_x2, sum_x, sum_x, k );
+ return fmax( 0.0, _ts_sum_to_covariance( sum_x2, sum_x, sum_x, k ) );
}
uint_t _ts_circle_increment( uint_t i, uint_t k )
{
+ if ( k == 0 ) return i;
return ( i + 1 ) % k;
-}
-
-
-// ts_constant_t * ts_constant_init( uint_t n, ... )
-// {
-// va_list args;
-// va_start( args, n );
-
-// for( uint_t i = 0; i < n; i++ )
-// {
-
-// }
-// } \ No newline at end of file
+} \ No newline at end of file