├── .gitignore ├── HISTORY ├── LICENSE ├── README.md ├── app ├── config.app ├── deg2rad.c ├── makefile ├── rad2deg.c ├── zm_ave.c ├── zm_histogram.c ├── zm_kmeans.c ├── zm_le.c ├── zm_pexp.c ├── zm_psolve.c └── zm_seqmod.c ├── config.org ├── doc ├── dox.conf └── makefile ├── example ├── complex │ ├── complex.ztk │ ├── complex_test.c │ ├── complex_ztk_test.c │ ├── makefile │ └── realpow_test.c ├── data │ ├── makefile │ ├── peak_test.c │ └── sg_test.c ├── eig │ ├── condnum_test.c │ ├── eigsys_test.c │ └── makefile ├── fourier │ ├── fourier_test.c │ └── makefile ├── graph │ ├── astar_dijkstra_test.c │ ├── astar_inc_test.c │ ├── gng_test.c │ ├── graph_test.c │ ├── makefile │ ├── rrt_connect_test.c │ ├── rrt_escape_test.c │ ├── rrt_star_test.c │ └── rrt_test.c ├── intg │ ├── cmp_test.c │ └── makefile ├── ip │ ├── clothoid_test.c │ ├── ip_benchmark_test.c │ ├── lag_vel_acc_approx_test.c │ ├── makefile │ ├── pex_boundary_lsm_test.c │ ├── pex_boundary_test.c │ ├── pex_lsm_test.c │ ├── pex_test.c │ ├── seg_test.c │ └── trapezoidal_velocity_test.c ├── le │ ├── hotelling_test.c │ ├── le_benchmark_test.c │ ├── le_gen_benchmark_test.c │ ├── le_nullspace_test.c │ ├── le_ref_test.c │ ├── le_sr_nullspace_test.c │ ├── makefile │ └── matdecomp_benchmark_test.c ├── makeall.sh ├── makefile ├── mat │ ├── makefile │ ├── mat_img_test.c │ ├── mat_norm_test.c │ └── mat_reg_test.c ├── misc │ ├── ceil_test.c │ ├── cycloid_test.c │ ├── line_test.c │ ├── makefile │ ├── permutation_test.c │ ├── sigmoid_test.c │ └── smoothstep_test.c ├── mva │ ├── cluster_gen_test.c │ ├── gmm_bic_test.c │ ├── gmm_lsm_test.c │ ├── kmeans_lsm_test.c │ ├── kmeans_test.c │ ├── kmedoids_test.c │ ├── makefile │ ├── pca_test.c │ ├── rand_test.c │ ├── ransac_multi_test.c │ ├── ransac_test.c │ ├── xmeans_bic_test.c │ └── xmeans_test.c ├── nle │ ├── dm_test.c │ ├── makefile │ ├── single_test.c │ └── ss_test.c ├── nurbs │ ├── bspline_test.c │ ├── makefile │ ├── nurbs_circle_test.c │ ├── nurbs_diff_test.c │ ├── nurbs_nn_test.c │ ├── nurbs_test.c │ └── nurbs_ztk_test.c ├── ode │ ├── adams_test.c │ ├── beuler_test.c │ ├── cmp2_test.c │ ├── cmp_dc_test.c │ ├── cmp_test.c │ ├── deferred_correction_test.c │ ├── dualpend.conf │ ├── dualpend_test.c │ ├── euler_test.c │ ├── gauss_test.c │ ├── gear_test.c │ ├── heun_test.c │ ├── makefile │ ├── ode2_leapfrog_test.c │ ├── ode2_regular_test.c │ ├── ode2_sympl_test.c │ ├── radau_test.c │ ├── rk4_test.c │ ├── rk5_test.c │ ├── rkf45_test.c │ ├── rkg_test.c │ └── tr_test.c ├── opt │ ├── dm_test.c │ ├── lcp_benchmark_test.c │ ├── line_search_test.c │ ├── lp_benchmark_test.c │ ├── lp_stdcnv_solve_test.c │ ├── lp_stdcnv_test.c │ ├── makefile │ ├── opt_benchmark_test.c │ ├── qp_benchmark_test.c │ └── qp_test.c ├── oscillator │ ├── kuramoto_test.c │ ├── makefile │ ├── matsuoka_test.c │ └── vandelpol_test.c ├── parse │ ├── makefile │ └── parse_test.c ├── pex │ ├── makefile │ ├── pex_diff_test.c │ ├── pex_io_test.c │ ├── pex_modulo_test.c │ ├── pex_out_test.c │ └── pex_test.dat ├── rand │ ├── beta_test.c │ ├── binom2_test.c │ ├── binom_test.c │ ├── cauchy_test.c │ ├── chisqr_test.c │ ├── exp_test.c │ ├── f_test.c │ ├── gamma_test.c │ ├── geo_test.c │ ├── logistic_test.c │ ├── makefile │ ├── montecarlo_test.c │ ├── mt_test.c │ ├── nd_test.c │ ├── normal_test.c │ ├── poisson_test.c │ ├── power_test.c │ ├── t_test.c │ ├── tri_test.c │ └── weibull_test.c ├── seq │ ├── makefile │ ├── seq_add_test.c │ ├── seq_del_test.c │ ├── seq_jump_test.c │ └── test.zvs ├── sf │ ├── bessel_i_test.c │ ├── bessel_j_test.c │ ├── bessel_k_test.c │ ├── bessel_y_test.c │ ├── beta_test.c │ ├── erf_test.c │ ├── fresnel_gen_test.c │ ├── fresnel_test.c │ ├── gamma_test.c │ └── makefile ├── stat │ ├── distrib_test.c │ ├── histogram_test.c │ ├── makefile │ └── normaldistrib_test.c └── vec │ ├── makefile │ ├── rdp_test.c │ ├── tree_test.c │ ├── vec_ring_cat_test.c │ └── vec_sort_test.c ├── include └── zm │ ├── zm.h │ ├── zm_cmat.h │ ├── zm_complex.h │ ├── zm_complex_arith.h │ ├── zm_complex_pe.h │ ├── zm_cvec.h │ ├── zm_data.h │ ├── zm_data_fft.h │ ├── zm_errmsg.h │ ├── zm_fourier.h │ ├── zm_gng.h │ ├── zm_graph.h │ ├── zm_graph_search.h │ ├── zm_ieee.h │ ├── zm_intg.h │ ├── zm_ip.h │ ├── zm_ip_akima.h │ ├── zm_ip_chebyshev.h │ ├── zm_ip_clothoid.h │ ├── zm_ip_lagrange.h │ ├── zm_ip_lin.h │ ├── zm_ip_pex.h │ ├── zm_ip_spline.h │ ├── zm_ip_trvelprof.h │ ├── zm_le.h │ ├── zm_le_gen.h │ ├── zm_le_lq.h │ ├── zm_le_lu.h │ ├── zm_le_lyapnov.h │ ├── zm_le_mat_inv.h │ ├── zm_le_mat_mpinv.h │ ├── zm_le_pivot.h │ ├── zm_le_tridiag.h │ ├── zm_mat.h │ ├── zm_mat_eig.h │ ├── zm_misc.h │ ├── zm_mva.h │ ├── zm_mva_cluster.h │ ├── zm_mva_gmm.h │ ├── zm_mva_ransac.h │ ├── zm_nle.h │ ├── zm_nle_dm.h │ ├── zm_nle_se.h │ ├── zm_nle_ss.h │ ├── zm_nurbs.h │ ├── zm_ode.h │ ├── zm_ode2.h │ ├── zm_ode_adams.h │ ├── zm_ode_beuler.h │ ├── zm_ode_bk4.h │ ├── zm_ode_dc.h │ ├── zm_ode_erk.h │ ├── zm_ode_euler.h │ ├── zm_ode_gear.h │ ├── zm_ode_heun.h │ ├── zm_ode_rk4.h │ ├── zm_ode_rkg.h │ ├── zm_opt.h │ ├── zm_opt_direct.h │ ├── zm_opt_dm.h │ ├── zm_opt_ga.h │ ├── zm_opt_lcp.h │ ├── zm_opt_line.h │ ├── zm_opt_lp.h │ ├── zm_opt_nm.h │ ├── zm_opt_pso.h │ ├── zm_opt_qp.h │ ├── zm_oscil.h │ ├── zm_parse.h │ ├── zm_pex.h │ ├── zm_pex_eq.h │ ├── zm_pi.h │ ├── zm_rand.h │ ├── zm_raw.h │ ├── zm_raw_mat.h │ ├── zm_raw_vec.h │ ├── zm_rrt.h │ ├── zm_seq.h │ ├── zm_sf.h │ ├── zm_sf_bessel.h │ ├── zm_sf_erf.h │ ├── zm_sf_fresnel.h │ ├── zm_sf_gamma.h │ ├── zm_stat.h │ ├── zm_stat_histogram.h │ ├── zm_vec.h │ ├── zm_vec_array.h │ ├── zm_vec_list.h │ ├── zm_vec_ring.h │ └── zm_vec_tree.h ├── lib └── .gitkeep ├── libinfo ├── makefile ├── src ├── config.lib ├── makefile ├── zm_cmat.c ├── zm_complex.c ├── zm_complex_arith.c ├── zm_complex_pe.c ├── zm_cvec.c ├── zm_data.c ├── zm_data_fft.c ├── zm_fourier.c ├── zm_gng.c ├── zm_graph.c ├── zm_graph_search.c ├── zm_ieee.c ├── zm_intg.c ├── zm_ip.c ├── zm_ip_akima.c ├── zm_ip_chebyshev.c ├── zm_ip_clothoid.c ├── zm_ip_lagrange.c ├── zm_ip_lin.c ├── zm_ip_pex.c ├── zm_ip_spline.c ├── zm_ip_trvelprof.c ├── zm_le.c ├── zm_le_gen.c ├── zm_le_lq.c ├── zm_le_lu.c ├── zm_le_lyapnov.c ├── zm_le_mat_inv.c ├── zm_le_mat_mpinv.c ├── zm_le_pivot.c ├── zm_le_tridiag.c ├── zm_mat.c ├── zm_mat_eig.c ├── zm_misc.c ├── zm_mva.c ├── zm_mva_cluster.c ├── zm_mva_gmm.c ├── zm_mva_ransac.c ├── zm_nle_dm.c ├── zm_nle_se.c ├── zm_nle_ss.c ├── zm_nurbs.c ├── zm_ode.c ├── zm_ode2.c ├── zm_ode_adams.c ├── zm_ode_beuler.c ├── zm_ode_bk4.c ├── zm_ode_dc.c ├── zm_ode_erk.c ├── zm_ode_euler.c ├── zm_ode_gear.c ├── zm_ode_heun.c ├── zm_ode_rk4.c ├── zm_ode_rkg.c ├── zm_opt_direct.c ├── zm_opt_dm.c ├── zm_opt_ga.c ├── zm_opt_lcp_ip.c ├── zm_opt_lcp_lemke.c ├── zm_opt_line.c ├── zm_opt_lp_pdip.c ├── zm_opt_lp_simplex.c ├── zm_opt_lp_stdcnv.c ├── zm_opt_nm.c ├── zm_opt_pso.c ├── zm_opt_qp.c ├── zm_opt_qp_asm.c ├── zm_oscil_kura.c ├── zm_oscil_vdp.c ├── zm_parse.c ├── zm_pex.c ├── zm_pex_eq.c ├── zm_rand.c ├── zm_raw_mat.c ├── zm_raw_vec.c ├── zm_rrt.c ├── zm_seq.c ├── zm_sf_bessel.c ├── zm_sf_erf.c ├── zm_sf_fresnel.c ├── zm_sf_gamma.c ├── zm_stat.c ├── zm_stat_histogram.c ├── zm_vec.c ├── zm_vec_array.c ├── zm_vec_list.c ├── zm_vec_ring.c └── zm_vec_tree.c ├── test ├── cmat_test.c ├── complex_test.c ├── cvec_test.c ├── data_test.c ├── dijkstra_test.c ├── ieee_test.c ├── ip_test.c ├── le_test.c ├── makefile ├── mat_eig_test.c ├── mat_test.c ├── misc_test.c ├── nurbs_test.c ├── pex_test.c ├── raw_mat_test.c ├── raw_vec_test.c ├── stat_test.c └── vec_test.c └── tools ├── config.tools └── makefile /.gitignore: -------------------------------------------------------------------------------- 1 | config 2 | include/*/*_export.h 3 | src/*.o 4 | lib/* 5 | !lib/.gitkeep 6 | tools/* 7 | !tools/makefile 8 | !tools/config.tools 9 | app/* 10 | !app/*.c 11 | !app/makefile 12 | !app/config.app 13 | test/test.log 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2024, Tomomichi Sugihara (Zhidao) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /app/config.app: -------------------------------------------------------------------------------- 1 | APPLINK= 2 | APPLINKCPP= 3 | DEPENDENCY= 4 | -------------------------------------------------------------------------------- /app/deg2rad.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void usage(void) 4 | { 5 | fprintf( stderr, "Usage: deg2rad ...\n" ); 6 | fprintf( stderr, " deg2rad \n" ); 7 | exit( 1 ); 8 | } 9 | 10 | void deg2rad_file(FILE *fp, int id) 11 | { 12 | char buf[BUFSIZ]; 13 | double val; 14 | int i; 15 | 16 | while( fgets( buf, BUFSIZ, fp ) ){ 17 | val = 0; 18 | for( i=0; i 2 | 3 | void usage(void) 4 | { 5 | fprintf( stderr, "Usage: rad2deg ...\n" ); 6 | fprintf( stderr, " rad2deg \n" ); 7 | exit( 1 ); 8 | } 9 | 10 | void rad2deg_file(FILE *fp, int id) 11 | { 12 | char buf[BUFSIZ]; 13 | double val; 14 | int i; 15 | 16 | while( fgets( buf, BUFSIZ, fp ) ){ 17 | val = 0; 18 | for( i=0; i 2 | 3 | void le_usage(char *cmd) 4 | { 5 | ZRUNERROR( "Usage: %s ", cmd ); 6 | exit( 1 ); 7 | } 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | zMat a; 12 | zVec b = NULL, ans = NULL; 13 | FILE *fp; 14 | 15 | if( argc < 2 ) le_usage( argv[0] ); 16 | if( !( fp = fopen( argv[1], "r" ) ) ){ 17 | ZOPENERROR( argv[1] ); 18 | return 1; 19 | } 20 | if( !( a = zMatFScan( fp ) ) || 21 | !( b = zVecFScan( fp ) ) || 22 | !( ans = zVecAlloc( zMatColSize(a) ) ) ){ 23 | ZALLOCERROR(); 24 | goto TERMINATE; 25 | } 26 | zLESolveGauss( a, b, ans ); 27 | zVecPrint( ans ); 28 | 29 | TERMINATE: 30 | zMatFree( a ); 31 | zVecFree( b ); 32 | zVecFree( ans ); 33 | fclose( fp ); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /app/zm_pexp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pexp_usage(char *cmd) 4 | { 5 | ZRUNERROR( "Usage: %s <.zv file>\n", cmd ); 6 | ZRUNERROR( " : %s\n", cmd ); 7 | ZRUNERROR( " (format) ...\n" ); 8 | ZRUNERROR( " : %s -help\n", cmd ); 9 | ZRUNERROR( " (to show this message.)\n" ); 10 | exit( EXIT_SUCCESS ); 11 | } 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | zPex pex; 16 | zVec fact; 17 | FILE *fp; 18 | 19 | if( argc < 2 ){ 20 | fp = stdin; 21 | } else{ 22 | if( strcmp( argv[1], "-help" ) == 0 ) 23 | pexp_usage( argv[0] ); 24 | if( !( fp = fopen( argv[1], "r" ) ) ){ 25 | ZOPENERROR( argv[1] ); 26 | return EXIT_FAILURE; 27 | } 28 | } 29 | if( !( fact = zVecFScan( fp ) ) ){ 30 | ZALLOCERROR(); 31 | return EXIT_FAILURE; 32 | } 33 | if( fp != stdin ) fclose( fp ); 34 | if( !( pex = zPexExp( fact ) ) ){ 35 | ZALLOCERROR(); 36 | return EXIT_FAILURE; 37 | } 38 | 39 | printf( "Factor(s):\n" ); 40 | zVecPrint( fact ); 41 | printf( "Expr:\n" ); 42 | zPexExprX( pex ); 43 | zVecFree( fact ); 44 | zPexFree( pex ); 45 | return EXIT_SUCCESS; 46 | } 47 | -------------------------------------------------------------------------------- /app/zm_psolve.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void psolve_usage(char *cmd) 4 | { 5 | ZRUNERROR( "Usage: %s ", cmd ); 6 | exit( 1 ); 7 | } 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | zPex pex; 12 | zCVec ans; 13 | int dim, i; 14 | FILE *fp; 15 | 16 | if( argc < 2 ){ 17 | fp = stdin; 18 | } else{ 19 | if( strcmp( argv[1], "-help" ) == 0 ) 20 | psolve_usage( argv[0] ); 21 | if( !( fp = fopen( argv[1], "r" ) ) ){ 22 | ZOPENERROR( argv[1] ); 23 | return EXIT_FAILURE; 24 | } 25 | } 26 | if( !( pex = zPexFScan( fp ) ) ){ 27 | ZALLOCERROR(); 28 | return EXIT_FAILURE; 29 | } 30 | if( fp != stdin ) fclose( fp ); 31 | 32 | dim = zPexDim( pex ); 33 | if( !( ans = zCVecAlloc( dim ) ) ) return EXIT_FAILURE; 34 | printf( "Equation:\n" ); 35 | zPexExprX( pex ); 36 | printf( "= 0\n" ); 37 | zPexDKA( pex, ans, zTOL, 0 ); 38 | printf( "Answer(s):\n" ); 39 | for( i=0; i 2 | 3 | int main(void) 4 | { 5 | zComplex c1, c2, c3, c; 6 | 7 | zComplexCreate( &c1, 1, 0 ); 8 | printf( "C1 = " ); zComplexPrint( &c1 ); zEndl(); 9 | printf( " abs = %f\n", zComplexAbs(&c1) ); 10 | printf( " arg = %f\n", zRad2Deg(zComplexArg(&c1)) ); 11 | 12 | zComplexCreatePolar( &c2, 1, zDeg2Rad(45) ); 13 | printf( "C2 = " ); zComplexPrint( &c2 ); zEndl(); 14 | printf( " abs = %f\n", zComplexAbs(&c2) ); 15 | printf( " arg = %f\n", zRad2Deg(zComplexArg(&c2)) ); 16 | 17 | zComplexConj( &c2, &c3 ); 18 | printf( "C2* = " ); zComplexPrint( &c3 ); zEndl(); 19 | zComplexCMul( &c2, &c3, &c ); 20 | printf( "C2.C3 = " ); zComplexPrint( &c ); zEndl(); 21 | zComplexCDiv( &c2, &c3, &c ); 22 | printf( "C2/C3 = " ); zComplexPrint( &c ); zEndl(); 23 | 24 | zComplexCreate( &c1, 0.5, 0.5*sqrt(3) ); 25 | printf( "C1 = " ); zComplexPrint( &c1 ); zEndl(); 26 | 27 | zComplexPow( &c1, 6, &c3 ); 28 | printf( "C1^6 = " ); zComplexPrint( &c3 ); zEndl(); 29 | 30 | zComplexCreate( &c, 1, 1 ); 31 | printf( "C = " ); zComplexPrint( &c ); zEndl(); 32 | zComplexCPow( &c1, &c, &c2 ); 33 | printf( "C1^C = " ); zComplexPrint( &c2 ); zEndl(); 34 | 35 | zComplexCLog( &c2, &c1, &c3 ); 36 | printf( "log_C1 C2 = " ); zComplexPrint( &c3 ); zEndl(); 37 | 38 | zComplexNormalize( &c, &c ); 39 | printf( "C/|C| = " ); zComplexPrint( &c ); zEndl(); 40 | 41 | return 0; 42 | } 43 | 44 | /* 45 | * valid output: 46 | * 47 | * 1 48 | * abs = 1 49 | * arg = 0 50 | * 0.7070 + 0.7070 i 51 | * abs = 1 52 | * arg = 45 53 | * 0.7070 - 0.7070 i 54 | * 1 55 | * 0 + i 56 | * 0.5 + 0.8660 i 57 | * 1 58 | * 0.1757 + 0.3039 i 59 | * 1 + i 60 | * 0.7070 + 0.7070 i 61 | */ 62 | -------------------------------------------------------------------------------- /example/complex/complex_ztk_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void eval_test(ZTK *ztk) 4 | { 5 | zComplex c; 6 | 7 | ZTKRewind( ztk ); 8 | do{ 9 | if( ZTKKeyCmp( ztk, "complex" ) ){ 10 | eprintf( "original string = %s\t-> ", ZTKVal(ztk) ); 11 | zComplexPrint( zComplexFromZTK( &c, ztk ) ); 12 | zEndl(); 13 | } 14 | } while( ZTKKeyNext(ztk) ); 15 | } 16 | 17 | int main(int argc, char *argv[]) 18 | { 19 | ZTK ztk; 20 | 21 | ZTKInit( &ztk ); 22 | 23 | eprintf("\nparsing...\n"); 24 | ZTKParse( &ztk, "complex.ztk" ); 25 | eprintf("done.\n\n"); 26 | ZTKPrint( &ztk ); 27 | eval_test( &ztk ); 28 | 29 | ZTKDestroy( &ztk ); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /example/complex/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/complex/realpow_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 1000 4 | #define R 3.2 5 | 6 | int main(void) 7 | { 8 | zComplex c1, c2, c3; 9 | double theta; 10 | int i; 11 | 12 | zComplexZero( &c3 ); 13 | for( i=0; i<=N; i++ ){ 14 | theta = 4 * zPI * i / N; 15 | zComplexCreatePolar( &c1, 1.0, theta ); 16 | zComplexPow( &c1, R, &c2 ); 17 | zComplexPowRef( &c1, R, &c3, &c3 ); 18 | printf( "%f %f %f %f %f %f %f\n", theta, c1.re, c1.im, c2.re, c2.im, c3.re, c3.im ); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /example/data/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/data/peak_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define XMAX 1.0 5 | #define DIV 1000 6 | 7 | #define SAMPLE_N (DIV*2+1) 8 | 9 | #define FOURIER_DIM 5 10 | #define DIM 5 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | zFourier fourier; 15 | double sample[SAMPLE_N]; 16 | double smooth[SAMPLE_N]; 17 | zIndex peakidx, peakidx_sg; 18 | int i; 19 | FILE *fp; 20 | 21 | zRandInit(); 22 | zFourierAlloc( &fourier, FOURIER_DIM ); 23 | for( i=0; i 2 | #include 3 | 4 | #define XMAX 1.0 5 | #define DIV 1000 6 | 7 | #define SAMPLE_N (DIV*2+1) 8 | 9 | #define FOURIER_DIM 5 10 | #define DIM 5 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | zFourier fourier; 15 | double sample[SAMPLE_N]; 16 | double smooth[SAMPLE_N]; 17 | int i; 18 | 19 | zRandInit(); 20 | zFourierAlloc( &fourier, FOURIER_DIM ); 21 | for( i=0; i 2 | 3 | zMat hilbert_mat(zMat m) 4 | { 5 | int i, j; 6 | 7 | for( i=0; i 2 | 3 | #define N 1 4 | 5 | bool assert_mat_eig_one(double a[], int n) 6 | { 7 | int i; 8 | zCVec eigval, eigvec, vt; 9 | zMat ma; 10 | zCMat cma, eigbase; 11 | zComplex z; 12 | bool result = true; 13 | const double tol = 1.0e-7; 14 | 15 | ma = zMatCloneArray( a, n, n ); 16 | cma = zCMatAllocSqr( n ); 17 | eigval = zCVecAlloc( n ); 18 | eigbase = zCMatAllocSqr( n ); 19 | eigvec = zCVecAlloc( n ); 20 | vt = zCVecAlloc( n ); 21 | zMatEig( ma, eigval, eigbase, 0 ); 22 | /* confirmation */ 23 | zMatToCMat( ma, cma ); 24 | for( i=0; i 2 | 3 | #define FFT_TEST_N 100 4 | #define FFT_TEST_DIM 4 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | zFourier f; 9 | double phase; 10 | int i; 11 | FILE *fp; 12 | 13 | zRandInit(); 14 | zFourierAlloc( &f, FFT_TEST_DIM ); 15 | for( i=0; i 2 | 3 | void gen_vec(zVecList *vl, int np, int nc, double xmin, double ymin, double xmax, double ymax, double rmax) 4 | { 5 | int i, j; 6 | zVec vc; 7 | double xc, yc, r, s, c; 8 | 9 | zListInit( vl ); 10 | vc = zVecAlloc( 2 ); 11 | for( i=0; idata ); 32 | fclose( fp ); 33 | } 34 | 35 | #define NP 300 36 | #define NC 6 37 | #define R 1.5 38 | 39 | int main(int argc, char *argv[]) 40 | { 41 | zGNG gng; 42 | zVecList points; 43 | int np, nc, i; 44 | FILE *fp1, *fp2; 45 | char filename[BUFSIZ]; 46 | 47 | zRandInit(); 48 | np = argc > 1 ? atoi( argv[1] ) : NP; 49 | nc = argc > 2 ? atoi( argv[2] ) : NC; 50 | gen_vec( &points, np, nc, R, R, 10-R, 10-R, R ); 51 | vec_output( &points ); 52 | 53 | zGNGInit( &gng, 2, NULL, &points ); 54 | zGNGSetBatchTrialSize( &gng, 10 ); 55 | 56 | fp2 = fopen( "n", "w" ); 57 | for( i=0; i 2 | 3 | void *test_dup(void *data) 4 | { 5 | int *idata; 6 | 7 | if( !( idata = zAlloc( int, 1 ) ) ) return NULL; 8 | *idata = *(int *)data; 9 | return idata; 10 | } 11 | 12 | bool test_equal(void *d1, void *d2) 13 | { 14 | return *(int *)d1 == *(int *)d2; 15 | } 16 | 17 | void test_fprint(FILE *fp, void *data) 18 | { 19 | fprintf( fp, "[%d]", *(int *)data ); 20 | } 21 | 22 | void test_destroy(void *data) 23 | { 24 | zFree( data ); 25 | } 26 | 27 | int main(int argc, char *argv[]) 28 | { 29 | zGraph graph; 30 | int val, i1, i2; 31 | 32 | zGraphInit( &graph ); 33 | graph.dup = test_dup; 34 | graph.equal = test_equal; 35 | graph.fprint = test_fprint; 36 | graph.destroy = test_destroy; 37 | val = 1; zGraphAddNode( &graph, &val ); 38 | val = 4; zGraphAddNode( &graph, &val ); 39 | val = 3; zGraphAddNode( &graph, &val ); 40 | val = 2; zGraphAddNode( &graph, &val ); 41 | i1 = 1; i2 = 2; zGraphBiconnect( &graph, &i1, &i2, 10 ); 42 | i1 = 1; i2 = 3; zGraphBiconnect( &graph, &i1, &i2, 20 ); 43 | i1 = 2; i2 = 4; zGraphBiconnect( &graph, &i1, &i2, 10 ); 44 | i1 = 3; i2 = 4; zGraphConnect( &graph, &i1, &i2, 5 ); 45 | zGraphFPrint( stdout, &graph ); 46 | 47 | printf( ">> destroying\n" ); 48 | zGraphDestroy( &graph ); 49 | zGraphFPrint( stdout, &graph ); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /example/graph/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/intg/cmp_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define TEST 1 4 | 5 | double f_org(double t) 6 | { 7 | #if TEST == 1 8 | return sin(t) + t + 1; 9 | #else 10 | return 0.5 * t * ( t - 1 ) + 1; 11 | #endif 12 | } 13 | 14 | double f_dif(double t) 15 | { 16 | #if TEST == 1 17 | return cos(t) + 1; 18 | #else 19 | return t - 0.5; 20 | #endif 21 | } 22 | 23 | void output(double t, double s_true, zIntg intg[], int n) 24 | { 25 | int i; 26 | 27 | printf( "%f %.16f", t, s_true ); 28 | for( i=0; i 2 | 3 | #define N 100 4 | 5 | void plot(zClothoid *cl) 6 | { 7 | int i; 8 | double s, x, y; 9 | 10 | for( i=0; i<=N; i++ ){ 11 | zClothoidXY( cl, ( s = (double)i/N ), &x, &y ); 12 | printf( "%.10g %.10g %.10g\n", s, x, y ); 13 | } 14 | } 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | zClothoid cl; 19 | 20 | zClothoidCreateSegment( &cl, 0, 0,-0.5*zPI, 2, 1, 0.5*zPI ); 21 | plot( &cl ); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /example/ip/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/ip/pex_boundary_lsm_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | zPexIP pc; 6 | zVec tvec, xvec; 7 | double t; 8 | 9 | tvec = zVecCreateList( 3, 1.0, 2.0, 4.0 ); 10 | xvec = zVecCreateList( 3, 4.0, 0.0, 2.0 ); 11 | zPexIPCreateBounderyLSM( &pc, 5.0, 0, 0, 0, 1, 0, 0, 8, tvec, xvec ); 12 | 13 | for( t=0; t<=zPexIPTerm(&pc); t+=0.01 ) 14 | printf( "%.10f %.10f\n", t, zPexIPVal( &pc, t ) ); 15 | 16 | zPexIPFree( &pc ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /example/ip/pex_boundary_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define T 1000 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | zPexIP pc; 8 | int i; 9 | double x; 10 | 11 | x = ( argc <= 1 ) ? 1 : atof(argv[1]); 12 | zPexIPCreateBoundary( &pc, T, 0, 0, 0, x, 0, 0, NULL ); 13 | for( i=0; i<=T; i++ ) 14 | printf( "%.10f %.10f\n", (double)i, zPexIPVal( &pc, i ) ); 15 | zPexIPFree( &pc ); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /example/ip/pex_lsm_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | zPexIP pc; 6 | zVec tvec, xvec; 7 | double t; 8 | 9 | tvec = zVecCreateList( 3, 1.0, 2.0, 4.0 ); 10 | xvec = zVecCreateList( 3, 4.0, 0.0, 2.0 ); 11 | zPexIPCreateLSM( &pc, 5.0, 4, tvec, xvec ); 12 | 13 | for( t=0; t<=zPexIPTerm(&pc); t+=0.01 ) 14 | printf( "%.10f %.10f\n", t, zPexIPVal( &pc, t ) ); 15 | 16 | zPexIPFree( &pc ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /example/ip/pex_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | zPexIP pc; 6 | double t; 7 | int dim, i; 8 | 9 | for( dim=1; dim<=3; dim++ ){ 10 | zPexIPAlloc( &pc, 3, dim ); 11 | for( i=0; i<=dim; i++ ) 12 | zPexIPSetCoeff( &pc, i, 1 ); 13 | for( t=-zPexIPTerm(&pc); t<=zPexIPTerm(&pc); t+=0.01 ) 14 | printf( "%.10f %.10f %.10f\n", zPexIPVal(&pc,t), zPexIPVel(&pc,t), zPexIPAcc(&pc,t) ); 15 | zPexIPFree( &pc ); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /example/ip/seg_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | zSeq seq; 6 | zIPData dat; 7 | int point_num; 8 | double t, tmax; 9 | int i; 10 | /* example data array */ 11 | double tp[] = { 0, 1, 3, 6 }; 12 | 13 | /* creation of x-values and y-values vector */ 14 | point_num = sizeof(tp) / sizeof(double); 15 | zSeqInit( &seq ); 16 | for( t=0, i=0; i tmax ) break; 28 | } 29 | 30 | /* destruction of instances */ 31 | zIPDataFree( &dat ); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /example/ip/trapezoidal_velocity_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DT 0.01 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | zTRVelProf trvelprof; 8 | double t; 9 | int i, step; 10 | 11 | #ifdef __cplusplus 12 | trvelprof.create( -1, -2, 5, 4, argc > 1 ? atof(argv[1]) : 5 ); 13 | step = trvelprof.term() / DT + 1; 14 | for( i=0; i<=step; i++ ){ 15 | t = i * DT; 16 | printf( "%g %g %g\n", trvelprof.distance(t), trvelprof.velocity(t), trvelprof.acceleration(t) ); 17 | } 18 | #else 19 | zTRVelProfCreate( &trvelprof, -1, -2, 5, 4, argc > 1 ? atof(argv[1]) : 5 ); 20 | step = zTRVelProfTerm( &trvelprof ) / DT + 1; 21 | for( i=0; i<=step; i++ ){ 22 | t = i * DT; 23 | printf( "%g %g %g\n", zTRVelProfDist(&trvelprof,t), zTRVelProfVel(&trvelprof,t), zTRVelProfAcc(&trvelprof,t) ); 24 | } 25 | #endif 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /example/le/hotelling_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 5 4 | 5 | int main(void) 6 | { 7 | zMat a, b, m; 8 | 9 | a = zMatAllocSqr( N ); 10 | b = zMatAllocSqr( N ); 11 | m = zMatAllocSqr( N ); 12 | zMatRandUniform( a, -1e10, 1e10 ); 13 | zMatInv( a, b ); 14 | zMulMatMat( a, b, m ); 15 | zMatPrint( m ); 16 | 17 | zMatInvHotelling( a, b, zTOL, 0 ); 18 | zMulMatMat( a, b, m ); 19 | zMatPrint( m ); 20 | 21 | zMatFreeAtOnce( 3, a, b, m ); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /example/le/le_benchmark_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 1000 4 | #define SIZE 10 5 | #define TOL (1.0e-10) 6 | 7 | int main(void) 8 | { 9 | zMat a, l, u, aa; 10 | zVec b, x, ans; 11 | zIndex index; 12 | int count_cramel, count_gauss, count_lu, count_ri, count_gs; 13 | int i; 14 | 15 | zRandInit(); 16 | a = zMatAllocSqr( SIZE ); 17 | aa = zMatAllocSqr( SIZE ); 18 | l = zMatAllocSqr( SIZE ); 19 | u = zMatAllocSqr( SIZE ); 20 | b = zVecAlloc( SIZE ); 21 | x = zVecAlloc( SIZE ); 22 | ans = zVecAlloc( SIZE ); 23 | index = zIndexAlloc( SIZE ); 24 | count_cramel = count_gauss = count_lu = count_ri = count_gs = 0; 25 | for( i=0; i 2 | 3 | #define M 300 4 | #define N 400 5 | 6 | #define TURN 20 7 | 8 | #define D_MAX 10.0 9 | 10 | void check(zMat a, zVec b, zVec ans, zVec e, clock_t c1, clock_t c2, const char *key) 11 | { 12 | zMulMatVec( a, ans, e ); 13 | zVecSub( b, e, e ); 14 | printf( "%s:%.1g %ld ", key, zVecNorm(e), (c2-c1)/1000 ); 15 | } 16 | 17 | void test(zMat a, zVec b, zVec w, zVec w2, zVec x, zVec e, zVec aux) 18 | { 19 | clock_t c1, c2; 20 | 21 | zVecRandUniform( aux, -10, 10 ); 22 | 23 | c1 = clock(); 24 | zLESolveSR( a, b, w, w2, x ); 25 | c2 = clock(); 26 | check( a, b, x, e, c1, c2, "SR" ); 27 | 28 | c1 = clock(); 29 | zLESolveSRAux( a, b, w, w2, x, aux ); 30 | c2 = clock(); 31 | check( a, b, x, e, c1, c2, "SR(aux)" ); 32 | 33 | c1 = clock(); 34 | zLESolveMP( a, b, w, w2, x ); 35 | c2 = clock(); 36 | check( a, b, x, e, c1, c2, "MP" ); 37 | 38 | c1 = clock(); 39 | zLESolveMPAux( a, b, w, w2, x, aux ); 40 | c2 = clock(); 41 | check( a, b, x, e, c1, c2, "MP(aux)" ); 42 | 43 | printf( "\n" ); 44 | } 45 | 46 | int main(int argc, char *argv[]) 47 | { 48 | int i, j; 49 | zMat a; 50 | zVec b, w, x, e, w2, aux; 51 | int turn; 52 | 53 | zRandInit(); 54 | a = zMatAlloc( M, N ); 55 | b = zVecAlloc( M ); 56 | e= zVecAlloc( M ); 57 | w = zVecAlloc( N ); 58 | w2= zVecAlloc( M ); 59 | x = zVecAlloc( N ); 60 | aux = zVecAlloc( N ); 61 | 62 | for( i=0; i 1 ? atoi(argv[1]) : TURN; 72 | while( turn-- > 0 ) test( a, b, w, w2, x, e, aux ); 73 | 74 | zMatFree( a ); 75 | zVecFreeAtOnce( 6, b, e, w, w2, x, aux ); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /example/le/le_ref_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void test(zMat a, zVec b, zVec w, zVec d, zVec x, zVec y) 4 | { 5 | zVec tmp; 6 | 7 | tmp = zVecAlloc( zVecSizeNC(d) ); 8 | zLESolveRefMin( a, b, w, d, x ); 9 | zMulMatVec( a, x, y ); 10 | zVecSubDRC( y, b ); 11 | zVecSub( d, x ,tmp ); 12 | zVecAmp( tmp, w, x ); 13 | printf( "|residual|=%.10g |error|=%.10g\n", zVecInnerProd(tmp,x), zVecNorm(y) ); 14 | zVecFree( tmp ); 15 | } 16 | 17 | #define N 100 18 | #define M 300 19 | int main(void) 20 | { 21 | zMat a; 22 | zVec b, w, x, d, y; 23 | int i; 24 | 25 | zRandInit(); 26 | a = zMatAlloc( N, M ); zMatRandUniform( a, -10, 10 ); 27 | b = zVecAlloc( N ); zVecRandUniform( b, -10, 10 ); 28 | w = zVecAlloc( M ); 29 | d = zVecAlloc( M ); 30 | x = zVecAlloc( M ); 31 | y = zVecAlloc( N ); 32 | 33 | zLESolveNormMin( a, b, NULL, d ); 34 | for( i=0; i 2 | 3 | #define M 300 4 | #define N 400 5 | 6 | #define TURN 20 7 | 8 | #define D_MAX 10.0 9 | 10 | void check(zMat a, zVec b, zVec ans, zVec e, const char *key) 11 | { 12 | zMulMatVec( a, ans, e ); 13 | zVecSub( b, e, e ); 14 | printf( "%s:%.1g ", key, zVecNorm(e) ); 15 | } 16 | 17 | void test(zMat a, zVec b, zVec w, zVec w2, zVec e, zVec aux) 18 | { 19 | zLEWorkspace le; 20 | zVec x1, x2, bb; 21 | 22 | x1 = zVecAlloc( N ); 23 | x2 = zVecAlloc( N ); 24 | bb = zVecAlloc( zVecSizeNC(b) ); 25 | zLEWorkspaceAlloc( &le, NULL, N ); 26 | zVecRandUniform( aux, -10, 10 ); 27 | 28 | zLESolveSRAux( a, b, w, w2, x1, aux ); 29 | check( a, b, x1, e, "SR(aux)" ); 30 | zLESolveSRAuxDST( a, b, w, w2, x2, aux, &le, bb ); 31 | check( a, b, x2, e, "SR(aux,DST)" ); 32 | zVecSub( x1, x2, le.s ); 33 | printf( "error = %.10g\n", zVecNorm(le.s) ); 34 | 35 | zLEWorkspaceFree( &le ); 36 | } 37 | 38 | int main(int argc, char *argv[]) 39 | { 40 | int i, j; 41 | zMat a; 42 | zVec b, w, e, w2, aux; 43 | int turn; 44 | 45 | zRandInit(); 46 | a = zMatAlloc( M, N ); 47 | b = zVecAlloc( M ); 48 | e= zVecAlloc( M ); 49 | w = zVecAlloc( N ); 50 | w2= zVecAlloc( M ); 51 | aux = zVecAlloc( N ); 52 | 53 | for( i=0; i 1 ? atoi(argv[1]) : TURN; 63 | while( turn-- > 0 ) test( a, b, w, w2, e, aux ); 64 | 65 | zMatFree( a ); 66 | zVecFreeAtOnce( 5, b, e, w, w2, aux ); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /example/le/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/makeall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | for sampledir in * 4 | do 5 | if [ -d ${sampledir} ]; then 6 | cd ${sampledir} > /dev/null 7 | if [ -e makefile ]; then 8 | make $1 9 | fi 10 | cd - > /dev/null 11 | fi 12 | done 13 | 14 | exit 0 15 | -------------------------------------------------------------------------------- /example/makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-ansi -Wall -O3 -funroll-loops -std=c99 -g `zm-config --cflags` 3 | LINK=`zm-config -l` 4 | # -std=c99 is for gamma function test. 5 | 6 | CXX=g++ 7 | CXXFLAGS=-std=c++17 -Wall -O3 -funroll-loops -g `zm-config --cflags` 8 | LINKCXX=`zm-config -lcpp` 9 | 10 | TARGET=$(shell ls *.c | xargs -i basename {} .c | tr -s "\n" " ") $(shell ls *.cpp | xargs -i basename {} .cpp | tr -s "\n" " ") 11 | 12 | all: $(TARGET) 13 | %: %.c 14 | $(CC) $(CFLAGS) -o $@ $< $(LINK) 15 | %: %.cpp 16 | $(CXX) $(CXXFLAGS) -o $@ $< $(LINKCXX) 17 | clean : 18 | -@rm -f *.o *~ core *test $(TARGET) 19 | -------------------------------------------------------------------------------- /example/mat/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/mat/mat_img_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ROW 10 4 | #define COL 10 5 | 6 | zMat mat_create_rand(int row, int col) 7 | { 8 | zMat m; 9 | 10 | m = zMatAlloc( ROW, COL ); 11 | zMatRandUniform( m, -10, 10 ); 12 | return m; 13 | } 14 | 15 | zMat mat_create_ut_rand(int row, int col) 16 | { 17 | int i, j; 18 | zMat m; 19 | 20 | m = zMatAlloc( ROW, COL ); 21 | for( i=0; i 2 | 3 | #define N 3 4 | #define M 3 5 | 6 | int main(void) 7 | { 8 | zMat m; 9 | zVec v; 10 | int i, j; 11 | 12 | zRandInit(); 13 | m = zMatAlloc( N, M ); 14 | v = zVecAlloc( M ); 15 | zMatRandUniform( m, -10, 10 ); 16 | zMatPrint( m ); 17 | printf( "||m||_2 = %g\n", zMatNorm(m) ); 18 | printf( "||m||_inf = %g\n", zMatInfNorm(m) ); 19 | for( i=0; i 2 | 3 | int main(void) 4 | { 5 | zMat m; 6 | 7 | zRandInit(); 8 | m = zMatAlloc( 5, 6 ); 9 | zMatRandUniform( m, -10, 10 ); 10 | zMatPrint( m ); 11 | 12 | zMatColReg( m, 3 ); 13 | zMatPrint( m ); 14 | 15 | zMatFree( m ); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /example/misc/ceil_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int i; 6 | double v; 7 | 8 | for( i=-45; i<=45; i++ ){ 9 | v = 0.1 * i; 10 | printf( "%g %g %g %g %g %g %g\n", v, ceil(v), floor(v), round(v), trunc(v), zCeil(v), zRound(v) ); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /example/misc/cycloid_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DIV 100 4 | 5 | int main(void) 6 | { 7 | double x, y, t; 8 | int i; 9 | 10 | for( i=0; i<=DIV; i++ ){ 11 | t = (double)i / DIV; 12 | x = 7 * zCycloidX( t ) + 5; 13 | y = 3 * zCycloidY( t ) + 4; 14 | printf( "%f %f\n", x, y ); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /example/misc/line_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | double x, y; 6 | 7 | for( x=0; x<=30; x+=1.0 ){ 8 | y = zLine( x, 1,-2, 20, 4 ); 9 | printf( "%f %f\n", x, y ); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /example/misc/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/misc/permutation_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N1 100 4 | #define N2 20 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | int n1, n2; 9 | 10 | n1 = argc > 1 ? atof(argv[1]) : N1; 11 | n2 = argc > 2 ? atof(argv[2]) : N2; 12 | printf( "%d_P_%d = %f\n", n1, n2, zPermutation( n1, n2 ) ); 13 | printf( "%d! = %f\n", n1, zFactorial( n1 ) ); 14 | printf( "%d_C_%d = %f\n", n1, n2, zCombination( n1, n2 ) ); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /example/misc/sigmoid_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | double x, y; 6 | 7 | for( x=-30; x<=30; x+=1 ){ 8 | y = 5 * zSigmoid( 0.25*x ); 9 | printf( "%f %f\n", x, y ); 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /example/misc/smoothstep_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 100 4 | 5 | int main(void) 6 | { 7 | double x; 8 | int i, j; 9 | 10 | for( i=-N; i<=2*N; i++ ){ 11 | x = (double)i / N; 12 | for( j=0; j<5; j++ ){ 13 | printf( " %g %g", zSmoothStep( x, j ), zSmoothStepDif( x, j ) ); 14 | } 15 | printf( "\n" ); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /example/mva/cluster_gen_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double gen_r_uniform(double rmax){ return zRandF( 0, rmax ); } 5 | double gen_r_normald(double rmax){ return zRandND( NULL, 0, 0.5*rmax ); } 6 | 7 | bool gen_vec(double (* gen_func)(double), int dim, int np, int nc, double rate_outlier, double min, double max) 8 | { 9 | zVec vc, v; 10 | double rmax, r; 11 | int i, j, k; 12 | bool ret = true; 13 | 14 | vc = zVecAlloc( dim ); 15 | v = zVecAlloc( dim ); 16 | if( !vc || !v ){ 17 | ret = false; 18 | goto TERMINATE; 19 | } 20 | rmax = fabs( max - min ) * sqrt(dim) / (2*nc); 21 | for( i=0; i 1 && argv[1][0] == 'u' ? gen_r_uniform : gen_r_normald, 60 | argc > 2 ? atoi( argv[2] ) : DIM, 61 | argc > 3 ? atoi( argv[3] ) : NUM_POINTS, 62 | argc > 4 ? atoi( argv[4] ) : NUM_CLUSTER, 63 | argc > 5 ? atof( argv[5] ) : RATE_OUTLIER, 64 | argc > 6 ? atof( argv[6] ) : MIN, 65 | argc > 7 ? atof( argv[7] ) : MAX ) ) return EXIT_FAILURE; 66 | return EXIT_SUCCESS; 67 | } 68 | -------------------------------------------------------------------------------- /example/mva/kmeans_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void gen_vec(zVecList *vl, int np, int nc, double xmin, double ymin, double xmax, double ymax) 4 | { 5 | int i, j; 6 | zVec vc; 7 | double xc, yc, rmax; 8 | double r, t; 9 | 10 | zListInit( vl ); 11 | vc = zVecAlloc( 2 ); 12 | rmax = sqrt( zSqr( xmax - xmin ) + zSqr( ymax - ymin ) ) / ( 2 * nc ); 13 | for( i=0; i 1 ? atoi( argv[1] ) : NC; 39 | np = argc > 2 ? atoi( argv[2] ) : NP; 40 | gen_vec( &points, np, nc, 0, 0, 10, 10 ); 41 | zVecMClusterInit( &mc, 2 ); 42 | printf( "K-means completed in %d times of iteration.\n", zVecMClusterKMeans( &mc, &points, nc ) ); 43 | zVecMClusterValuePrintFile( &mc, "" ); 44 | score = zVecMClusterMeanSilhouette( &mc ); 45 | zVecMClusterSilhouettePrintFile( &mc, "s" ); 46 | printf( "mean silhouette = %.10g\n", score ); 47 | 48 | zVecMClusterDestroy( &mc ); 49 | zVecListDestroy( &points ); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /example/mva/kmedoids_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void gen_vec(zVecList *vl, int np, int nc, double xmin, double ymin, double xmax, double ymax) 4 | { 5 | int i, j; 6 | zVec vc; 7 | double xc, yc, rmax; 8 | double r, t; 9 | 10 | zListInit( vl ); 11 | vc = zVecAlloc( 2 ); 12 | rmax = sqrt( zSqr( xmax - xmin ) + zSqr( ymax - ymin ) ) / ( 2 * nc ); 13 | for( i=0; i 1 ? atoi( argv[1] ) : NC; 47 | np = argc > 2 ? atoi( argv[2] ) : NP; 48 | gen_vec( &points, np, nc, 0, 0, 10, 10 ); 49 | zVecMClusterInit( &mc, 2 ); 50 | printf( "K-medoids completed in %d times of iteration.\n", zVecMClusterKMedoids( &mc, &points, nc ) ); 51 | zVecMClusterValuePrintFile( &mc, "" ); 52 | zVecMClusterDestroy( &mc ); 53 | zVecListDestroy( &points ); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /example/mva/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/mva/pca_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void gen_vec(zVecList *vl, int n, double xc, double yc, double r1, double r2, double theta) 4 | { 5 | int i; 6 | zVec vc; 7 | double s, c, st, ct, xt, yt, d; 8 | 9 | zListInit( vl ); 10 | zSinCos( theta, &s, &c ); 11 | for( i=0; i 1 ? atoi( argv[1] ) : N; 39 | cr = argc > 2 ? atof( argv[2] ) : CR; 40 | gen_vec( &points, n, 10, 10, 10, 5, zDeg2Rad(30) ); 41 | fp = fopen( "s", "w" ); 42 | zVecListFPrint( fp, &points ); 43 | fclose( fp ); 44 | 45 | p = zVecAlloc( DIM ); 46 | mean = zVecAlloc( DIM ); 47 | score = zVecAlloc( DIM ); 48 | loading = zMatAllocSqr( DIM ); 49 | printf( "executing PCA. the number of PC = %d\n", 50 | zVecListPCA( &points, cr, mean, score, loading ) ); 51 | 52 | fp = fopen( "a", "w" ); 53 | zListForEach( &points, pc ){ 54 | zVecSub( pc->data, mean, p ); 55 | zMulMatTVec( loading, p, score ); 56 | zVecValueFPrint( fp, score ); 57 | } 58 | fclose( fp ); 59 | 60 | zVecListDestroy( &points ); 61 | zVecFreeAtOnce( 3, p, mean, score ); 62 | zMatFree( loading ); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /example/mva/rand_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void gen_vec(zVecList *vl, int n, double xc, double yc, double r1, double r2, double theta) 4 | { 5 | int i; 6 | zVec vc; 7 | double s, c, st, ct, xt, yt, d; 8 | 9 | zListInit( vl ); 10 | zSinCos( theta, &s, &c ); 11 | for( i=0; i 1 ? atoi( argv[1] ) : N; 36 | gen_vec( &src, n, 10, 10, 10, 5, zDeg2Rad(30) ); 37 | fp = fopen( "s", "w" ); 38 | zVecListFPrint( fp, &src ); 39 | fclose( fp ); 40 | 41 | p = zVecAlloc( DIM ); 42 | mean = zVecAlloc( DIM ); 43 | cov = zMatAllocSqr( DIM ); 44 | zVecListMeanCov( &src, mean, cov ); 45 | 46 | zVecListGenRandND( &dest, N, mean, cov ); 47 | fp = fopen( "d", "w" ); 48 | zVecListFPrint( fp, &dest ); 49 | fclose( fp ); 50 | 51 | zVecListDestroy( &src ); 52 | zVecListDestroy( &dest ); 53 | zVecFree( p ); 54 | zVecFree( mean ); 55 | zMatFree( cov ); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /example/mva/xmeans_bic_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void gen_vec(zVecList *vl, int np, int nc, double xmin, double ymin, double xmax, double ymax) 5 | { 6 | int i, j; 7 | zVec vc; 8 | double xc, yc, rmax; 9 | double r, t; 10 | 11 | zListInit( vl ); 12 | vc = zVecAlloc( 2 ); 13 | rmax = sqrt( zSqr( xmax - xmin ) + zSqr( ymax - ymin ) ) / ( 2 * nc ); 14 | for( i=0; i 1 ? atoi( argv[1] ) : NP; 42 | nc = argc > 2 ? atoi( argv[2] ) : NC; 43 | gen_vec( &points, np, nc, 0, 0, 10, 10 ); 44 | zVecMClusterInit( &mc, 2 ); 45 | printf( "X-means completed in %d times of iteration.\n", 46 | zVecMClusterXMeansBIC( &mc, &points ) ); 47 | 48 | zListForEach( zVecMClusterClusterList(&mc), vcc ){ 49 | sprintf( filename, "%d", i++ ); 50 | fp = fopen( filename, "w" ); 51 | zVecClusterValueFPrint( fp, &vcc->data ); 52 | fclose( fp ); 53 | } 54 | zVecMClusterDestroy( &mc ); 55 | zVecListDestroy( &points ); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /example/mva/xmeans_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void gen_vec(zVecList *vl, int np, int nc, double xmin, double ymin, double xmax, double ymax) 5 | { 6 | int i, j; 7 | zVec vc; 8 | double xc, yc, rmax; 9 | double r, t; 10 | 11 | zListInit( vl ); 12 | vc = zVecAlloc( 2 ); 13 | rmax = sqrt( zSqr( xmax - xmin ) + zSqr( ymax - ymin ) ) / ( 2 * nc ); 14 | for( i=0; i 1 ? atoi( argv[1] ) : NP; 42 | nc = argc > 2 ? atoi( argv[2] ) : NC; 43 | gen_vec( &points, np, nc, 0, 0, 10, 10 ); 44 | zVecMClusterInit( &mc, 2 ); 45 | printf( "X-means completed in %d times of iteration.\n", 46 | zVecMClusterXMeansDensity( &mc, &points ) ); 47 | 48 | zListForEach( zVecMClusterClusterList(&mc), vcc ){ 49 | sprintf( filename, "%d", i++ ); 50 | fp = fopen( filename, "w" ); 51 | zVecClusterValueFPrint( fp, &vcc->data ); 52 | fclose( fp ); 53 | } 54 | zVecMClusterDestroy( &mc ); 55 | zVecListDestroy( &points ); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /example/nle/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/nle/single_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double f(double x, void *priv) 4 | { 5 | /* 6 | return (x+3)*(x+1)*(x-1)*(x-3); 7 | */ 8 | return exp(-x)-x; 9 | } 10 | 11 | int main(void) 12 | { 13 | double x; 14 | 15 | printf( "binary section method\n" ); 16 | x = zNLE_Bisec( f, -10, 10, NULL, zTOL, 0 ); 17 | printf( "f(%f) = %g\n", x, f(x,NULL) ); 18 | 19 | printf( "Secant method\n" ); 20 | x = zNLE_Secant( f, -10, 10, NULL, zTOL, 0 ); 21 | printf( "f(%f) = %g\n", x, f(x,NULL) ); 22 | 23 | printf( "Regula-Falsi method\n" ); 24 | x = zNLE_RF( f, -10, 10, NULL, zTOL, 0 ); 25 | printf( "f(%f) = %g\n", x, f(x,NULL) ); 26 | 27 | printf( "Van Wijngaarden-Dekker-Brent method\n" ); 28 | x = zNLE_VDB( f, -10, 10, NULL, zTOL, 0 ); 29 | printf( "f(%f) = %g\n", x, f(x,NULL) ); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /example/nle/ss_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | zVec f(zVec x, zVec y, void *util) 4 | { 5 | zVecSetElem( y, 0, sqrt(zVecElem(x,0)) ); 6 | zVecSetElem( y, 1, cos(zVecElem(x,0)) ); 7 | zVecSetElem( y, 2, 2.0/(zSqr(zVecElem(x,0))+1.0)+1 ); 8 | zVecSetElem( y, 3, 0.5*(zVecElem(x,0)+3) ); 9 | zVecSetElem( y, 4, (1-zSqr(zVecElem(x,0)))/(1+sqrt(1+zSqr(zVecElem(x,0)))) ); 10 | zVecSetElem( y, 5, 1.0/(1.0+zVecElem(x,0)) ); 11 | return y; 12 | } 13 | 14 | int main(void) 15 | { 16 | zVec x; 17 | 18 | x = zVecAlloc( 6 ); 19 | zVecSetAll( x, 2.0 ); 20 | zSSSolve( f, x, NULL, 0 ); 21 | zVecPrint( x ); 22 | printf( ">> assertion <<\n" ); 23 | f( x, x, NULL ); 24 | zVecPrint( x ); 25 | zVecFree( x ); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /example/nurbs/bspline_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define STEP 100 4 | #define DIM 3 5 | 6 | void output_src(zSeq *seq) 7 | { 8 | FILE *fp; 9 | zSeqCell *cp; 10 | 11 | fp = fopen( "src", "w" ); 12 | zListForEach( seq, cp ) 13 | fprintf( fp, "%f %f\n", zVecElemNC(cp->data.v,0), zVecElemNC(cp->data.v,1) ); 14 | fclose( fp ); 15 | } 16 | 17 | int main(int argc, char *argv[]) 18 | { 19 | zBSpline bspline; 20 | zSeq seq; 21 | double xp[] = { 0.0, 2.0,-2.0, 0.0, 2.0,-2.0, 0.0 }; 22 | double yp[] = { 0.0, 2.0, 3.0, 6.0, 7.5, 9.0,10.0 }; 23 | double knot[] = { 0, 0, 0, 0, 1, 2, 3, 4, 4, 4, 4 }; 24 | zVec v; 25 | double t; 26 | int num, i; 27 | 28 | num = sizeof(xp) / sizeof(double); 29 | zListInit( &seq ); 30 | for( i=num-1; i>=0; i-- ){ 31 | v = zVecCreateList( 2, xp[i], yp[i] ); 32 | zSeqEnqueue( &seq, v, 1.0 ); 33 | } 34 | output_src( &seq ); 35 | zBSplineCreate( &bspline, &seq, DIM ); 36 | zSeqFree( &seq ); 37 | for( i=0; i 2 | 3 | #define STEP 100 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | zNURBS nurbs; 8 | zSeq seq; 9 | zVec v; 10 | int num, i; 11 | double xp[] = { 1.0, 1.0, 0.0, -1.0, -1.0, -1.0, 0.0, 1.0, 1.0 }; 12 | double yp[] = { 0.0, 1.0, 1.0, 1.0, 0.0, -1.0, -1.0, -1.0, 0.0 }; 13 | 14 | /* create xy-values */ 15 | num = sizeof(xp) / sizeof(double); 16 | zListInit( &seq ); 17 | for( i=0; i 3 | 4 | #define STEP 100 5 | #define DIM 3 6 | 7 | void output_src(zSeq *seq) 8 | { 9 | FILE *fp; 10 | zSeqCell *cp; 11 | 12 | fp = fopen( "src", "w" ); 13 | zListForEach( seq, cp ) 14 | fprintf( fp, "%f %f\n", zVecElemNC(cp->data.v,0), zVecElemNC(cp->data.v,1) ); 15 | fclose( fp ); 16 | } 17 | 18 | int main(int argc, char *argv[]) 19 | { 20 | zNURBS nurbs; 21 | zSeq seq; 22 | double xp[] = { 0.0, 2.0,-2.0, 0.0, 2.0,-2.0, 0.0 }; 23 | double yp[] = { 0.0, 2.0, 3.0, 6.0, 7.5, 9.0,10.0 }; 24 | double weight[] = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }; 25 | double knot[] = { 0, 0, 0, 0, 1, 2, 3, 4, 4, 4, 4 }; 26 | zVec v; 27 | double t; 28 | int num, i; 29 | 30 | num = sizeof(xp) / sizeof(double); 31 | zListInit( &seq ); 32 | for( i=num-1; i>=0; i-- ){ 33 | v = zVecCreateList( 2, xp[i], yp[i] ); 34 | zSeqEnqueue( &seq, v, 1.0 ); 35 | } 36 | output_src( &seq ); 37 | zNURBSCreate( &nurbs, &seq, DIM ); 38 | zSeqFree( &seq ); 39 | for( i=0; i 2 | 3 | #define STEP 100 4 | #define N 50 5 | 6 | void nn_test(zNURBS *nurbs) 7 | { 8 | int i; 9 | FILE *fp; 10 | zVec v, n, nn; 11 | 12 | v = zVecAlloc( zVecSizeNC( zNURBSCP(nurbs,0) ) ); 13 | n = zVecAlloc( zVecSizeNC( zNURBSCP(nurbs,0) ) ); 14 | nn = zVecAlloc( zVecSizeNC( zNURBSCP(nurbs,0) ) ); 15 | 16 | fp = fopen( "p", "w" ); 17 | for( i=0; i<=STEP; i++ ){ 18 | if( zNURBSVec( nurbs, (double)i/STEP, v ) ) 19 | zVecValueFPrint( fp, v ); 20 | } 21 | fclose( fp ); 22 | fp = fopen( "nn", "w" ); 23 | for( i=0; idata.v,0), zVecElemNC(cp->data.v,1) ); 42 | fclose( fp ); 43 | } 44 | 45 | #define DIM 3 46 | 47 | int main(int argc, char *argv[]) 48 | { 49 | zNURBS nurbs; 50 | zSeq seq; 51 | zVec v; 52 | int num, i; 53 | /* example data array */ 54 | double xp[] = { 2.0, 3.0, 5.0, 4.0, 5.0, 7.0 }; 55 | double yp[] = { 3.0,-1.0,-2.0, 0.0, 4.0, 1.5 }; 56 | 57 | zRandInit(); 58 | /* creation of x-values and y-values vector */ 59 | num = sizeof(xp) / sizeof(double); 60 | zListInit( &seq ); 61 | for( i=0; i 2 | 3 | #define STEP 100 4 | 5 | void test_weight(zNURBS *nurbs, int w) 6 | { 7 | double t; 8 | int i; 9 | FILE *fp; 10 | char filename[BUFSIZ]; 11 | zVec v; 12 | 13 | sprintf( filename, "w%d", w ); 14 | fp = fopen( filename, "w" ); 15 | zNURBSSetWeight( nurbs, 4, (double)w ); 16 | v = zVecAlloc( zVecSizeNC( zNURBSCP(nurbs,0) ) ); 17 | zNURBSSetSlice( nurbs, STEP ); 18 | for( i=0; i<=zNURBSSlice(nurbs); i++ ){ 19 | t = zNURBSKnotSlice( nurbs, i ); 20 | if( zNURBSVec( nurbs, t, v ) ) 21 | zVecValueFPrint( fp, v ); 22 | } 23 | zVecFree( v ); 24 | fclose( fp ); 25 | } 26 | 27 | void output_src(zSeq *seq) 28 | { 29 | FILE *fp; 30 | zSeqCell *cp; 31 | 32 | fp = fopen( "src", "w" ); 33 | zListForEach( seq, cp ) 34 | fprintf( fp, "%f %f\n", zVecElemNC(cp->data.v,0), zVecElemNC(cp->data.v,1) ); 35 | fclose( fp ); 36 | } 37 | 38 | #define ORDER 3 39 | 40 | int main(int argc, char *argv[]) 41 | { 42 | zNURBS nurbs; 43 | zSeq seq; 44 | zVec v; 45 | int order, num, i; 46 | double xp[] = { 2.0, 3.0, 5.0, 4.0, 5.0, 7.0 }; 47 | double yp[] = { 3.0,-1.0,-2.0, 0.0, 4.0, 1.5 }; 48 | 49 | order = argc > 1 ? atoi(argv[1]) : ORDER; 50 | /* create x-y vectors */ 51 | num = sizeof(xp) / sizeof(double); 52 | zListInit( &seq ); 53 | for( i=0; i 2 | 3 | #define TEST_NURBS_FILENAME "test_nurbs.ztk" 4 | 5 | void create_test_nurbs(zNURBS *nurbs) 6 | { 7 | zSeq seq; 8 | zVec v; 9 | int n, i; 10 | double xp[] = { 2.0, 3.0, 5.0, 4.0, 5.0, 7.0 }; 11 | double yp[] = { 3.0,-1.0,-2.0, 0.0, 4.0, 1.5 }; 12 | FILE *fp; 13 | 14 | n = sizeof(xp) / sizeof(double); 15 | zListInit( &seq ); 16 | for( i=0; i 1 ){ 63 | read_test_nurbs( &nurbs ); 64 | } else{ 65 | create_test_nurbs( &nurbs ); 66 | } 67 | output_nurbs( &nurbs ); 68 | zNURBSDestroy( &nurbs ); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /example/ode/adams_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.5 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, Adams, NULL, NULL ); 21 | zODEInit( &ode, 2, 4, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.5 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, BEuler, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | #define A 100.0 4 | #define A0 10.0 5 | 6 | /* sample: exponential function */ 7 | zVec dp(double t, zVec p, void *dummy, zVec dp) 8 | { 9 | zVecSetElem( dp, 0, -A*zVecElem(p,0) ); 10 | return dp; 11 | } 12 | 13 | #define DT 0.01 14 | #define STEP 20 15 | 16 | #define ODES 10 17 | 18 | void output(double t, zVec *x) 19 | { 20 | int i; 21 | double a; 22 | 23 | a = A0*exp(-A*t); 24 | #if 0 25 | /* output error */ 26 | for( i=0; i 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | void output(zVec x1, zVec x2) 12 | { 13 | printf( "%.10g %.10g %.10g %.10g\n", 14 | zVecElem(x1,0), zVecElem(x1,1), zVecElem(x2,0), zVecElem(x2,1) ); 15 | } 16 | 17 | #define DT 0.5 18 | #define T 10.0 19 | 20 | int main(void) 21 | { 22 | zODE ode, ode_dc; 23 | zVec x, x_dc; 24 | double t; 25 | 26 | zODEAssign( &ode, Radau, NULL, NULL ); 27 | zODEAssign( &ode_dc, Radau, NULL, NULL ); 28 | zODEInit( &ode, 2, 0, dp ); 29 | zODEInitDC( &ode_dc, 2, 0, dp ); 30 | x = zVecCreateList( 2, 1.0, 0.0 ); 31 | x_dc = zVecClone( x ); 32 | output( x, x_dc ); 33 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.1 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, Euler, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.5 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, Gauss, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.5 12 | #define T 10.0 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, Gear, NULL, NULL ); 21 | zODEInit( &ode, 2, argc > 1 ? atoi(argv[1]) : 4, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zODEInitHist_Gear( &ode, x ); /* special for Gear method */ 24 | zVecValuePrint( x ); 25 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.1 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, Heun, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec ddp(double t, zVec x, zVec v, void *dummy, zVec a) 5 | { 6 | zVecSetElem( a, 0,-zVecElem(x,0) ); 7 | return a; 8 | } 9 | 10 | #define DT 0.1 11 | #define T 1000.0 12 | 13 | void output2(zVec x, zVec v) 14 | { 15 | printf( "%f %f\n", zVecElem(x,0), zVecElem(v,0) ); 16 | } 17 | 18 | int main(void) 19 | { 20 | zODE2 ode; 21 | zVec x, v; 22 | double t; 23 | 24 | zODE2Assign( &ode, Leapfrog, NULL, NULL, NULL, NULL ); 25 | zODE2Init( &ode, 1, 0, ddp ); 26 | x = zVecCreateList( 1, 1.0 ); 27 | v = zVecAlloc( 1 ); 28 | zODE2InitHistLeapfrog( &ode, x, v, DT ); 29 | output2( x, v ); 30 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec ddp(double t, zVec x, zVec dx, void *dummy, zVec ddx) 5 | { 6 | zVecSetElem( ddx, 0,-zVecElem(x,0) ); 7 | return ddx; 8 | } 9 | 10 | #define DT 0.1 11 | #define T 1000.0 12 | 13 | void output2(zVec x, zVec dx) 14 | { 15 | printf( "%f %f\n", zVecElem(x,0), zVecElem(dx,0) ); 16 | } 17 | 18 | int main(void) 19 | { 20 | zODE2 ode; 21 | zVec x, dx; 22 | double t; 23 | 24 | zODE2Assign( &ode, Regular, NULL, NULL, NULL, NULL ); 25 | zODE2AssignRegular( &ode, RK4 ); 26 | zODE2Init( &ode, 1, 0, ddp ); 27 | x = zVecCreateList( 1, 1.0 ); 28 | dx = zVecAlloc( 1 ); 29 | output2( x, dx ); 30 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec ddp(double t, zVec x, zVec dx, void *dummy, zVec ddx) 5 | { 6 | zVecSetElem( ddx, 0,-zVecElem(x,0) ); 7 | return ddx; 8 | } 9 | 10 | #define DT 0.1 11 | #define T 1000.0 12 | 13 | void output2(zVec x, zVec dx) 14 | { 15 | printf( "%f %f\n", zVecElem(x,0), zVecElem(dx,0) ); 16 | } 17 | 18 | int main(void) 19 | { 20 | zODE2 ode; 21 | zVec x, dx; 22 | double t; 23 | 24 | zODE2Assign( &ode, Sympl, NULL, NULL, NULL, NULL ); 25 | zODE2Init( &ode, 1, 0, ddp ); 26 | x = zVecCreateList( 1, 1.0 ); 27 | dx = zVecAlloc( 1 ); 28 | output2( x, dx ); 29 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.5 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, Radau, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.1 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, RK4, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.1 12 | #define T 10.0 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | if( argc > 1 ){ 21 | switch( atoi(argv[1]) ){ 22 | case 1: zODEAssign( &ode, CK45, NULL, NULL ); break; 23 | case 2: zODEAssign( &ode, DP45, NULL, NULL ); break; 24 | default: zODEAssign( &ode, RKF45, NULL, NULL ); break; 25 | } 26 | } else 27 | zODEAssign( &ode, RKF45, NULL, NULL ); 28 | zODEInit( &ode, 2, 0, dp ); 29 | x = zVecCreateList( 2, 1.0, 0.0 ); 30 | zVecValuePrint( x ); 31 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.5 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, RKF45, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.1 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, RKG, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | /* sample: unit circle function */ 4 | zVec dp(double t, zVec p, void *dummy, zVec v) 5 | { 6 | zVecSetElem( v, 0,-zVecElem(p,1) ); 7 | zVecSetElem( v, 1, zVecElem(p,0) ); 8 | return v; 9 | } 10 | 11 | #define DT 0.5 12 | #define T 10.0 13 | 14 | int main(void) 15 | { 16 | zODE ode; 17 | zVec x; 18 | double t; 19 | 20 | zODEAssign( &ode, TR, NULL, NULL ); 21 | zODEInit( &ode, 2, 0, dp ); 22 | x = zVecCreateList( 2, 1.0, 0.0 ); 23 | zVecValuePrint( x ); 24 | for( t=0; t 2 | 3 | double eval(double x, void *dummy){ 4 | /* return cos(x); */ 5 | return x*(x*(x*(x-12)+47)-60); /* x=1.25 */ 6 | return fabs( (x-1.6)*(x-10)*(x+8) ); /* x=1.6 */ 7 | return (x*x+x+1)*(x-3)*(x-3); /* x=3 */ 8 | return x*x*(x-1)*(x-4)*(x-5)*(x-5); /* symmetric about x=2.5 */ 9 | } 10 | 11 | int main(void) 12 | { 13 | double x; 14 | 15 | x = zOptLineGSEC( eval, -10, 20, NULL, 0 ); 16 | printf( "golden section method : x* = %.16f ( evaluation = %.16f )\n", x, eval( x, NULL ) ); 17 | x = zOptLineBisec( eval, -10, 20, NULL, 0 ); 18 | printf( "bisection method : x* = %.16f ( evaluation = %.16f )\n", x, eval( x, NULL ) ); 19 | x = zOptLineBrent( eval, -10, 20, NULL, 0 ); 20 | printf( "Brent's method : x* = %.16f ( evaluation = %.16f )\n", x, eval( x, NULL ) ); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /example/opt/lp_stdcnv_solve_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | double a_arr[] = { 6 | 3.0, 1.0, 7 | 2.5, 2.0, 8 | 1.0, 2.0, 9 | }; 10 | double b_arr[] = { 11 | 9.0, 12.5, 8.0, 12 | }; 13 | double c_arr[] = { 14 | -3.0, -2.0, 15 | }; 16 | int row = 3, col = 2; 17 | /* answer: [2 3] */ 18 | 19 | zMat a, as; 20 | zVec b, c, cs, x, xs; 21 | double cost; 22 | 23 | a = zMatCloneArray( a_arr, row, col ); 24 | b = zVecCloneArray( b_arr, row ); 25 | c = zVecCloneArray( c_arr, col ); 26 | x = zVecAlloc( col ); 27 | 28 | printf( "\n" ); 29 | printf( "Ax<=b, x>=0, c^T x -> min.\n" ); 30 | printf( "A: " ); zMatPrint( a ); 31 | printf( "b: " ); zVecPrint( b ); 32 | printf( "c: " ); zVecPrint( c ); 33 | zLPIneq2Std( a, c, x, &as, &cs, &xs ); 34 | printf( "standardized form \n" ); 35 | printf( "Ax=b, x>=0, c^T x -> min.\n" ); 36 | printf( "A: " ); zMatPrint( as ); 37 | printf( "b: " ); zVecPrint( b ); 38 | printf( "c: " ); zVecPrint( cs ); 39 | 40 | printf( "\n" ); 41 | if( !zLPSolveSimplex( as, b, cs, xs, &cost ) ){ 42 | printf( "failed.\n" ); 43 | return 0; 44 | } 45 | zVecGet( xs, 0, x ); 46 | zVecPrint( x ); 47 | printf( "cost=%f\n", cost ); 48 | 49 | printf( "\n" ); 50 | if( !zLPSolvePDIP_PC( as, b, cs, xs, &cost ) ){ 51 | printf( "failed.\n" ); 52 | return 0; 53 | } 54 | zVecGet( xs, 0, x ); 55 | zVecPrint( x ); 56 | printf( "cost=%f\n", cost ); 57 | 58 | zMatFree( a ); 59 | zVecFree( b ); 60 | zVecFree( c ); 61 | zVecFree( x ); 62 | zMatFree( as ); 63 | zVecFree( cs ); 64 | zVecFree( xs ); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /example/opt/lp_stdcnv_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | double a_arr[] = { 6 | 3.0, 1.0, 7 | 4.0, 2.0, 8 | 1.0, 2.0, 9 | }; 10 | double c_arr[] = { 11 | -3.0, -2.0, 12 | }; 13 | int row = 3, col = 2; 14 | 15 | zMat a, as; 16 | zVec c, cs, x, xs; 17 | 18 | a = zMatCloneArray( a_arr, row, col ); 19 | c = zVecCloneArray( c_arr, col ); 20 | x = zVecAlloc( col ); 21 | 22 | printf( "\n" ); 23 | printf( "A: " ); zMatPrint( a ); 24 | printf( "c: " ); zVecPrint( c ); 25 | 26 | printf( "\n" ); 27 | zLPIneq2Std( a, c, x, &as, &cs, &xs ); 28 | printf( "A: " ); zMatPrint( as ); 29 | printf( "c: " ); zVecPrint( cs ); 30 | zMatFree( as ); 31 | zVecFree( cs ); 32 | zVecFree( xs ); 33 | 34 | printf( "\n" ); 35 | zLPUnb2Std( a, c, x, &as, &cs, &xs ); 36 | printf( "A: " ); zMatPrint( as ); 37 | printf( "c: " ); zVecPrint( cs ); 38 | zMatFree( as ); 39 | zVecFree( cs ); 40 | zVecFree( xs ); 41 | 42 | zMatFree( a ); 43 | zVecFree( c ); 44 | zVecFree( x ); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /example/opt/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/opt/qp_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | double qarray[] = { 6 | 1, 2,-4, 5 7 | }; 8 | double aarray[] = { 9 | 1, 2 10 | }; 11 | zMat q, a; 12 | zVec z, b, x; 13 | double cost; 14 | 15 | q = zMatAllocSqr( 2 ); 16 | a = zMatAlloc( 1, 2 ); 17 | z = zVecAlloc( 2 ); 18 | b = zVecAlloc( 1 ); 19 | x = zVecAlloc( 2 ); 20 | 21 | zMatCopyArray( qarray, 2, 2, q ); 22 | zMatCopyArray( aarray, 1, 2, a ); 23 | zVecSetElem( b, 0, 3 ); 24 | zMatPrint( q ); 25 | zMatPrint( a ); 26 | zVecPrint( b ); 27 | zQPSolveASM( q, z, a, b, x, &cost ); 28 | zVecPrint( x ); 29 | 30 | zMatFree( q ); 31 | zMatFree( a ); 32 | zVecFree( b ); 33 | zVecFree( x ); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /example/oscillator/kuramoto_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DT 0.001 4 | #define T 10.0 5 | 6 | void output(double t, zOsc *osc, double x) 7 | { 8 | printf( "%f %.10f %.10f %.10f %.10f\n", t, zOscOutput(osc), x, sin(zOscOutput(osc)), sin(x) ); 9 | } 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | zOsc osc; 14 | double x; 15 | int i, n; 16 | 17 | n = T / DT; 18 | if( !zOscCreateKura( &osc, zPI, 100, zPI/2 ) ) return 1; 19 | zOscInit( &osc, 0, 0 ); 20 | output( 0, &osc, 0 ); 21 | for( i=0; i<=n; i++ ){ 22 | x = zPI_2*sin(2*zPI*i*DT); 23 | zOscUpdate( &osc, x, DT ); 24 | output( i*DT, &osc, x ); 25 | } 26 | zOscDestroy( &osc ); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /example/oscillator/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/oscillator/vandelpol_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DT 0.005 4 | #define T 10.0 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | zOsc osc; 9 | double term, eps, amp, x0, v0; 10 | int i, n; 11 | 12 | n = T / DT; 13 | term = argc > 1 ? atof(argv[1]) : 1.0; 14 | eps = argc > 2 ? atof(argv[2]) : 1.5; 15 | amp = argc > 3 ? atof(argv[3]) : 1.0; 16 | x0 = argc > 4 ? atof(argv[4]) : 0.5; 17 | v0 = argc > 5 ? atof(argv[5]) : 0.0; 18 | if( !zOscCreateVDP( &osc, term, eps, amp ) ) return 1; 19 | zOscInit( &osc, x0, v0 ); 20 | for( i=0; i<=n; i++ ){ 21 | zOscUpdate( &osc, 0, DT ); 22 | printf( "%f %.10f\n", i*DT, zOscOutput(&osc) ); 23 | } 24 | zOscDestroy( &osc ); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /example/parse/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/parse/parse_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #if 0 4 | #define STR " +12.3e-4 * a - ( 5+b )/7 " 5 | #elif 0 6 | #define STR "2*3+1*4/3" 7 | #elif 0 8 | #define STR "sin(a)" 9 | #elif 0 10 | #define STR " " 11 | #elif 0 12 | #define STR "(3+3)!" 13 | #elif 0 14 | #define STR "1 2 3" 15 | #elif 1 16 | #define STR "sin(0.5*pi)" 17 | #elif 0 18 | #define STR "2*sin(1+2,3*4+5,6)" 19 | #elif 0 20 | #define STR "testfunc(1+2,3*4+5,6,2*3,5)" 21 | #else 22 | #define STR "{3*(1+2)+4}" 23 | #endif 24 | 25 | int main(int argc, char *argv[]) 26 | { 27 | char buf[BUFSIZ]; 28 | zmParser parser; 29 | 30 | argc > 1 ? strcpy( buf, argv[1] ) : strcpy( buf, STR ); 31 | zmParserInit( &parser ); 32 | printf( "expr: %s\n", buf ); 33 | zmParse( &parser, buf ); 34 | printf( "eval = %f\n", zmParseEval( &parser ) ); 35 | zmParserDestroy( &parser ); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /example/pex/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/pex/pex_diff_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | zPex p, pd, pi; 6 | 7 | p = zPexCreateList( 5, 1.0,-3.0, 7.0, 3.0,-4.0, 2.0 ); 8 | 9 | pd = zPexDif( p ); 10 | pi = zPexIntg( p ); 11 | 12 | printf( "f(x) = " ); 13 | zPexExprX( p ); 14 | printf( "f'(x) = " ); 15 | zPexExprX( pd ); 16 | printf( "F(x) = " ); 17 | zPexExprX( pi ); 18 | 19 | zPexFree( p ); 20 | zPexFree( pd ); 21 | zPexFree( pi ); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /example/pex/pex_io_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | zPex p; 6 | FILE *fp; 7 | 8 | if( !( fp = fopen( "pex_test.dat", "r" ) ) ) 9 | return 1; 10 | p = zPexFScan( fp ); 11 | fclose( fp ); 12 | 13 | zPexExprX( p ); 14 | zPexPrint( p ); 15 | zPexFree( p ); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /example/pex/pex_modulo_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 10 4 | 5 | int main(void) 6 | { 7 | double a; 8 | zPex p1, p2; 9 | zComplex arg, c; 10 | 11 | zRandInit(); 12 | a = (double)zRandI(-10,10); 13 | p1 = zPexAlloc( N ); 14 | zVecLinSpace( p1, 1, N+1 ); 15 | p2 = zPexAlloc( N ); 16 | zPexModulo( p1, a, p2 ); 17 | zPexExprX( p1 ); 18 | printf( "modulo: x%c%f\n", a>0?'+':'-', fabs(a) ); 19 | zVecPrint( p2 ); 20 | 21 | zComplexCreate( &arg, -a, 0 ); 22 | zPexCVal( p1, &arg, &c ); 23 | printf( "check: P(-a) = " ); 24 | zComplexPrint( &c ); 25 | printf( " = %.10g\n", zPexCoeff(p2,0) ); 26 | 27 | zPexFree( p1 ); 28 | zPexFree( p2 ); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /example/pex/pex_out_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | double x; 6 | zPex p; 7 | 8 | p = zPexCreateList( 3, 1.0,-1.0, 3.0,-1.0 ); 9 | zPexFExprX( stderr, p ); 10 | 11 | for( x=-2.0; x<=3.0; x+=0.01 ) 12 | printf( "%f %f %f %f %f %f\n", 13 | zPexDifVal( p, 0, x ), 14 | zPexDifVal( p, 1, x ), 15 | zPexDifVal( p, 2, x ), 16 | zPexDifVal( p, 3, x ), 17 | zPexDifVal( p, 4, x ), 18 | zPexDifVal( p, 5, x ) ); 19 | 20 | zPexFree( p ); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /example/pex/pex_test.dat: -------------------------------------------------------------------------------- 1 | 3 1.0 -2.0 3.5 -4.7 2 | -------------------------------------------------------------------------------- /example/rand/beta_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 10000 4 | #define DIV 100 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x, a, b; 12 | 13 | zRandInitMT( NULL ); 14 | a = argc > 1 ? atof( argv[1] ) : 0.5; 15 | b = argc > 2 ? atof( argv[2] ) : 0.5; 16 | fp = fopen( "bd", "w" ); 17 | for( i=0; i 2 | 3 | #define N 10000 4 | #define DIV 20 5 | int count_x[DIV], count_y[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x, y; 12 | double r; 13 | 14 | zRandInitMT( NULL ); 15 | r = argc > 1 ? atof(argv[1]) : 0.5; 16 | fp = fopen( "2d", "w" ); 17 | for( i=0; i 2 | 3 | #define N 10000 4 | #define M 1000 5 | #define DIV M 6 | int count[DIV]; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | FILE *fp; 11 | int i; 12 | int x; 13 | double p; 14 | 15 | zRandInitMT( NULL ); 16 | p = argc > 1 ? atof(argv[1]) : 0.5; 17 | fp = fopen( "bd", "w" ); 18 | for( i=0; i 2 | 3 | #define N 1000 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | FILE *fp; 8 | int i; 9 | double x, min, max; 10 | int d, div, *count; 11 | 12 | zRandInitMT( NULL ); 13 | min = argc > 1 ? atof( argv[1] ) : -100000; 14 | max = argc > 2 ? atof( argv[2] ) : 100000; 15 | div = max - min + 1; 16 | count = (int *)calloc( div, sizeof(int) ); 17 | fp = fopen( "cd", "w" ); 18 | for( i=0; i max ? div-1 : ( (int)x < min ? 0 : (int)x + div/2 ); 21 | fprintf( fp, "%.15f %d\n", x, ++count[d] ); 22 | } 23 | fclose( fp ); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /example/rand/chisqr_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 10000 4 | #define DIV 1000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x, a; 12 | 13 | zRandInitMT( NULL ); 14 | a = argc > 1 ? atof( argv[1] ) : 0.5; 15 | fp = fopen( "cd", "w" ); 16 | for( i=0; i 2 | 3 | #define N 10000 4 | #define DIV 1000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x; 12 | 13 | zRandInitMT( NULL ); 14 | fp = fopen( "ed", "w" ); 15 | for( i=0; i 2 | 3 | #define N 10000 4 | #define DIV 10000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x, a, b; 12 | int d; 13 | 14 | zRandInitMT( NULL ); 15 | a = argc > 1 ? atof( argv[1] ) : 0.1; 16 | b = argc > 2 ? atof( argv[2] ) : 1.0; 17 | fp = fopen( "fd", "w" ); 18 | for( i=0; i= DIV ) d = DIV-1; 22 | fprintf( fp, "%.15f %d\n", x, ++count[d] ); 23 | } 24 | fclose( fp ); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /example/rand/gamma_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 10000 4 | #define DIV 1000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x, a; 12 | 13 | zRandInitMT( NULL ); 14 | a = argc > 1 ? atof( argv[1] ) : 0.5; 15 | fp = fopen( "gd", "w" ); 16 | for( i=0; i 2 | 3 | #define N 10000 4 | int *count; 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | FILE *fp; 9 | int i; 10 | int div, x; 11 | double p; 12 | 13 | zRandInitMT( NULL ); 14 | p = argc > 1 ? atof(argv[1]) : 0.5; 15 | div = 10.0 / p; 16 | count = (int *)calloc( div, sizeof(int) ); 17 | fp = fopen( "gd", "w" ); 18 | for( i=0; i 2 | 3 | #define N 10000 4 | #define DIV 30 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x; 12 | 13 | zRandInitMT( NULL ); 14 | fp = fopen( "ld", "w" ); 15 | for( i=0; i 2 | 3 | #define N 100000 4 | 5 | int test_glibc(int n) 6 | { 7 | double x, y; 8 | int nc; 9 | int i; 10 | 11 | zRandInit(); 12 | for( nc=0, i=0; i 1 ? atoi( argv[1] ) : N; 50 | eval( n, test_glibc, "Glibc" ); 51 | eval( n, test_mt, "MT " ); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /example/rand/mt_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | double x, y, z; 6 | long long n; 7 | FILE *fp; 8 | int i; 9 | 10 | n = 2 << 10; 11 | printf( "%lld\n", n ); 12 | zRandInit(); 13 | fp = fopen( "mt", "w" ); 14 | for( i=0; i 2 | 3 | #define N 5 4 | 5 | int main(void) 6 | { 7 | long long n; 8 | FILE *fp; 9 | char filename[BUFSIZ]; 10 | int i, j; 11 | 12 | n = 2 << 10; 13 | zRandInit(); 14 | for( i=0; i 2 | 3 | #define N 80000 4 | #define DIV 1000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x; 12 | 13 | zRandInitMT( NULL ); 14 | fp = fopen( "nd", "w" ); 15 | for( i=0; i 2 | 3 | #define N 10000 4 | #define DIV 1000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | int x; 12 | double alpha; 13 | 14 | zRandInitMT( NULL ); 15 | alpha = argc > 1 ? atof( argv[1] ) : 10; 16 | fp = fopen( "pd", "w" ); 17 | for( i=0; i= DIV ) x = DIV - 1; 20 | fprintf( fp, "%d %d\n", x, ++count[x] ); 21 | } 22 | fclose( fp ); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /example/rand/power_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 10000 4 | #define M 100 5 | #define DIV 1000 6 | int count[DIV]; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | FILE *fp; 11 | int i; 12 | double x, n; 13 | 14 | zRandInitMT( NULL ); 15 | n = argc > 1 ? atof(argv[1]) : M; 16 | fp = fopen( "pd", "w" ); 17 | for( i=0; i 2 | 3 | #define N 10000 4 | #define DIV 1000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | double x; 10 | FILE *fp; 11 | int i; 12 | double n; 13 | int d; 14 | 15 | zRandInitMT( NULL ); 16 | n = argc > 1 ? atof(argv[1]) : 2.0; 17 | if( n < 2.0 ){ 18 | ZRUNWARN( "probably unappropriate n=%f, modified to be 2.0", n ); 19 | n = 2.0; 20 | } 21 | fp = fopen( "td", "w" ); 22 | for( i=0; i 2 | 3 | #define N 100000 4 | #define DIV 1000 5 | int count[2*DIV+1]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x; 12 | 13 | zRandInitMT( NULL ); 14 | fp = fopen( "td", "w" ); 15 | for( i=0; i 2 | 3 | #define N 10000 4 | #define DIV 1000 5 | int count[DIV]; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FILE *fp; 10 | int i; 11 | double x; 12 | double alpha; 13 | 14 | zRandInitMT( NULL ); 15 | alpha = argc > 1 ? atof(argv[1]) : 0.5; 16 | fp = fopen( "wd", "w" ); 17 | for( i=0; i=DIV ? DIV-1 : (int)x] ); 20 | } 21 | fclose( fp ); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /example/seq/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/seq/seq_add_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DT 0.1 4 | 5 | int main(void) 6 | { 7 | zSeq seq; 8 | zVec v; 9 | int i; 10 | 11 | zSeqInit( &seq ); 12 | for( i=0; i<10; i++ ){ 13 | v = zVecCreateList( 1, (double)i ); 14 | zSeqEnqueue( &seq, v, DT ); 15 | } 16 | zSeqPrintFile( &seq, "dummy" ); 17 | zSeqFree( &seq ); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /example/seq/seq_del_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DT 0.1 4 | 5 | int main(void) 6 | { 7 | zSeq seq; 8 | zVec v; 9 | zSeqCell *cp; 10 | double t = 0; 11 | int i; 12 | 13 | zSeqInit( &seq ); 14 | for( i=0; i<10; i++ ){ 15 | v = zVecCreateList( 1, (double)i ); 16 | zSeqEnqueue( &seq, v, DT ); 17 | } 18 | while( !zListIsEmpty( &seq ) ){ 19 | cp = zSeqDequeue( &seq ); 20 | printf( "%g ", t ); 21 | zVecPrint( cp->data.v ); 22 | t += cp->data.dt; 23 | zSeqCellFree( cp ); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /example/seq/seq_jump_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | zSeq seq; 6 | zSeqCell *cp; 7 | int i; 8 | 9 | zSeqScanFile( &seq, "test" ); 10 | /* a warning is issued at the end. */ 11 | for( i=0; i<=zListSize(&seq); i++ ){ 12 | if( !( cp = zSeqJump( &seq, i ) ) ) continue; 13 | printf( "jump to %2d : %g ", i, cp->data.dt ); 14 | zVecPrint( cp->data.v ); 15 | } 16 | zSeqFree( &seq ); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /example/seq/test.zvs: -------------------------------------------------------------------------------- 1 | 0.1 3 0 0 0 2 | 0.1 3 0 0 1 3 | 0.1 3 0 1 2 4 | 0.1 3 1 2 3 5 | 0.1 3 2 3 4 6 | 0.1 3 3 4 5 7 | 0.1 3 4 5 6 8 | 0.1 3 5 6 7 9 | 0.1 3 6 7 8 10 | 0.1 3 7 8 9 11 | 0.1 3 8 9 10 12 | 0.1 3 9 10 10 13 | 0.1 3 10 10 10 14 | -------------------------------------------------------------------------------- /example/sf/bessel_i_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int i; 6 | double x; 7 | 8 | for( i=-1000; i<=1000; i++ ){ 9 | x = 0.02 * ((double)i); 10 | printf( "%.16f %.16f %.16f %.16f %.16f\n", x, zBesselI(0,x), zBesselI(1,x), zBesselI(2,x), zBesselI(3,x) ); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /example/sf/bessel_j_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int i; 6 | double x; 7 | 8 | for( i=-1000; i<=1000; i++ ){ 9 | x = 0.02 * ((double)i); 10 | printf( "%.10f %.10f %.10f %.10f %.10f\n", x, zBesselJ(0,x), zBesselJ(1,x), zBesselJ(2,x), zBesselJ(3,x) ); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /example/sf/bessel_k_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int i; 6 | double x; 7 | 8 | for( i=-1000; i<=1000; i++ ){ 9 | x = 0.02 * ((double)i); 10 | printf( "%.16f %.16f %.16f %.16f %.16f\n", x, zBesselK(0,x), zBesselK(1,x), zBesselK(2,x), zBesselK(3,x) ); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /example/sf/bessel_y_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int i; 6 | double x; 7 | 8 | for( i=1; i<=1000; i++ ){ 9 | x = 0.02 * ((double)i); 10 | printf( "%.16f %.16f %.16f %.16f %.16f\n", x, zBesselY(0,x), zBesselY(1,x), zBesselY(2,x), zBesselY(3,x) ); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /example/sf/beta_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int i, j; 6 | double z, w; 7 | 8 | for( i=1; i<=30; i++ ){ 9 | z = ((double)i) / 10.0; 10 | for( j=1; j<=50; j++ ){ 11 | w = ((double)j) / 10.0; 12 | printf( "%.10f %.10f %.10f\n", z, w, zBeta(z,w) ); 13 | } 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /example/sf/erf_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double erf_ap1(double x) 4 | { 5 | double a, x2, val; 6 | 7 | x2 = x*x; 8 | a = ( 8 * ( zPI - 3 ) / ( 3 * zPI * ( zPI - 4 ) ) ) * x2; 9 | val = sqrt( 1 - exp( -x2 * ( 4/zPI + a ) / ( 1 + a ) ) ); 10 | return x >= 0 ? val : -val; 11 | } 12 | 13 | /* http://invar6.blog.fc2.com/blog-entry-16.html */ 14 | double erf_ap2(double x) 15 | { 16 | long double x2, val; 17 | 18 | x2 = x*x; 19 | val = sqrt( 1 - exp( x2 * -1.2732395447351626861510701069801 ) 20 | * ( 1 + x2 * x2 * ( 3.8256894018072952549677698975590e-2 21 | + x2 * (-3.3924118631479317799557719571888e-3 22 | + x2 * ( 6.8005689892920536537036277150071e-4 23 | + x2 * (-7.0227051009098722650256537001220e-5 24 | + x2 * ( 7.6148050059630352704610740846049e-6 25 | + x2 * (-6.6700609990522378462586732796781e-7 26 | + x2 * ( 5.3870107438215609185130430012239e-8 27 | + x2 * (-3.8693709096400193758628383851182e-9 28 | + x2 * 2.5441273130247078605512789263896e-10 ) ) ) ) ) ) ) ) ) ); 29 | return x >= 0 ? val : -val; 30 | } 31 | 32 | /* Williamson=Yamashita formula */ 33 | double erf_wy(double x) 34 | { 35 | long double x2, val; 36 | 37 | x2 = x*x; 38 | val = sqrt( 1 - exp( x2 * -1.2732395447351626861510701069801 ) 39 | * ( 1 + x2 * x2 * ( 0.1101999999998335 40 | / ( x2 + 7.2085122317063002 ) 41 | + 0.0219999999999997 ) ) ); 42 | return x >= 0 ? val : -val; 43 | } 44 | 45 | 46 | #define W 5.0 47 | #define DIV 1000 48 | 49 | int main(int argc, char *argv[]) 50 | { 51 | int i; 52 | double x, f, f0, f1, f2, f3; 53 | 54 | for( i=0; i<=DIV; i++ ){ 55 | x = W * ( (double)i/DIV - 0.5 ); 56 | f = erf(x); 57 | f0 = zErf(x); 58 | f1 = erf_ap1(x); 59 | f2 = erf_ap2(x); 60 | f3 = erf_wy(x); 61 | printf( "%.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g\n", x, f, f0, f1, f2, f3, f-f0, f-f1, f-f2, f-f3 ); 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /example/sf/fresnel_gen_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 1000 4 | #define DIV 6 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | int i, k; 9 | double x, s, c, a; 10 | 11 | for( i=-N; i<=N; i++ ){ 12 | x = 5*(double)i/N; 13 | printf( "%.10g", x ); 14 | for( k=-DIV; k<=DIV; k++ ){ 15 | a = zPI*k/DIV; 16 | zFresnelIntgGen( x, zPI/12, zPI/3-a, a, &s, &c ); 17 | printf( " %.10g %.10g", c, s ); 18 | } 19 | printf( "\n" ); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /example/sf/fresnel_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 1000 4 | #define DIV 3 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | int i, k; 9 | double x, s, c; 10 | 11 | for( i=-N; i<=N; i++ ){ 12 | x = 10*(double)i/N; 13 | zFresnelIntg( x, &s, &c ); 14 | printf( "%.10g %.10g %.10g", x, s, c ); 15 | for( k=1; k<=DIV; k++ ){ 16 | zFresnelIntgScale( x, 1+(zPI_2-1)*(double)k/DIV, &s, &c ); 17 | printf( " %.10g %.10g", s, c ); 18 | } 19 | zFresnelIntgPI_2( x, &s, &c ); 20 | printf( " %.10g %.10g\n", s, c ); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /example/sf/gamma_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int i; 6 | double x; 7 | 8 | for( i=-5000; i<=5000; i++ ){ 9 | x = ((double)i) / 1000.0; 10 | printf( "%.10f %.10f %.10f %.10f\n", x, exp(zLnGamma(x)), zGamma(x), tgamma(x) ); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /example/sf/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/stat/distrib_test.c: -------------------------------------------------------------------------------- 1 | /* distribution test */ 2 | /* this example shows the use of 3 | * 1: normal distribution 4 | * 2: poisson's distribution 5 | * 3: binomial distribution 6 | */ 7 | #include 8 | 9 | #define DX (double)1.0 10 | 11 | int main(void) 12 | { 13 | int i; 14 | double x; 15 | 16 | for( i=0; i<=20; i++ ){ 17 | x = DX * i; 18 | printf( "%f %f %f %f\n", x, zNormalDistrib( x, 3, 2 ), zPoissonDistrib( x, 10 ), zBinDistrib( x, 20, 0.5 ) ); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /example/stat/histogram_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define N 1000 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | zHistogram histogram; 9 | double data[N]; 10 | int i; 11 | 12 | zRandInit(); 13 | for( i=0; i 1 ? atoi(argv[1]) : 20 ); 16 | zHistogramFPrint( stdout, &histogram ); 17 | zHistogramDestroy( &histogram ); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /example/stat/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/stat/normaldistrib_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DX (double)1.0 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int i; 8 | double x; 9 | 10 | for( i=-17; i<=20; i++ ){ 11 | x = DX * i; 12 | printf( "%.10g %.10g %.10g\n", x, zNormalDistrib( x, 3, 2 ), zNormalCumDistrib( x, 3, 2 ) ); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /example/vec/makefile: -------------------------------------------------------------------------------- 1 | ../makefile -------------------------------------------------------------------------------- /example/vec/rdp_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 1000 4 | 5 | int main(void) 6 | { 7 | zVecList list; 8 | zVec v; 9 | double angle; 10 | int i; 11 | FILE *fp; 12 | 13 | zListInit( &list ); 14 | v = zVecAlloc( 2 ); 15 | for( i=0; i 2 | 3 | #define DIM 3 4 | #define N 5 5 | 6 | int main(void) 7 | { 8 | zVec s, k; 9 | zVecRing v; 10 | int i; 11 | double val; 12 | 13 | s = zVecAlloc( DIM ); 14 | k = zVecCreateList( N, 1.0/1.0, 1.0/2.0, 1.0/3.0, 1.0/4.0, 1.0/5.0 ); 15 | zVecRingAlloc( &v, DIM, N ); 16 | for( i=0; i 2 | 3 | #define N 20 4 | 5 | int main(void) 6 | { 7 | int i; 8 | zVec v; 9 | zIndex idx; 10 | 11 | zRandInit(); 12 | v = zVecAlloc(N); 13 | idx = zIndexCreate(N); 14 | for( i=0; i 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | 55 | #endif /* __ZM_H__ */ 56 | -------------------------------------------------------------------------------- /include/zm/zm_complex_pe.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_complex_pe - complex number class: polynomial equation solver. 5 | */ 6 | 7 | #ifndef __ZM_COMPLEX_PE_H__ 8 | #define __ZM_COMPLEX_PE_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief solve quadratic and cubic equation. 15 | * 16 | * zQESolve() finds two answers including duplicate answer of 17 | * a quadratic equation \a a x^2 + \a b x + \a c = 0. 18 | * \a a must be nonzero. 19 | * The answers are put into an array pointed by \a ans. 20 | * 21 | * zCESolve() finds three answers including duplicate answer of 22 | * a cubic equation \a a x^3 + \a b x^2 + \a c x + \a d = 0 23 | * by Cardano's formula (originally derived by Fontana=Tartaglia). 24 | * \a a must be nonzero. 25 | * The answers are put into an array pointed by \a ans. 26 | * \return 27 | * zQESolve() and zCESolve() return \a ans if succeeding. 28 | * If \a a is zero, the null pointer is returned. 29 | * \notes 30 | * The size of the array pointed by \a ans must be larger than 31 | * 2 for zQESolve() and 3 for zCESolve(), respectively. 32 | * If not, anything might happen. 33 | */ 34 | __ZM_EXPORT zComplex *zQESolve(double a, double b, double c, zComplex ans[]); 35 | __ZM_EXPORT zComplex *zCESolve(double a, double b, double c, double d, zComplex ans[]); 36 | 37 | __END_DECLS 38 | 39 | #endif /* __ZM_COMPLEX_PE_H__ */ 40 | -------------------------------------------------------------------------------- /include/zm/zm_data.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_data.h 5 | * \brief data analysis. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_DATA_H__ 10 | #define __ZM_DATA_H__ 11 | 12 | #include 13 | 14 | __BEGIN_DECLS 15 | 16 | /*! \brief pick up peaks a data sequence. */ 17 | __ZM_EXPORT zIndex zDataPeak(double src[], size_t n, uint w); 18 | 19 | /*! \brief sort an array of double-precision floating-point vector in the descent order. */ 20 | __ZM_EXPORT double *zDataSort(double data[], size_t n); 21 | 22 | /*! \brief sort an integer vector in the descent order of the corresponding samples. */ 23 | __ZM_EXPORT zIndex zDataSortIndex(double data[], size_t n, zIndex index); 24 | 25 | /*! \brief sort an integer vector in the descent order of absolute values of the corresponding samples. */ 26 | __ZM_EXPORT zIndex zDataSortAbsIndex(double data[], size_t n, zIndex index); 27 | 28 | /*! \brief smooth a data sequence based on Savitzky-Golay's method */ 29 | __ZM_EXPORT bool zDataSmoothSG(double src[], size_t n, size_t w, uint dim, double dest[]); 30 | 31 | /*! \brief pick up peaks of a smoothed data sequence based on Savitzky-Golay's method. */ 32 | __ZM_EXPORT zIndex zDataPeakSG(double src[], size_t n, size_t w, uint dim); 33 | 34 | __END_DECLS 35 | 36 | #include 37 | 38 | #endif /* __ZM_DATA_H__ */ 39 | -------------------------------------------------------------------------------- /include/zm/zm_fourier.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_fourier - Fourier series. 5 | */ 6 | 7 | #ifndef __ZM_FOURIER_H__ 8 | #define __ZM_FOURIER_H__ 9 | 10 | #include 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \struct zFourier 15 | * \brief finite Fourier series class 16 | */ 17 | ZDEF_STRUCT( __ZM_CLASS_EXPORT, zFourier ){ 18 | int n; /*!< \brief number of terms */ 19 | double *sc; /*!< \brief coefficients on sine terms */ 20 | double *cc; /*!< \brief coefficients on cosine terms */ 21 | }; 22 | 23 | /*! \brief set a coefficient on a sine term without checking the index. */ 24 | #define zFourierSetSinCoeffNC(f,i,c) ( (f)->sc[i] = (c) ) 25 | /*! \brief set a coefficient on a cosine term without checking the index. */ 26 | #define zFourierSetCosCoeffNC(f,i,c) ( (f)->cc[i] = (c) ) 27 | 28 | /*! \brief set a coefficient on a sine term. */ 29 | #define zFourierSetSinCoeff(f,i,c) ( (i) >= 0 && (i) < (f)->n ? zFourierSetSinCoeffNC( f, i, (i) == 0 ? 0 : (c) ) : 0) 30 | /*! \brief set a coefficient on a cosine term. */ 31 | #define zFourierSetCosCoeff(f,i,c) ( (i) >= 0 && (i) < (f)->n ? zFourierSetCosCoeffNC( f, i, c ) : 0 ) 32 | 33 | /*! \brief allocate internal memory for Fourier series. */ 34 | __ZM_EXPORT zFourier *zFourierAlloc(zFourier *f, int n); 35 | 36 | /*! \brief free internal memory for Fourier series. */ 37 | __ZM_EXPORT void zFourierFree(zFourier *f); 38 | 39 | /*! \brief value of a Fourier series. */ 40 | __ZM_EXPORT double zFourierVal(zFourier *f, double t); 41 | 42 | /*! \brief velocity of a Fourier series. */ 43 | __ZM_EXPORT double zFourierVel(zFourier *f, double t); 44 | 45 | /*! \brief acceleration of a Fourier series. */ 46 | __ZM_EXPORT double zFourierAcc(zFourier *f, double t); 47 | 48 | /*! \brief print out coefficients of a Fourier series. */ 49 | __ZM_EXPORT void zFourierFPrint(FILE *fp, zFourier *fourier); 50 | 51 | __END_DECLS 52 | 53 | #endif /* __ZM_FOURIER_H__ */ 54 | -------------------------------------------------------------------------------- /include/zm/zm_graph_search.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_graph_search.h 5 | * \brief shortest-path-finding methods for graph class. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_GRAPH_SEARCH_H__ 10 | #define __ZM_GRAPH_SEARCH_H__ 11 | 12 | /* NOTE: never include this header file in user programs. */ 13 | 14 | __BEGIN_DECLS 15 | 16 | /* ********************************************************** */ 17 | /*! \defgroup graph. 18 | * \{ *//* ************************************************** */ 19 | 20 | /*! \brief find the shortest path of a graph by A* algorithm. 21 | * 22 | * zGraphSearchAStar() finds the shortest path of a graph \a graph 23 | * from \a start to \a goal, where they are the node identifiers. 24 | * The resulted path is stored in \a path in the order from the 25 | * start to the goal. 26 | * \retval the minimum cost to get to the goal from the start. 27 | * \notes a heuristic function should be assigned in advance as 28 | * \a graph->h = h, for instance. 29 | */ 30 | __ZM_EXPORT double zGraphSearchAStar(zGraph *graph, void *start, void *goal, void *util, zGraphNodeList *path); 31 | 32 | /*! \brief find the shortest path of a graph by Dijkstra's method. 33 | * 34 | * zGraphSearchDijkstra() finds the shortest path of a graph \a graph 35 | * from \a start to \a goal, where they are the node identifiers. 36 | * The resulted path is stored in \a path in the order from the 37 | * start to the goal. 38 | * \retval the minimum cost to get to the goal from the start. 39 | */ 40 | __ZM_EXPORT double zGraphSearchDijkstra(zGraph *graph, void *start, void *goal, zGraphNodeList *path); 41 | 42 | /*! \} */ 43 | 44 | __END_DECLS 45 | 46 | #endif /* __ZM_GRAPH_SEARCH_H__ */ 47 | -------------------------------------------------------------------------------- /include/zm/zm_ieee.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_ieee.h 5 | * \brief IEEE conforming irregular values. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_IEEE_H__ 10 | #define __ZM_IEEE_H__ 11 | 12 | /* NOTE: never include this header file in user programs. */ 13 | 14 | __BEGIN_DECLS 15 | 16 | /*! \cond __ieee_fp_t only required for my HUGE_VAL and NAN */ 17 | typedef union{ ubyte c[8]; double d; } __ieee_fp_t; 18 | /*! \endcond */ 19 | 20 | /*! \brief HUGE_VAL conforming to IEEE 754 floating point. */ 21 | __ZM_EXPORT const __ieee_fp_t zm_huge_val; 22 | #ifndef HUGE_VAL 23 | #define HUGE_VAL ( zm_huge_val.d ) 24 | #endif /* HUGE_VAL */ 25 | 26 | /*! \brief check if the value \a x is infinity. */ 27 | __ZM_EXPORT int zIsInf(double x); 28 | 29 | /*! \brief Not-a-number conforming to IEEE 754 floating point. */ 30 | __ZM_EXPORT const __ieee_fp_t zm_nan_val; 31 | #ifndef NAN 32 | #define NAN ( zm_nan_val.d ) 33 | #endif /* NAN */ 34 | 35 | /*! \brief check if the value \a x is not-a-number. */ 36 | __ZM_EXPORT int zIsNan(double x); 37 | 38 | /*! \brief check if the value is a finite number. */ 39 | __ZM_EXPORT int zIsFinite(double x); 40 | 41 | __END_DECLS 42 | 43 | #endif /* __ZM_IEEE_H__ */ 44 | -------------------------------------------------------------------------------- /include/zm/zm_ip_akima.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ip_akima - interpolation: Akima's interpolation (1970). 5 | */ 6 | 7 | #ifndef __ZM_IP_AKIMA_H__ 8 | #define __ZM_IP_AKIMA_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief create Akima interpolator. 15 | * 16 | * zIPCreateAkima() creates an Akima interpolator \a ip proposed by H. Akima in 1970. 17 | * \a seq is a sequence of points to be interpolated. 18 | * It connects the points in a similar way to a handwriting. 19 | * 20 | * zIPCreateModifiedAkima() creates a modified Akima interporator \a ip from a sequence of points \a seq 21 | * to be interpolated. 22 | * \return 23 | * zIPCreateAkima() and zIPCreateModifiedAkima() return a pointer \a ip when they succeed to create the 24 | * interpolator. Otherwise, the null pointer is returned. 25 | */ 26 | __ZM_EXPORT bool zIPCreateAkima(zIP *ip, const zSeq *seq); 27 | __ZM_EXPORT bool zIPCreateModifiedAkima(zIP *ip, const zSeq *seq); 28 | 29 | __END_DECLS 30 | 31 | #endif /* __ZM_IP_AKIMA_H__ */ 32 | -------------------------------------------------------------------------------- /include/zm/zm_ip_chebyshev.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ip_chebyshev - interpolation: Chebyshev's interpolation. 5 | */ 6 | 7 | #ifndef __ZM_IP_CHEBYSHEV_H__ 8 | #define __ZM_IP_CHEBYSHEV_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* Chebyshev's interpolation interpolates n points by a combination of Chebyshev base functions. */ 15 | __ZM_EXPORT bool zIPCreateChebyshev(zIP *ip, const zSeq *seq); 16 | 17 | __END_DECLS 18 | 19 | #endif /* __ZM_IP_CHEBYSHEV_H__ */ 20 | -------------------------------------------------------------------------------- /include/zm/zm_ip_clothoid.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_ip_clothoid.h 5 | * \brief interpolation: clothoid curve interpolation. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_IP_CLOTHOID_H__ 10 | #define __ZM_IP_CLOTHOID_H__ 11 | 12 | /* NOTE: never include this header file in user programs. */ 13 | 14 | __BEGIN_DECLS 15 | 16 | /*! \struct clothoid curve interpolator. */ 17 | ZDEF_STRUCT( __ZM_CLASS_EXPORT, zClothoid ){ 18 | double x0, y0; /*!< \brief initial point */ 19 | double f0, f1; /*!< \brief initial and terminal angle */ 20 | double fc; /*!< \brief curvature */ 21 | double fs; /*!< \brief shrinkage */ 22 | double _h; /*!< \brief length of segment */ 23 | }; 24 | 25 | /*! \brief create a segment of clothoid curve. 26 | * 27 | * zClothoidCreateSegment() creates a segment of clothoid curve \a cl that 28 | * connects (\a x0, \a y0) and (\a x1, \a y1) with the initial and terminal 29 | * angles \a f0 and \a f1, respectively. 30 | * \return 31 | * zClothoidCreateSegment() returns a pointer \a cl. 32 | */ 33 | __ZM_EXPORT zClothoid *zClothoidCreateSegment(zClothoid *cl, double x0, double y0, double f0, double x1, double y1, double f1); 34 | 35 | /*! \brief x-y values of a clothoid curve. 36 | * 37 | * zClothoidXY() finds a coordinate on a clothoid curve segment \a cl at 38 | * a parameter \a s. The result is stored into (\a x, \a y). 39 | * \return 40 | * zClothoidXY() returns the true value if it succeeds to compute the values. 41 | * Otherwise, the false value is returned. 42 | */ 43 | __ZM_EXPORT bool zClothoidXY(const zClothoid *cl, double s, double *x, double *y); 44 | 45 | __END_DECLS 46 | 47 | #endif /* __ZM_IP_CLOTHOID_H__ */ 48 | -------------------------------------------------------------------------------- /include/zm/zm_ip_lagrange.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ip_lagrange - interpolation: Lagrange's interpolation. 5 | */ 6 | 7 | #ifndef __ZM_IP_LAGRANGE_H__ 8 | #define __ZM_IP_LAGRANGE_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* \brief create Lagrange interpolator. 15 | * 16 | * zIPCreateLagrange() creates a Lagrange interpolator \a ip. 17 | * It connects given n points by n-1 th order polynomial curve. 18 | * \a seq is a sequence of points to be interpolated. 19 | * \return 20 | * zIPCreateLagrange() returns a pointer \a ip when it succeeds to create the interpolator. Otherwise, it 21 | * returns the null pointer. 22 | */ 23 | __ZM_EXPORT bool zIPCreateLagrange(zIP *ip, const zSeq *seq); 24 | 25 | __END_DECLS 26 | 27 | #endif /* __ZM_IP_LAGRANGE_H__ */ 28 | -------------------------------------------------------------------------------- /include/zm/zm_ip_lin.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ip_lin - interpolation: linear interpolation. 5 | */ 6 | 7 | #ifndef __ZM_IP_LIN_H__ 8 | #define __ZM_IP_LIN_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* \brief linear interpolator. 15 | * 16 | * zIPCreateLinear() creates a linear interpolator \a ip. 17 | * It connects given n points by segmented lines. 18 | * \a seq is a sequence of points to be interpolated. 19 | * \return 20 | * zIPCreateLinear() returns a pointer \a ip when it succeeds to create the interpolator. Otherwise, it 21 | * returns the null pointer. 22 | */ 23 | __ZM_EXPORT bool zIPCreateLinear(zIP *ip, const zSeq *seq); 24 | 25 | __END_DECLS 26 | 27 | #endif /* __ZM_IP_LIN_H__ */ 28 | -------------------------------------------------------------------------------- /include/zm/zm_le_lq.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_le_lq - linear equation: LQ/QR decomposition. 5 | */ 6 | 7 | #ifndef __ZM_LE_LQ_H__ 8 | #define __ZM_LE_LQ_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief LQ/QR decomposition based on Gram=Schmidt's method. 15 | * 16 | * zMatDecompLQDST() decompose given matrix \a m into a lower triangular 17 | * matrix \a l and a normalized orthogonal matrix \a q, based on Gram=Schmidt's 18 | * orthogonalization method, namely, 19 | * \a l \a q = \a m. 20 | * It destroys \a m during the computation. 21 | * 22 | * zMatDecompLQ() does LQ decomposition without destroying \a m. 23 | * 24 | * zQRDecomp() does QR decomposition, a transpose of \a zMatDecompLQ(), namely, 25 | * \a q \a r = \a m 26 | * \return 27 | * zMatDecompLQDST() and zMatDecompLQ() return column-rank of \a m. 28 | * zQRDecomp() returns row-rank of \a m. 29 | * \notes 30 | * The null pointer is assignable for \a l or \a r. 31 | * When the null pointer is given for them, these functions compute only the 32 | * orthogonal matrix \a q. 33 | * 34 | * If one column / row or more are dependent on other columns / rows, those 35 | * functions fail to decompose the given matrix into an orthogonal space. 36 | */ 37 | __ZM_EXPORT int zMatDecompLQDST(zMat m, zMat l, zMat q, zIndex idx); 38 | __ZM_EXPORT int zMatDecompLQ(const zMat m, zMat l, zMat q, zIndex idx); 39 | __ZM_EXPORT int zMatDecompLQReg(const zMat m, zMat l, zMat q, zIndex idx); 40 | 41 | /*! \brief LQ decomposition with an automatic matrix allocation and resize. 42 | */ 43 | __ZM_EXPORT int zMatDecompLQAlloc(const zMat m, zMat *l, zMat *q, zIndex *idx); 44 | 45 | __ZM_EXPORT int zMatDecompQR(const zMat m, zMat q, zMat r, zIndex idx); 46 | 47 | __END_DECLS 48 | 49 | #endif /* __ZM_LE_LQ_H__ */ 50 | -------------------------------------------------------------------------------- /include/zm/zm_le_lu.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_le_lu - linear equation: LU decomposition. 5 | */ 6 | 7 | #ifndef __ZM_LE_LU_H__ 8 | #define __ZM_LE_LU_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief LU decomposition. 15 | * 16 | * zMatDecompLU() decomposes the matrix \a m into a lower 17 | * triangular matrix \a l and an upper triangular matrix \a u 18 | * with Crout method. 19 | * \a index is an index vector for order discription. 20 | * 21 | * zMatDecompLUReg() regresses \a l and \a u after decomposing 22 | * \a m, if the rank of \a m less than its size. 23 | * 24 | * zMatDecompLUDST() destroys \a m during decomposition. 25 | * \return 26 | * zMatDecompLUDST(), zMatDecompLU() and zMatDecompLUReg() return the 27 | * rank of \a m, which becomes the same with the minimum of 28 | * the row and column size of \a m when \a m is full rank. 29 | */ 30 | __ZM_EXPORT int zMatDecompLUDST(zMat m, zMat l, zMat u, zIndex idx); 31 | __ZM_EXPORT int zMatDecompLU(const zMat m, zMat l, zMat u, zIndex idx); 32 | __ZM_EXPORT int zMatDecompLUReg(const zMat m, zMat l, zMat u, zIndex idx); 33 | __ZM_EXPORT int zMatDecompLUAlloc(const zMat m, zMat *l, zMat *u, zIndex *idx); 34 | 35 | /* Cholesky decomposition. 36 | * 37 | * zMatDecompCholesky() decomposes a symmetric matrix \a m into 38 | * \a l \a l ^T. 39 | * 40 | * zMatDecompCholeskyDST() destroys \a m during decomposition. 41 | * \return 42 | * zMatDecompCholeskyDST() and zMatDecompCholesky() return the rank 43 | * of \a m. In case they fail to allocate internal memory for 44 | * the computation, -1 is returned. 45 | */ 46 | __ZM_EXPORT int zMatDecompCholeskyDST(zMat m, zMat l, zIndex index); 47 | __ZM_EXPORT int zMatDecompCholesky(const zMat m, zMat l, zIndex index); 48 | __ZM_EXPORT int zMatDecompCholeskyReg(const zMat m, zMat l, zIndex idx); 49 | __ZM_EXPORT int zMatDecompCholeskyAlloc(const zMat m, zMat *l, zIndex *idx); 50 | 51 | __END_DECLS 52 | 53 | #endif /* __ZM_LE_LU_H__ */ 54 | -------------------------------------------------------------------------------- /include/zm/zm_le_lyapnov.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_le_lyapnov - linear equation: Lyapnov equation. 5 | */ 6 | 7 | #ifndef __ZM_LE_LYAPNOV_H__ 8 | #define __ZM_LE_LYAPNOV_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief Lyapnov equation solver. 15 | * 16 | * zLELyapnovSolve() solves Lyapnov equation which forms: 17 | * X A + A^T X = B 18 | * where A, B and X are square matrices of the same size. 19 | * \a a, \a b and \a ans correspond to A, B and X, respectively. 20 | * \return 21 | * zLELyapnovSolve() returns the pointer to \a ans if succeeds. 22 | * The null pointer is returned in the following cases. 23 | * 1. Any of \a a, \a b and \a ans is not a square matrix. 24 | * 2. The size of \a a, \a b and \a ans are different from the others. 25 | * 3. Fails to allocate working memory. 26 | * 4. \a a is a singular matrix. 27 | * \notes 28 | * It internally calls zLESolveGaussDST(). 29 | * \sa 30 | * zLESolveGaussDST 31 | */ 32 | __ZM_EXPORT zMat zLELyapnovSolve(const zMat a, const zMat b, zMat ans); 33 | 34 | __END_DECLS 35 | 36 | #endif /* __ZM_LE_LYAPNOV_H__ */ 37 | -------------------------------------------------------------------------------- /include/zm/zm_le_mat_mpinv.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_le_mpinv - linear equation: Moore=Penrose inverse matrix. 5 | */ 6 | 7 | #ifndef __ZM_LE_MPINV_H__ 8 | #define __ZM_LE_MPINV_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief Moore=Penrose inverse matrix. 15 | * 16 | * zMatMPInv() calculates the Moore=Penrose inverse matrix of an 17 | * arbitrary matrix \a m. The result is put into \a mp. 18 | * Suppose a matrix B is the Moore=Penrose inverse matrix of 19 | * a matrix A. The following relations are satisfied. 20 | * A B A = A 21 | * B A B = B 22 | * ( A B )^T = A B 23 | * ( B A )^T = B A 24 | * 25 | * The computation algorithm is based on Penrose s iterative method. 26 | * \return 27 | * zMatMPInv() returns the rank of the original matrix \a m. 28 | */ 29 | __ZM_EXPORT int zMatMPInv(const zMat m, zMat mp); 30 | __ZM_EXPORT int zMatMPInvPenrose(const zMat m, zMat mp); 31 | 32 | /*! \brief MP inverse with its null space. 33 | */ 34 | __ZM_EXPORT int zMatMPInvNull(const zMat m, zMat mp, zMat mn); 35 | 36 | __ZM_EXPORT zMat zMulMPInvMatMat(const zMat m1, const zMat m2, zMat m); 37 | 38 | __END_DECLS 39 | 40 | #endif /* __ZM_LE_MPINV_H__ */ 41 | -------------------------------------------------------------------------------- /include/zm/zm_le_pivot.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_le_pivot - linear equation: pivoting. 5 | */ 6 | 7 | #ifndef __ZM_LE_PIVOT_H__ 8 | #define __ZM_LE_PIVOT_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief matrix pivoting. 15 | * 16 | * zMatPivoting() does partial pivoting on the matrix \a m on the \a c th column. 17 | * \a index is a row-ordering index vector. 18 | * Pivoting begins from \a r of \a index row. For example, if \a index is { 1, 3, 0, 2 } and \a r and 19 | * \a c are 2 and 1, respectively, the first column of \a m has to be already pivoted. Then, this 20 | * function begins pivoting on the second column, examining 0th and 2nd factor in this order. 21 | * 22 | * zMatPivotingDiag() only tries to do pivoting on the diagonal components. 23 | * Namely, pivoting is done on the \a i th column, beginning from \a i of index row. 24 | * 25 | * zMatMatSweepOut() and zMatVecSweepOut() sweep out components on the \a c th 26 | * column of the matrix \a m1 other than at the \a r th row and of the 27 | * vector other than \a r th, setting them all for 0, and according to 28 | * it, modifies the matrix \a m2 and the vector \a v, respectively. 29 | * They simultaneously divide the components on the \a r th row of \a m1 30 | * and \a m, respectively. 31 | * \return 32 | * zMatPivoting() and zMatPivotingDiag() return the index, or the row, of the 33 | * new pivot on the \a c th column of \a m. 34 | * 35 | * zMatMatSweepOut() and zMatVecSweepOut() return the value originally at the 36 | * pivot. After calling these functions, the value at the same position 37 | * changes to 1. 38 | */ 39 | __ZM_EXPORT int zMatPivoting(zMat m, zIndex index, int r, int c); 40 | __ZM_EXPORT int zMatPivotingDiag(zMat m, zIndex index, int i); 41 | __ZM_EXPORT double zMatMatSweepOut(zMat m1, zMat m2, int r, int c); 42 | __ZM_EXPORT double zMatVecSweepOut(zMat m, zVec v, int r, int c); 43 | 44 | __END_DECLS 45 | 46 | #endif /* __ZM_LE_PIVOT_H__ */ 47 | -------------------------------------------------------------------------------- /include/zm/zm_le_tridiag.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_le_tridiag - linear equation: tridiagonal equation. 5 | */ 6 | 7 | #ifndef __ZM_LE_TRIDIAG_H__ 8 | #define __ZM_LE_TRIDIAG_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief tridiagonal equation solver. 15 | * 16 | * zLETridiagSolve() solves a linear tridiagonal equation m x = d, where 17 | * m = | b0 c0 0 . . 0 | 18 | * | a1 b1 c1 . . 0 | 19 | * | 0 a2 b2 c2 . 0 | 20 | * | 0 0 a3 . . . | 21 | * | . . . | 22 | * | . . . | 23 | * | 0 0 . . a(n-1) b(n-1) | 24 | * d = [ d0 d1 ... d(n-1) ]^T 25 | * Vectors \a a, \a b, \a c, and \a d store the above (a0, ..., a(n-1)), (b0, ..., b(n-1)), (c0, ..., c(n-1)), 26 | * and (d0, ..., d(n-1)), respectively. Note that the first component of \a a and the last component of 27 | * \a c does not have any meanings. 28 | * The answer will be set in \a ans. 29 | * 30 | * zLETridiagSolveDST() destructively modifies \a a, \a b, \a c and \a d during solving the equation. 31 | * \return 32 | * zLETridiagSolve() and zLETridiagSolveDST() return a pointer \a ans if they succeed. Otherwise, they 33 | * return the null pointer. 34 | */ 35 | __ZM_EXPORT zVec zLETridiagSolveDST(zVec a, zVec b, zVec c, zVec d, zVec ans); 36 | __ZM_EXPORT zVec zLETridiagSolve(const zVec a, const zVec b, const zVec c, const zVec d, zVec ans); 37 | 38 | __END_DECLS 39 | 40 | #endif /* __ZM_LE_TRIDIAG_H__ */ 41 | -------------------------------------------------------------------------------- /include/zm/zm_nle.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_nle - nonlinear equation. 5 | */ 6 | 7 | #ifndef __ZM_NLE_H__ 8 | #define __ZM_NLE_H__ 9 | 10 | #include 11 | 12 | #include /* single nonlinear equation */ 13 | #include /* simultaneous nonlinear equation solver by descent method */ 14 | #include /* successive substitution method */ 15 | 16 | #endif /* __ZM_NLE_H__ */ 17 | -------------------------------------------------------------------------------- /include/zm/zm_nle_se.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_nle_se - nonlinear equation: single nonlinear equation solver. 5 | */ 6 | 7 | #ifndef __ZM_NLE_SE_H__ 8 | #define __ZM_NLE_SE_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* Bisec: bisection method */ 15 | __ZM_EXPORT double zNLE_Bisec(double (* f)(double,void*), double x1, double x2, void *priv, double tol, int iter); 16 | /* Secant: Secant method */ 17 | __ZM_EXPORT double zNLE_Secant(double (* f)(double,void*), double x1, double x2, void *priv, double tol, int iter); 18 | /* RF: Regula-Falsi method */ 19 | __ZM_EXPORT double zNLE_RF(double (* f)(double,void*), double x1, double x2, void *priv, double tol, int iter); 20 | /* VDB: Van Wijngaarden-Dekker-Brent method */ 21 | __ZM_EXPORT double zNLE_VDB(double (* f)(double,void*), double x1, double x2, void *priv, double tol, int iter); 22 | 23 | __END_DECLS 24 | 25 | #endif /* __ZM_NLE_SE_H__ */ 26 | -------------------------------------------------------------------------------- /include/zm/zm_nle_ss.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_nle_ss - nonlinear equation: successive substitution method. 5 | */ 6 | 7 | #ifndef __ZM_NLE_SS_H__ 8 | #define __ZM_NLE_SS_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief successive substitution method. 15 | * 16 | * zSSSolve() solves an equation \a x=\a f(\a x) by 17 | * successive substitution method. The iteration will 18 | * converge when the maximum eigenvalue of the Jacobian 19 | * matrix of \a f is less than 1. 20 | * \a iter is the maximum iteration number. If zero is 21 | * given for \a iter, Z_MAX_ITER_NUM is applied instead. 22 | * The function forms as \a f(\a x, \a y, \a util), 23 | * where \a util is a programmer's utility, and the 24 | * function value is stored into \a y. 25 | * \return 26 | * a pointer \a x is returned. 27 | */ 28 | __ZM_EXPORT zVec zSSSolve(zVec (* f)(const zVec,zVec,void*) ,zVec x, void *util, int iter); 29 | 30 | __END_DECLS 31 | 32 | #endif /* __ZM_NLE_SS_H__ */ 33 | -------------------------------------------------------------------------------- /include/zm/zm_ode_adams.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_adams - ordinary differential equation quadrature: 5 | * Adams's linear multistep method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_ADAMS_H__ 9 | #define __ZM_ODE_ADAMS_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* Predictor-Corrector method with a combination of 16 | * Adams=Bashforth / Adams=Moulton formulae. 17 | */ 18 | __ZM_EXPORT zODE *zODEInit_Adams(zODE *ode, int dim, int step, zVec (* f)(double,zVec,void*,zVec)); 19 | __ZM_EXPORT void zODEDestroy_Adams(zODE *ode); 20 | __ZM_EXPORT zVec zODEUpdate_Adams(zODE *ode, double t, zVec x, double dt, void *util); 21 | 22 | __END_DECLS 23 | 24 | #endif /* __ZM_ODE_ADAMS_H__ */ 25 | -------------------------------------------------------------------------------- /include/zm/zm_ode_beuler.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_beuler - ordinary differential equation quadrature: 5 | * backward Euler method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_BEULER_H__ 9 | #define __ZM_ODE_BEULER_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* backward Euler method - easiest way of implicit solutions. 16 | */ 17 | __ZM_EXPORT zODE *zODEInit_BEuler(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 18 | __ZM_EXPORT void zODEDestroy_BEuler(zODE *ode); 19 | __ZM_EXPORT zVec zODEUpdate_BEuler(zODE *ode, double t, zVec x, double dt, void *util); 20 | 21 | /* trapezoidal formula method - A-stable implicit solutions. 22 | */ 23 | #define zODEInit_TR zODEInit_BEuler 24 | #define zODEDestroy_TR zODEDestroy_BEuler 25 | __ZM_EXPORT zVec zODEUpdate_TR(zODE *ode, double t, zVec x, double dt, void *util); 26 | 27 | __END_DECLS 28 | 29 | #endif /* __ZM_ODE_BEULER_H__ */ 30 | -------------------------------------------------------------------------------- /include/zm/zm_ode_bk4.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_bk4 - ordinary differential equation quadrature: 5 | * Butcher-Kuntzmann method (Gauss method and Radau method). 6 | */ 7 | 8 | #ifndef __ZM_ODE_BK4_H__ 9 | #define __ZM_ODE_BK4_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* Butcher-Kuntzmann method is a two-step fourth-dimension 16 | * implicit Runge-Kutta method. 17 | * In implicit Runge-Kutta method, it is known that dimension 18 | * is no more than twice steps, and thus, BK4 is thought 19 | * to be the most practical. 20 | */ 21 | __ZM_EXPORT zODE *zODEInit_BK4(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 22 | __ZM_EXPORT void zODEDestroy_BK4(zODE *ode); 23 | 24 | /* 'BK4' is a dummy keyword, since Butcher-Kuntzmann method 25 | * is a general framework. As body implementations of solvers, 26 | * 'Gauss' and 'Radau' are prepared. 27 | */ 28 | __ZM_EXPORT zVec zODEUpdate_Gauss(zODE *ode, double t, zVec x, double dt, void *util); 29 | __ZM_EXPORT zVec zODEUpdate_Radau(zODE *ode, double t, zVec x, double dt, void *util); 30 | 31 | #define zODEInit_Gauss zODEInit_BK4 32 | #define zODEDestroy_Gauss zODEDestroy_BK4 33 | #define zODEInit_Radau zODEInit_BK4 34 | #define zODEDestroy_Radau zODEDestroy_BK4 35 | 36 | __END_DECLS 37 | 38 | #endif /* __ZM_ODE_BK4_H__ */ 39 | -------------------------------------------------------------------------------- /include/zm/zm_ode_dc.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_dc - ordinary differential equation quadrature: 5 | * deferred correction with Richardson's extrapolation. 6 | */ 7 | 8 | #ifndef __ZM_ODE_DC_H__ 9 | #define __ZM_ODE_DC_H__ 10 | 11 | __BEGIN_DECLS 12 | 13 | /*! 14 | */ 15 | __ZM_EXPORT zODE *zODEInitDC(zODE *ode, int dim, int step, zVec (* f)(double,zVec,void*,zVec)); 16 | __ZM_EXPORT void zODEDestroyDC(zODE *ode); 17 | __ZM_EXPORT zVec zODEUpdateDC(zODE *ode, double t, zVec x, double dt, void *util); 18 | 19 | __END_DECLS 20 | 21 | #endif /* __ZM_ODE_DC_H__ */ 22 | -------------------------------------------------------------------------------- /include/zm/zm_ode_erk.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_erk - ordinary differential equation quadrature: 5 | * embedded Runge-Kutta method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_ERK_H__ 9 | #define __ZM_ODE_ERK_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* Some solvers in the class of embedded Runge-Kutta methods, 16 | * which automatically modify quantized integration time dt in 17 | * accordance with the error estimated, are provided. 18 | */ 19 | 20 | /* Runge-Kutta-Fehlberg method is forth-dimension-embedded 21 | * six-step fifth-dimension Runge-Kutta method. 22 | */ 23 | __ZM_EXPORT zODE *zODEInit_RKF45(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 24 | __ZM_EXPORT void zODEDestroy_RKF45(zODE *ode); 25 | __ZM_EXPORT zVec zODEUpdate_RKF45(zODE *ode, double t, zVec x, double dt, void *util); 26 | 27 | /* Cash-Karp method is forth-dimension-embedded 28 | * six-step fifth-dimension Runge-Kutta method. 29 | */ 30 | __ZM_EXPORT zODE *zODEInit_CK45(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 31 | __ZM_EXPORT void zODEDestroy_CK45(zODE *ode); 32 | __ZM_EXPORT zVec zODEUpdate_CK45(zODE *ode, double t, zVec x, double dt, void *util); 33 | 34 | /* Dormand-Prince method is forth-dimension-embedded 35 | * seven-step fifth-dimension Runge-Kutta method. 36 | */ 37 | __ZM_EXPORT zODE *zODEInit_DP45(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 38 | __ZM_EXPORT void zODEDestroy_DP45(zODE *ode); 39 | __ZM_EXPORT zVec zODEUpdate_DP45(zODE *ode, double t, zVec x, double dt, void *util); 40 | 41 | __END_DECLS 42 | 43 | #endif /* __ZM_ODE_ERK_H__ */ 44 | -------------------------------------------------------------------------------- /include/zm/zm_ode_euler.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_euler - ordinary differential equation quadrature: 5 | * Euler method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_EULER_H__ 9 | #define __ZM_ODE_EULER_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* Euler method is too known to describe:-) 16 | */ 17 | __ZM_EXPORT zODE *zODEInit_Euler(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 18 | __ZM_EXPORT void zODEDestroy_Euler(zODE *ode); 19 | __ZM_EXPORT zVec zODEUpdate_Euler(zODE *ode, double t, zVec x, double dt, void *util); 20 | 21 | __END_DECLS 22 | 23 | #endif /* __ZM_ODE_EULER_H__ */ 24 | -------------------------------------------------------------------------------- /include/zm/zm_ode_gear.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_gear - ordinary differential equation quadrature: 5 | * Gear method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_GEAR_H__ 9 | #define __ZM_ODE_GEAR_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* Gear method is based on the Backward Differentiation 16 | * Formula(BDF), one of the implicit linear multistep methods. 17 | * It has a stiffly stable property against stiff systems. 18 | * 19 | * Note that the initial series of variables should be set 20 | * properly. They are set for zeros in default, which may 21 | * cause stable but largely different numerical behaviors. 22 | */ 23 | __ZM_EXPORT zODE *zODEInit_Gear(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 24 | __ZM_EXPORT void zODEDestroy_Gear(zODE *ode); 25 | __ZM_EXPORT zVec zODEUpdate_Gear(zODE *ode, double t, zVec x, double dt, void *util); 26 | 27 | /*! \brief initialize history with a given vector. 28 | * 29 | * zODEInitHist_Gear() is particularly prepared for Gear method. 30 | * Since Gear method is affected by the choice of initial series 31 | * of variable, it perhaps works better to set them for the initial 32 | * value than nothing. 33 | * Call this function, and it fills the initial history with \a x. 34 | * \return 35 | * zODEInitHist_Gear() returns no values. 36 | * \notes 37 | * Since it is specialized to Gear method, \a ode should be assigned 38 | * for Gear and initialized in advance. 39 | */ 40 | __ZM_EXPORT void zODEInitHist_Gear(zODE *ode, zVec x); 41 | 42 | __END_DECLS 43 | 44 | #endif /* __ZM_ODE_GEAR_H__ */ 45 | -------------------------------------------------------------------------------- /include/zm/zm_ode_heun.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_heun - ordinary differential equation quadrature: 5 | * Heun method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_HEUN_H__ 9 | #define __ZM_ODE_HEUN_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* Here, the third-dimension Runge-Kutta method is named Heun method. 16 | * To be strict, it is one of the modified Euler methods by Heun. 17 | */ 18 | __ZM_EXPORT zODE *zODEInit_Heun(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 19 | __ZM_EXPORT void zODEDestroy_Heun(zODE *ode); 20 | __ZM_EXPORT zVec zODEUpdate_Heun(zODE *ode, double t, zVec x, double dt, void *util); 21 | 22 | __END_DECLS 23 | 24 | #endif /* __ZM_ODE_HEUN_H__ */ 25 | -------------------------------------------------------------------------------- /include/zm/zm_ode_rk4.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_rk4 - ordinary differential equation quadrature: 5 | * classical Runge-Kutta method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_RK4_H__ 9 | #define __ZM_ODE_RK4_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* classical Runge-Kutta method is also known as 16 | * forth-dimension Runge-Kutta method. 17 | * It assures fifth-order error. 18 | * Four is the maximum step number which coincides with 19 | * the dimention of error, so that it is thought to be 20 | * the most practical, while some objectives exist. 21 | */ 22 | __ZM_EXPORT zODE *zODEInit_RK4(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 23 | __ZM_EXPORT void zODEDestroy_RK4(zODE *ode); 24 | __ZM_EXPORT zVec zODEUpdate_RK4(zODE *ode, double t, zVec x, double dt, void *util); 25 | 26 | __END_DECLS 27 | 28 | #endif /* __ZM_ODE_RK4_H__ */ 29 | -------------------------------------------------------------------------------- /include/zm/zm_ode_rkg.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_rkg - ordinary differential equation quadrature: 5 | * Runge-Kutta-Gill method. 6 | */ 7 | 8 | #ifndef __ZM_ODE_RKG_H__ 9 | #define __ZM_ODE_RKG_H__ 10 | 11 | /* NOTE: never include this header file in user programs. */ 12 | 13 | __BEGIN_DECLS 14 | 15 | /* Runge-Kutta-Gill method is a variation of calssical 16 | * Runge-Kutta method. It is advantageous where less 17 | * workspace is required and rounding error is well absorbed. 18 | */ 19 | __ZM_EXPORT zODE *zODEInit_RKG(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)); 20 | __ZM_EXPORT void zODEDestroy_RKG(zODE *ode); 21 | __ZM_EXPORT zVec zODEUpdate_RKG(zODE *ode, double t, zVec x, double dt, void *util); 22 | 23 | __END_DECLS 24 | 25 | #endif /* __ZM_ODE_RKG_H__ */ 26 | -------------------------------------------------------------------------------- /include/zm/zm_opt.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_opt - optimization tools. 5 | */ 6 | 7 | #ifndef __ZM_OPT_H__ 8 | #define __ZM_OPT_H__ 9 | 10 | #include 11 | 12 | #include /* line search */ 13 | #include /* linear programming */ 14 | #include /* linear complementary problem */ 15 | #include /* quadratic programming */ 16 | 17 | /*! \macro small quantity for finite-difference derivative */ 18 | #define Z_OPT_EPS ( 1.0e-6 ) 19 | 20 | #include /* descent method */ 21 | 22 | #include /* DIviding RECTangle method */ 23 | #include /* downhill simplex/polytope method */ 24 | #include /* genetic algorithm */ 25 | #include /* Particle Swarm Optimization method */ 26 | 27 | #endif /* __ZM_OPT_H__ */ 28 | -------------------------------------------------------------------------------- /include/zm/zm_opt_direct.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_opt_direct - optimization tools: DIRECT method. 5 | */ 6 | 7 | #ifndef __ZM_OPT_DIRECT_H__ 8 | #define __ZM_OPT_DIRECT_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* DIviding RECTangle method by Jones, Perttunen and Stuckman (1993). 15 | * reference: 16 | * D. R. Jones, C. D. Perttunen and B. E. Stuckman, Lipschizian Optimization Without 17 | * the Lipschitz Constant, Journal of Optimization Theory and Application, Vol. 79, 18 | * No. 1, pp. 157--181, 1993. 19 | */ 20 | __ZM_EXPORT int zOptSolveDIRECT(double (* f)(const zVec,void*), void *util, const zVec min, const zVec max, int iter, double tol, zVec ans, double *eval); 21 | 22 | #define ZOPT_DIRECT_MAX_ITER_NUM 1000 23 | 24 | __END_DECLS 25 | 26 | #endif /* __ZM_OPT_DIRECT_H__ */ 27 | -------------------------------------------------------------------------------- /include/zm/zm_opt_lcp.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_opt_lcp - optimization tools: linear complementarity problem. 5 | */ 6 | 7 | #ifndef __ZM_OPT_LCP_H__ 8 | #define __ZM_OPT_LCP_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief linear complementarity problem solver. 15 | * 16 | * zLCPSolveLemke() solves a linear complementarity problem (LCP) by 17 | * Lemke's method (1965 C. E. Lemke) which finds a combination of two 18 | * vectors \a w and \a z that satisfies: 19 | * \a w = \a m \a z + \a p, \a w >= 0 and \a z >= 0. 20 | * where \a m is a square matrix. 21 | * 22 | * zLCPSolveIP() also solves LCP by Potra s predictor-corrector algorithm 23 | * for infeasible-interior-point method (1994 F. Potra and R Sheng). 24 | * \return 25 | * zLCPSolveLemke() and zLCPSolveIP() return the true value if succeeding 26 | * to get the solution, or the false value if there are vector/matrix 27 | * size mismatch, bad memory allocation, non-feasible solution and 28 | * infinite solution are found. 29 | * \notes 30 | * Since \a w is often not required to be answered, it is allowed to give 31 | * the null pointer for \a w to skip the operation. 32 | */ 33 | __ZM_EXPORT bool zLCPSolveLemke(const zMat m, const zVec p, zVec w, zVec z); 34 | __ZM_EXPORT bool zLCPSolveIP(const zMat m, const zVec p, zVec w, zVec z); 35 | 36 | __END_DECLS 37 | 38 | #endif /* __ZM_OPT_LCP_H__ */ 39 | -------------------------------------------------------------------------------- /include/zm/zm_opt_line.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_opt_line - optimization tools: line search. 5 | */ 6 | 7 | #ifndef __ZM_OPT_LINE_H__ 8 | #define __ZM_OPT_LINE_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* ********************************************************** */ 15 | /* CLASS: zOptLine 16 | * line search class 17 | * ********************************************************** */ 18 | 19 | /*! \brief golden section proportion */ 20 | #define Z_GSEC 0.38196601125911 21 | 22 | /*! \brief golden section method */ 23 | __ZM_EXPORT double zOptLineGSEC(double (*eval)(double,void*), double a, double b, void *util, int iter); 24 | 25 | /*! \brief bisection method */ 26 | __ZM_EXPORT double zOptLineBisec(double (*eval)(double,void*), double a, double b, void *util, int iter); 27 | 28 | /*! \brief Brent's method */ 29 | __ZM_EXPORT double zOptLineBrent(double (*eval)(double,void*), double a, double b, void *util, int iter); 30 | 31 | __END_DECLS 32 | 33 | #endif /* __ZM_OPT_LINE_H__ */ 34 | -------------------------------------------------------------------------------- /include/zm/zm_opt_nm.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_opt_nm - optimization tools: Nelder-Mead's downhill simplex method. 5 | */ 6 | 7 | #ifndef __ZM_OPT_NM_H__ 8 | #define __ZM_OPT_NM_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief solve an optimization problem by Nelder-Mead method. 15 | * 16 | * zOptSolveNM() solves an optimization problem by Nelder-Mead method, which 17 | * is also known as the downhill simplex/polytope method proposed by Nelder 18 | * and Mead (1965). 19 | */ 20 | __ZM_EXPORT int zOptSolveNM(double (* f)(const zVec,void*), void *util, const zVec min, const zVec max, int iter, double tol, zVec ans, double *eval); 21 | 22 | __END_DECLS 23 | 24 | #endif /* __ZM_OPT_NM_H__ */ 25 | -------------------------------------------------------------------------------- /include/zm/zm_opt_pso.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_opt_pso - optimization tools: Particle Swarm Optimization method. 5 | */ 6 | 7 | #ifndef __ZM_OPT_PSO_H__ 8 | #define __ZM_OPT_PSO_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* Particle Swarm Optimizaiton method by Kennedy and Eberhart (1995). 15 | * reference: 16 | * J. Kennedy and R. Eberhart, Particle swarm optimization, in Proceedings of 17 | * IEEE International Conference on Neural Networks, pp. 1942-1948, 1995. 18 | */ 19 | 20 | #define ZOPT_PSO_DEFAULT_NUM 100 21 | #define ZOPT_PSO_DEFAULT_C1 2.0 22 | #define ZOPT_PSO_DEFAULT_C2 2.0 23 | #define ZOPT_PSO_DEFAULT_VEL_RATE 0.1 24 | 25 | /*! \brief solve an optimization problem by Particle Swarm Optimization method. 26 | * 27 | * zOptSolvePSO() solves an optimization problem by Particle Swarm Optimization 28 | * method proposed by Kennedy and Eberhart (1995). 29 | */ 30 | __ZM_EXPORT int zOptSolvePSO(double (* f)(const zVec,void*), void *util, const zVec min, const zVec max, int iter, double tol, int num, double c1, double c2, double vel_rate, zVec ans, double *eval); 31 | 32 | /*! \brief solve an optimization problem by Particle Swarm Optimization method. 33 | * 34 | * zOptSolvePSODefault() solves an optimization problem by Particle Swarm 35 | * Optimization method with default parameters. 36 | */ 37 | __ZM_EXPORT int zOptSolvePSODefault(double (* f)(const zVec,void*), void *util, const zVec min, const zVec max, int iter, double tol, zVec ans, double *eval); 38 | 39 | __END_DECLS 40 | 41 | #endif /* __ZM_OPT_PSO_H__ */ 42 | -------------------------------------------------------------------------------- /include/zm/zm_pex_eq.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_pex_eq - polynomial expression class: polynomial equation solver. 5 | */ 6 | 7 | #ifndef __ZM_PEX_EQ_H__ 8 | #define __ZM_PEX_EQ_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /*! \brief numerical solution of polynomial equation. 15 | * 16 | * zPexBHDST(), zPexBH() and zPexDKA() solve a polynomial equation 17 | * \a a=0 based on Bairstow-Hitchcock's method and Durand-Kerner-Aberth's 18 | * method, respectively. 19 | * 20 | * All answers are put into an array pointed by \a ans as complex numbers. 21 | * \a ans must have enough size. 22 | * \a tol is a tolerance to terminate iteration. 23 | * \a iter is the maximum number of iteration. When it runs over \a iter 24 | * times, it is aborted. If zero is given for \a iter, Z_MAX_ITER_NUM 25 | * defined in zm_misc.h is used instead. 26 | * zPexBHDST() is a destructive version of zPexBD(), which destroys \a a 27 | * in the computation process. 28 | * \note 29 | * Since Bairstow-Hitchcock's method is an iteration, zPexBH() may not 30 | * converge due to bad initialization. 31 | * zPexDKA() is advantageous to it from this viewpoint. 32 | * \return 33 | * zPexBHDST() and zPexBH() return a pointer \a ans if succeeding. 34 | * Otherwise, the null pointer is returned. 35 | */ 36 | #define ZM_PEX_EQ_TOL ( 1.0e-7 ) 37 | __ZM_EXPORT zCVec zPexBHDST(zPex a, zCVec ans, double tol, int iter); 38 | __ZM_EXPORT zCVec zPexBH(zPex a, zCVec ans, double tol, int iter); 39 | __ZM_EXPORT zCVec zPexDKA(zPex a, zCVec ans, double tol, int iter); 40 | 41 | __END_DECLS 42 | 43 | #endif /* __ZM_PEX_EQ_H__ */ 44 | -------------------------------------------------------------------------------- /include/zm/zm_pi.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_pi.h 5 | * \brief circle ratio. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_PI_H__ 10 | #define __ZM_PI_H__ 11 | 12 | /*! \brief circle ratio (pi). */ 13 | #define zPI 3.14159265358979323846 14 | /*! \brief circle ratio times 2 (2 pi). */ 15 | #define zPIx2 6.28318530717958647692 16 | /*! \brief circle ratio divided by 2 (pi / 2). */ 17 | #define zPI_2 1.57079632679489661923 18 | 19 | /*! \brief radian per degree (pi / 180). */ 20 | #define zRAD_PER_DEG 0.01745329251994329547 21 | /*! \brief degree per radian (180 / pi). */ 22 | #define zDEG_PER_RAD 57.29577951308232286465 23 | 24 | /*! \brief convert a value \a x in degree to a radian value. */ 25 | #define zDeg2Rad(x) ( (x)*zRAD_PER_DEG ) 26 | /*! \brief convert a value \a x in radian to a degree value. */ 27 | #define zRad2Deg(x) ( (x)*zDEG_PER_RAD ) 28 | 29 | #endif /* __ZM_PI_H__ */ 30 | -------------------------------------------------------------------------------- /include/zm/zm_raw.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_raw - raw vector and matrix. 5 | */ 6 | 7 | #ifndef __ZM_RAW_H__ 8 | #define __ZM_RAW_H__ 9 | 10 | #include 11 | 12 | /* ********************************************************** */ 13 | /* CATEGORY: 14 | * vector/matrix calculation using raw double precision 15 | * floating-point value array 16 | * ********************************************************** */ 17 | 18 | #include 19 | #include 20 | 21 | #endif /* __ZM_RAW_H__ */ 22 | -------------------------------------------------------------------------------- /include/zm/zm_sf.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_sf.h 5 | * \brief special functions. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_SF_H__ 10 | #define __ZM_SF_H__ 11 | 12 | #include 13 | 14 | #include /* Gauss's error function */ 15 | #include /* gamma and beta function */ 16 | #include /* Bessel functions */ 17 | #include /* Fresnel integral */ 18 | 19 | #endif /* __ZM_SF_H__ */ 20 | -------------------------------------------------------------------------------- /include/zm/zm_sf_erf.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_sf_erf.h 5 | * \brief Gauss's error function. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_SF_ERF_H__ 10 | #define __ZM_SF_ERF_H__ 11 | 12 | /* NOTE: never include this header file in user programs. */ 13 | 14 | __BEGIN_DECLS 15 | 16 | /* ********************************************************** */ 17 | /*! \defgroup sf special functions. 18 | * \{ *//* ************************************************** */ 19 | 20 | /*! \brief error function approximated by Taylor series. 21 | * 22 | * zErfN() is an approximation of Gauss's error function, which 23 | * is defined by the integral of 2/sqrt(pi) exp(-t^2) about t 24 | * from minus infinity to \a x. The approximation is done by 25 | * Taylor series expantion up to \a n 'th term. 26 | */ 27 | __ZM_EXPORT double zErfN(double x, int n); 28 | 29 | /*! \brief error function. 30 | * 31 | * zErf() is Gauss's error function, which is defined by the 32 | * integral of 2/sqrt(pi) exp(-t^2) about t from minus infinity 33 | * to \a x. It is equivalent with zErfN( x, Z_ERF_DEFAULT_ORDER ). 34 | */ 35 | #define Z_ERF_DEFAULT_ORDER 35 36 | #define zErf(x) zErfN( x, Z_ERF_DEFAULT_ORDER ) 37 | 38 | /*! \} */ 39 | 40 | __END_DECLS 41 | 42 | #endif /* __ZM_SF_ERF_H__ */ 43 | -------------------------------------------------------------------------------- /include/zm/zm_sf_fresnel.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_sf_fresnel.h 5 | * \brief Fresnel integral. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_SF_FRESNEL_H__ 10 | #define __ZM_SF_FRESNEL_H__ 11 | 12 | /* NOTE: never include this header file in user programs. */ 13 | 14 | __BEGIN_DECLS 15 | 16 | /*! \brief Fresnel integral 17 | * 18 | * zFresnelIntgPI_2() computes the Fresnel integral, which is defined as 19 | * S(\a x) = int_0^\a x sin(pi/2 u^2) du 20 | * C(\a x) = int_0^\a x cos(pi/2 u^2) du 21 | * zFresnelIntgScale() finds a scaled version of the Fresnel integral as 22 | * S(\a x) = int_0^\a x sin(\a f u^2) du 23 | * C(\a x) = int_0^\a x cos(\a f u^2) du 24 | * where \a f is a scale factor. 25 | * zFresnelIntg() computes the normalized Fresnel integral as 26 | * S(\a x) = int_0^x sin(u^2) du 27 | * C(\a x) = int_0^x cos(u^2) du 28 | * In whichever case, S(x) and C(x) are stored in \a s and \a c, respectively. 29 | * \return 30 | * zFresnelIntgPI_2(), zFresnelIntgScale() and zFresnelIntg() return the 31 | * false value if an internal iteration fails. Otherwise, the true value 32 | * is returned. 33 | */ 34 | __ZM_EXPORT bool zFresnelIntgPI_2(double x, double *s, double *c); 35 | __ZM_EXPORT bool zFresnelIntgScale(double x, double f, double *s, double *c); 36 | __ZM_EXPORT bool zFresnelIntg(double x, double *s, double *c); 37 | 38 | /*! \brief generalized Fresnel integral 39 | * 40 | * zFresnelIntgGen() computes a generalized Fresnel integral as 41 | * S(\a x) = int_0^\a x sin(\a f0 + \a f1 u + \a f2 u^2) du 42 | * C(\a x) = int_0^\a x cos(\a f0 + \a f1 u + \a f2 u^2) du 43 | * where \a f0, \a f1 and \a f2 can be any real numbers. 44 | * \return 45 | * zFresnelIntgGen() internally calls zFresnelIntgScale() The returned 46 | * value conforms to it. 47 | */ 48 | __ZM_EXPORT bool zFresnelIntgGen(double x, double f0, double f1, double f2, double *s, double *c); 49 | 50 | __END_DECLS 51 | 52 | #endif /* __ZM_SF_FRESNEL_H__ */ 53 | -------------------------------------------------------------------------------- /include/zm/zm_sf_gamma.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | */ 4 | /*! \file zm_sf_gamma.h 5 | * \brief gamma and beta function. 6 | * \author Zhidao 7 | */ 8 | 9 | #ifndef __ZM_SF_GAMMA_H__ 10 | #define __ZM_SF_GAMMA_H__ 11 | 12 | /* NOTE: never include this header file in user programs. */ 13 | 14 | __BEGIN_DECLS 15 | 16 | /* ********************************************************** */ 17 | /*! \defgroup sf special functions. 18 | * \{ *//* ************************************************** */ 19 | 20 | /*! \brief gamma function. 21 | * 22 | * zGamma() is the gamma function defined by the integral 23 | * of t^(\a x-1)*exp(-t) about t from zero to infinity for 24 | * an arbitrary value \a x. 25 | * \sa 26 | * zLnGamma 27 | */ 28 | __ZM_EXPORT double zGamma(double x); 29 | 30 | /*! \brief natural logarithmic gamma function. 31 | * 32 | * zLnGamma() is a natural logarithm of zGamma(). 33 | * Due to the property of logarithm functions, it is undefined 34 | * for negative gamma function values. It is useful to avoid 35 | * an overflow even for a modest \a x value. 36 | * \note 37 | * As is noted in the above description, exp(zLnGamma()) 38 | * is not necessarily equivalent to zGamma(). Programmers 39 | * should pay attention to the definition range of zLnGamma() 40 | * when using. 41 | * \sa 42 | * zGamma 43 | */ 44 | __ZM_EXPORT double zLnGamma(double x); 45 | 46 | /*! \brief beta function. 47 | * 48 | * zBeta() is the beta function defined by 49 | * zGamma(\a z)*zGamma(\a w)/zGamma(\a z+\a w) for a set of 50 | * any positive values \a z and \a w. 51 | */ 52 | __ZM_EXPORT double zBeta(double z, double w); 53 | 54 | /*! \} */ 55 | 56 | __END_DECLS 57 | 58 | #endif /* __ZM_SF_GAMMA_H__ */ 59 | -------------------------------------------------------------------------------- /include/zm/zm_vec_array.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_vec_array - vector array class. 5 | */ 6 | 7 | #ifndef __ZM_VEC_ARRAY_H__ 8 | #define __ZM_VEC_ARRAY_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | /* ********************************************************** */ 15 | /* CLASS: zVecArray 16 | * vector array class. 17 | * ********************************************************** */ 18 | 19 | zArrayClass( zVecArray, zVec ); 20 | 21 | /*! \brief allocate vector array. 22 | * 23 | * zVecArrayAlloc() allocate \a num \a dim -dimensional vectors in \a array. 24 | * \retval true if succeeding to allocate. 25 | * \retval false if failing to allocate. 26 | */ 27 | __ZM_EXPORT bool zVecArrayAlloc(zVecArray *array, int dim, int num); 28 | 29 | /*! \brief free vector array. 30 | * 31 | * zVecArrayFree() frees a vector array \a array. 32 | */ 33 | __ZM_EXPORT void zVecArrayFree(zVecArray *array); 34 | 35 | /*! \brief an element of a vector in an array */ 36 | #define zVecArrayElem(a,i,j) zVecElemNC( *zArrayElem(a,i), j ) 37 | 38 | /*! \brief fill a vector array uniformly with the same vector. 39 | * 40 | * zVecArrayFill() fills a vector array \a array uniformly with 41 | * a vector \a v i.e. it sets all the vectors of \a array for \a v. 42 | * \retval \a v if succeeding. 43 | * \retval the null pointer if the vector size of \a array is 44 | * inconsistent with \a v. 45 | */ 46 | __ZM_EXPORT zVec zVecArrayFill(zVecArray *array, zVec v); 47 | 48 | __END_DECLS 49 | 50 | #endif /* __ZM_VEC_ARRAY_H__ */ 51 | -------------------------------------------------------------------------------- /include/zm/zm_vec_tree.h: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_vec_tree - vector binary tree (kd-tree) class. 5 | */ 6 | 7 | #ifndef __ZM_VEC_TREE_H__ 8 | #define __ZM_VEC_TREE_H__ 9 | 10 | /* NOTE: never include this header file in user programs. */ 11 | 12 | __BEGIN_DECLS 13 | 14 | typedef struct _zVecTree{ 15 | int split; /*!< split index */ 16 | zVec v; /*!< spliting vertex */ 17 | zVec vmin; /*!< minimum corner of bounding box */ 18 | zVec vmax; /*!< maximum corner of bounding box */ 19 | struct _zVecTree *s[2]; /*!< binary branches */ 20 | } zVecTree; 21 | 22 | __ZM_EXPORT zVecTree *zVecTreeInit(zVecTree *tree, int dim); 23 | __ZM_EXPORT void zVecTreeDestroy(zVecTree *tree); 24 | 25 | __ZM_EXPORT zVecTree *zVecTreeAdd(zVecTree *tree, zVec v); 26 | 27 | __ZM_EXPORT double zVecTreeNN(const zVecTree *tree, const zVec v, zVecTree **nn); 28 | 29 | __END_DECLS 30 | 31 | #endif /* __ZM_VEC_TREE_H__ */ 32 | -------------------------------------------------------------------------------- /lib/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mi-lib/zm/9b1eb145419ccfb6995824cf81552f25bc335e8d/lib/.gitkeep -------------------------------------------------------------------------------- /libinfo: -------------------------------------------------------------------------------- 1 | PROJNAME=zm 2 | VERSION=1.12.14 3 | DEPENDENCY="zeda=1.11.0" 4 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | MAKEFILEGEN=`which zeda-makefile-gen` 2 | MAKEDEB=`which zeda-deb-gen` 3 | 4 | .PHONY: doc test example 5 | 6 | all: 7 | ifeq ($(MAKEFILEGEN),) 8 | echo "ZEDA not installed." 9 | else 10 | @$(MAKEFILEGEN) | make -f - 11 | endif 12 | test: 13 | @$(MAKEFILEGEN) | make -f - test 14 | doc: 15 | @$(MAKEFILEGEN) | make -f - doc 16 | example: 17 | @$(MAKEFILEGEN) | make -f - example 18 | clean: 19 | @$(MAKEFILEGEN) | make -f - clean 20 | install: 21 | @$(MAKEFILEGEN) | make -f - install 22 | uninstall: 23 | @$(MAKEFILEGEN) | make -f - uninstall 24 | deb: 25 | ifeq ($(MAKEDEB),) 26 | echo "ZEDA is not installed." 27 | else 28 | @$(MAKEDEB) 29 | endif 30 | -------------------------------------------------------------------------------- /src/config.lib: -------------------------------------------------------------------------------- 1 | OBJ=zm_ieee.o zm_misc.o\ 2 | zm_sf_erf.o zm_sf_gamma.o zm_sf_bessel.o zm_sf_fresnel.o\ 3 | zm_rand.o zm_stat.o zm_stat_histogram.o\ 4 | zm_complex.o zm_complex_arith.o zm_complex_pe.o\ 5 | zm_raw_vec.o zm_raw_mat.o\ 6 | zm_vec.o zm_vec_array.o zm_vec_list.o zm_vec_tree.o zm_vec_ring.o zm_mat.o\ 7 | zm_cvec.o zm_cmat.o\ 8 | zm_le.o zm_le_pivot.o zm_le_lu.o zm_le_lq.o zm_le_mat_inv.o zm_le_mat_mpinv.o zm_le_tridiag.o zm_le_gen.o zm_le_lyapnov.o\ 9 | zm_mat_eig.o\ 10 | zm_mva.o zm_mva_ransac.o zm_mva_cluster.o zm_mva_gmm.o\ 11 | zm_seq.o\ 12 | zm_pex.o zm_pex_eq.o zm_fourier.o\ 13 | zm_ip.o zm_ip_lin.o zm_ip_lagrange.o zm_ip_chebyshev.o zm_ip_spline.o zm_ip_akima.o zm_ip_pex.o zm_ip_clothoid.o zm_ip_trvelprof.o\ 14 | zm_nurbs.o\ 15 | zm_data.o zm_data_fft.o\ 16 | zm_opt_line.o\ 17 | zm_opt_lp_stdcnv.o zm_opt_lp_simplex.o zm_opt_lp_pdip.o\ 18 | zm_opt_lcp_lemke.o zm_opt_lcp_ip.o\ 19 | zm_opt_qp.o zm_opt_qp_asm.o\ 20 | zm_opt_direct.o zm_opt_nm.o zm_opt_ga.o zm_opt_pso.o zm_opt_dm.o\ 21 | zm_nle_se.o zm_nle_dm.o zm_nle_ss.o\ 22 | zm_ode.o zm_ode_dc.o zm_ode_euler.o zm_ode_heun.o zm_ode_rk4.o zm_ode_rkg.o zm_ode_erk.o zm_ode_adams.o zm_ode_beuler.o zm_ode_bk4.o zm_ode_gear.o zm_ode2.o\ 23 | zm_intg.o zm_oscil_vdp.o zm_oscil_kura.o\ 24 | zm_graph.o zm_graph_search.o\ 25 | zm_rrt.o\ 26 | zm_gng.o\ 27 | zm_parse.o 28 | -------------------------------------------------------------------------------- /src/makefile: -------------------------------------------------------------------------------- 1 | MAKEFILEGEN=`which zeda-makefile-gen` 2 | 3 | all: 4 | @$(MAKEFILEGEN) export_header | make -f - 5 | @$(MAKEFILEGEN) lib | make -f - 6 | %.o: %.c 7 | @$(MAKEFILEGEN) lib | make -f - $@ 8 | clean: 9 | @$(MAKEFILEGEN) lib | make -f - clean 10 | -------------------------------------------------------------------------------- /src/zm_complex_pe.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_complex_pe - complex number class: polynomial equation solver. 5 | */ 6 | 7 | #include 8 | 9 | /* solve quadratic equation. */ 10 | zComplex *zQESolve(double a, double b, double c, zComplex ans[]) 11 | { 12 | double d, q; 13 | 14 | if( a == 0 ){ 15 | ZRUNERROR( ZM_ERR_PE_DEFLATED ); 16 | return NULL; 17 | } 18 | d = b*b - 4*a*c; 19 | if( d >= 0 ){ 20 | d = sqrt( d ); 21 | q = -0.5 * ( b + ( b >= 0 ? d : -d ) ); 22 | zComplexCreate( &ans[0], q/a, 0 ); 23 | zComplexCreate( &ans[1], c/q, 0 ); 24 | } else{ 25 | a *= 2; 26 | d = sqrt( -d ) / a; 27 | b /= -a; 28 | zComplexCreate( &ans[0], b, d ); 29 | zComplexCreate( &ans[1], b, -d ); 30 | } 31 | return ans; 32 | } 33 | 34 | /* solve a cubic equation algebraically by Cardano's formula. */ 35 | zComplex *zCESolve(double a, double b, double c, double d, zComplex ans[]) 36 | { 37 | double g, p, q, w, z1, z2; 38 | 39 | if( a == 0 ){ 40 | ZRUNERROR( ZM_ERR_PE_DEFLATED ); 41 | return NULL; 42 | } 43 | b /= 3 * a; 44 | c /= a; 45 | d /= a; 46 | p = c/3 - b*b; 47 | q = -b*(2*b*b-c) - d; 48 | g = q*q + 4*p*p*p; 49 | if( g > 0 ){ 50 | w = sqrt( g ); 51 | z1 = zCbrt( 0.5 * ( q + w ) ); 52 | z2 = zCbrt( 0.5 * ( q - w ) ); 53 | zComplexCreate( &ans[0], z1 + z2 - b, 0 ); 54 | zComplexCreate( &ans[1], -0.5*(z1+z2)-b, 0.5*sqrt(3)*(z1-z2) ); 55 | zComplexConj( &ans[1], &ans[2] ); 56 | } else{ 57 | z1 = ( q != 0 ) ? atan2( sqrt(-g), q ) : zPI_2; 58 | w = 2 * sqrt( -p ); /* p is necessarily negative. */ 59 | zComplexCreate( &ans[0], w * cos( z1 /3) - b, 0 ); 60 | zComplexCreate( &ans[1], -w * cos((zPI-z1)/3) - b, 0 ); 61 | zComplexCreate( &ans[2], -w * cos((zPI+z1)/3) - b, 0 ); 62 | } 63 | return ans; 64 | } 65 | -------------------------------------------------------------------------------- /src/zm_fourier.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_fourier - Fourier series. 5 | */ 6 | 7 | #include 8 | 9 | /* allocate internal memory for Fourier series. */ 10 | zFourier *zFourierAlloc(zFourier *f, int n) 11 | { 12 | f->n = n; 13 | f->sc = zAlloc( double, f->n ); 14 | f->cc = zAlloc( double, f->n ); 15 | if( !f->sc || !f->cc ){ 16 | zFourierFree( f ); 17 | return NULL; 18 | } 19 | return f; 20 | } 21 | 22 | /* free internal memory for Fourier series. */ 23 | void zFourierFree(zFourier *f) 24 | { 25 | zFree( f->sc ); 26 | zFree( f->cc ); 27 | f->n = 0; 28 | } 29 | 30 | /* value of a Fourier series. */ 31 | double zFourierVal(zFourier *f, double t) 32 | { 33 | int i; 34 | double s, c, val = 0; 35 | 36 | for( i=0; in; i++ ){ 37 | zSinCos( i*t, &s, &c ); 38 | val += f->sc[i]*s + f->cc[i]*c; 39 | } 40 | return val; 41 | } 42 | 43 | /* velocity of a Fourier series. */ 44 | double zFourierVel(zFourier *f, double t) 45 | { 46 | int i; 47 | double s, c, val = 0; 48 | 49 | for( i=0; in; i++ ){ 50 | zSinCos( i*t, &s, &c ); 51 | val += i * ( f->sc[i]*c - f->cc[i]*s ); 52 | } 53 | return val; 54 | } 55 | 56 | /* acceleration of a Fourier series. */ 57 | double zFourierAcc(zFourier *f, double t) 58 | { 59 | int i; 60 | double s, c, val = 0; 61 | 62 | for( i=0; in; i++ ){ 63 | zSinCos( i*t, &s, &c ); 64 | val -= i*i * ( f->sc[i]*s + f->cc[i]*c ); 65 | } 66 | return val; 67 | } 68 | 69 | /* print out coefficients of a Fourier series. */ 70 | void zFourierFPrint(FILE *fp, zFourier *fourier) 71 | { 72 | int i; 73 | 74 | fprintf( fp, "%d\n", fourier->n ); 75 | for( i=0; in; i++ ) 76 | fprintf( fp, " %.10g", fourier->sc[i] ); 77 | fprintf( fp, "\n" ); 78 | for( i=0; in; i++ ) 79 | fprintf( fp, " %.10g", fourier->cc[i] ); 80 | fprintf( fp, "\n" ); 81 | } 82 | -------------------------------------------------------------------------------- /src/zm_ieee.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ieee - IEEE conforming irregular values. 5 | */ 6 | 7 | #include 8 | 9 | /* HUGE_VAL conforming to IEEE 754 floating point. */ 10 | const __ieee_fp_t zm_huge_val = { 11 | # if __BYTE_ORDER == __BIG_ENDIAN 12 | { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 } 13 | # endif 14 | # if __BYTE_ORDER == __LITTLE_ENDIAN 15 | { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f } 16 | # endif 17 | }; 18 | 19 | /* check if the value is infinity. */ 20 | int zIsInf(double x) 21 | { 22 | __ieee_fp_t val; 23 | 24 | val.d = x; 25 | if( val.c[6] != 0xf0 || 26 | ( val.c[5] | val.c[4] | val.c[3] | val.c[2] | val.c[1] | val.c[0] ) != 0 ) 27 | return 0; 28 | return val.c[7] == 0x7f ? 1 : ( val.c[7] == 0xff ? -1 : 0 ); 29 | } 30 | 31 | /* Not-a-number conforming to IEEE 754 floating point. */ 32 | const __ieee_fp_t zm_nan_val = { 33 | # if __BYTE_ORDER == __BIG_ENDIAN 34 | { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 } 35 | # endif 36 | # if __BYTE_ORDER == __LITTLE_ENDIAN 37 | { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f } 38 | # endif 39 | }; 40 | 41 | /* check if the value is not-a-number. */ 42 | int zIsNan(double x) 43 | { 44 | __ieee_fp_t val; 45 | 46 | val.d = x; 47 | return ( ( val.c[7] & 0x7f ) == 0x7f && ( val.c[6] & 0xf0 ) == 0xf0 && 48 | ( ( val.c[6] & 0xf ) | val.c[5] | val.c[4] | val.c[3] | 49 | val.c[2] | val.c[1] | val.c[0] ) ) ? 1 : 0; 50 | } 51 | 52 | /* check if the value is a finite number. */ 53 | int zIsFinite(double x) 54 | { 55 | return zIsInf( x ) != 1 && zIsInf( x ) != -1 && !zIsNan( x ) ? 1 : 0; 56 | } 57 | -------------------------------------------------------------------------------- /src/zm_intg.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_intg - numerical integration. 5 | */ 6 | 7 | #include 8 | 9 | /* zIntgInit 10 | * - initialize integrator. 11 | */ 12 | void zIntgInit(zIntg *intg, double s0, double x0) 13 | { 14 | intg->s = s0; 15 | intg->x0 = x0; 16 | 17 | intg->_dt = 0; 18 | intg->_x = 0; 19 | } 20 | 21 | /* zIntgRect 22 | * - rectangular integration. 23 | */ 24 | double zIntgRect(zIntg *intg, double x, double dt) 25 | { 26 | intg->s += intg->x0 * dt; 27 | intg->x0 = x; 28 | return intg->s; 29 | } 30 | 31 | /* zIntgTR 32 | * - trapezoidal integration. 33 | */ 34 | double zIntgTR(zIntg *intg, double x, double dt) 35 | { 36 | intg->s += 0.5 * ( intg->x0 + x ) * dt; 37 | intg->x0 = x; 38 | return intg->s; 39 | } 40 | 41 | /* zIntgQuad 42 | * - integration with an arrangement of Simpson's formula. 43 | */ 44 | double zIntgQuad(zIntg *intg, double x, double dt) 45 | { 46 | double a, b, c, d; 47 | 48 | if( intg->_dt == 0 ){ 49 | intg->_dt = dt; 50 | intg->_x = intg->x0; 51 | return zIntgTR( intg, x, dt ); 52 | } 53 | d = dt + intg->_dt; 54 | a = dt / ( 6*intg->_dt ) + 0.5; 55 | b = ( dt/3 + 0.5*intg->_dt ) / d; 56 | c = - dt*dt / ( 6*intg->_dt*d ); 57 | intg->s += ( intg->x0 * a + x * b + intg->_x * c ) * dt; 58 | intg->_x = intg->x0; 59 | intg->_dt = dt; 60 | intg->x0 = x; 61 | return intg->s; 62 | } 63 | -------------------------------------------------------------------------------- /src/zm_ip.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ip - interpolation. 5 | */ 6 | 7 | #include 8 | 9 | /* ********************************************************** */ 10 | /* CLASS: zIP 11 | * interpolation curve class 12 | * ********************************************************** */ 13 | 14 | /* assign a sequence to an interpolator and allocate the internal workspace. */ 15 | bool zIPDataAlloc(zIPData *dat, const zSeq *seq) 16 | { 17 | zSeqCell *cp; 18 | zIPKnotCell *kp; 19 | double t = 0; 20 | int i = 0; 21 | 22 | if( zListIsEmpty(seq) ){ 23 | ZRUNWARN( ZM_WARN_SEQ_EMPTY ); 24 | return true; 25 | } 26 | zArrayAlloc( &dat->knot, zIPKnotCell, zListSize(seq) ); 27 | if( zArraySize(&dat->knot) == 0 ) return false; 28 | if( !zVecArrayAlloc( &dat->va, zVecSize(zListHead(seq)->data.v), zListSize(seq) ) ){ 29 | ZALLOCERROR(); 30 | zIPDataFree( dat ); 31 | return false; 32 | } 33 | dat->seq = seq; 34 | zListForEachRew( dat->seq, cp ){ 35 | kp = zIPDataKnot(dat,i++); 36 | kp->t = ( t += cp->data.dt ); 37 | kp->cp = cp; 38 | } 39 | return true; 40 | } 41 | 42 | /* free the internal workspace of an interpolator. */ 43 | void zIPDataFree(zIPData *dat) 44 | { 45 | zArrayFree( &dat->knot ); 46 | zVecArrayFree( &dat->va ); 47 | dat->seq = NULL; 48 | } 49 | 50 | /* find a segment in which the absessi is included. */ 51 | int zIPDataSeg(const zIPData *dat, double t) 52 | { 53 | int i, j, k; 54 | 55 | for( i=0, j=zIPDataSize(dat)-1; ; ){ 56 | if( ( k = ( i + j ) / 2 ) == i ) break; 57 | if( zIPDataTime(dat,k) > t ) j = k; 58 | else i = k; 59 | } 60 | return i; 61 | } 62 | -------------------------------------------------------------------------------- /src/zm_ip_clothoid.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ip_clothoid - interpolation: clothoid curve interpolation. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | /* argument angle of terminal displacement vector of a clothoid curve segment. */ 11 | static double _zClothoidArg(double x0, double y0, double f0, double f1, double fs) 12 | { 13 | double s, c; 14 | 15 | zFresnelIntgGen( 1, f0, f1 - f0 - fs, fs, &s, &c ); 16 | return atan2( s - y0, c - x0 ); 17 | } 18 | 19 | /* create a segment of clothoid curve. */ 20 | zClothoid *zClothoidCreateSegment(zClothoid *cl, double x0, double y0, double f0, double x1, double y1, double f1) 21 | { 22 | double a, a0, a1, am, s, c; 23 | double fs, fs0, fs1; 24 | int i, iter = 0; 25 | 26 | a = atan2( y1 - y0, x1 - x0 ); 27 | a0 = _zClothoidArg( x0, y0, f0, f1, ( fs0 =-2*zPI ) ); 28 | a1 = _zClothoidArg( x0, y0, f0, f1, ( fs1 = 2*zPI ) ); 29 | if( a0 > a1 ){ 30 | zSwap( double, a0, a1 ); 31 | zSwap( double, fs0, fs1 ); 32 | } 33 | ZITERINIT( iter ); 34 | for( fs=0, i=0; i am ){ 38 | fs0 = fs; 39 | a0 = am; 40 | } else{ 41 | fs1 = fs; 42 | a1 = am; 43 | } 44 | fs = 0.5 * ( fs0 + fs1 ); 45 | } 46 | if( i >= iter ) ZITERWARN( iter ); 47 | cl->x0 = x0; 48 | cl->y0 = y0; 49 | zFresnelIntgGen( 1, ( cl->f0 = f0 ), ( cl->fc = f1 - f0 - fs ), ( cl->fs = fs ), &s, &c ); 50 | cl->f1 = f1; 51 | cl->_h = sqrt( ( zSqr( x1 - x0 ) + zSqr( y1 - y0 ) ) / ( s*s + c*c ) ); 52 | return cl; 53 | } 54 | 55 | /* x-y values of a clothoid curve. */ 56 | bool zClothoidXY(const zClothoid *cl, double s, double *x, double *y) 57 | { 58 | double dx, dy; 59 | 60 | if( !zFresnelIntgGen( s, cl->f0, cl->fc, cl->fs, &dy, &dx ) ) return false; 61 | *x = cl->x0 + cl->_h * dx; 62 | *y = cl->x0 + cl->_h * dy; 63 | return true; 64 | } 65 | -------------------------------------------------------------------------------- /src/zm_le_lyapnov.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_le_lyapnov - linear equation: Lyapnov equation. 5 | */ 6 | 7 | #include 8 | 9 | /* Lyapnov equation solver. */ 10 | zMat zLELyapnovSolve(const zMat a, const zMat b, zMat ans) 11 | { 12 | zVecStruct bvec, ansvec; 13 | zVec s; 14 | zMat aw, bt; 15 | zIndex idx; 16 | int n, nn, i, j, k; 17 | 18 | if( !zMatIsSqr(a) || !zMatIsSqr(b) || !zMatIsSqr(ans) ){ 19 | ZRUNERROR( ZM_ERR_MAT_NOTSQR ); 20 | return NULL; 21 | } 22 | if( !zMatSizeEqual(a,ans) || !zMatSizeEqual(b,ans) ){ 23 | ZRUNERROR( ZM_ERR_MAT_SIZEMISMATCH ); 24 | return NULL; 25 | } 26 | n = zMatRowSizeNC(ans); 27 | nn = n * n; 28 | bt = zMatAllocSqr( n ); 29 | aw = zMatAllocSqr( nn ); 30 | idx = zIndexCreate( nn ); 31 | s = zVecAlloc( nn ); 32 | if( !bt || !aw || !idx || !s ){ 33 | ans = NULL; 34 | goto TERMINATE; 35 | } 36 | zMatTNC( b, bt ); 37 | zVecSetSizeNC( &bvec, nn ); 38 | zVecBufNC(&bvec) = zMatBuf(bt); 39 | zVecSetSizeNC( &ansvec, nn ); 40 | zVecBufNC(&ansvec) = zMatBuf(ans); 41 | for( i=0; i 8 | 9 | /* tridiagonal equation solver (destructive). */ 10 | zVec zLETridiagSolveDST(zVec a, zVec b, zVec c, zVec d, zVec ans) 11 | { 12 | int i; 13 | int n; 14 | 15 | n = zVecSize( a ); 16 | for( i=1; i=0; i-- ) 27 | zVecSetElemNC( ans, i, 28 | (zVecElemNC(d,i)-zVecElemNC(c,i)*zVecElemNC(ans,i+1)) / zVecElemNC(b,i) ); 29 | return ans; 30 | } 31 | 32 | /* tridiagonal equation solver. */ 33 | zVec zLETridiagSolve(const zVec a, const zVec b, const zVec c, const zVec d, zVec ans) 34 | { 35 | zVec acp, bcp, dcp; 36 | 37 | if( !zVecSizeEqual(a,b) || !zVecSizeEqual(b,c) || 38 | !zVecSizeEqual(c,d) || !zVecSizeEqual(d,ans) ){ 39 | ZRUNERROR( ZM_ERR_VEC_SIZEMISMATCH ); 40 | return NULL; 41 | } 42 | acp = zVecClone( a ); 43 | bcp = zVecClone( b ); 44 | dcp = zVecClone( d ); 45 | if( acp && bcp && dcp ) 46 | ans = zLETridiagSolveDST( acp, bcp, c, dcp, ans ); 47 | zVecFreeAtOnce( 3, acp, bcp, dcp ); 48 | return ans; 49 | } 50 | -------------------------------------------------------------------------------- /src/zm_nle_ss.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_nle_ss - nonlinear equation: successive substitution method. 5 | */ 6 | 7 | #include 8 | 9 | /* successive substitution method to solve equation x=f(x). */ 10 | zVec zSSSolve(zVec (* f)(const zVec,zVec,void*), zVec x, void *util, int iter) 11 | { 12 | int i; 13 | zVec y, e; 14 | 15 | y = zVecAlloc( zVecSizeNC(x) ); 16 | e = zVecAlloc( zVecSizeNC(x) ); 17 | if( !y || !e ){ 18 | x = NULL; 19 | goto TERMINATE; 20 | } 21 | ZITERINIT( iter ); 22 | for( i=0; i 8 | 9 | /* concatenate deviation in finite time step to the current variable vector. */ 10 | zVec _zODECatDefault(zVec x, double dt, zVec dx, zVec xn, void *dummy) 11 | { 12 | return zVecCatNC( x, dt, dx, xn ); 13 | } 14 | 15 | /* subtract a variable vector from another. */ 16 | zVec _zODESubDefault(zVec x1, zVec x2, zVec dx, void *dummy) 17 | { 18 | return zVecSubNC( x1, x2, dx ); 19 | } 20 | 21 | void zODEAssignFunc(zODE *ode, zVec (* catf)(zVec,double,zVec,zVec,void*), zVec (* subf)(zVec,zVec,zVec,void*)) 22 | { 23 | ode->cat = catf ? catf : _zODECatDefault; 24 | ode->sub = subf ? subf : _zODESubDefault; 25 | } 26 | -------------------------------------------------------------------------------- /src/zm_ode_dc.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_dc - ordinary differential equation quadrature: 5 | * deferred correction with Richardson's extrapolation. 6 | */ 7 | 8 | #include 9 | 10 | /* zODEInitDC 11 | * - initialize ODE solver enabling deferred correction. 12 | */ 13 | zODE *zODEInitDC(zODE *ode, int dim, int step, zVec (* f)(double,zVec,void*,zVec)) 14 | { 15 | ode->_x1 = zVecAlloc( dim ); 16 | ode->_x2 = zVecAlloc( dim ); 17 | if( !ode->_x1 || !ode->_x2 ){ 18 | ZALLOCERROR(); 19 | return NULL; 20 | } 21 | return zODEInit( ode, dim, step, f ); 22 | } 23 | 24 | /* zODEDestroyDC 25 | * - destroy ODE solver enabling deferred correction. 26 | */ 27 | void zODEDestroyDC(zODE *ode) 28 | { 29 | zVecFree( ode->_x1 ); 30 | zVecFree( ode->_x2 ); 31 | zODEDestroy( ode ); 32 | } 33 | 34 | #define ZODE_DC_TOL ( 1.0e-6 ) 35 | /* zODEUpdateDC 36 | * - update state vector by solving ODE enabling deferred correction. 37 | */ 38 | zVec zODEUpdateDC(zODE *ode, double t, zVec x, double dt, void *util) 39 | { 40 | /* full-step update */ 41 | zVecCopyNC( x, ode->_x1 ); 42 | zODEUpdate( ode, t, ode->_x1, dt, util ); 43 | /* half-step-twice update */ 44 | zVecCopyNC( x, ode->_x2 ); 45 | dt *= 0.5; 46 | zODEUpdate( ode, t, ode->_x2, dt, util ); 47 | zODEUpdate( ode, t+dt, ode->_x2, dt, util ); 48 | /* evaluation */ 49 | if( zVecDist( ode->_x1, ode->_x2 ) > ZODE_DC_TOL ){ 50 | zODEUpdateDC( ode, t, x, dt, util ); 51 | zODEUpdateDC( ode, t+dt, x, dt, util ); 52 | } else{ /* Richardson's extrapolation */ 53 | zVecMul( ode->_x2, 2, x ); 54 | ode->sub( x, ode->_x1, x, util ); 55 | } 56 | return x; 57 | } 58 | -------------------------------------------------------------------------------- /src/zm_ode_euler.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_euler - ordinary differential equation quadrature: 5 | * Euler method. 6 | */ 7 | 8 | #include 9 | 10 | /* initialize ODE solver based on Euler method. */ 11 | zODE* zODEInit_Euler(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)) 12 | { 13 | if( !( ode->_ws = zVecAlloc( dim ) ) ){ 14 | ZALLOCERROR(); 15 | return NULL; 16 | } 17 | ode->f = f; 18 | return ode; 19 | } 20 | 21 | /* destroy ODE solver. */ 22 | void zODEDestroy_Euler(zODE *ode) 23 | { 24 | zVecFree( (zVec)ode->_ws ); 25 | ode->f = NULL; 26 | } 27 | 28 | /* directly integrate variable by ODE based on Euler method. */ 29 | zVec zODEUpdate_Euler(zODE *ode, double t, zVec x, double dt, void *util) 30 | { 31 | ode->f( t, x, util, (zVec)ode->_ws ); 32 | return ode->cat( x, dt, (zVec)ode->_ws, x, util ); 33 | } 34 | -------------------------------------------------------------------------------- /src/zm_ode_heun.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_ode_heun - ordinary differential equation quadrature: 5 | * Heun method. 6 | */ 7 | 8 | #include 9 | 10 | typedef struct{ 11 | zVec x, k[3]; 12 | } _zODE_Heun; 13 | 14 | /* initialize ODE solver based on Heun method. */ 15 | zODE* zODEInit_Heun(zODE *ode, int dim, int dummy, zVec (* f)(double,zVec,void*,zVec)) 16 | { 17 | _zODE_Heun *ws; 18 | 19 | if( !( ws = zAlloc( _zODE_Heun, 1 ) ) || 20 | !( ws->x = zVecAlloc( dim ) ) /* incremental vector */ || 21 | !( ws->k[0] = zVecAlloc( dim ) ) /* step-1 vector */ || 22 | !( ws->k[1] = zVecAlloc( dim ) ) /* step-2 vector */ || 23 | !( ws->k[2] = zVecAlloc( dim ) ) /* step-3 vector */ ){ 24 | ZALLOCERROR(); 25 | return NULL; 26 | } 27 | ode->f = f; 28 | ode->_ws = ws; 29 | return ode; 30 | } 31 | 32 | /* destroy ODE solver. */ 33 | void zODEDestroy_Heun(zODE *ode) 34 | { 35 | _zODE_Heun *ws; 36 | 37 | ws = (_zODE_Heun *)ode->_ws; 38 | zVecFreeAtOnce( 4, ws->x, ws->k[0], ws->k[1], ws->k[2] ); 39 | zFree( ode->_ws ); 40 | ode->f = NULL; 41 | } 42 | 43 | /* directly integrate variable by ODE based on classical Runge-Kutta method. */ 44 | zVec zODEUpdate_Heun(zODE *ode, double t, zVec x, double dt, void *util) 45 | { 46 | _zODE_Heun *ws; 47 | 48 | ws = (_zODE_Heun *)ode->_ws; 49 | ode->f( t, x, util, ws->k[0] ); 50 | ode->cat( x, dt/3, ws->k[0], ws->x, util ); 51 | ode->f( t+dt/3, ws->x, util, ws->k[1] ); 52 | ode->cat( x, dt*2/3, ws->k[1], ws->x, util ); 53 | ode->f( t+dt*2/3, ws->x, util, ws->k[2] ); 54 | 55 | ode->cat( x, 0.25*dt, ws->k[0], x, util ); 56 | ode->cat( x, 0.75*dt, ws->k[2], x, util ); 57 | return x; 58 | } 59 | -------------------------------------------------------------------------------- /src/zm_oscil_kura.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_oscil_kura - nonlinear oscillator: Kuramoto's oscillator. 5 | */ 6 | 7 | #include 8 | 9 | typedef struct{ 10 | double wn; /* natural frequency */ 11 | double ke; /* entrainment gain */ 12 | double po; /* phase offset */ 13 | double p; /* phase */ 14 | double w; /* state */ 15 | } zOscKura; 16 | 17 | /* initialize Kuramoto's oscillator. */ 18 | static void _zOscInitKura(void *prp, double p0, double w0) 19 | { 20 | ((zOscKura *)prp)->p = p0; 21 | ((zOscKura *)prp)->w = w0; 22 | } 23 | 24 | /* update the internal state of Kuramoto's oscillator. */ 25 | static double _zOscUpdateKura(void *prp, double u, double dt) 26 | { 27 | zOscKura *kura; 28 | 29 | kura = (zOscKura *)prp; 30 | kura->w = kura->wn + kura->ke * sin( u - kura->p - kura->po ); 31 | return kura->p += kura->w * dt; 32 | } 33 | 34 | /* output a value of Kuramoto's oscillator. */ 35 | static double _zOscOutputKura(void *prp) 36 | { 37 | return ((zOscKura *)prp)->p; 38 | } 39 | 40 | static zOscCom _z_osc_com_kura = { 41 | _zOscInitKura, 42 | _zOscUpdateKura, 43 | _zOscOutputKura, 44 | }; 45 | 46 | /* create Kuramoto's oscillator. */ 47 | zOsc *zOscCreateKura(zOsc *osc, double wn, double ke, double po) 48 | { 49 | zOscKura *kura; 50 | 51 | if( !( kura = zAlloc( zOscKura, 1 ) ) ){ 52 | ZALLOCERROR(); 53 | return NULL; 54 | } 55 | kura->wn = wn; 56 | kura->ke = ke; 57 | kura->po = po; 58 | osc->prp = kura; 59 | osc->com = &_z_osc_com_kura; 60 | return osc; 61 | } 62 | -------------------------------------------------------------------------------- /src/zm_oscil_vdp.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_oscil_vdp - nonlinear oscillator: van der Pol's oscillator. 5 | */ 6 | 7 | #include 8 | 9 | typedef struct{ 10 | double omega; /* natural angular frequency */ 11 | double eps; /* damping coefficient */ 12 | double amp; /* output amplitude */ 13 | double _x, _v; /* internal state */ 14 | } zOscVDP; 15 | 16 | /* initialize van der Pol's oscillator. */ 17 | static void _zOscInitVDP(void *prp, double x0, double v0) 18 | { 19 | ((zOscVDP *)prp)->_x = x0 / ((zOscVDP *)prp)->amp; 20 | ((zOscVDP *)prp)->_v = v0 / ((zOscVDP *)prp)->amp; 21 | } 22 | 23 | /* output a value of van der Pol's oscillator. */ 24 | static double _zOscOutputVDP(void *prp) 25 | { 26 | return ((zOscVDP *)prp)->amp * ((zOscVDP *)prp)->_x; 27 | } 28 | 29 | /* update the internal state of van der Pol's oscillator. */ 30 | static double _zOscUpdateVDP(void *prp, double u, double dt) 31 | { 32 | zOscVDP *vdp; 33 | double a; 34 | 35 | vdp = (zOscVDP *)prp; 36 | a = -vdp->omega * ( vdp->eps*( zSqr(vdp->_x) - 1 ) * vdp->_v + vdp->omega*vdp->_x ) - u; 37 | vdp->_v += a * dt; 38 | vdp->_x += vdp->_v * dt; 39 | return _zOscOutputVDP( prp ); 40 | } 41 | 42 | static zOscCom _z_osc_com_vdp = { 43 | _zOscInitVDP, 44 | _zOscUpdateVDP, 45 | _zOscOutputVDP, 46 | }; 47 | 48 | /* create van der Pol's oscillator. */ 49 | zOsc *zOscCreateVDP(zOsc *osc, double t, double damp, double amp) 50 | { 51 | zOscVDP *vdp; 52 | 53 | if( zIsTiny(t) ){ 54 | ZRUNERROR( ZM_ERR_OSCIL_INVALID_TERM ); 55 | return NULL; 56 | } 57 | if( !( vdp = zAlloc( zOscVDP, 1 ) ) ){ 58 | ZALLOCERROR(); 59 | return NULL; 60 | } 61 | vdp->omega = zPIx2 / t; 62 | vdp->eps = damp; 63 | vdp->amp = amp * 0.5; /* natural amplitude of VDP is 2 */ 64 | osc->prp = vdp; 65 | osc->com = &_z_osc_com_vdp; 66 | return osc; 67 | } 68 | -------------------------------------------------------------------------------- /src/zm_sf_erf.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_sf_erf - special functions : Gauss's error function. 5 | */ 6 | 7 | #include 8 | 9 | /* error function approximated by Taylor series. */ 10 | double zErfN(double x, int n) 11 | { 12 | double x2, s, d; 13 | int i, j; 14 | 15 | x2 = x*x; 16 | for( s=0, i=0; i<=n; i++ ){ 17 | d = 1; 18 | for( j=1; j<=i; j++ ) 19 | d *= -x2/j; 20 | s += d*x/(2*i+1); 21 | } 22 | return 2*s / sqrt(zPI); 23 | } 24 | -------------------------------------------------------------------------------- /src/zm_sf_gamma.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_sf_gamma - special functions : gamma and beta function. 5 | */ 6 | 7 | #include 8 | 9 | /* a seed value of gamma function. */ 10 | static double _zGammaSeed(double x) 11 | { 12 | double s = 1.000000000190015; 13 | 14 | s -= 0.000005395239384953 / (x+6); 15 | s += 0.001208650973866179 / (x+5); 16 | s -= 1.231739572450155 / (x+4); 17 | s += 24.01409824083091 / (x+3); 18 | s -= 86.50532032941677 / (x+2); 19 | s += 76.18009172947146 / (x+1); 20 | return 2.5066282746310005 * s / x; 21 | } 22 | 23 | /* a vias value of gamma function. */ 24 | static double _zGammaBias(double x) 25 | { 26 | return ( x + 0.5 ) * log( x + 5.5 ) - x - 5.5; 27 | } 28 | 29 | /* gamma function. */ 30 | double zGamma(double x) 31 | { 32 | return exp( _zGammaBias(x) ) * _zGammaSeed(x); 33 | } 34 | 35 | /* logarithm of gamma function. */ 36 | double zLnGamma(double x) 37 | { 38 | return _zGammaBias(x) + log( _zGammaSeed(x) ); 39 | } 40 | 41 | /* beta function. */ 42 | double zBeta(double z, double w) 43 | { 44 | return exp( zLnGamma(z) + zLnGamma(w) - zLnGamma(z+w) ); 45 | } 46 | -------------------------------------------------------------------------------- /src/zm_vec_array.c: -------------------------------------------------------------------------------- 1 | /* ZM - Z's Mathematics Toolbox 2 | * Copyright (C) 1998 Tomomichi Sugihara (Zhidao) 3 | * 4 | * zm_vec_array - vector array class. 5 | */ 6 | 7 | #include 8 | 9 | /* allocate a vector array. */ 10 | bool zVecArrayAlloc(zVecArray *array, int dim, int num) 11 | { 12 | int i; 13 | 14 | zArrayAlloc( array, zVec, num ); 15 | for( i=0; i 8 | 9 | /* allocate a vector ring. */ 10 | bool zVecRingAlloc(zVecRing *ring, int dim, int num) 11 | { 12 | int i; 13 | 14 | zRingAlloc( ring, zVec, num ); 15 | for( i=0; i 2 | 3 | void assert_get_put(void) 4 | { 5 | const int rowsize = 12; 6 | const int colsize = 10; 7 | zCMat mat_test1, mat_test2; 8 | zCVec vec_test1, vec_test2; 9 | int i; 10 | bool result; 11 | 12 | mat_test1 = zCMatAlloc( rowsize, colsize ); 13 | mat_test2 = zCMatAlloc( rowsize, colsize ); 14 | vec_test1 = zCVecAlloc( colsize ); 15 | vec_test2 = zCVecAlloc( rowsize ); 16 | zCMatRandUniform( mat_test1, -10, 10, -5, 5 ); 17 | zCMatRandUniform( mat_test2, -10, 10, -5, 5 ); 18 | 19 | zCMatGetRow( mat_test1, 3, vec_test1 ); 20 | for( result=true, i=0; i 2 | 3 | #define TEST_VEC_SIZE 1000 4 | 5 | void assert_misc(void) 6 | { 7 | zCVec v1, v2, v3; 8 | 9 | v1 = zCVecAlloc( TEST_VEC_SIZE ); 10 | zCVecRandUniform( v1, -10, -10, 10, 10 ); 11 | v2 = zCVecClone( v1 ); 12 | v3 = zCVecAlloc( TEST_VEC_SIZE ); 13 | zCVecRandUniform( v3, -10, -10, 10, 10 ); 14 | zAssert( zCVecEqual, zCVecEqual(v1,v2,zTOL) && !zCVecEqual(v1,v3,zTOL) ); 15 | 16 | zCVecFree( v1 ); 17 | zCVecFree( v2 ); 18 | zCVecFree( v3 ); 19 | } 20 | 21 | int main(void) 22 | { 23 | zRandInit(); 24 | assert_misc(); 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /test/ieee_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | #ifndef __WINDOWS__ 6 | double val; 7 | 8 | val = 1.0 / 0.0; 9 | zAssert( zIsInf, zIsInf( HUGE_VAL ) == 1 && 10 | zIsInf(-HUGE_VAL ) == -1 && 11 | zIsInf( log( 0.0 ) ) == -1 && 12 | zIsInf( val ) == 1 && 13 | zIsInf( NAN ) == 0 && 14 | zIsInf( 0 ) == 0 ); 15 | zAssert( zIsNan, zIsNan( NAN ) && 16 | zIsNan( sqrt( -1 ) ) && 17 | zIsNan( log( -1 ) ) && 18 | !zIsNan( HUGE_VAL ) && 19 | !zIsNan( 0 ) ); 20 | zAssert( zIsFinite, zIsFinite( 1.0 ) && 21 | zIsFinite(-1.0 ) && 22 | !zIsFinite( HUGE_VAL ) && 23 | !zIsFinite(-HUGE_VAL ) && 24 | !zIsFinite( sqrt( -1 ) ) ); 25 | #endif /* __WINDOWS__ */ 26 | return EXIT_SUCCESS; 27 | } 28 | -------------------------------------------------------------------------------- /test/ip_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void assert_spline_cubicequation(void) 4 | { 5 | zSeq seq; 6 | zIP ip; 7 | zVec v, a, b, c, d, v_cubic; 8 | const int test_num = 100; 9 | int point_num, i, k; 10 | double tp[] = { 0, 2, 3, 5, 6 }; 11 | double vp[] = { 0, 3, 1,-2, 4 }; 12 | double t; 13 | bool result = true; 14 | 15 | point_num = sizeof(tp) / sizeof(double); 16 | zSeqInit( &seq ); 17 | for( t=0, i=0; i 2 | 3 | void assert_nurbs_clone(void) 4 | { 5 | zNURBS src, dest; 6 | zSeq seq; 7 | zVec vs, vd; 8 | int num, i, k; 9 | const int testnum = 100; 10 | double xp[] = { 2.0, 3.0, 5.0, 4.0, 5.0, 7.0 }; 11 | double yp[] = { 3.0,-1.0,-2.0, 0.0, 4.0, 1.5 }; 12 | bool result = true; 13 | 14 | num = sizeof(xp) / sizeof(double); 15 | zListInit( &seq ); 16 | for( i=0; i