├── .devcontainer └── devcontainer.json ├── .gitattributes ├── .github └── workflows │ └── go.yml ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── all.bash ├── chk ├── README.md ├── assert.go ├── auxiliary.go ├── chk.go ├── deriv.go ├── print.go ├── recover.go ├── t_assert_test.go ├── t_auxiliary_test.go ├── t_chk_test.go ├── t_derivs_test.go ├── t_print_test.go ├── t_recover_test.go ├── t_testing_test.go └── testing.go ├── fun ├── README.md ├── chebyinterp.go ├── chebyshev.go ├── data │ ├── README.md │ ├── as-17-elliptic-integrals-table17.5-big.cmp │ ├── as-17-elliptic-integrals-table17.5-small.cmp │ ├── as-17-elliptic-integrals-table17.5a.txt │ ├── as-17-elliptic-integrals-table17.5b.txt │ ├── as-17-elliptic-integrals-table17.5c.txt │ ├── as-17-elliptic-integrals-table17.6-big.cmp │ ├── as-17-elliptic-integrals-table17.6-small.cmp │ ├── as-17-elliptic-integrals-table17.6a.txt │ ├── as-17-elliptic-integrals-table17.6b.txt │ ├── as-17-elliptic-integrals-table17.6c.txt │ ├── as-17-elliptic-integrals-table17.9-big.cmp │ ├── as-17-elliptic-integrals-table17.9-small.cmp │ ├── as-17-elliptic-integrals-table17.9.txt │ ├── as-9-bessel-integer-big.cmp │ ├── as-9-bessel-integer-sml.cmp │ ├── as-9-bessel-integer-tables9.txt │ ├── as-9-modbessel-integer-big.cmp │ ├── as-9-modbessel-integer-neg.cmp │ ├── as-9-modbessel-integer-sml.cmp │ ├── as-9-modbessel-integer-tables9.txt │ ├── genBesselInteger.py │ ├── genEllipInt1table.py │ ├── genEllipInt2table.py │ ├── genEllipInt3table.py │ └── genModBesselInteger.py ├── datainterp.go ├── definitions.go ├── dft.go ├── ellipticintegral.go ├── fftw │ ├── README.md │ ├── flags.go │ ├── plan1d.go │ ├── plan2d.go │ ├── t_fftw1d_test.go │ ├── t_fftw2d_test.go │ └── t_init_test.go ├── fourierinterp.go ├── functions.go ├── genorthopoly.go ├── interpcubic.go ├── interpquad.go ├── lagrange.go ├── modbessel.go ├── multiinterp.go ├── run_bench.go ├── sinusoid.go ├── t_b_functions_test.go ├── t_b_imagpown_test.go ├── t_b_interp_test.go ├── t_b_multiinterp_test.go ├── t_bessel_test.go ├── t_chebyinterp_test.go ├── t_chebyshev_test.go ├── t_dft_test.go ├── t_elliptic_test.go ├── t_fourier_test.go ├── t_functions_test.go ├── t_genorthopoly_test.go ├── t_init_test.go ├── t_interp_test.go ├── t_interpcubic_test.go ├── t_interpquad_test.go ├── t_lagcheby_test.go ├── t_lagrange_test.go ├── t_multiinterp_test.go └── t_sinusoid_test.go ├── gm ├── README.md ├── basicgeom.go ├── bezier.go ├── bins.go ├── bspline.go ├── data │ ├── README.md │ ├── m_nurbs08a.msh │ ├── m_nurbs08b.msh │ └── m_nurbs08c.msh ├── factory_nurbs.go ├── factory_tfinite.go ├── grid.go ├── metis.go ├── metrics.go ├── msh │ ├── README.md │ ├── data │ │ ├── README.md │ │ ├── cubeandtet.msh │ │ ├── mesh01.msh │ │ ├── meshelems1.png │ │ ├── meshelems1.svg │ │ └── singleq4square1x1.msh │ ├── generators.go │ ├── integration.go │ ├── mesh.go │ ├── output.go │ ├── quadpoints.go │ ├── s_hexs.go │ ├── s_lins.go │ ├── s_quads.go │ ├── s_tets.go │ ├── s_tris.go │ ├── shapes.go │ ├── t_b_integ_test.go │ ├── t_generators_test.go │ ├── t_init_test.go │ ├── t_integ_test.go │ ├── t_mesh_test.go │ ├── t_quadpts_test.go │ ├── t_shapes_test.go │ └── topology.go ├── nurbs.go ├── nurbspatch.go ├── octree.go ├── t_basicgeom_test.go ├── t_bezier_test.go ├── t_bins_test.go ├── t_bspline_test.go ├── t_check_test.go ├── t_factory_test.go ├── t_grid_test.go ├── t_init_test.go ├── t_metis_test.go ├── t_nurbs_methods_test.go ├── t_nurbs_test.go ├── t_nurbspatch_test.go ├── t_octree_test.go ├── t_transfinite_test.go ├── topologynurbs.go ├── transfinite.go └── tri │ ├── README.md │ ├── data │ ├── README.md │ ├── tri_delaunay01.png │ └── tri_draw01.png │ ├── delaunay.go │ ├── generate.go │ ├── mesh.go │ ├── t_delaunay_test.go │ ├── t_generate_test.go │ ├── t_init_test.go │ ├── triangle.c │ ├── triangle.h │ ├── triangle_README.txt │ └── trianglex.c ├── go.mod ├── hb ├── README.md ├── array.go ├── attributes.go ├── deep2.go ├── deep3.go ├── file.go ├── ints.go ├── t_basic_test.go └── t_init_test.go ├── io ├── README.md ├── args.go ├── auxiliary.go ├── basic.go ├── data │ ├── README.md │ ├── mat01.dat │ └── table01.dat ├── encoder.go ├── external.go ├── fileio.go ├── parsing.go ├── printing.go ├── t_args_test.go ├── t_basic_test.go ├── t_encoder_test.go ├── t_external_test.go ├── t_fileio_test.go ├── t_init_test.go ├── t_parsing_test.go └── t_readtable_test.go ├── la ├── README.md ├── blas1.go ├── blas2.go ├── blas3.go ├── data │ ├── README.md │ ├── small-sparse-matrix-complex-sym.mtx │ ├── small-sparse-matrix-complex.mtx │ ├── small-sparse-matrix-sym.mtx │ └── small-sparse-matrix.mtx ├── densesol.go ├── eigen.go ├── equations.go ├── flags.go ├── jacobi.go ├── matrix.go ├── matrix_ops.go ├── oblas │ ├── README.md │ ├── conversions.go │ ├── data │ │ ├── README.md │ │ ├── auxiliary.py │ │ ├── dgeev01.py │ │ ├── dgemm01.py │ │ ├── dgesv01.py │ │ ├── dgesvd01.py │ │ ├── dgesvd02.py │ │ ├── dgesvd03.py │ │ ├── dgetrf01.py │ │ ├── dpotrf01.py │ │ ├── dsyrk01.py │ │ ├── dsyrk02.py │ │ ├── jacobi01.py │ │ ├── zdgesvd01.py │ │ ├── zgemm01.py │ │ ├── zgemv01.py │ │ ├── zgesvd02.py │ │ ├── zgetrf01.py │ │ ├── zherk01.py │ │ ├── zpotrf01.py │ │ └── zsyrk01.py │ ├── flags.go │ ├── oblas.go │ ├── openblas_cblas.h │ ├── openblas_config.h │ ├── t_conversions_test.go │ ├── t_init_test.go │ └── t_oblas_test.go ├── sparse_blas.go ├── sparse_config.go ├── sparse_conversions.go ├── sparse_matrix.go ├── sparse_solver.go ├── sparse_solver_mumps.go ├── sparse_solver_umfpack.go ├── t_blas1_test.go ├── t_blas2_test.go ├── t_blas3_test.go ├── t_densesol_test.go ├── t_eigen_test.go ├── t_equations_test.go ├── t_init_test.go ├── t_jacobi_test.go ├── t_matrix_ops_test.go ├── t_matrix_test.go ├── t_sp_blas_test.go ├── t_sp_conversions_test.go ├── t_sp_matrix_test.go ├── t_sp_solver_mumps_test.go ├── t_sp_solver_test.go ├── t_sp_solver_umfpack_test.go ├── t_vector_test.go ├── testing.go └── vector.go ├── num ├── README.md ├── auxiliary.go ├── bracket.go ├── brent.go ├── cubicequation.go ├── data │ ├── README.md │ ├── nls01.log │ ├── nls02.log │ ├── nls03.log │ ├── nls04.log │ └── num_brent01.png ├── definitions.go ├── deriv.go ├── fitdata.go ├── jacobian.go ├── linesearch.go ├── linesolver.go ├── nlsolver.go ├── nlsolverconfig.go ├── qpck │ ├── README.md │ ├── connect.c │ ├── connect.h │ ├── d1mach.f │ ├── dqag.f │ ├── dqage.f │ ├── dqagi.f │ ├── dqagie.f │ ├── dqagp.f │ ├── dqagpe.f │ ├── dqags.f │ ├── dqagse.f │ ├── dqawc.f │ ├── dqawce.f │ ├── dqawf.f │ ├── dqawfe.f │ ├── dqawo.f │ ├── dqawoe.f │ ├── dqaws.f │ ├── dqawse.f │ ├── dqc25c.f │ ├── dqc25f.f │ ├── dqc25s.f │ ├── dqcheb.f │ ├── dqelg.f │ ├── dqk15.f │ ├── dqk15i.f │ ├── dqk15w.f │ ├── dqk21.f │ ├── dqk31.f │ ├── dqk41.f │ ├── dqk51.f │ ├── dqk61.f │ ├── dqmomo.f │ ├── dqng.f │ ├── dqpsrt.f │ ├── dqwgtc.f │ ├── dqwgtf.f │ ├── dqwgts.f │ ├── flags.go │ ├── getfiles.bash │ ├── i1mach.f │ ├── quadpack.go │ ├── t_chebmo_test.go │ ├── t_init_test.go │ ├── t_quadpack_test.go │ ├── t_spectral_test.go │ └── xerror.f ├── quadDiscreteSimpson.go ├── quadDiscreteTrapz.go ├── quadElementary.go ├── quadGauss.go ├── quadrature.go ├── t_bracketer_test.go ├── t_brent_test.go ├── t_deriv_test.go ├── t_equations_test.go ├── t_fitdata_test.go ├── t_init_test.go ├── t_jacobian_test.go ├── t_linesolver_test.go ├── t_nlsolver_test.go ├── t_nlsolverconfig_test.go ├── t_quadDisc_test.go ├── t_quadElem_test.go ├── t_quadGauss_test.go └── t_quadrature_test.go ├── ode ├── README.md ├── bweuler.go ├── config.go ├── data │ ├── README.md │ ├── dr1_radau5.cmp │ ├── dr2_dopri5_dense.txt │ ├── dr_dop853.txt │ ├── dr_dop853_dense.txt │ ├── dr_dopri5.txt │ ├── dr_dopri5_dense.txt │ ├── log_mpi.txt │ ├── log_numjac_mpi.txt │ ├── log_ode.txt │ ├── output_testode01.txt │ ├── rober.png │ ├── rober_radau5_cpp.dat │ ├── t_erk04.png │ ├── t_erk05.png │ └── vdpol_radau5_for.dat ├── defs.go ├── erk.go ├── erkhigh.go ├── fweuler.go ├── highlevel.go ├── methods.go ├── ode.go ├── output.go ├── problems.go ├── radau5.go ├── rkwork.go ├── stat.go ├── t_bweuler_test.go ├── t_dopri5_test.go ├── t_dopri8_test.go ├── t_erk_test.go ├── t_fweuler_test.go ├── t_highlevel_test.go ├── t_init_test.go ├── t_moeuler_test.go ├── t_ode_test.go └── t_radau5_test.go ├── opt ├── README.md ├── conjgrad.go ├── convergence.go ├── data │ ├── README.md │ ├── adlittle.dat │ ├── adlittle.mps │ ├── afiro.dat │ ├── afiro.mps │ ├── kb2.dat │ ├── kb2.mps │ ├── opt_comparison01a.png │ ├── opt_comparison01b.png │ ├── opt_conjgrad01.png │ ├── opt_ipm01.png │ ├── opt_ipm02.png │ ├── pilot4.dat │ ├── pilot4.mps │ ├── share1b.dat │ ├── share1b.mps │ └── x_cvxopt.py ├── factory.go ├── graddesc.go ├── history.go ├── linesearch.go ├── linipm.go ├── nonlinsolver.go ├── powell.go ├── problem.go ├── readproblem.go ├── t_conjgrad_test.go ├── t_factobjs_test.go ├── t_graddesc_test.go ├── t_init_test.go ├── t_linesearch_test.go ├── t_linipm_test.go ├── t_nonlinsolver_test.go ├── t_powell_test.go └── x_cvxopt.py ├── pde ├── README.md ├── boundaryconds.go ├── fdm.go ├── spc.go ├── t_bryconds_test.go ├── t_fdm_test.go ├── t_init_test.go └── t_spc_test.go ├── rnd ├── README.md ├── data │ ├── README.md │ ├── all.bash │ ├── frechet.R │ ├── frechet.dat │ ├── gumbel.R │ ├── gumbel.dat │ ├── lognormal.R │ ├── lognormal.dat │ ├── normal.R │ ├── normal.dat │ ├── rnd_haltonAndLatin01.png │ ├── rnd_lognormalDistribution.png │ ├── uniform.R │ └── uniform.dat ├── dist_frechet.go ├── dist_gumbel.go ├── dist_lognormal.go ├── dist_normal.go ├── dist_uniform.go ├── distribution.go ├── dsfmt │ ├── LICENSE-dSFMT.txt │ ├── README.md │ ├── connectdsfmt.c │ ├── connectdsfmt.h │ ├── dSFMT-common.h │ ├── dSFMT-params.h │ ├── dSFMT-params19937.h │ ├── dSFMT.c │ ├── dSFMT.h │ ├── dsfmt.go │ ├── t_dsfmt_test.go │ └── t_init_test.go ├── halton.go ├── histogram.go ├── hypercube.go ├── ltqnorm.go ├── primes.go ├── random.go ├── random_sfmt.go ├── rotations.go ├── sfmt │ ├── LICENSE-SFMT.txt │ ├── README.md │ ├── SFMT-common.h │ ├── SFMT-params.h │ ├── SFMT-params19937.h │ ├── SFMT-sse2.h │ ├── SFMT.c │ ├── SFMT.h │ ├── connectsfmt.c │ ├── connectsfmt.h │ ├── sfmt.go │ ├── t_b_test.go │ ├── t_init_test.go │ └── t_sfmt_test.go ├── stat.go ├── t_b_sfmt_test.go ├── t_b_test.go ├── t_dist_frechet_test.go ├── t_dist_gumbel_test.go ├── t_dist_lognormal_test.go ├── t_dist_normal_test.go ├── t_dist_uniform_test.go ├── t_hist_test.go ├── t_hypercube_test.go ├── t_init_test.go ├── t_random_sfmt_test.go ├── t_random_test.go ├── t_stat_test.go ├── variables.go └── versions.txt ├── utl ├── README.md ├── auxiliary.go ├── constants.go ├── data │ ├── README.md │ ├── basic02.dat │ ├── json01.dat │ └── json02.dat ├── deepslices.go ├── list.go ├── mapops.go ├── mylab.go ├── observer.go ├── params.go ├── pareto.go ├── postprocess.go ├── printing.go ├── profiling.go ├── searching.go ├── serialize.go ├── sorting.go ├── t_auxiliary_test.go ├── t_b_test.go ├── t_constants_test.go ├── t_deepslices_test.go ├── t_init_test.go ├── t_list_test.go ├── t_maps_test.go ├── t_mylab_test.go ├── t_nonlinspace_test.go ├── t_observer_test.go ├── t_params_test.go ├── t_pareto_test.go ├── t_postprocess_test.go ├── t_searching_test.go ├── t_serialize_test.go └── t_sorting_test.go ├── zdocs ├── vscode-gosl-01.gif ├── vscode-intellisense-01.png └── vscode-open-in-container.gif └── zscripts ├── dk-build-image.bash ├── dk-publish.bash ├── dk-shell-into.bash └── microsoft ├── common-debian.sh ├── go-debian.sh └── zdownload.bash /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Gosl", 3 | "image": "gosl/gosl", 4 | "settings": { 5 | "go.useLanguageServer": true, 6 | "go.gopath": "/go", 7 | "go.goroot": "/usr/local/go", 8 | "go.toolsGopath": "/go/bin" 9 | }, 10 | "runArgs": [ 11 | "--cap-add=SYS_PTRACE", 12 | "--security-opt", 13 | "seccomp=unconfined" 14 | ], 15 | "extensions": [ 16 | "golang.Go" 17 | ], 18 | "forwardPorts": [ 19 | 9000 20 | ], 21 | "remoteUser": "vscode" 22 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | gm/tri/triangle.c linguist-vendored 2 | rnd/dsfmt/dSFMT.c linguist-vendored 3 | rnd/sfmt/SFMT.c linguist-vendored 4 | num/qpck/*.f linguist-vendored -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Build and Test 2 | on: [pull_request, push] 3 | jobs: 4 | build_and_test: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - uses: actions/checkout@v2 8 | - name: Install Libraries 9 | run: | 10 | sudo apt-get install -y --no-install-recommends \ 11 | gcc \ 12 | gfortran \ 13 | libfftw3-dev \ 14 | liblapacke-dev \ 15 | libmetis-dev \ 16 | libmumps-seq-dev \ 17 | libopenblas-dev \ 18 | libsuitesparse-dev 19 | - name: Install Go 20 | uses: actions/setup-go@v2 21 | with: 22 | go-version: ^1.20 23 | - name: Build and test all 24 | run: | 25 | bash ./all.bash 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # auto-generated file 2 | xautogencgoflags.go 3 | 4 | # vs code files 5 | .vscode 6 | debug 7 | 8 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 9 | *.o 10 | *.a 11 | *.so 12 | *.pyc 13 | 14 | # Folders 15 | _obj 16 | _test 17 | 18 | # Architecture specific extensions/prefixes 19 | *.[568vq] 20 | [568vq].out 21 | 22 | *.cgo1.go 23 | *.cgo2.c 24 | _cgo_defun.c 25 | _cgo_gotypes.go 26 | _cgo_export.* 27 | 28 | _testmain.go 29 | 30 | *.exe 31 | *.test 32 | *.prof 33 | 34 | # Temporary files 35 | *.goa 36 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:22.04 2 | 3 | # disable tzdata questions 4 | ENV DEBIAN_FRONTEND=noninteractive 5 | 6 | # use bash 7 | SHELL ["/bin/bash", "-c"] 8 | 9 | # install apt-utils 10 | RUN apt-get update -y && \ 11 | apt-get install -y apt-utils 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 ) \ 12 | && apt-get clean && rm -rf /var/lib/apt/lists/* 13 | 14 | # essential tools 15 | RUN apt-get update -y && apt-get install -y --no-install-recommends \ 16 | ca-certificates \ 17 | netbase \ 18 | curl \ 19 | git \ 20 | && apt-get clean && rm -rf /var/lib/apt/lists/* 21 | 22 | # required compilers and libraries for gosl 23 | RUN apt-get update -y && apt-get install -y --no-install-recommends \ 24 | gcc \ 25 | gfortran \ 26 | libfftw3-dev \ 27 | liblapacke-dev \ 28 | libmetis-dev \ 29 | libmumps-seq-dev \ 30 | libopenblas-dev \ 31 | libsuitesparse-dev \ 32 | && apt-get clean && rm -rf /var/lib/apt/lists/* 33 | 34 | # configure basic system 35 | ARG INSTALL_ZSH="true" 36 | ARG USERNAME="vscode" 37 | ARG USER_UID="1000" 38 | ARG USER_GID=$USER_UID 39 | ARG UPGRADE_PACKAGES="true" 40 | COPY zscripts/microsoft/common-debian.sh /tmp/ 41 | RUN bash /tmp/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \ 42 | && apt-get clean -y && rm -rf /var/lib/apt/lists/* && rm /tmp/common-debian.sh 43 | 44 | # install Go tools 45 | ARG GO_VERSION="latest" 46 | ARG GOROOT="/usr/local/go" 47 | ARG GOPATH="/go" 48 | ARG UPDATE_RC="true" 49 | ARG INSTALL_GO_TOOLS="true" 50 | ENV GO111MODULE=auto 51 | COPY zscripts/microsoft/go-debian.sh /tmp/ 52 | RUN bash /tmp/go-debian.sh "${GO_VERSION}" "${GOROOT}" "${GOPATH}" "${USERNAME}" "${UPDATE_RC}" "${INSTALL_GO_TOOLS}" \ 53 | && apt-get clean -y && rm /tmp/go-debian.sh 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Dorival Pedroso. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright notice, this 7 | list of conditions and the following disclaimer. 8 | 9 | * Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | * Neither the name of Gosl nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 21 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | 28 | -------------------------------------------------------------------------------- /all.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | PKGS_ESSENTIAL="chk io utl la/oblas la" 6 | 7 | PKGS_ALL=" \ 8 | fun/fftw fun \ 9 | gm/tri gm/msh gm \ 10 | hb \ 11 | num/qpck num \ 12 | ode \ 13 | opt \ 14 | pde \ 15 | rnd/sfmt rnd/dsfmt rnd \ 16 | " 17 | 18 | install_and_test(){ 19 | HERE=`pwd` 20 | PKG=$1 21 | DOTEST=$2 22 | echo 23 | echo 24 | echo "=== compiling $PKG =============================================================" 25 | cd $PKG 26 | touch *.go 27 | go install 28 | if [ "$DOTEST" -eq 1 ]; then 29 | go test 30 | fi 31 | cd $HERE 32 | } 33 | 34 | for p in $PKGS_ESSENTIAL; do 35 | install_and_test $p 1 36 | done 37 | 38 | for p in $PKGS_ALL; do 39 | install_and_test $p 1 40 | done 41 | 42 | echo 43 | echo "=== SUCCESS! ============================================================" 44 | -------------------------------------------------------------------------------- /chk/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. chk. Check code and unit test tools 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/chk.svg)](https://pkg.go.dev/github.com/cpmech/gosl/chk) 4 | 5 | Package `chk` provides tools to check numerical results and to perform unit tests. 6 | 7 | ## API 8 | 9 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/chk) 10 | -------------------------------------------------------------------------------- /chk/assert.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package chk 6 | 7 | // IntAssert asserts that a is equal to b (ints) 8 | func IntAssert(a, b int) { 9 | if AssertOn { 10 | if a != b { 11 | CallerInfo(3) 12 | CallerInfo(2) 13 | PanicSimple("Assert failed: ints are different: %d != %d", a, b) 14 | } 15 | } 16 | } 17 | 18 | // IntAssertLessThan asserts that a < b (ints) 19 | func IntAssertLessThan(a, b int) { 20 | if AssertOn { 21 | if a < b { 22 | return 23 | } 24 | CallerInfo(3) 25 | CallerInfo(2) 26 | PanicSimple("Assert failed: %d < %d is incorrect", a, b) 27 | } 28 | } 29 | 30 | // IntAssertLessThanOrEqualTo asserts that a ≤ b (ints) 31 | func IntAssertLessThanOrEqualTo(a, b int) { 32 | if AssertOn { 33 | if a <= b { 34 | return 35 | } 36 | CallerInfo(3) 37 | CallerInfo(2) 38 | PanicSimple("Assert failed: %d ≤ %d is incorrect", a, b) 39 | } 40 | } 41 | 42 | // Float64assert asserts that a is equal to b (floats) 43 | func Float64assert(a, b float64) { 44 | if AssertOn { 45 | if a != b { 46 | CallerInfo(3) 47 | CallerInfo(2) 48 | PanicSimple("Assert failed: float64 are different: %g != %g", a, b) 49 | } 50 | } 51 | } 52 | 53 | // StrAssert asserts that a is equal to b (strings) 54 | func StrAssert(a, b string) { 55 | if AssertOn { 56 | if a != b { 57 | CallerInfo(3) 58 | CallerInfo(2) 59 | PanicSimple("Assert failed: strings are different: %s != %s", a, b) 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /chk/auxiliary.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package chk 6 | 7 | // max returns the max between two floats 8 | func max(a, b float64) float64 { 9 | if a > b { 10 | return a 11 | } 12 | return b 13 | } 14 | 15 | // min returns the min between two floats 16 | func min(a, b float64) float64 { 17 | if a < b { 18 | return a 19 | } 20 | return b 21 | } 22 | -------------------------------------------------------------------------------- /chk/chk.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package chk contains functions for checking and testing computations 6 | package chk 7 | 8 | import "fmt" 9 | 10 | var ( 11 | // AssertOn activates or deactivates asserts 12 | AssertOn = true 13 | 14 | // Verbose turn on verbose mode 15 | Verbose = false 16 | 17 | // ColorsOn turn on use of colours on console 18 | ColorsOn = true 19 | ) 20 | 21 | // PanicSimple panics without calling CallerInfo 22 | func PanicSimple(msg string, prm ...interface{}) { 23 | panic(fmt.Sprintf(msg, prm...)) 24 | } 25 | 26 | // Panic calls CallerInfo and panics 27 | func Panic(msg string, prm ...interface{}) { 28 | CallerInfo(4) 29 | CallerInfo(3) 30 | CallerInfo(2) 31 | panic(fmt.Sprintf(msg, prm...)) 32 | } 33 | 34 | // Err returns a new error 35 | func Err(msg string, prm ...interface{}) error { 36 | return fmt.Errorf(msg, prm...) 37 | } 38 | -------------------------------------------------------------------------------- /chk/recover.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package chk 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "testing" 11 | ) 12 | 13 | // Recover catches panics and call os.Exit(1) on 'panic' 14 | func Recover() { 15 | if err := recover(); err != nil { 16 | fmt.Printf("ERROR: %v\n", err) 17 | os.Exit(1) 18 | } 19 | } 20 | 21 | // RecoverTst catches panics in tests. Test will fail on 'panic' 22 | func RecoverTst(tst *testing.T) { 23 | if err := recover(); err != nil { 24 | tst.Errorf("%v\n", err) 25 | tst.FailNow() 26 | } 27 | } 28 | 29 | // RecoverTstPanicIsOK catches panics in tests. Test must 'panic' to be OK 30 | func RecoverTstPanicIsOK(tst *testing.T) { 31 | if err := recover(); err == nil { 32 | tst.Errorf("Test should have panicked\n") 33 | tst.FailNow() 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /chk/t_auxiliary_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package chk 6 | 7 | import "testing" 8 | 9 | func TestMinMax01(tst *testing.T) { 10 | 11 | //Verbose = true 12 | PrintTitle("MinMax01") 13 | 14 | if min(1, 2) != 1 { 15 | tst.Errorf("min() failed\n") 16 | return 17 | } 18 | 19 | if min(2, 1) != 1 { 20 | tst.Errorf("min() failed\n") 21 | return 22 | } 23 | 24 | if max(1, 2) != 2 { 25 | tst.Errorf("max() failed\n") 26 | return 27 | } 28 | 29 | if max(2, 1) != 2 { 30 | tst.Errorf("max() failed\n") 31 | return 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /chk/t_chk_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package chk 6 | 7 | import "testing" 8 | 9 | func TestErr01(tst *testing.T) { 10 | 11 | //Verbose = true 12 | PrintTitle("Err01") 13 | 14 | err := Err("TEST_ERR = %d", 666) 15 | if err.Error() != "TEST_ERR = 666" { 16 | panic("Err failed") 17 | } 18 | 19 | PrintOk("hello from PrintOk => ") 20 | } 21 | -------------------------------------------------------------------------------- /chk/t_recover_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package chk 6 | 7 | import ( 8 | "testing" 9 | ) 10 | 11 | func TestRecover01(tst *testing.T) { 12 | 13 | //Verbose = true 14 | PrintTitle("Recover01. Panic is OK") 15 | 16 | defer RecoverTstPanicIsOK(tst) 17 | panic("panic now") 18 | } 19 | -------------------------------------------------------------------------------- /fun/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. fun. Special functions, DFT, FFT, Bessel, elliptical integrals, orthogonal polynomials, interpolators 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/fun.svg)](https://pkg.go.dev/github.com/cpmech/gosl/fun) 4 | 5 | This package implements _special_ functions such as orthogonal polynomials and elliptical functions 6 | of first, second and third kind. 7 | 8 | Routines to interpolate and/or assist on spectral methods are also available; e.g. FourierInterp, 9 | ChebyInterp. 10 | 11 | ## API 12 | 13 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/fun) 14 | -------------------------------------------------------------------------------- /fun/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. fun. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing. 4 | 5 | The files starting with "as-" correspond to results from (Abramowitz and Stegun, 1972) [1] 6 | 7 | The .txt files are tables for visual inspection of results whereas .cmp files are for automatic 8 | "unit testing". 9 | 10 | The .py files can be used to re-generate all tables (using SciPy). 11 | 12 | ## References 13 | 14 | [1] Abramowitz M, Stegun IA (1972) Handbook of Mathematical Functions with Formulas, Graphs, 15 | and Mathematical Tables. U.S. Department of Commerce, NIST 16 | -------------------------------------------------------------------------------- /fun/data/as-17-elliptic-integrals-table17.5-small.cmp: -------------------------------------------------------------------------------- 1 | phi k F 2 | 0.000000000000000e+00 8.715574274765817e-02 0.000000000000000e+00 3 | 5.235987755982988e-01 8.715574274765817e-02 5.237709509615746e-01 4 | 7.853981633974483e-01 8.715574274765817e-02 7.859411089683984e-01 5 | 1.047197551196598e+00 8.715574274765817e-02 1.048367145544390e+00 6 | 1.570796326794897e+00 8.715574274765817e-02 1.573792130924768e+00 7 | 0.000000000000000e+00 3.420201433256687e-01 0.000000000000000e+00 8 | 5.235987755982988e-01 3.420201433256687e-01 5.262839905622028e-01 9 | 7.853981633974483e-01 3.420201433256687e-01 7.939814299617325e-01 10 | 1.047197551196598e+00 3.420201433256687e-01 1.065968913708522e+00 11 | 1.570796326794897e+00 3.420201433256687e-01 1.620025899124204e+00 12 | 0.000000000000000e+00 7.660444431189780e-01 0.000000000000000e+00 13 | 5.235987755982988e-01 7.660444431189780e-01 5.378676499559212e-01 14 | 7.853981633974483e-01 7.660444431189780e-01 8.343124726282660e-01 15 | 1.047197551196598e+00 7.660444431189780e-01 1.164316364623915e+00 16 | 1.570796326794897e+00 7.660444431189780e-01 1.935581096004722e+00 17 | 0.000000000000000e+00 8.660254037844386e-01 0.000000000000000e+00 18 | 5.235987755982988e-01 8.660254037844386e-01 5.422291098035529e-01 19 | 7.853981633974483e-01 8.660254037844386e-01 8.512237490711855e-01 20 | 1.047197551196598e+00 8.660254037844386e-01 1.212596615254979e+00 21 | 1.570796326794897e+00 8.660254037844386e-01 2.156515647499643e+00 22 | 0.000000000000000e+00 1.000000000000000e+00 0.000000000000000e+00 23 | 5.235987755982988e-01 1.000000000000000e+00 5.493061443340548e-01 24 | 7.853981633974483e-01 1.000000000000000e+00 8.813735870195429e-01 25 | 1.047197551196598e+00 1.000000000000000e+00 1.316957896924816e+00 26 | 1.570796326794897e+00 1.000000000000000e+00 inf 27 | -------------------------------------------------------------------------------- /fun/data/as-17-elliptic-integrals-table17.6-small.cmp: -------------------------------------------------------------------------------- 1 | phi k E 2 | 0.000000000000000e+00 8.715574274765817e-02 0.000000000000000e+00 3 | 5.235987755982988e-01 8.715574274765817e-02 5.234266999995338e-01 4 | 7.853981633974483e-01 8.715574274765817e-02 7.848558618897050e-01 5 | 1.047197551196598e+00 8.715574274765817e-02 1.046030117290430e+00 6 | 1.570796326794897e+00 8.715574274765817e-02 1.567809073977622e+00 7 | 0.000000000000000e+00 3.420201433256687e-01 0.000000000000000e+00 8 | 5.235987755982988e-01 3.420201433256687e-01 5.209376964633315e-01 9 | 7.853981633974483e-01 3.420201433256687e-01 7.769740185127193e-01 10 | 1.047197551196598e+00 3.420201433256687e-01 1.028972213953049e+00 11 | 1.570796326794897e+00 3.420201433256687e-01 1.523799205259774e+00 12 | 0.000000000000000e+00 7.660444431189780e-01 0.000000000000000e+00 13 | 5.235987755982988e-01 7.660444431189780e-01 5.099950056602216e-01 14 | 7.853981633974483e-01 7.660444431189780e-01 7.413704735319425e-01 15 | 1.047197551196598e+00 7.660444431189780e-01 9.492982950786207e-01 16 | 1.570796326794897e+00 7.660444431189780e-01 1.305539094297794e+00 17 | 0.000000000000000e+00 8.660254037844386e-01 0.000000000000000e+00 18 | 5.235987755982988e-01 8.660254037844386e-01 5.060920724657259e-01 19 | 7.853981633974483e-01 8.660254037844386e-01 7.282241554573459e-01 20 | 1.047197551196598e+00 8.660254037844386e-01 9.183932943163252e-01 21 | 1.570796326794897e+00 8.660254037844386e-01 1.211056027568460e+00 22 | 0.000000000000000e+00 1.000000000000000e+00 0.000000000000000e+00 23 | 5.235987755982988e-01 1.000000000000000e+00 4.999999999999999e-01 24 | 7.853981633974483e-01 1.000000000000000e+00 7.071067811865475e-01 25 | 1.047197551196598e+00 1.000000000000000e+00 8.660254037844386e-01 26 | 1.570796326794897e+00 1.000000000000000e+00 1.000000000000000e+00 27 | -------------------------------------------------------------------------------- /fun/data/genBesselInteger.py: -------------------------------------------------------------------------------- 1 | import scipy.special as sp 2 | import numpy as np 3 | 4 | # Reference: 5 | # [1] Abramowitz M, Stegun IA (1972) Handbook of Mathematical Functions with Formulas, Graphs, 6 | # and Mathematical Tables. U.S. Department of Commerce, NIST 7 | 8 | # generate tables 9.* starting at page 390 of [1] 9 | def gentable(X): 10 | 11 | # header 12 | l = '%5s %18s %13s %13s %13s %13s %13s\n' % ('x','J0(x)','J1(x)','J2(x)','Y0(x)','Y1(x)','Y2(x)') 13 | 14 | # table 15 | for i, x in enumerate(X): 16 | l += '%5.2f %18.15f %13.10f %13.10f %13.10f %13.10f %13.10f\n' % (x, sp.j0(x), sp.j1(x), sp.jv(2,x), sp.y0(x), sp.y1(x), sp.yn(2,x)) 17 | if (i+1) % 5 == 0: 18 | l += '\n' 19 | return l 20 | 21 | # generate data for comparison 22 | def gendata(X): 23 | l = '%5s%23s%23s%23s%23s%23s%23s\n' % ('x', 'J0', 'J1', 'J2', 'Y0', 'Y1', 'Y2') 24 | for i, x in enumerate(X): 25 | l += '%5.2f%23.15e%23.15e%23.15e%23.15e%23.15e%23.15e\n' % (x, sp.j0(x), sp.j1(x), sp.jv(2,x), sp.y0(x), sp.y1(x), sp.yn(2,x)) 26 | return l 27 | 28 | # write file 29 | def savefile(l, fn): 30 | f = open(fn,'w') 31 | f.write(l) 32 | f.close() 33 | print 'file <%s> written' % fn 34 | 35 | # generate tables 9.* starting at page 390 of [1] 36 | l = gentable(np.linspace(0,5.0,51)) 37 | savefile(l, '/tmp/as-9-bessel-integer-tables9.txt') 38 | 39 | # data for comparison---small 40 | X = np.linspace(0,15,21) 41 | l = gendata(X) 42 | savefile(l, '/tmp/as-9-bessel-integer-sml.cmp') 43 | 44 | # data for comparison---big 45 | X = np.linspace(0,15,151) 46 | l = gendata(X) 47 | savefile(l, '/tmp/as-9-bessel-integer-big.cmp') 48 | -------------------------------------------------------------------------------- /fun/data/genModBesselInteger.py: -------------------------------------------------------------------------------- 1 | import scipy.special as sp 2 | import numpy as np 3 | 4 | # Reference: 5 | # [1] Abramowitz M, Stegun IA (1972) Handbook of Mathematical Functions with Formulas, Graphs, 6 | # and Mathematical Tables. U.S. Department of Commerce, NIST 7 | 8 | # generate tables 9.* starting at page 416 of [1] 9 | def gentable(X): 10 | 11 | # header 12 | l = '%5s %13s %13s %13s %13s %13s %13s\n' % ('x','exp(-x)*I0(x)','exp(-x)*I1(x)','x^{-2}*I2(x)','exp(x)*K0(x)','exp(x)*K1(x)','x^2*K2(x)') 13 | 14 | # table 15 | for i, x in enumerate(X): 16 | a = np.exp(-x) 17 | A = 1.0/a 18 | B = x*x 19 | b = 1.0/B 20 | l += '%5.2f %13.10f %13.10f %13.10f %13.10f %13.10f %13.10f\n' % (x, a*sp.i0(x), a*sp.i1(x), b*sp.iv(2,x), A*sp.k0(x), A*sp.k1(x), B*sp.kn(2,x)) 21 | if (i+1) % 5 == 0: 22 | l += '\n' 23 | return l 24 | 25 | # generate data for comparison 26 | def gendata(X): 27 | l = '%5s%23s%23s%23s%23s%23s%23s%23s%23s\n' % ('x', 'I0', 'I1', 'I2', 'I3', 'K0', 'K1', 'K2', 'K3') 28 | for i, x in enumerate(X): 29 | l += '%5.2f%23.15e%23.15e%23.15e%23.15e%23.15e%23.15e%23.15e%23.15e\n' % (x, sp.i0(x), sp.i1(x), sp.iv(2,x), sp.iv(3,x), sp.k0(x), sp.k1(x), sp.kn(2,x), sp.kn(3,x)) 30 | return l 31 | 32 | # generate data for comparison 33 | def gendataNeg(X): 34 | l = '%6s%23s%23s%23s%23s\n' % ('x', 'I0', 'I1', 'I2', 'I3') 35 | for i, x in enumerate(X): 36 | l += '%6.2f%23.15e%23.15e%23.15e%23.15e\n' % (x, sp.i0(x), sp.i1(x), sp.iv(2,x), sp.iv(3,x)) 37 | return l 38 | 39 | # write file 40 | def savefile(l, fn): 41 | f = open(fn,'w') 42 | f.write(l) 43 | f.close() 44 | print 'file <%s> written' % fn 45 | 46 | # generate tables 9.* starting at page 416 of [1] 47 | l = gentable(np.linspace(0,5.0,51)) 48 | savefile(l, '/tmp/as-9-modbessel-integer-tables9.txt') 49 | 50 | # data for comparison---small 51 | X = np.linspace(0,20,41) 52 | l = gendata(X) 53 | savefile(l, '/tmp/as-9-modbessel-integer-sml.cmp') 54 | 55 | # data for comparison---big 56 | X = np.linspace(0,20,201) 57 | l = gendata(X) 58 | savefile(l, '/tmp/as-9-modbessel-integer-big.cmp') 59 | 60 | # data for comparison---negative 61 | X = np.linspace(-10,0,41) 62 | l = gendataNeg(X) 63 | savefile(l, '/tmp/as-9-modbessel-integer-neg.cmp') 64 | -------------------------------------------------------------------------------- /fun/dft.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fun 6 | 7 | import ( 8 | "math" 9 | 10 | "github.com/cpmech/gosl/fun/fftw" 11 | ) 12 | 13 | // Dft1d computes the discrete Fourier transform (DFT) in 1D. 14 | // It replaces data by its discrete Fourier transform, if inverse==false 15 | // or replaces data by its inverse discrete Fourier transform, if inverse==true 16 | // 17 | // Computes: 18 | // N-1 -i 2 π j k / N __ 19 | // forward: X[k] = Σ x[j] ⋅ e with i = √-1 20 | // j=0 21 | // 22 | // N-1 +i 2 π j k / N 23 | // inverse: Y[k] = Σ y[j] ⋅ e thus x[k] = Y[k] / N 24 | // j=0 25 | // 26 | // NOTE: (1) the inverse operation does not divide by N 27 | // (2) ideally, N=len(data) is an integer power of 2. 28 | // (3) using FFTW: http://fftw.org/fftw3_doc/What-FFTW-Really-Computes.html 29 | // 30 | func Dft1d(data []complex128, inverse bool) { 31 | plan := fftw.NewPlan1d(data, inverse, false) 32 | defer plan.Free() 33 | plan.Execute() 34 | } 35 | 36 | // dft1dslow computes the discrete Fourier transform of x (complex) by using the "slow" method; i.e. 37 | // by directly computing the DFT summation using N² operations. 38 | // NOTE: This function is useful for verifications (testing) only. 39 | func dft1dslow(x []complex128) (X []complex128) { 40 | N := len(x) 41 | X = make([]complex128, N) 42 | for n := 0; n < N; n++ { 43 | for k := 0; k < N; k++ { 44 | a := 2.0 * math.Pi * float64(k*n) / float64(N) 45 | X[n] += x[k] * ExpMix(a) // x[k]⋅exp(-a) 46 | } 47 | } 48 | return 49 | } 50 | -------------------------------------------------------------------------------- /fun/fftw/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. fun/fftw. Wrapper to FFTW3 2 | 3 | [![PkgGoDev](https://pkg.go.dev/badge/github.com/cpmech/gosl/fun/fftw)](https://pkg.go.dev/github.com/cpmech/gosl/fun/fftw) 4 | 5 | This package wraps the [Fast Fourier Transform library (FFTW)](http://www.fftw.org) 6 | 7 | ## API 8 | 9 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/fun/fftw) 10 | -------------------------------------------------------------------------------- /fun/fftw/flags.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fftw 6 | 7 | /* 8 | #cgo LDFLAGS: -lfftw3 -lm 9 | */ 10 | import "C" 11 | -------------------------------------------------------------------------------- /fun/fftw/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fftw 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /fun/t_b_imagpown_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fun 6 | 7 | import ( 8 | "math/cmplx" 9 | "testing" 10 | ) 11 | 12 | var ( 13 | benchmarkingResC complex128 14 | ) 15 | 16 | func BenchmarkImagPowN(b *testing.B) { 17 | var res complex128 18 | for i := 0; i < b.N; i++ { 19 | for n := 0; n < 200; n++ { 20 | res = ImagPowN(n) 21 | } 22 | } 23 | benchmarkingResC = res 24 | } 25 | 26 | func BenchmarkImagPowNcmplx(b *testing.B) { 27 | var res complex128 28 | for i := 0; i < b.N; i++ { 29 | for n := 0; n < 200; n++ { 30 | res = cmplx.Pow(1i, complex(float64(n), 0)) 31 | } 32 | } 33 | benchmarkingResC = res 34 | } 35 | 36 | func BenchmarkImagXpowN(b *testing.B) { 37 | var res complex128 38 | x := 2.5 39 | for i := 0; i < b.N; i++ { 40 | for n := 0; n < 200; n++ { 41 | res = ImagXpowN(x, n) 42 | } 43 | } 44 | benchmarkingResC = res 45 | } 46 | 47 | func BenchmarkImagXpowNcmplx(b *testing.B) { 48 | var res complex128 49 | x := 2.5 50 | for i := 0; i < b.N; i++ { 51 | for n := 0; n < 200; n++ { 52 | res = cmplx.Pow(complex(0, x), complex(float64(n), 0)) 53 | } 54 | } 55 | benchmarkingResC = res 56 | } 57 | -------------------------------------------------------------------------------- /fun/t_b_interp_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fun 6 | 7 | import ( 8 | "math/rand" 9 | "testing" 10 | ) 11 | 12 | var ( 13 | benchmarkingX []float64 14 | benchmarkingY []float64 15 | benchmarkingRes float64 16 | ) 17 | 18 | func init() { 19 | N := 1 << 10 20 | //N := 1 << 20 // 2²⁰ = 1,048,576 21 | benchmarkingX = make([]float64, N) 22 | benchmarkingY = make([]float64, N) 23 | for i := 0; i < N; i++ { 24 | benchmarkingX[i] = 100 * float64(i) / float64(N) 25 | benchmarkingY[i] = -5.0 + 10.0*rand.Float64() 26 | } 27 | } 28 | 29 | func BenchmarkInterp(b *testing.B) { 30 | o := NewDataInterp("lin", 1, benchmarkingX, benchmarkingY) 31 | var res float64 32 | for i := 0; i < b.N; i++ { 33 | res = interpoRunSearch(o) 34 | } 35 | benchmarkingRes = res 36 | } 37 | 38 | func BenchmarkInterpNoHunt(b *testing.B) { 39 | o := NewDataInterp("lin", 1, benchmarkingX, benchmarkingY) 40 | o.DisableHunt = true 41 | var res float64 42 | for i := 0; i < b.N; i++ { 43 | res = interpoRunSearch(o) 44 | } 45 | benchmarkingRes = res 46 | } 47 | 48 | func interpoRunSearch(o *DataInterp) (res float64) { 49 | Mseq := 1000 50 | Mrnd := 100 51 | for j := 0; j < Mseq; j++ { 52 | x := 100 * float64(j) / float64(Mseq) 53 | res = o.P(x) 54 | } 55 | for j := 0; j < Mrnd; j++ { 56 | x := 100 * rand.Float64() 57 | res = o.P(x) 58 | } 59 | return 60 | } 61 | -------------------------------------------------------------------------------- /fun/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fun 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /fun/t_interp_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fun 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func TestInterp01(tst *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("Interp01. linear interpolation") 17 | 18 | xx := []float64{0, 1, 2, 3, 4, 5} 19 | yy := []float64{0.50, 0.20, 0.20, 0.05, 0.01, 0.00} 20 | 21 | o := NewDataInterp("lin", 1, xx, yy) 22 | 23 | for i, x := range xx { 24 | chk.Float64(tst, "P(xi)", 1e-17, o.P(x), yy[i]) 25 | } 26 | 27 | xref := []float64{1.0 / 3.0, 2.5, 2.0 / 3.0, 1.1, 1.5, 3.5, 4.5} 28 | yref := []float64{0.4, 0.125, 0.3, 0.2, 0.2, 0.03, 0.005} 29 | for i, x := range xref { 30 | chk.Float64(tst, "P(xref)", 1e-16, o.P(x), yref[i]) 31 | } 32 | } 33 | 34 | func TestInterp02(tst *testing.T) { 35 | 36 | //verbose() 37 | chk.PrintTitle("Interp02. polynomial interpolation") 38 | 39 | xx := []float64{0, 1, 2, 3, 4, 5} 40 | yy := []float64{0.50, 0.20, 0.20, 0.05, 0.01, 0.00} 41 | 42 | for _, p := range []int{1, 2, 3} { 43 | 44 | o := NewDataInterp("poly", p, xx, yy) 45 | 46 | for i, x := range xx { 47 | chk.Float64(tst, "P(xi)", 1e-17, o.P(x), yy[i]) 48 | } 49 | 50 | if o.m == 2 { 51 | xref := []float64{1.0 / 3.0, 2.5, 2.0 / 3.0, 1.1, 1.5, 3.5, 4.5} 52 | yref := []float64{0.4, 0.125, 0.3, 0.2, 0.2, 0.03, 0.005} 53 | for i, x := range xref { 54 | chk.Float64(tst, "P(xref)", 1e-16, o.P(x), yref[i]) 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /fun/t_multiinterp_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fun 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func TestMultiInterp01(t *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("MultiInterp01. bilinear interpolation") 17 | 18 | // checking f(x,y) = x^2 + 2y^2 19 | f := []float64{ 20 | 0.00, 0.25, 1.00, 4.00, 21 | 2.00, 2.25, 3.00, 6.00, 22 | 8.00, 8.25, 9.00, 12.00, 23 | } 24 | 25 | xx := []float64{0.0, 0.5, 1.0, 2.0} 26 | yy := []float64{0.0, 1.0, 2.0} 27 | 28 | o := NewBiLinear(f, xx, yy) 29 | 30 | for i, x := range xx { 31 | for j, y := range yy { 32 | chk.Float64(t, "P(x,y)", 1e-17, o.P(x, y), f[i+j*len(xx)]) 33 | } 34 | } 35 | 36 | fref := []float64{1.125, 1.625, 3.2, 8.4} 37 | xref := []float64{0.25, 0.75, 1.2, 1.2} 38 | yref := []float64{0.5, 0.5, 0.8, 1.8} 39 | 40 | for i := 0; i < len(fref); i++ { 41 | chk.Float64(t, "P(xref,yref)", 1e-17, o.P(xref[i], yref[i]), fref[i]) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /fun/t_sinusoid_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package fun 6 | 7 | import ( 8 | "math" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/chk" 12 | "github.com/cpmech/gosl/utl" 13 | ) 14 | 15 | func TestSinusoid01(tst *testing.T) { 16 | 17 | //verbose() 18 | chk.PrintTitle("Sinusoid01. sinusoid equation 01") 19 | 20 | // data 21 | π := math.Pi // 3.14159265359... 22 | T := 1.5 // period [s] 23 | A0 := 1.7 // mean value 24 | C1 := 1.0 // amplitude 25 | θ := π / 3.0 // phase shift [rad] 26 | sa := NewSinusoidEssential(T, A0, C1, θ) 27 | sb := NewSinusoidBasis(T, A0, sa.A[1], sa.B[1]) 28 | 29 | // check setup data 30 | chk.Float64(tst, "Period", 1e-15, sa.Period, sb.Period) 31 | chk.Float64(tst, "Frequency", 1e-15, sa.Frequency, sb.Frequency) 32 | chk.Float64(tst, "PhaseShift", 1e-15, sa.PhaseShift, sb.PhaseShift) 33 | chk.Float64(tst, "MeanValue", 1e-15, sa.MeanValue, sb.MeanValue) 34 | chk.Float64(tst, "Amplitude", 1e-15, sa.Amplitude, sb.Amplitude) 35 | chk.Float64(tst, "AngularFreq", 1e-15, sa.AngularFreq, sb.AngularFreq) 36 | chk.Float64(tst, "TimeShift", 1e-15, sa.TimeShift, sb.TimeShift) 37 | chk.Array(tst, "A", 1e-15, sa.A, sb.A) 38 | chk.Array(tst, "B", 1e-15, sa.B, sb.B) 39 | 40 | // check essen vs basis 41 | tt := utl.LinSpace(-sa.TimeShift, 2.5, 7) 42 | y1 := make([]float64, len(tt)) 43 | y2 := make([]float64, len(tt)) 44 | for i := 0; i < len(tt); i++ { 45 | y1[i] = sa.Yessen(tt[i]) 46 | y2[i] = sb.Ybasis(tt[i]) 47 | } 48 | chk.Array(tst, "essen vs basis", 1e-15, y1, y2) 49 | 50 | // check periodicity 51 | if !sa.TestPeriodicity(0, 4*π, 10) { 52 | chk.Panic("failed\n") 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /gm/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. gm. Geometry algorithms and structures 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/gm.svg)](https://pkg.go.dev/github.com/cpmech/gosl/gm) 4 | 5 | This package provides some functions to help with the solution of geometry problems. It also 6 | includes some routines loosely related with geometry. 7 | 8 | ## API 9 | 10 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/gm) 11 | -------------------------------------------------------------------------------- /gm/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. gm. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /gm/data/m_nurbs08a.msh: -------------------------------------------------------------------------------- 1 | { 2 | "verts" : [ 3 | { "id": 0, "tag": -2, "c":[-1.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 4 | { "id": 1, "tag": 0, "c":[-1.00000000000000000e+00, 4.14213562373095090e-01, 0.00000000000000000e+00, 8.53553390593273731e-01] }, 5 | { "id": 2, "tag": 0, "c":[-4.14213562373095090e-01, 1.00000000000000000e+00, 0.00000000000000000e+00, 8.53553390593273731e-01] }, 6 | { "id": 3, "tag": -1, "c":[ 0.00000000000000000e+00, 1.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 7 | { "id": 4, "tag": -2, "c":[-2.50000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 8 | { "id": 5, "tag": 0, "c":[-2.50000000000000000e+00, 7.50000000000000000e-01, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 9 | { "id": 6, "tag": 0, "c":[-7.50000000000000000e-01, 2.50000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 10 | { "id": 7, "tag": -1, "c":[ 0.00000000000000000e+00, 2.50000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 11 | { "id": 8, "tag": -4, "c":[-4.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 12 | { "id": 9, "tag": -3, "c":[-4.00000000000000000e+00, 4.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] }, 13 | { "id": 10, "tag": -1, "c":[ 0.00000000000000000e+00, 4.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00] } 14 | ], 15 | "nurbss" : [ 16 | { "id":0, "gnd":2, "ords":[2,2,0], 17 | "knots":[ 18 | [ 0.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00, 5.00000000000000000e-01, 1.00000000000000000e+00, 1.00000000000000000e+00, 1.00000000000000000e+00], 19 | [ 0.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00, 1.00000000000000000e+00, 1.00000000000000000e+00] 20 | ], 21 | "ctrls":[0,1,2,3,4,5,6,7,8,9,9,10] 22 | } 23 | ], 24 | "cells" : [ 25 | { "id": 0, "tag":-1, "nrb":0, "part":0, "type":"nurbs", "span":[2,3,2,3], "verts":[0,1,2,4,5,6,8,9,9] }, 26 | { "id": 1, "tag":-2, "nrb":0, "part":0, "type":"nurbs", "span":[3,4,2,3], "verts":[1,2,3,5,6,7,9,9,10] } 27 | ] 28 | } -------------------------------------------------------------------------------- /gm/msh/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. gm/msh. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /gm/msh/data/cubeandtet.msh: -------------------------------------------------------------------------------- 1 | { 2 | "verts":[ 3 | {"i":0, "t": 0, "x":[0.0,0.0,0.0]}, 4 | {"i":1, "t": 0, "x":[1.0,0.0,0.0]}, 5 | {"i":2, "t":12, "x":[1.0,1.0,0.0]}, 6 | {"i":3, "t": 0, "x":[0.0,1.0,0.0]}, 7 | {"i":4, "t":14, "x":[0.0,0.0,1.0]}, 8 | {"i":5, "t": 0, "x":[1.0,0.0,1.0]}, 9 | {"i":6, "t": 0, "x":[1.0,1.0,1.0]}, 10 | {"i":7, "t": 0, "x":[0.0,1.0,1.0]}, 11 | {"i":8, "t":18, "x":[0.0,2.0,0.0]} 12 | ], 13 | "cells":[ 14 | {"i":0, "t":1, "p":0, "y":"hex8", "v":[0,1,2,3,4,5,6,7], "et":[10,11,12,13, 0,0,0,0, 0,0,15,0], "ft":[100,101,200,0,300,0]}, 15 | {"i":1, "t":1, "p":0, "y":"tet4", "v":[3,2,8,7], "et":[12,12,12,0,66,0], "ft":[100,0,300,400]} 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /gm/msh/data/mesh01.msh: -------------------------------------------------------------------------------- 1 | { 2 | "verts":[ 3 | {"i": 0, "t":1, "x":[0.0, 0.0]}, 4 | {"i": 1, "t":1, "x":[0.5, 0.0]}, 5 | {"i": 2, "t":1, "x":[1.0, 0.0]}, 6 | {"i": 3, "t":2, "x":[1.4, 0.25]}, 7 | {"i": 4, "t":3, "x":[0.0, 0.5]}, 8 | {"i": 5, "t":3, "x":[0.5, 0.5]}, 9 | {"i": 6, "t":3, "x":[1.0, 0.5]}, 10 | {"i": 7, "t":4, "x":[1.4, 0.75]}, 11 | {"i": 8, "t":5, "x":[0.0, 1.0]}, 12 | {"i": 9, "t":5, "x":[0.5, 1.0]}, 13 | {"i":10, "t":5, "x":[1.0, 1.0]} 14 | ], 15 | "cells":[ 16 | {"i":0, "t":1, "p":0, "y":"qua4", "v":[0, 1, 5, 4], "et":[10, 0, 0, 13] }, 17 | {"i":1, "t":1, "p":1, "y":"qua4", "v":[1, 2, 6, 5], "et":[10, 0, 0, 0] }, 18 | {"i":2, "t":1, "p":2, "y":"tri3", "v":[2, 3, 6], "et":[11, 11, 0] }, 19 | {"i":3, "t":2, "p":0, "y":"qua4", "v":[4, 5, 9, 8], "et":[ 0, 0, 12, 13] }, 20 | {"i":4, "t":2, "p":1, "y":"qua4", "v":[5, 6,10, 9], "et":[ 0, 0, 12, 0] }, 21 | {"i":5, "t":2, "p":2, "y":"tri3", "v":[6, 7,10], "et":[11, 11, 0] } 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /gm/msh/data/meshelems1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/gm/msh/data/meshelems1.png -------------------------------------------------------------------------------- /gm/msh/data/singleq4square1x1.msh: -------------------------------------------------------------------------------- 1 | { 2 | "verts" : [ 3 | { "i": 0, "t":-1, "x":[0.0, 0.0] }, 4 | { "i": 1, "t": 0, "x":[1.0, 0.0] }, 5 | { "i": 2, "t":-1, "x":[1.0, 1.0] }, 6 | { "i": 3, "t": 0, "x":[0.0, 1.0] } 7 | ], 8 | "cells" : [ 9 | { "i":0, "t":-1, "p":0, "y":"qua4", "v":[0,1,2,3], "et":[-10, -11, -12, -13] } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /gm/msh/output.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package msh 6 | 7 | import "github.com/cpmech/gosl/io" 8 | 9 | // String returns a JSON representation of *Vert 10 | func (o *Vertex) String() string { 11 | l := io.Sf("{\"i\":%d, \"t\":%d, \"x\":[", o.ID, o.Tag) 12 | for i, x := range o.X { 13 | if i > 0 { 14 | l += ", " 15 | } 16 | l += io.Sf("%g", x) 17 | } 18 | l += "] }" 19 | return l 20 | } 21 | 22 | // String returns a JSON representation of *Cell 23 | func (o *Cell) String() string { 24 | l := io.Sf("{\"i\":%d, \"t\":%d, \"p\":%d, \"y\":%q, \"v\":[", o.ID, o.Tag, o.Part, o.TypeKey) 25 | for i, x := range o.V { 26 | if i > 0 { 27 | l += ", " 28 | } 29 | l += io.Sf("%d", x) 30 | } 31 | if len(o.EdgeTags) > 0 { 32 | l += "], \"et\":[" 33 | for i, x := range o.EdgeTags { 34 | if i > 0 { 35 | l += ", " 36 | } 37 | l += io.Sf("%d", x) 38 | } 39 | } 40 | if len(o.FaceTags) > 0 { 41 | l += "], \"ft\":[" 42 | for i, x := range o.FaceTags { 43 | if i > 0 { 44 | l += ", " 45 | } 46 | l += io.Sf("%d", x) 47 | } 48 | } 49 | l += "] }" 50 | return l 51 | } 52 | -------------------------------------------------------------------------------- /gm/msh/t_b_integ_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package msh 6 | 7 | import ( 8 | "math" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/la" 12 | ) 13 | 14 | var ( 15 | benchmarkMesh *Mesh 16 | benchmarkMint *MeshIntegrator 17 | benchmarkIx float64 18 | ) 19 | 20 | func init() { 21 | r, R := 1.0, 3.0 22 | nr, na := 10, 35 23 | benchmarkMesh = GenRing2d(TypeQua17, nr, na, r, R, 2.0*math.Pi) 24 | benchmarkMint = NewMeshIntegrator(benchmarkMesh, 1) 25 | } 26 | 27 | func BenchmarkInteg(b *testing.B) { 28 | fcnIx := func(x la.Vector) (f float64) { 29 | f = x[1] * x[1] 30 | return 31 | } 32 | var Ix float64 33 | for i := 0; i < b.N; i++ { 34 | Ix = benchmarkMint.IntegrateSv(0, fcnIx) 35 | } 36 | benchmarkIx = Ix 37 | } 38 | -------------------------------------------------------------------------------- /gm/msh/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package msh 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /gm/t_bezier_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package gm 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/utl" 12 | ) 13 | 14 | func Test_bezier01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("bezier01. quadratic Bezier.") 18 | 19 | bez := BezierQuad{ 20 | Q: [][]float64{ 21 | {-1, 1}, 22 | {0.5, -2}, 23 | {2, 4}, 24 | }, 25 | } 26 | 27 | np := 21 28 | T := utl.LinSpace(0, 1, np) 29 | X := make([]float64, np) 30 | Y := make([]float64, np) 31 | X2 := utl.LinSpace(-1.0, 2.0, np) 32 | Y2 := make([]float64, np) 33 | C := make([]float64, 2) 34 | for i, t := range T { 35 | bez.Point(C, t) 36 | X[i] = C[0] 37 | Y[i] = C[1] 38 | Y2[i] = X2[i] * X2[i] 39 | chk.Float64(tst, "y=y", 1e-15, Y[i], X[i]*X[i]) 40 | } 41 | 42 | XX, YY, _ := bez.GetPoints(T) 43 | chk.Array(tst, "X", 1e-15, X, XX) 44 | chk.Array(tst, "Y", 1e-15, Y, YY) 45 | 46 | Xq, Yq, _ := bez.GetControlCoords() 47 | chk.Array(tst, "Xq", 1e-15, Xq, []float64{-1, 0.5, 2}) 48 | chk.Array(tst, "Yq", 1e-15, Yq, []float64{1, -2, 4}) 49 | } 50 | -------------------------------------------------------------------------------- /gm/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package gm 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /gm/t_nurbs_methods_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package gm 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func TestNurbsMethods01(tst *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("NurbsMethods01") 17 | 18 | nrb := FactoryNurbs.Surf2dExample1() 19 | chk.Int(tst, "Gnd()", nrb.Gnd(), 2) 20 | chk.Int(tst, "Ord(0)", nrb.Ord(0), 2) 21 | chk.Int(tst, "Ord(1)", nrb.Ord(1), 1) 22 | chk.Float64(tst, "U(0,0)", 1e-15, nrb.U(0, 0), 0) 23 | chk.Float64(tst, "Udelta(0)", 1e-15, nrb.Udelta(0), 3) 24 | chk.Float64(tst, "Udelta(1)", 1e-15, nrb.Udelta(1), 1) 25 | chk.Float64(tst, "UfromR(0,0.0)", 1e-15, nrb.UfromR(0, 0.0), 1.5) 26 | chk.Float64(tst, "UfromR(1,0.0)", 1e-15, nrb.UfromR(1, 0.0), 0.5) 27 | chk.Int(tst, "NumBasis(0)", nrb.NumBasis(0), 5) 28 | chk.Int(tst, "NumBasis(1)", nrb.NumBasis(1), 2) 29 | chk.IntDeep2(tst, "NonZeroSpans(0)", nrb.NonZeroSpans(0), [][]int{{2, 3}, {3, 4}, {4, 5}}) 30 | chk.IntDeep2(tst, "NonZeroSpans(1)", nrb.NonZeroSpans(1), [][]int{{1, 2}}) 31 | } 32 | -------------------------------------------------------------------------------- /gm/tri/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. gm/tri. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /gm/tri/data/tri_delaunay01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/gm/tri/data/tri_delaunay01.png -------------------------------------------------------------------------------- /gm/tri/data/tri_draw01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/gm/tri/data/tri_draw01.png -------------------------------------------------------------------------------- /gm/tri/delaunay.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package tri wraps Triangle to perform mesh generation a Delaunay triangulation 6 | package tri 7 | 8 | /* 9 | #cgo CFLAGS: -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast 10 | #cgo LDFLAGS: -lm 11 | #include "triangle.h" 12 | */ 13 | import "C" 14 | 15 | import ( 16 | "unsafe" 17 | 18 | "github.com/cpmech/gosl/chk" 19 | "github.com/cpmech/gosl/utl" 20 | ) 21 | 22 | // Delaunay computes 2D Delaunay triangulation using Triangle 23 | // Input: 24 | // X = { x0, x1, x2, ... Npoints } 25 | // Y = { y0, y1, y2, ... Npoints } 26 | // Ouptut: 27 | // Verts = { { x0, y0 }, { x1, y1 }, { x2, y2 } ... Nvertices } 28 | // Cells = { { id0, id1, id2 }, { id0, id1, id2 } ... Ncellls } 29 | func Delaunay(X, Y []float64, verbose bool) (Verts [][]float64, Cells [][]int) { 30 | 31 | // input 32 | chk.IntAssert(len(X), len(Y)) 33 | n := len(X) 34 | verb := 0 35 | if verbose { 36 | verb = 1 37 | } 38 | 39 | // perform triangulation 40 | var T C.struct_triangulateio 41 | defer func() { C.tiofree(&T) }() 42 | res := C.delaunay2d( 43 | &T, 44 | C.int(n), 45 | (*C.double)(unsafe.Pointer(&X[0])), 46 | (*C.double)(unsafe.Pointer(&Y[0])), 47 | C.int(verb), 48 | ) 49 | if res != 0 { 50 | chk.Panic("Delaunay2d failed: Triangle returned %d code\n", res) 51 | } 52 | 53 | // output 54 | nverts := int(T.numberofpoints) 55 | ncells := int(T.numberoftriangles) 56 | Verts = utl.Alloc(nverts, 2) 57 | Cells = utl.IntAlloc(ncells, 3) 58 | for i := 0; i < nverts; i++ { 59 | Verts[i][0] = float64(C.getpoint(C.int(i), 0, &T)) 60 | Verts[i][1] = float64(C.getpoint(C.int(i), 1, &T)) 61 | } 62 | for i := 0; i < ncells; i++ { 63 | Cells[i][0] = int(C.getcorner(C.int(i), 0, &T)) 64 | Cells[i][1] = int(C.getcorner(C.int(i), 1, &T)) 65 | Cells[i][2] = int(C.getcorner(C.int(i), 2, &T)) 66 | } 67 | return 68 | } 69 | -------------------------------------------------------------------------------- /gm/tri/mesh.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package tri 6 | 7 | // Definitions 8 | // 9 | // 2D: 10 | // Nodes Edges 11 | // 12 | // y 2 13 | // | @ @ 14 | // +--x / \ / \ 15 | // 5 / \ 4 / \ 16 | // @ @ 2 / \ 1 17 | // / \ / \ 18 | // / \ / \ 19 | // @-----@-----@ @-----------@ 20 | // 0 3 1 0 21 | // 22 | 23 | // Vertex holds vertex data 24 | type Vertex struct { 25 | ID int // identifier 26 | Tag int // tag 27 | X []float64 // coordinates [2] 28 | } 29 | 30 | // Cell holds cell data 31 | type Cell struct { 32 | ID int // identifier 33 | Tag int // tag 34 | V []int // vertices 35 | EdgeTags []int // edge tags (2D or 3D) 36 | } 37 | 38 | // Mesh defines mesh data 39 | type Mesh struct { 40 | Verts []*Vertex // vertices 41 | Cells []*Cell // cells 42 | } 43 | -------------------------------------------------------------------------------- /gm/tri/t_delaunay_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package tri 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func Test_delaunay01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("delaunay01") 18 | 19 | // points 20 | X := []float64{0, 1, 1, 0, 0.5} 21 | Y := []float64{0, 0, 1, 1, 0.5} 22 | 23 | // generate 24 | V, C := Delaunay(X, Y, chk.Verbose) 25 | 26 | // check 27 | xout := make([]float64, len(V)) 28 | yout := make([]float64, len(V)) 29 | for i, v := range V { 30 | io.Pforan("vert %2d : coords = %v\n", i, v) 31 | xout[i] = v[0] 32 | yout[i] = v[1] 33 | } 34 | chk.Array(tst, "X", 1e-15, xout, X) 35 | chk.Array(tst, "Y", 1e-15, yout, Y) 36 | for i, c := range C { 37 | io.Pforan("cell %2d : verts = %v\n", i, c) 38 | } 39 | chk.Ints(tst, "verts of cell 0", C[0], []int{3, 0, 4}) 40 | chk.Ints(tst, "verts of cell 1", C[1], []int{4, 1, 2}) 41 | chk.Ints(tst, "verts of cell 2", C[2], []int{1, 4, 0}) 42 | chk.Ints(tst, "verts of cell 3", C[3], []int{4, 2, 3}) 43 | } 44 | -------------------------------------------------------------------------------- /gm/tri/t_generate_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package tri 6 | 7 | import ( 8 | "math" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/chk" 12 | "github.com/cpmech/gosl/io" 13 | ) 14 | 15 | func TestGenerate01(tst *testing.T) { 16 | 17 | //verbose() 18 | chk.PrintTitle("Generate01") 19 | 20 | l := 1.0 21 | h := math.Sqrt(3.0) * l / 2.0 22 | A := l * h / 2.0 23 | 24 | p := &Input{ 25 | []*Point{ 26 | {11, 0.0, 0.0}, 27 | {22, l, 0.0}, 28 | {33, l / 2.0, h}, 29 | }, 30 | []*Segment{ 31 | {100, 0, 1}, 32 | {200, 1, 2}, 33 | {300, 2, 0}, 34 | }, 35 | []*Region{ 36 | {10, 1.0, l / 2.0, h / 2.0}, 37 | }, 38 | []*Hole{}, 39 | } 40 | 41 | globalMaxArea := A / 4.0 42 | globalMinAngle := 45.0 43 | o2 := true 44 | m := p.Generate(globalMaxArea, globalMinAngle, o2, chk.Verbose, "") 45 | 46 | io.Pf("\nVertices:\n") 47 | for _, v := range m.Verts { 48 | io.Pf("%+v\n", v) 49 | } 50 | 51 | io.Pf("\nCells:\n") 52 | for _, c := range m.Cells { 53 | io.Pf("%+v\n", c) 54 | } 55 | 56 | chk.Int(tst, "vert0: tag", m.Verts[0].Tag, 11) 57 | chk.Int(tst, "vert1: tag", m.Verts[1].Tag, 22) 58 | chk.Int(tst, "vert2: tag", m.Verts[2].Tag, 33) 59 | chk.Ints(tst, "cell0: verts", m.Cells[0].V, []int{4, 2, 5, 6, 7, 8}) 60 | chk.Ints(tst, "cell0: etags", m.Cells[0].EdgeTags, []int{200, 300, 0}) 61 | chk.Ints(tst, "cell1: verts", m.Cells[1].V, []int{4, 5, 3, 8, 9, 10}) 62 | chk.Ints(tst, "cell1: etags", m.Cells[1].EdgeTags, []int{0, 0, 0}) 63 | chk.Ints(tst, "cell2: verts", m.Cells[2].V, []int{5, 0, 3, 11, 12, 9}) 64 | chk.Ints(tst, "cell2: etags", m.Cells[2].EdgeTags, []int{300, 100, 0}) 65 | chk.Ints(tst, "cell3: verts", m.Cells[3].V, []int{3, 1, 4, 13, 14, 10}) 66 | chk.Ints(tst, "cell3: etags", m.Cells[3].EdgeTags, []int{100, 200, 0}) 67 | } 68 | -------------------------------------------------------------------------------- /gm/tri/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package tri 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/cpmech/gosl 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /hb/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. hb. Read/Write hb (go-binary gob) files 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/hb.svg)](https://pkg.go.dev/github.com/cpmech/gosl/hb) 4 | 5 | This package is inspired by the Hierarchical Data File (HDF) format but uses go-binary (gob) _stream of gobs_ instead. 6 | 7 | Data is stored in binary streams and then to files by providing a **path** and the data. Here, the order of commands is important. 8 | 9 | Example of writing data: 10 | 11 | ```go 12 | f := Create("/tmp/gosl/hb", "basic01") 13 | defer f.Close() 14 | f.PutArray("/u", uSource) 15 | f.PutArray("/displacements/u", []float64{4, 5, 6}) 16 | f.PutArray("/displacements/v", []float64{40, 50, 60}) 17 | f.PutArray("/time0/ip0/a0/u", []float64{7, 8, 9}) 18 | f.PutArray("/time1/ip0/b0/u", []float64{70, 80, 90}) 19 | f.PutInts("/someints", []int{100, 200, 300, 400}) 20 | f.PutInt("/data/oneint", 123) 21 | f.PutFloat64("/data/onef64", 123.456) 22 | ``` 23 | 24 | Example of reading data (must be in the same order used during writing): 25 | 26 | ```go 27 | g := Open("/tmp/gosl/hb", "basic01") 28 | defer g.Close() 29 | u := g.GetArray("/u") 30 | du := g.GetArray("/displacements/u") 31 | dv := g.GetArray("/displacements/v") 32 | t0i0a0u := g.GetArray("/time0/ip0/a0/u") 33 | t1i0b0u := g.GetArray("/time1/ip0/b0/u") 34 | someints := g.GetInts("/someints") 35 | oneint := g.GetInt("/data/oneint") 36 | onef64 := g.GetFloat64("/data/onef64") 37 | ``` 38 | 39 | ## API 40 | 41 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/hb) 42 | -------------------------------------------------------------------------------- /hb/deep2.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package hb 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/utl" 10 | ) 11 | 12 | // PutDeep2 puts a Deep2 slice into file 13 | // Input: 14 | // path -- HDF5 path such as "/myvec" or "/group/myvec" 15 | // a -- slice of slices of float64 16 | // Note: Slice will be serialized 17 | func (o *File) PutDeep2(path string, a [][]float64) { 18 | m := len(a) 19 | if m < 1 { 20 | chk.Panic("cannot put empty Deep2 into file. path = %q", path) 21 | } 22 | n := len(a[0]) 23 | if n < 1 { 24 | chk.Panic("cannot put empty Deep2 into file. path = %q", path) 25 | } 26 | aser := utl.SerializeDeep2(a) 27 | o.putArray(path, []int{m, n}, aser) 28 | } 29 | 30 | // GetDeep2 gets a Deep2 slice (that was serialized). Memory will be allocated 31 | func (o *File) GetDeep2(path string) (a [][]float64) { 32 | dims, aser := o.getArray(path, false, true) 33 | return utl.DeserializeDeep2(aser, dims[0], dims[1]) 34 | } 35 | 36 | // GetDeep2raw returns the serialized data corresponding to a Deep2 slice 37 | func (o *File) GetDeep2raw(path string) (m, n int, a []float64) { 38 | var dims []int 39 | dims, a = o.getArray(path, false, true) 40 | m, n = dims[0], dims[1] 41 | return 42 | } 43 | -------------------------------------------------------------------------------- /hb/deep3.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package hb 6 | 7 | import "github.com/cpmech/gosl/utl" 8 | 9 | // PutDeep3 puts a deep slice with 3 levels and name described in path into HDF5 file 10 | // Input: 11 | // path -- HDF5 path such as "/myvec" or "/group/myvec" 12 | // a -- slice of slices of slices of float64 13 | // Note: Slice will be serialized 14 | func (o *File) PutDeep3(path string, a [][][]float64) { 15 | I, P, S := utl.SerializeDeep3(a) 16 | o.putArray(path+"/S", []int{len(S)}, S) 17 | o.putIntsNoGroup(path+"/I", I) 18 | o.putIntsNoGroup(path+"/P", P) 19 | } 20 | 21 | // GetDeep3 gets a deep slice with 3 levels from file. Memory will be allocated 22 | func (o *File) GetDeep3(path string) (a [][][]float64) { 23 | _, S := o.getArray(path+"/S", false, false) 24 | _, I := o.getInts(path+"/I", false, false) 25 | _, P := o.getInts(path+"/P", false, false) 26 | a = utl.DeserializeDeep3(I, P, S, false) 27 | return 28 | } 29 | -------------------------------------------------------------------------------- /hb/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package hb 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /io/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. io. Input/output, read/write files, and print commands 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/io.svg)](https://pkg.go.dev/github.com/cpmech/gosl/io) 4 | 5 | This sub-package helps with reading and writing files, printing nice formatted messages (with 6 | colours), and parsing strings. 7 | 8 | ## Examples 9 | 10 | ### Read and write files 11 | 12 | To write and read a file: 13 | 14 | ```go 15 | theline := "Hello World !!!" 16 | io.WriteFileSD("/tmp/gosl", "filestring.txt", theline) 17 | 18 | f, err := io.OpenFileR("/tmp/gosl/filestring.txt") 19 | if err != nil { 20 | chk.Panic("%v", err) 21 | } 22 | 23 | io.ReadLinesFile(f, func(idx int, line string) (stop bool) { 24 | Pforan("line = %v\n", line) 25 | chk.String(tst, line, theline) 26 | return 27 | }) 28 | ``` 29 | 30 | ## API 31 | 32 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/io) 33 | -------------------------------------------------------------------------------- /io/auxiliary.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | // the following auxiliary functions cannot be in utl; 8 | // otherwise there will be cyclic referencing 9 | 10 | func imin(a, b int) int { 11 | if a < b { 12 | return a 13 | } 14 | return b 15 | } 16 | 17 | func imax(a, b int) int { 18 | if a > b { 19 | return a 20 | } 21 | return b 22 | } 23 | 24 | func min(a, b float64) float64 { 25 | if a < b { 26 | return a 27 | } 28 | return b 29 | } 30 | 31 | func max(a, b float64) float64 { 32 | if a > b { 33 | return a 34 | } 35 | return b 36 | } 37 | -------------------------------------------------------------------------------- /io/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. io. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /io/data/mat01.dat: -------------------------------------------------------------------------------- 1 | 1 2 3 4 2 | 3 | 10 20 30 40 4 | # this is a comment 5 | -1 -2 -3 -4 6 | 7 | line with text here 8 | -------------------------------------------------------------------------------- /io/data/table01.dat: -------------------------------------------------------------------------------- 1 | # this is a comment 2 | a b c d 3 | # comment 4 | 1 2 3 666 5 | 4 5 6 777 6 | # another comment here 7 | 7 8 9 641 8 | # last comment 9 | -------------------------------------------------------------------------------- /io/encoder.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | import ( 8 | "encoding/gob" 9 | "encoding/json" 10 | goio "io" 11 | ) 12 | 13 | // Encoder defines encoders; e.g. gob or json 14 | type Encoder interface { 15 | Encode(e interface{}) error 16 | } 17 | 18 | // Decoder defines decoders; e.g. gob or json 19 | type Decoder interface { 20 | Decode(e interface{}) error 21 | } 22 | 23 | // NewEncoder returns a new encoder 24 | func NewEncoder(w goio.Writer, enctype string) Encoder { 25 | if enctype == "json" { 26 | return json.NewEncoder(w) 27 | } 28 | return gob.NewEncoder(w) 29 | } 30 | 31 | // NewDecoder returns a new decoder 32 | func NewDecoder(r goio.Reader, enctype string) Decoder { 33 | if enctype == "json" { 34 | return json.NewDecoder(r) 35 | } 36 | return gob.NewDecoder(r) 37 | } 38 | -------------------------------------------------------------------------------- /io/printing.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | // StrThickLine returns a thick line (using '=') 8 | func StrThickLine(n int) (l string) { 9 | for i := 0; i < n; i++ { 10 | l += "=" 11 | } 12 | return l + "\n" 13 | } 14 | 15 | // StrThinLine returns a thin line (using '-') 16 | func StrThinLine(n int) (l string) { 17 | for i := 0; i < n; i++ { 18 | l += "-" 19 | } 20 | return l + "\n" 21 | } 22 | 23 | // StrSpaces returns a line with spaces 24 | func StrSpaces(n int) (l string) { 25 | for i := 0; i < n; i++ { 26 | l += " " 27 | } 28 | return 29 | } 30 | -------------------------------------------------------------------------------- /io/t_args_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func Test_args01(tst *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("args01") 17 | 18 | fn, fnk := ArgToFilename(0, "simulation", ".sim", true) 19 | Pforan("fn = %v\n", fn) 20 | Pforan("fnk = %v\n", fnk) 21 | chk.String(tst, fn, "simulation.sim") 22 | chk.String(tst, fnk, "simulation") 23 | 24 | resFloat := ArgToFloat(1, 456) 25 | chk.Float64(tst, "456", 1e-17, resFloat, 456) 26 | 27 | resInt := ArgToInt(1, 123) 28 | if resInt != 123 { 29 | tst.Errorf("test failed: resInt != 123\n") 30 | return 31 | } 32 | 33 | resBool := ArgToBool(1, true) 34 | if !resBool { 35 | tst.Errorf("test failed: resBool != true\n") 36 | return 37 | } 38 | 39 | resString := ArgToString(1, "myname") 40 | chk.String(tst, resString, "myname") 41 | 42 | tab := ArgsTable( 43 | "1 2 3 INPUT PARAMETERS 3 2 1", 44 | "first argument", "first", true, 45 | "second argument", "second", "string", 46 | "third argument", "third", 123, 47 | "fourth argument", "fourth", 666.0, 48 | ) 49 | 50 | Pf("\n%v\n", tab) 51 | 52 | chk.String(tst, tab, 53 | ` 1 2 3 INPUT PARAMETERS 3 2 1 54 | =================================== 55 | description key value 56 | ----------------------------------- 57 | first argument first true 58 | second argument second string 59 | third argument third 123 60 | fourth argument fourth 666 61 | =================================== 62 | `) 63 | } 64 | -------------------------------------------------------------------------------- /io/t_encoder_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | import ( 8 | "bytes" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/chk" 12 | ) 13 | 14 | func TestEncDec01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("EncDec01") 18 | 19 | // data 20 | dat := struct{ A, B int }{123, 456} 21 | 22 | // ------- A ------- 23 | 24 | // writer 25 | wbuf := new(bytes.Buffer) 26 | 27 | // encoder 28 | enc := NewEncoder(wbuf, "json") 29 | 30 | // encode 31 | enc.Encode(&dat) 32 | chk.String(tst, string(wbuf.Bytes()), `{"A":123,"B":456}`+"\n") 33 | 34 | // ------- B ------- 35 | 36 | // reader 37 | rbuf := bytes.NewBuffer(wbuf.Bytes()) 38 | 39 | // decoder 40 | dec := NewDecoder(rbuf, "json") 41 | 42 | // decode 43 | res := struct{ A, B int }{} 44 | dec.Decode(&res) 45 | if res.A != 123 { 46 | tst.Errorf("A should be 123") 47 | return 48 | } 49 | if res.B != 456 { 50 | tst.Errorf("A should be 456") 51 | return 52 | } 53 | } 54 | 55 | func TestEncDec02(tst *testing.T) { 56 | 57 | //verbose() 58 | chk.PrintTitle("EncDec02") 59 | 60 | // data 61 | dat := struct{ A, B int }{123, 456} 62 | 63 | // ------- A ------- 64 | 65 | // writer 66 | wbuf := new(bytes.Buffer) 67 | 68 | // encoder 69 | enc := NewEncoder(wbuf, "gob") 70 | 71 | // encode 72 | enc.Encode(&dat) 73 | // cannot check wbuf 74 | 75 | // ------- B ------- 76 | 77 | // reader 78 | rbuf := bytes.NewBuffer(wbuf.Bytes()) 79 | 80 | // decoder 81 | dec := NewDecoder(rbuf, "gob") 82 | 83 | // decode 84 | res := struct{ A, B int }{} 85 | dec.Decode(&res) 86 | if res.A != 123 { 87 | tst.Errorf("A should be 123") 88 | return 89 | } 90 | if res.B != 456 { 91 | tst.Errorf("A should be 456") 92 | return 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /io/t_external_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | import ( 8 | "os/exec" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/chk" 12 | ) 13 | 14 | func Test_runcmd01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("runcmd01") 18 | 19 | ver := false 20 | Pforan("running 'ls -la'\n") 21 | out := RunCmd(ver, "ls", "-la") 22 | Pfblue2("\noutput:\n%v\n", out) 23 | } 24 | 25 | func Test_pipe01(tst *testing.T) { 26 | 27 | //verbose() 28 | chk.PrintTitle("runcmd01") 29 | 30 | Pforan("running pipe\n") 31 | 32 | // find $DIR -type f # Find all files 33 | dir := "." 34 | find := exec.Command("find", dir, "-type", "f") 35 | 36 | // | grep -v '/[._]' # Ignore hidden/temporary files 37 | egrep := exec.Command("grep", "-v", `/[._]`) 38 | 39 | // | sort -t. -k2 # Sort by file extension 40 | sort := exec.Command("sort", "-t.", "-k2") 41 | 42 | output, stderr := Pipeline(find, egrep, sort) 43 | Pfblue2("\noutput:\n%v\n", string(output)) 44 | Pfcyan("stderr:\n%v\n", string(stderr)) 45 | } 46 | -------------------------------------------------------------------------------- /io/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | ) 10 | 11 | func init() { 12 | Verbose = false 13 | } 14 | 15 | func verbose() { 16 | Verbose = true 17 | chk.Verbose = true 18 | } 19 | -------------------------------------------------------------------------------- /io/t_readtable_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package io 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func TestReadTable01(tst *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("ReadTable 01") 17 | 18 | keys, res := ReadTable("data/table01.dat") 19 | 20 | chk.Strings(tst, "keys", keys, []string{"a", "b", "c", "d"}) 21 | chk.Array(tst, "a", 1.0e-17, res["a"], []float64{1, 4, 7}) 22 | chk.Array(tst, "b", 1.0e-17, res["b"], []float64{2, 5, 8}) 23 | chk.Array(tst, "c", 1.0e-17, res["c"], []float64{3, 6, 9}) 24 | chk.Array(tst, "d", 1.0e-17, res["d"], []float64{666, 777, 641}) 25 | } 26 | 27 | func TestReadMatrix01(tst *testing.T) { 28 | 29 | //verbose() 30 | chk.PrintTitle("ReadMatrix 01") 31 | 32 | res := ReadMatrix("data/mat01.dat") 33 | 34 | chk.Deep2(tst, "mat", 1.0e-17, res, [][]float64{ 35 | {1, 2, 3, 4}, 36 | {10, 20, 30, 40}, 37 | {-1, -2, -3, -4}, 38 | }) 39 | 40 | Pforan("res = %v\n", res) 41 | } 42 | -------------------------------------------------------------------------------- /la/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. la/data. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /la/data/small-sparse-matrix-complex-sym.mtx: -------------------------------------------------------------------------------- 1 | %%MatrixMarket matrix coordinate complex symmetric 2 | %------------------------------------------------------------------------------- 3 | % UF Sparse Matrix Collection, Tim Davis 4 | % http://www.cise.ufl.edu/research/sparse/matrices/GHS_psdef/inline_1 5 | % name: gosl/small_sparse_matrix 6 | % [sample matrix] 7 | % id: 0000 8 | % date: 2020 9 | % author: Gosl 10 | % ed: Gosl 11 | % fields: name title A Zeros id date author ed kind 12 | % kind: blah blah blah 13 | %------------------------------------------------------------------------------- 14 | 5 5 7 15 | 1 1 +2 0 16 | 2 1 +3 1 17 | 3 2 -1 0 18 | 4 3 +2 2 19 | 4 4 +3 0 20 | 5 2 +6 -1 21 | 5 5 +1 0 22 | -------------------------------------------------------------------------------- /la/data/small-sparse-matrix-complex.mtx: -------------------------------------------------------------------------------- 1 | %%MatrixMarket matrix coordinate complex general 2 | %------------------------------------------------------------------------------- 3 | % UF Sparse Matrix Collection, Tim Davis 4 | % http://www.cise.ufl.edu/research/sparse/matrices/GHS_psdef/inline_1 5 | % name: gosl/small_sparse_matrix 6 | % [sample matrix] 7 | % id: 0000 8 | % date: 2020 9 | % author: Gosl 10 | % ed: Gosl 11 | % fields: name title A Zeros id date author ed kind 12 | % kind: blah blah blah 13 | %------------------------------------------------------------------------------- 14 | 5 5 13 15 | 1 1 +2.0 0 16 | 1 2 +3.0 0 17 | 2 1 +3.0 1 18 | 2 3 +4.0 0 19 | 2 5 +6.0 0 20 | 3 2 -1.0 2 21 | 3 3 -3.0 0 22 | 3 4 +2.0 0 23 | 4 3 +1.0 -3 24 | 5 2 +4.0 0 25 | 5 3 +2.0 0 26 | 5 4 0 4 27 | 5 5 +1.0 -5 -------------------------------------------------------------------------------- /la/data/small-sparse-matrix-sym.mtx: -------------------------------------------------------------------------------- 1 | %%MatrixMarket matrix coordinate real symmetric 2 | %------------------------------------------------------------------------------- 3 | % UF Sparse Matrix Collection, Tim Davis 4 | % http://www.cise.ufl.edu/research/sparse/matrices/GHS_psdef/inline_1 5 | % name: gosl/small_sparse_matrix 6 | % [sample matrix] 7 | % id: 0000 8 | % date: 2020 9 | % author: Gosl 10 | % ed: Gosl 11 | % fields: name title A Zeros id date author ed kind 12 | % kind: blah blah blah 13 | %------------------------------------------------------------------------------- 14 | 5 5 7 15 | 1 1 +2 16 | 2 1 +3 17 | 3 2 -1 18 | 4 3 +2 19 | 4 4 +3 20 | 5 2 +6 21 | 5 5 +1 22 | -------------------------------------------------------------------------------- /la/data/small-sparse-matrix.mtx: -------------------------------------------------------------------------------- 1 | %%MatrixMarket matrix coordinate real general 2 | %------------------------------------------------------------------------------- 3 | % UF Sparse Matrix Collection, Tim Davis 4 | % http://www.cise.ufl.edu/research/sparse/matrices/GHS_psdef/inline_1 5 | % name: gosl/small_sparse_matrix 6 | % [sample matrix] 7 | % id: 0000 8 | % date: 2020 9 | % author: Gosl 10 | % ed: Gosl 11 | % fields: name title A Zeros id date author ed kind 12 | % kind: blah blah blah 13 | %------------------------------------------------------------------------------- 14 | 5 5 12 15 | 1 1 +2.0 16 | 2 1 +3.0 17 | 1 2 +3.0 18 | 3 2 -1.0 19 | 5 2 +4.0 20 | 2 3 +4.0 21 | 3 3 -3.0 22 | 4 3 +1.0 23 | 5 3 +2.0 24 | 3 4 +2.0 25 | 2 5 +6.0 26 | 5 5 +1.0 27 | -------------------------------------------------------------------------------- /la/flags.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package la 6 | 7 | /* 8 | #cgo CFLAGS: -O2 -I/usr/include/mumps -I/usr/include/suitesparse 9 | #cgo LDFLAGS: -lumfpack -lamd -lcholmod -lcolamd -lsuitesparseconfig -lopenblas -lgfortran 10 | #cgo LDFLAGS: -ldmumps_seq -lzmumps_seq -lmumps_common_seq -lpord 11 | */ 12 | import "C" 13 | -------------------------------------------------------------------------------- /la/oblas/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. la/oblas. Wrapper to OpenBLAS 2 | 3 | [![PkgGoDev](https://pkg.go.dev/badge/github.com/cpmech/gosl/la/oblas)](https://pkg.go.dev/github.com/cpmech/gosl/la/oblas) 4 | 5 | This subpackge implements a light wrapper to OpenBLAS. Therefore, its routines are a little more 6 | _lower level_ than the ones in the parent package `la`. 7 | 8 | [Check also OpenBLAS](https://github.com/xianyi/OpenBLAS). 9 | 10 | ## API 11 | 12 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/la/oblas) 13 | -------------------------------------------------------------------------------- /la/oblas/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. la/oblas/data. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /la/oblas/data/auxiliary.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The Gosl Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style 3 | # license that can be found in the LICENSE file. 4 | 5 | import numpy as np 6 | 7 | def vprint(name, v, fmt='%+23.15e'): 8 | l = '%s := []float64{' % name 9 | for i in range(len(v)): 10 | if i > 0: l += ',' 11 | l += fmt % v[i] 12 | l += '}' 13 | print l 14 | 15 | def mprint(name, m, fmt='%+23.15e'): 16 | l = '%s := [][]float64{\n' % name 17 | for i in range(len(m)): 18 | l += ' {' 19 | for j in range(len(m[i])): 20 | if j > 0: l += ',' 21 | l += fmt % m[i][j] 22 | l += '},\n' 23 | l += '}' 24 | print l 25 | 26 | def vprintC(name, v, fmt='%+23.15e'): 27 | ff = fmt + ' ' + fmt + 'i' 28 | l = '%s := []complex128{' % name 29 | for i in range(len(v)): 30 | if i > 0: l += ',' 31 | l += ff % (v[i].real, v[i].imag) 32 | l += '}' 33 | print l 34 | 35 | def mprintC(name, m, fmt='%+23.15e', ztol=1e-16): 36 | ff = fmt + ' ' + fmt + 'i' 37 | l = '%s := [][]complex128{\n' % name 38 | for i in range(len(m)): 39 | l += ' {' 40 | for j in range(len(m[i])): 41 | if j > 0: l += ',' 42 | re, im = m[i][j].real, m[i][j].imag 43 | if np.abs(re) < ztol: re = 0.0 44 | if np.abs(im) < ztol: im = 0.0 45 | l += ff % (re, im) 46 | l += '},\n' 47 | l += '}' 48 | print l 49 | -------------------------------------------------------------------------------- /la/oblas/data/dgeev01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.matrix([ 6 | [+0.35, +0.45, -0.14, -0.17], 7 | [+0.09, +0.07, -0.54, +0.35], 8 | [-0.44, -0.33, -0.03, +0.17], 9 | [+0.25, -0.32, -0.13, +0.11], 10 | ], dtype=float) 11 | 12 | w, vl, vr = la.eig(a, left=True, right=True) 13 | 14 | vprintC('w', w) 15 | 16 | print 17 | for i in range(4): 18 | vprintC('vl%d'%i, vl[:,i]) 19 | 20 | print 21 | for i in range(4): 22 | vprintC('vr%d'%i, vr[:,i]) 23 | -------------------------------------------------------------------------------- /la/oblas/data/dgemm01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.matrix([ 6 | [1, 2, 0, 1, -1], 7 | [2, 3,-1, 1, +1], 8 | [1, 2, 0, 4, -1], 9 | [4, 0, 3, 1, +1], 10 | ], dtype=float) 11 | 12 | b = np.matrix([ 13 | [1, 0, 0], 14 | [0, 0, 3], 15 | [0, 0, 1], 16 | [1, 0, 1], 17 | [0, 2, 0], 18 | ], dtype=float) 19 | 20 | c = np.matrix([ 21 | [+0.50, 0, +0.25], 22 | [+0.25, 0, -0.25], 23 | [-0.25, 0, +0.00], 24 | [-0.25, 0, +0.00], 25 | ], dtype=float) 26 | 27 | print 0.5*np.dot(a, b) + 2*c 28 | -------------------------------------------------------------------------------- /la/oblas/data/dgesv01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | 4 | a = np.array([ 5 | [19.730 + 0.000j, 12.110 - 1.000j, 0.000 + 5.000j, 0.000 + 0.000j, 0.000 + 0.000j], 6 | [ 0.000 - 0.510j, 32.300 + 7.000j, 23.070 + 0.000j, 0.000 + 1.000j, 0.000 + 0.000j], 7 | [ 0.000 + 0.000j, 0.000 - 0.510j, 70.000 + 7.300j, 3.950 + 0.000j, 19.000 + 31.830j], 8 | [ 0.000 + 0.000j, 0.000 + 0.000j, 1.000 + 1.100j, 50.170 + 0.000j, 45.510 + 0.000j], 9 | [ 0.000 + 0.000j, 0.000 + 0.000j, 0.000 + 0.000j, 0.000 - 9.351j, 55.000 + 0.000j], 10 | ]) 11 | 12 | print a 13 | 14 | b = np.array([ 15 | 77.38 + 8.82j, 16 | 157.48 + 19.80j, 17 | 1175.62 + 20.69j, 18 | 912.12 - 801.75j, 19 | 550.00 - 1060.40j, 20 | ]) 21 | 22 | res = la.solve(a,b) 23 | for v in res: 24 | print '%23.15e + %23.15ei,' % (v.real, v.imag) 25 | print 26 | -------------------------------------------------------------------------------- /la/oblas/data/dgesvd01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import numpy.linalg as la 3 | from auxiliary import * 4 | 5 | A = np.array([ 6 | [1, 0, 0, 0, 2], 7 | [0, 0, 3, 0, 0], 8 | [0, 0, 0, 0, 0], 9 | [0, 2, 0, 0, 0], 10 | ], dtype=float) 11 | 12 | U, s, Vt = la.svd(A) 13 | 14 | mprint('amat', A) 15 | mprint('uCorrect', U) 16 | vprint('sCorrect', s) 17 | mprint('vtCorrect', Vt) 18 | 19 | m, n = np.shape(A) 20 | ns = min([m, n]) 21 | 22 | S = np.zeros((m, n)) 23 | for i in range(ns): 24 | S[i,i] = s[i] 25 | USVt = np.dot(U, np.dot(S, Vt)) 26 | 27 | print 'A =\n', A 28 | print 'USVt =\n', USVt 29 | print np.allclose(A, USVt) 30 | -------------------------------------------------------------------------------- /la/oblas/data/dgesvd02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import numpy.linalg as la 3 | from auxiliary import * 4 | 5 | A = np.array([ 6 | [-5.773502691896260e-01, -5.773502691896260e-01, 1], 7 | [ 5.773502691896260e-01, -5.773502691896260e-01, 1], 8 | [-5.773502691896260e-01, 5.773502691896260e-01, 1], 9 | [ 5.773502691896260e-01, 5.773502691896260e-01, 1], 10 | ], dtype=float) 11 | 12 | U, s, Vt = la.svd(A) 13 | 14 | mprint('amat', A) 15 | mprint('uCorrect', U) 16 | vprint('sCorrect', s) 17 | mprint('vtCorrect', Vt) 18 | 19 | m, n = np.shape(A) 20 | ns = min([m, n]) 21 | 22 | S = np.zeros((m, n)) 23 | for i in range(ns): 24 | S[i,i] = s[i] 25 | USVt = np.dot(U, np.dot(S, Vt)) 26 | 27 | print 'A =\n', A 28 | print 'USVt =\n', USVt 29 | print np.allclose(A, USVt) 30 | -------------------------------------------------------------------------------- /la/oblas/data/dgesvd03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import numpy.linalg as la 3 | from auxiliary import * 4 | 5 | A = np.array([ 6 | [64, 2, 3, 61, 60, 6], 7 | [ 9, 55, 54, 12, 13, 51], 8 | [17, 47, 46, 20, 21, 43], 9 | [40, 26, 27, 37, 36, 30], 10 | [32, 34, 35, 29, 28, 38], 11 | [41, 23, 22, 44, 45, 19], 12 | [49, 15, 14, 52, 53, 11], 13 | [ 8, 58, 59, 5, 4, 62], 14 | ], dtype=float) 15 | 16 | U, s, Vt = la.svd(A) 17 | 18 | mprint('amat', A, '%3g') 19 | mprint('uCorrect', U) 20 | vprint('sCorrect', s) 21 | mprint('vtCorrect', Vt) 22 | 23 | m, n = np.shape(A) 24 | ns = min([m, n]) 25 | 26 | S = np.zeros((m, n)) 27 | for i in range(ns): 28 | S[i,i] = s[i] 29 | USVt = np.dot(U, np.dot(S, Vt)) 30 | 31 | print 'A =\n', A 32 | print 'USVt =\n', USVt 33 | print np.allclose(A, USVt) 34 | -------------------------------------------------------------------------------- /la/oblas/data/dgetrf01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.array([[1, 2, 0, 1], 6 | [2, 3,-1, 1], 7 | [1, 2, 0, 4], 8 | [4, 0, 3, 1]], dtype=float) 9 | 10 | lu, piv = la.lu_factor(a) 11 | 12 | print lu 13 | print piv 14 | 15 | mprint('lu', lu) 16 | mprint('ai', la.inv(a)) 17 | -------------------------------------------------------------------------------- /la/oblas/data/dpotrf01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.matrix([ 6 | [+3, +0, -3, +0], 7 | [+0, +3, +1, +2], 8 | [-3, +1, +4, +1], 9 | [+0, +2, +1, +3], 10 | ],dtype=float) 11 | 12 | res = la.cholesky(a, lower=False) 13 | mprint('aUp', res) 14 | 15 | res = la.cholesky(a, lower=True) 16 | mprint('aLo', res) 17 | -------------------------------------------------------------------------------- /la/oblas/data/dsyrk01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | 4 | a = np.array([ 5 | [+1, +2, +1, +1, -1, +0], 6 | [+2, +2, +1, +0, +0, +0], 7 | [+3, +1, +3, +1, +2, -1], 8 | [+1, +0, +1, -1, +0, +0], 9 | ]) 10 | 11 | tmp = np.array([ 12 | [+1, -1, +0, -1], 13 | [-1, -1, +1, +0], 14 | [-1, +1, +1, +1], 15 | [-1, +0, +1, -1], 16 | ]) 17 | 18 | c = np.dot(tmp,tmp.transpose()) 19 | 20 | print '....................... c' 21 | print c 22 | print la.det(c) 23 | print la.eigvals(c) 24 | print la.cholesky(c) 25 | 26 | res = 3.0 * np.dot(a,a.transpose()) - 1.0 * c 27 | 28 | print 29 | print '....................... 3*a*aT - 1*c' 30 | print res 31 | print la.det(res) 32 | print la.eigvals(res) 33 | print la.cholesky(res) 34 | -------------------------------------------------------------------------------- /la/oblas/data/dsyrk02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | 4 | a = np.array([ 5 | [+1, +2, +1, +1, -1, +0], 6 | [+2, +2, +1, +0, +0, +0], 7 | [+3, +1, +3, +1, +2, -1], 8 | [+1, +0, +1, -1, +0, +0], 9 | ]) 10 | 11 | tmp = np.array([ 12 | [+1, -1, +0, -1, +0, +0], 13 | [-1, -1, +1, +0, +0, +0], 14 | [-1, +1, +1, +1, +0, +0], 15 | [-1, +0, +1, -1, +0, +0], 16 | [-1, +0, +1, -1, +1, +0], 17 | [-1, +0, +1, -1, +0, +1], 18 | ]) 19 | 20 | c = np.dot(tmp,tmp.transpose()) 21 | 22 | print 23 | print '........................ c' 24 | print c 25 | print la.det(c) 26 | print la.eigvals(c) 27 | print la.cholesky(c) 28 | 29 | res = 3.0 * np.dot(a.transpose(),a) + 1.0 * c 30 | 31 | print 32 | print '........................ 3*aT*a + 1*c' 33 | print res 34 | print la.det(res) 35 | print la.eigvals(res) 36 | print la.cholesky(res) 37 | -------------------------------------------------------------------------------- /la/oblas/data/jacobi01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.matrix([ 6 | [2, 0, 0], 7 | [0, 3, 4], 8 | [0, 4, 9], 9 | ], dtype=float) 10 | 11 | w, vl, vr = la.eig(a, left=True, right=True) 12 | print 'w =', w 13 | print 'vl =\n', vl 14 | print 'vr =\n', vr 15 | 16 | w, v = la.eigh(a) 17 | print 18 | print 'w =', w 19 | print 'v =\n', v 20 | 21 | print 22 | print 23 | print 24 | 25 | b = np.matrix([ 26 | [1, 2, 3], 27 | [2, 3, 2], 28 | [3, 2, 2], 29 | ], dtype=float) 30 | 31 | w, vl, vr = la.eig(b, left=True, right=True) 32 | print 'w =', w 33 | print 'vl =\n', vl 34 | print 'vr =\n', vr 35 | 36 | w, v = la.eigh(b) 37 | print 38 | print 'w =', w 39 | print 'v =\n', v 40 | 41 | print 42 | print 43 | print 44 | 45 | c = np.matrix([ 46 | [1, 2, 3, 4, 5], 47 | [2, 3, 0, 2, 4], 48 | [3, 0, 2, 1, 3], 49 | [4, 2, 1, 1, 2], 50 | [5, 4, 3, 2, 1], 51 | ], dtype=float) 52 | 53 | for i in range(5): 54 | for j in range(5): 55 | if c[i,j] != c[j,i]: raise Exception("non symmetric") 56 | 57 | w, vl, vr = la.eig(c, left=True, right=True) 58 | print 'w =', w 59 | print 'vl =\n', vl 60 | print 'vr =\n', vr 61 | 62 | w, v = la.eigh(c) 63 | print 64 | print 'w =', w 65 | print 'v =\n', v 66 | 67 | vprint('l', w) 68 | mprint('Q', v) 69 | -------------------------------------------------------------------------------- /la/oblas/data/zdgesvd01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | A = np.array([ 6 | [+0, +1, +0, -1], 7 | [+1, +0, 1j, +0], 8 | [+0, 1j, +0, 1j], 9 | [-1, +0, 1j, +0], 10 | ], dtype=complex) 11 | A = A / np.sqrt(2.0) 12 | 13 | U, s, Vt = la.svd(A, lapack_driver='gesvd') 14 | 15 | mprintC('amat', A) 16 | mprintC('uCorrect', U) 17 | vprint('sCorrect', s) 18 | mprintC('vtCorrect', Vt) 19 | 20 | m, n = np.shape(A) 21 | ns = min([m, n]) 22 | 23 | S = np.zeros((m, n)) 24 | for i in range(ns): 25 | S[i,i] = s[i] 26 | USVt = np.dot(U, np.dot(S, Vt)) 27 | 28 | print 'A =\n', A 29 | print 'USVt =\n', USVt 30 | print np.allclose(A, USVt) 31 | -------------------------------------------------------------------------------- /la/oblas/data/zgemm01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.matrix([ 6 | [1, 2, 0+1j, 1, -1], 7 | [2, 3,-1-1j, 1, +1], 8 | [1, 2, 0+1j, 4, -1], 9 | [4, 0, 3-1j, 1, +1], 10 | ], dtype=complex) 11 | 12 | b = np.matrix([ 13 | [1, 0, 0+1j], 14 | [0, 0, 3-1j], 15 | [0, 0, 1+1j], 16 | [1, 0, 1-1j], 17 | [0, 2, 0+1j], 18 | ], dtype=complex) 19 | 20 | c = np.matrix([ 21 | [+0.50, 1j, +0.25], 22 | [+0.25, 1j, -0.25], 23 | [-0.25, 1j, +0.00], 24 | [-0.25, 1j, +0.00], 25 | ], dtype=complex) 26 | 27 | print (0.5-2j)*np.dot(a, b) + (2.0-4j)*c 28 | -------------------------------------------------------------------------------- /la/oblas/data/zgemv01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | a = np.array([[0.1 + 3j, 0.2, 0.3 - 0.3j], 4 | [1.0 + 2j, 0.2, 0.3 - 0.4j], 5 | [2.0 + 1j, 0.2, 0.3 - 0.5j], 6 | [3.0 + 0.1j, 0.2, 0.3 - 0.6j]], dtype=complex) 7 | 8 | alp = 0.5+1j 9 | bet = 2.0+1j 10 | 11 | x = np.array([20, 10, 30]) 12 | y = np.array([3, 1, 2, 4]) 13 | 14 | res = alp*np.dot(a,x) + bet*y 15 | print res 16 | 17 | y = res 18 | res = alp*np.dot(a.T,y) + bet*x 19 | print res 20 | -------------------------------------------------------------------------------- /la/oblas/data/zgesvd02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | A = np.array([ 6 | [ 0 , 3 , 2 , 1 ], 7 | [ 1 , 1j , 1j , 1j ], 8 | [ 2 , 2 , 2j , 2j ], 9 | [ 3 , 3 , 3 , 3j ], 10 | ], dtype=complex) 11 | 12 | U, s, Vt = la.svd(A, lapack_driver='gesvd') 13 | 14 | mprintC('amat', A) 15 | mprintC('uCorrect', U) 16 | vprint('sCorrect', s) 17 | mprintC('vtCorrect', Vt) 18 | 19 | m, n = np.shape(A) 20 | ns = min([m, n]) 21 | 22 | S = np.zeros((m, n)) 23 | for i in range(ns): 24 | S[i,i] = s[i] 25 | USVt = np.dot(U, np.dot(S, Vt)) 26 | 27 | print 'A =\n', A 28 | print 'USVt =\n', USVt 29 | print np.allclose(A, USVt) 30 | -------------------------------------------------------------------------------- /la/oblas/data/zgetrf01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.array([[1+1j, 2, 0, 1-1j], 6 | [2+1j, 3,-1, 1-1j], 7 | [1+1j, 2, 0, 4-1j], 8 | [4+1j, 0, 3, 1-1j]], dtype=complex) 9 | 10 | lu, piv = la.lu_factor(a) 11 | 12 | print lu 13 | print piv 14 | 15 | mprintC('lu', lu) 16 | mprintC('ai', la.inv(a)) 17 | -------------------------------------------------------------------------------- /la/oblas/data/zherk01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.matrix([ 6 | [+1-1j, +2, +1, +1, -1, +0+0j], 7 | [+2+0j, +2, +1, +0, +0, +0+1j], 8 | [+3+1j, +1, +3, +1, +2, -1+0j], 9 | [+1+0j, +0, +1, -1, +0, +0+1j], 10 | ],dtype=complex) 11 | 12 | tmp = np.matrix([ 13 | [+1-1j, -1, +0, -1+0j], 14 | [-1+0j, -1, +1, +0+0j], 15 | [-1+0j, +1, +1, +1+0j], 16 | [-1+0j, +0, +1, -1+1j], 17 | ],dtype=complex) 18 | 19 | c = np.dot(tmp,tmp.getH()) 20 | 21 | 22 | print '....................... c' 23 | print np.max(np.abs(c.getH() - c)) 24 | print c 25 | print la.det(c) 26 | print la.eigvals(c) 27 | print la.cholesky(c) 28 | 29 | res = 3.0 * np.dot(a,a.getH()) + 1.0 * c 30 | 31 | print 32 | print '....................... 3*a*aT + 1*c' 33 | print res 34 | print la.det(res) 35 | print la.eigvals(res) 36 | print la.cholesky(res) 37 | -------------------------------------------------------------------------------- /la/oblas/data/zpotrf01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.matrix([ 6 | [+4 + 0j, 0 + 1j, -3 + 1j, 0 + 2j], 7 | [+0 - 1j, 3 + 0j, +1 + 0j, 2 + 0j], 8 | [-3 - 1j, 1 + 0j, +4 + 0j, 1 - 1j], 9 | [+0 - 2j, 2 + 0j, +1 + 1j, 4 + 0j], 10 | ],dtype=complex) 11 | 12 | print a - a.getH() 13 | 14 | res = la.cholesky(a, lower=False) 15 | mprintC('aUp', res) 16 | 17 | res = la.cholesky(a, lower=True) 18 | mprintC('aLo', res) 19 | -------------------------------------------------------------------------------- /la/oblas/data/zsyrk01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy.linalg as la 3 | from auxiliary import * 4 | 5 | a = np.array([ 6 | [+1-1j, +2, +1, +1, -1, +0+0j], 7 | [+2+0j, +2, +1, +0, +0, +0+1j], 8 | [+3+1j, +1, +3, +1, +2, -1+0j], 9 | [+1+0j, +0, +1, -1, +0, +0+1j], 10 | ],dtype=complex) 11 | 12 | tmp = np.array([ 13 | [+1, -1, +0, -1], 14 | [-1, -1, +1, +0], 15 | [-1, +1, +1, +1], 16 | [-1, +0, +1, -1], 17 | ],dtype=complex) 18 | 19 | c = np.dot(tmp,tmp.conjugate()) 20 | c[0,0] += 1j 21 | c[3,3] -= 1j 22 | 23 | print '....................... c' 24 | print c 25 | print la.det(c) 26 | print la.eigvals(c) 27 | print la.cholesky(c) 28 | 29 | res = 3.0 * np.dot(a,a.transpose()) + 1.0 * c 30 | 31 | print 32 | print '....................... 3*a*aT + 1*c' 33 | print res 34 | print la.det(res) 35 | print la.eigvals(res) 36 | print la.cholesky(res) 37 | -------------------------------------------------------------------------------- /la/oblas/flags.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package oblas 6 | 7 | /* 8 | #cgo CFLAGS: -O2 -I/usr/local/include 9 | #cgo LDFLAGS: -lopenblas -llapacke -L/usr/local/lib 10 | */ 11 | import "C" 12 | -------------------------------------------------------------------------------- /la/oblas/t_conversions_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package oblas 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func TestSplitJoinComplex(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("SplitJoinComplex") 18 | 19 | v := []complex128{1 + 0.1i, 2 + 0.2i, 3 - 0.3i} 20 | vr, vi := GetSplitComplex(v) 21 | chk.Array(tst, "vr", 1e-17, vr, []float64{1, 2, 3}) 22 | chk.Array(tst, "vi", 1e-17, vi, []float64{0.1, 0.2, -0.3}) 23 | 24 | u := GetJoinComplex(vr, vi) 25 | chk.ArrayC(tst, "u=v", 1e-17, u, v) 26 | 27 | wr, wi := make([]float64, len(v)), make([]float64, len(v)) 28 | SplitComplex(wr, wi, v) 29 | chk.Array(tst, "wr", 1e-17, wr, []float64{1, 2, 3}) 30 | chk.Array(tst, "wi", 1e-17, wi, []float64{0.1, 0.2, -0.3}) 31 | 32 | w := make([]complex128, len(v)) 33 | JoinComplex(w, wr, wi) 34 | chk.ArrayC(tst, "w=v", 1e-17, w, v) 35 | } 36 | 37 | func TestConversions01(tst *testing.T) { 38 | 39 | //verbose() 40 | chk.PrintTitle("Conversions01. real") 41 | 42 | A := [][]float64{ 43 | {1, 2, +3, +4}, 44 | {5, 6, +7, +8}, 45 | {9, 0, -1, -2}, 46 | } 47 | m, n := len(A), len(A[0]) 48 | 49 | a := SliceToColMajor(A) 50 | 51 | row1 := ExtractRow(1, m, n, a) 52 | io.Pf("row1 = %v\n", row1) 53 | chk.Array(tst, "row1", 1e-17, row1, []float64{5, 6, 7, 8}) 54 | 55 | col2 := ExtractCol(2, m, n, a) 56 | io.Pf("col2 = %v\n", col2) 57 | chk.Array(tst, "col2", 1e-17, col2, []float64{3, 7, -1}) 58 | } 59 | 60 | func TestConversions02(tst *testing.T) { 61 | 62 | //verbose() 63 | chk.PrintTitle("Conversions02. complex") 64 | 65 | A := [][]complex128{ 66 | {1 + 0.1i, 2, +3, +4 - 0.4i}, 67 | {5 + 0.5i, 6, +7, +8 - 0.8i}, 68 | {9 + 0.9i, 0, -1, -2 + 1.0i}, 69 | } 70 | m, n := len(A), len(A[0]) 71 | 72 | a := SliceToColMajorC(A) 73 | 74 | row1 := ExtractRowC(1, m, n, a) 75 | io.Pf("row1 = %v\n", row1) 76 | chk.ArrayC(tst, "row1", 1e-17, row1, []complex128{5 + 0.5i, 6, 7, 8 - 0.8i}) 77 | 78 | col2 := ExtractColC(2, m, n, a) 79 | io.Pf("col2 = %v\n", col2) 80 | chk.ArrayC(tst, "col2", 1e-17, col2, []complex128{3, 7, -1}) 81 | } 82 | -------------------------------------------------------------------------------- /la/oblas/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package oblas 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /la/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package la 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /num/auxiliary.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package num 6 | 7 | import "math" 8 | 9 | // sgn returns a value with the same magnitude as a and the same sign as b 10 | // 11 | // returns: |a| * sign(b) 12 | // 13 | func sgn(a, b float64) float64 { 14 | if b < 0 { 15 | return -math.Abs(a) // return - |a| 16 | } 17 | return math.Abs(a) // return + |a| 18 | } 19 | 20 | func swap(a, b *float64) { 21 | *a, *b = *b, *a 22 | } 23 | 24 | func shft2(a, b *float64, c float64) { 25 | *a = *b 26 | *b = c 27 | } 28 | 29 | func shft3(a, b, c *float64, d float64) { 30 | *a = *b 31 | *b = *c 32 | *c = d 33 | } 34 | 35 | func mov3(a, b, c *float64, d, e, f float64) { 36 | *a = d 37 | *b = e 38 | *c = f 39 | } 40 | -------------------------------------------------------------------------------- /num/cubicequation.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package num 6 | 7 | import ( 8 | "math" 9 | 10 | "github.com/cpmech/gosl/fun" 11 | ) 12 | 13 | // EqCubicSolveReal solves a cubic equation, ignoring the complex answers. 14 | // The equation is specified by: 15 | // x³ + a x² + b x + c = 0 16 | // Notes: 17 | // 1) every cubic equation with real coefficients has at least one solution 18 | // x among the real numbers 19 | // 2) from Numerical Recipes 2007, page 228 20 | // Output: 21 | // x[i] -- roots 22 | // nx -- number of real roots: 1, 2 or 3 23 | func EqCubicSolveReal(a, b, c float64) (x1, x2, x3 float64, nx int) { 24 | 25 | // tolerance 26 | ϵ := 1.4e-9 27 | 28 | // auxiliary 29 | aa := a * a 30 | Q := (aa - 3.0*b) / 9.0 31 | R := (2.0*a*aa - 9.0*a*b + 27.0*c) / 54.0 32 | QQQ := Q * Q * Q 33 | 34 | // three real roots 35 | if R*R < QQQ { 36 | //if QQQ < ϵ { 37 | //return chk.Err("cannot compute roots of cubic equation because Q³ = %g < %g", QQQ, ϵ) 38 | //} 39 | θ := math.Acos(R / math.Sqrt(QQQ)) 40 | c := -2.0 * math.Sqrt(Q) 41 | x1 = c*math.Cos(θ/3.0) - a/3.0 42 | x2 = c*math.Cos((θ+2.0*math.Pi)/3.0) - a/3.0 43 | x3 = c*math.Cos((θ-2.0*math.Pi)/3.0) - a/3.0 44 | nx = 3 45 | return 46 | } 47 | 48 | // auxiliary 49 | A := -fun.Sign(R) * math.Pow(math.Abs(R)+math.Sqrt(R*R-QQQ), 1.0/3.0) 50 | B := 0.0 51 | if math.Abs(A) > ϵ { 52 | B = Q / A 53 | } 54 | 55 | // one root 56 | if math.Abs(A) < ϵ && math.Abs(B) < ϵ { 57 | x1 = -a / 3.0 58 | nx = 1 59 | return 60 | } 61 | 62 | // two roots 63 | if math.Abs(A-B) < ϵ { 64 | x1 = (A + B) - a/3.0 65 | x2 = -(A+B)/2.0 - a/3.0 66 | nx = 2 67 | return 68 | } 69 | 70 | // one real root 71 | x1 = (A + B) - a/3.0 72 | nx = 1 73 | return 74 | } 75 | -------------------------------------------------------------------------------- /num/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. num. data directory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /num/data/nls04.log: -------------------------------------------------------------------------------- 1 | 2 | ========================= nls04. finite differences problem ========================= 3 | 4 | -------------------- Analytical Jacobian ------------------- 5 | 0 : Ldx = 2.783221107171502e+09 : fx_max = 2.500000000000000e+03 6 | 1 : Ldx = 2.783221107171502e+09 : fx_max = 6.821210263296962e-13 7 | nFeval = 2, nJeval = 1 8 | 9 | U OK 10 | 11 | 12 | -------------------- Using NlSolver Structure (dense) ------ 13 | 14 | it Ldx fx_max 15 | (1.0e-04) (1.0e-12) 16 | 0 0.000000000000000e+00 2.500000000000000e+03 17 | 1 2.783221107171501e+09 1.364242052659392e-12 18 | 1 6.335213252939518e-07 6.821210263296962e-13 19 | . . . converged with fx_max. nit=1, nFeval=4, nJeval=2 20 | u1 == U1 OK 21 | 22 | 23 | -------------------- Numerical Jacobian -------------------- 24 | 0 : Ldx = 2.783351619062559e+09 : fx_max = 2.500000000000000e+03 25 | 1 : Ldx = 1.834694526226941e+05 : fx_max = 5.603094111734208e-01 26 | 2 : Ldx = 7.474951336349258e-03 : fx_max = 1.857756615208928e-08 27 | 3 : Ldx = 7.474951336349258e-03 : fx_max = 5.684341886080801e-13 28 | nFeval = 4, nJeval = 3 29 | 30 | U OK 31 | 32 | 33 | checking Jacobian @ [24.999999999999996 14.77272727272727 9.09090909090909 4.545454545454545 35.22727272727272 24.999999999999996 17.045454545454543 9.09090909090909 40.90909090909091 32.954545454545446 25 14.772727272727273 45.454545454545446 40.90909090909091 35.22727272727273 25] 34 | PASS 35 | ok code.google.com/p/gosl/num 0.015s 36 | -------------------------------------------------------------------------------- /num/data/num_brent01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/num/data/num_brent01.png -------------------------------------------------------------------------------- /num/definitions.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package num implements fundamental numerical methods such as numerical derivative and quadrature, 6 | // root finding solvers (Brent's and Newton's methods), among others. 7 | package num 8 | 9 | import "math" 10 | 11 | // constants 12 | var ( 13 | MACHEPS = math.Nextafter(1, 2) - 1.0 // smallest number satisfying 1 + EPS > 1 14 | ) 15 | -------------------------------------------------------------------------------- /num/qpck/connect.c: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | double fcn(double* x, int* fid) { 6 | extern double gofcn(double, int); 7 | return gofcn(*x, *fid); 8 | } 9 | -------------------------------------------------------------------------------- /num/qpck/connect.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #ifndef CONNECT_H 6 | #define CONNECT_H 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | double fcn(double* x, int* fid); 13 | 14 | #ifdef __cplusplus 15 | } /* extern "C" */ 16 | #endif 17 | 18 | #endif // CONNECT_H 19 | -------------------------------------------------------------------------------- /num/qpck/dqwgtc.f: -------------------------------------------------------------------------------- 1 | double precision function dqwgtc(x,c,p2,p3,p4,kp) 2 | c***begin prologue dqwgtc 3 | c***refer to dqk15w 4 | c***routines called (none) 5 | c***revision date 810101 (yymmdd) 6 | c***keywords weight function, cauchy principal value 7 | c***author piessens,robert,appl. math. & progr. div. - k.u.leuven 8 | c de doncker,elise,appl. math. & progr. div. - k.u.leuven 9 | c***purpose this function subprogram is used together with the 10 | c routine qawc and defines the weight function. 11 | c***end prologue dqwgtc 12 | c 13 | double precision c,p2,p3,p4,x 14 | integer kp 15 | c***first executable statement dqwgtc 16 | dqwgtc = 0.1d+01/(x-c) 17 | return 18 | end 19 | -------------------------------------------------------------------------------- /num/qpck/dqwgtf.f: -------------------------------------------------------------------------------- 1 | double precision function dqwgtf(x,omega,p2,p3,p4,integr) 2 | c***begin prologue dqwgtf 3 | c***refer to dqk15w 4 | c***routines called (none) 5 | c***revision date 810101 (yymmdd) 6 | c***keywords cos or sin in weight function 7 | c***author piessens,robert, appl. math. & progr. div. - k.u.leuven 8 | c de doncker,elise,appl. math. * progr. div. - k.u.leuven 9 | c***end prologue dqwgtf 10 | c 11 | double precision dcos,dsin,omega,omx,p2,p3,p4,x 12 | integer integr 13 | c***first executable statement dqwgtf 14 | omx = omega*x 15 | go to(10,20),integr 16 | 10 dqwgtf = dcos(omx) 17 | go to 30 18 | 20 dqwgtf = dsin(omx) 19 | 30 return 20 | end 21 | -------------------------------------------------------------------------------- /num/qpck/dqwgts.f: -------------------------------------------------------------------------------- 1 | double precision function dqwgts(x,a,b,alfa,beta,integr) 2 | c***begin prologue dqwgts 3 | c***refer to dqk15w 4 | c***routines called (none) 5 | c***revision date 810101 (yymmdd) 6 | c***keywords weight function, algebraico-logarithmic 7 | c end-point singularities 8 | c***author piessens,robert,appl. math. & progr. div. - k.u.leuven 9 | c de doncker,elise,appl. math. & progr. div. - k.u.leuven 10 | c***purpose this function subprogram is used together with the 11 | c routine dqaws and defines the weight function. 12 | c***end prologue dqwgts 13 | c 14 | double precision a,alfa,b,beta,bmx,dlog,x,xma 15 | integer integr 16 | c***first executable statement dqwgts 17 | xma = x-a 18 | bmx = b-x 19 | dqwgts = xma**alfa*bmx**beta 20 | go to (40,10,20,30),integr 21 | 10 dqwgts = dqwgts*dlog(xma) 22 | go to 40 23 | 20 dqwgts = dqwgts*dlog(bmx) 24 | go to 40 25 | 30 dqwgts = dqwgts*dlog(xma)*dlog(bmx) 26 | 40 return 27 | end 28 | -------------------------------------------------------------------------------- /num/qpck/flags.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package qpck 6 | 7 | /* 8 | #cgo FFLAGS: -std=legacy 9 | #cgo LDFLAGS: -lopenblas -llapack -lgfortran -lm 10 | */ 11 | import "C" 12 | -------------------------------------------------------------------------------- /num/qpck/getfiles.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | FILES="\ 4 | dqage.f \ 5 | dqag.f \ 6 | dqagie.f \ 7 | dqagi.f \ 8 | dqagpe.f \ 9 | dqagp.f \ 10 | dqagse.f \ 11 | dqags.f \ 12 | dqawce.f \ 13 | dqawc.f \ 14 | dqawfe.f \ 15 | dqawf.f \ 16 | dqawoe.f \ 17 | dqawo.f \ 18 | dqawse.f \ 19 | dqaws.f \ 20 | dqc25c.f \ 21 | dqc25f.f \ 22 | dqc25s.f \ 23 | dqcheb.f \ 24 | dqelg.f \ 25 | dqk15.f \ 26 | dqk15i.f \ 27 | dqk15w.f \ 28 | dqk21.f \ 29 | dqk31.f \ 30 | dqk41.f \ 31 | dqk51.f \ 32 | dqk61.f \ 33 | dqmomo.f \ 34 | dqng.f \ 35 | dqpsrt.f \ 36 | dqwgtc.f \ 37 | dqwgtf.f \ 38 | dqwgts.f \ 39 | " 40 | 41 | # from SciPy 42 | #for f in $FILES; do 43 | #curl https://raw.githubusercontent.com/scipy/scipy/master/scipy/integrate/quadpack/$f > $f 44 | #done 45 | 46 | # extra files 47 | EXTRA="\ 48 | d1mach.f \ 49 | i1mach.f \ 50 | xerror.f \ 51 | " 52 | 53 | for f in $EXTRA; do 54 | curl https://raw.githubusercontent.com/scipy/scipy/master/scipy/special/mach/$f > $f 55 | done 56 | 57 | # from NetLib 58 | #for f in $FILES; do 59 | #curl http://www.netlib.org/quadpack/$f > $f 60 | #done 61 | -------------------------------------------------------------------------------- /num/qpck/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package qpck 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /num/qpck/t_spectral_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package qpck 6 | 7 | import ( 8 | . "math" 9 | 10 | "testing" 11 | 12 | "github.com/cpmech/gosl/chk" 13 | "github.com/cpmech/gosl/io" 14 | ) 15 | 16 | func TestSpecProb01(tst *testing.T) { 17 | 18 | //verbose() 19 | chk.PrintTitle("SpecProb01. Problem with cos(n⋅x)⋅exp(cos(x)-1)") 20 | 21 | π := Pi 22 | for i := 60; i < 100; i++ { 23 | n := float64(i) 24 | f1 := func(x float64) float64 { return Cos(n*x) * Exp(Cos(x)-1) } 25 | f2 := func(x float64) float64 { return Exp(Cos(x) - 1) } 26 | var A1 float64 27 | func() { 28 | if n > 75 && n != 80 { 29 | io.Pf("YES: it fails with n = %v\n", n) 30 | defer chk.RecoverTstPanicIsOK(tst) 31 | } 32 | A1, _, _, _ = Agse(0, f1, -π, π, 0, 0, nil, nil, nil, nil, nil) 33 | }() 34 | A2, _, _, _ := Awoe(0, f2, -π, π, n, 1, 0, 0, 0, 0, nil, nil, nil, nil, nil, nil, 0, nil) 35 | io.Pf("n=%2.f A1=%23.15e A2=%23.15e diff=%g\n", n, A1, A2, A1-A2) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /num/qpck/xerror.f: -------------------------------------------------------------------------------- 1 | SUBROUTINE XERROR(MESS,NMESS,L1,L2) 2 | C 3 | C THIS IS A DUMMY XERROR ROUTINE TO PRINT ERROR MESSAGES WITH NMESS 4 | C CHARACTERS. L1 AND L2 ARE DUMMY PARAMETERS TO MAKE THIS CALL 5 | C COMPATIBLE WITH THE SLATEC XERROR ROUTINE. THIS IS A FORTRAN 77 6 | C ROUTINE. 7 | C 8 | CHARACTER*(*) MESS 9 | NN=NMESS/70 10 | NR=NMESS-70*NN 11 | IF(NR.NE.0) NN=NN+1 12 | K=1 13 | PRINT 900 14 | 900 FORMAT(/) 15 | DO 10 I=1,NN 16 | KMIN=MIN0(K+69,NMESS) 17 | PRINT *, MESS(K:KMIN) 18 | K=K+70 19 | 10 CONTINUE 20 | PRINT 900 21 | RETURN 22 | END 23 | -------------------------------------------------------------------------------- /num/t_fitdata_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package num 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func TestLinFit01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("LinFit01") 18 | 19 | // data 20 | x := []float64{1, 2, 3, 4} 21 | y := []float64{1, 2, 3, 4} 22 | a, b, σa, σb, χ2 := LinFitSigma(x, y) 23 | io.Pforan("a=%v b=%v σa=%v σb=%v χ2=%v\n", a, b, σa, σb, χ2) 24 | chk.Float64(tst, "a", 1e-17, a, 0) 25 | chk.Float64(tst, "b", 1e-17, b, 1) 26 | chk.Float64(tst, "σa", 1e-17, σa, 0) 27 | chk.Float64(tst, "σb", 1e-17, σb, 0) 28 | chk.Float64(tst, "χ2", 1e-17, χ2, 0) 29 | } 30 | 31 | func TestLinFit02(tst *testing.T) { 32 | 33 | //verbose() 34 | chk.PrintTitle("LinFit02") 35 | 36 | // data 37 | x := []float64{1, 2, 3, 4} 38 | y := []float64{6, 5, 7, 10} 39 | a, b := LinFit(x, y) 40 | io.Pforan("a=%v b=%v\n", a, b) 41 | chk.Float64(tst, "a", 1e-17, a, 3.5) 42 | chk.Float64(tst, "b", 1e-17, b, 1.4) 43 | } 44 | -------------------------------------------------------------------------------- /num/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package num 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /num/t_nlsolverconfig_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package num 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func TestNlsConfig01(tst *testing.T) { 14 | 15 | // verbose() 16 | chk.PrintTitle("NlsConfig01") 17 | 18 | c := NewNlSolverConfig() 19 | 20 | // flags 21 | res := []bool{ 22 | c.Verbose, 23 | c.ConstantJacobian, 24 | c.LineSearch, 25 | c.EnforceConvRate, 26 | c.useDenseSolver, 27 | c.hasJacobianFunction, 28 | c.LinSolConfig.Verbose, 29 | } 30 | correct := []bool{false, false, false, false, false, false, false} 31 | chk.Bools(tst, "flags", res, correct) 32 | 33 | // tolerances 34 | chk.Float64(tst, "atol", 1e-15, c.atol, 1e-8) 35 | chk.Float64(tst, "rtol", 1e-15, c.rtol, 1e-8) 36 | chk.Float64(tst, "ftol", 1e-15, c.ftol, 1e-9) 37 | chk.Float64(tst, "fnewt", 1e-15, c.fnewt, 0.0001) 38 | } 39 | -------------------------------------------------------------------------------- /num/t_quadElem_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | package num 5 | 6 | import ( 7 | "math" 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func Test_QuadElem01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("QuadElem01. Trapz and Simpson Elementary") 18 | 19 | y := func(x float64) (res float64) { 20 | res = math.Sqrt(1.0 + math.Pow(math.Sin(x), 3.0)) 21 | return 22 | } 23 | Acor := 1.08268158558 24 | 25 | // trapezoidal rule 26 | var T QuadElementary 27 | T = new(ElementaryTrapz) 28 | T.Init(y, 0, 1, 1e-11) 29 | A := T.Integrate() 30 | io.Pforan("A = %v\n", A) 31 | chk.Float64(tst, "A", 1e-11, A, Acor) 32 | 33 | // Simpson's rule 34 | var S QuadElementary 35 | S = new(ElementarySimpson) 36 | S.Init(y, 0, 1, 1e-11) 37 | A = S.Integrate() 38 | io.Pforan("A = %v\n", A) 39 | chk.Float64(tst, "A", 1e-11, A, Acor) 40 | } 41 | -------------------------------------------------------------------------------- /num/t_quadGauss_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package num 6 | 7 | import ( 8 | "math" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/chk" 12 | "github.com/cpmech/gosl/io" 13 | ) 14 | 15 | func Test_quadGaussL01(tst *testing.T) { 16 | 17 | //verbose() 18 | chk.PrintTitle("quadGaussL01. Gauss-Legendre quadrature.") 19 | 20 | y := func(x float64) (res float64) { 21 | res = math.Sqrt(1.0 + math.Pow(math.Sin(x), 3.0)) 22 | return 23 | } 24 | Acor := 1.08268158558 25 | 26 | A := QuadGaussL10(0, 1, y) 27 | io.Pforan("A = %v\n", A) 28 | chk.Float64(tst, "A", 1e-12, A, Acor) 29 | } 30 | 31 | func Test_gaussLegXW01(tst *testing.T) { 32 | 33 | //verbose() 34 | chk.PrintTitle("gaussLegXW01. Gauss-Legendre x-w data.") 35 | 36 | // constants 37 | xRef := []float64{-0.9739065285171717, -0.8650633666889845, -0.6794095682990244, -0.4333953941292472, -0.1488743389816312, 0.1488743389816312, 0.4333953941292472, 0.6794095682990244, 0.8650633666889845, 0.9739065285171717} 38 | wRef := []float64{0.0666713443086881, 0.1494513491505806, 0.2190863625159821, 0.2692667193099963, 0.2955242247147529, 0.2955242247147529, 0.2692667193099963, 0.2190863625159821, 0.1494513491505806, 0.0666713443086881} 39 | 40 | xL, wL := GaussLegendreXW(-1, 1, 10) 41 | chk.Array(tst, "xL", 1e-15, xL, xRef) 42 | chk.Array(tst, "wL", 1e-15, wL, wRef) 43 | 44 | xJ, wJ := GaussJacobiXW(0, 0, 10) 45 | chk.Array(tst, "xJ", 1e-15, xJ, xRef) 46 | chk.Array(tst, "wJ", 1e-14, wJ, wRef) 47 | } 48 | -------------------------------------------------------------------------------- /num/t_quadrature_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package num 6 | 7 | import ( 8 | "math" 9 | "math/cmplx" 10 | "testing" 11 | 12 | "github.com/cpmech/gosl/chk" 13 | "github.com/cpmech/gosl/io" 14 | ) 15 | 16 | func TestQuadGen01(tst *testing.T) { 17 | 18 | //verbose() 19 | chk.PrintTitle("QuadGen01. using QUADPACK general function") 20 | 21 | f := func(x float64) float64 { return math.Sqrt(1.0 + math.Pow(math.Sin(x), 3.0)) } 22 | A := QuadGen(0, 1, 0, f) 23 | io.Pforan("A = %v\n", A) 24 | chk.Float64(tst, "A", 1e-12, A, 1.08268158558) 25 | } 26 | 27 | func TestQuadCs01(tst *testing.T) { 28 | 29 | //verbose() 30 | chk.PrintTitle("QuadCs01. using QUADPACK oscillatory function") 31 | 32 | ω := math.Pow(2.0, 3.4) 33 | f := func(x float64) float64 { return math.Exp(20.0 * (x - 1)) } 34 | A := QuadCs(0, 1, ω, true, 0, f) 35 | io.Pforan("A = %v\n", A) 36 | Aref := (20*math.Sin(ω) - ω*math.Cos(ω) + ω*math.Exp(-20)) / (math.Pow(20, 2) + math.Pow(ω, 2)) 37 | chk.Float64(tst, "A", 1e-16, A, Aref) 38 | } 39 | 40 | func TestQuadExpIx01(tst *testing.T) { 41 | 42 | //verbose() 43 | chk.PrintTitle("QuadExpIx01. ∫ x²⋅exp(i⋅m⋅x) dx using QUADPACK") 44 | 45 | f := func(x float64) float64 { return x * x } 46 | π := math.Pi 47 | a := 0.0 48 | b := 2.0 * π 49 | m := 4.0 50 | 51 | I := QuadExpIx(a, b, m, 0, f) 52 | 53 | ee := cmplx.Exp(complex(0, 2*π*m)) 54 | π2 := complex(π*π, 0) 55 | m2 := complex(m*m, 0) 56 | m3 := complex(m*m*m, 0) 57 | mπ4 := complex(4*π*m, 0) 58 | Iana := (2i+mπ4-4i*π2*m2)*ee/m3 - 2i/m3 59 | 60 | chk.AnaNumC(tst, "I", 1e-14, I, Iana, chk.Verbose) 61 | } 62 | 63 | func TestQuadExpIx02(tst *testing.T) { 64 | 65 | //verbose() 66 | chk.PrintTitle("QuadExpIx02. ∫ [p⋅cos(x)+q⋅sin(x)]⋅exp(i⋅m⋅x) dx") 67 | 68 | p := 2.0 69 | q := 3.0 70 | f := func(x float64) float64 { return p*math.Cos(x) + q*math.Sin(x) } 71 | π := math.Pi 72 | a := 0.0 73 | b := 2.0 * π 74 | m := 0.5 75 | 76 | I := QuadExpIx(a, b, m, 0, f) 77 | 78 | ee := cmplx.Exp(complex(0, 2*π*m)) 79 | Q := complex(q, 0) 80 | d := complex(m*m-1, 0) 81 | pmi := complex(0, p*m) 82 | Iana := (ee*Q-pmi*ee)/d - (Q-pmi)/d 83 | 84 | chk.AnaNumC(tst, "I", 1e-15, I, Iana, chk.Verbose) 85 | } 86 | -------------------------------------------------------------------------------- /ode/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. ode. Ordinary differential equations 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/ode.svg)](https://pkg.go.dev/github.com/cpmech/gosl/ode) 4 | 5 | Package `ode` implements solution techniques to ordinary differential equations, such as the 6 | Runge-Kutta method. Methods that can handle stiff problems are also available. 7 | 8 | ## Examples 9 | 10 | ### Robertson's Equation 11 | 12 | From Hairer-Wanner VII-p3 Eq.(1.4) [2]. 13 | 14 | Solution of Robertson's equation: 15 | 16 | ![](data/rober.png) 17 | 18 | ## Output of Tests 19 | 20 | ### Convergence of explicit Runge-Kutta methods 21 | 22 | [source code](t_erk_test.go) 23 | 24 | ![](data/t_erk04.png) 25 | 26 | ![](data/t_erk05.png) 27 | 28 | ## References 29 | 30 | [1] Hairer E, Norset SP, Wanner G. Solving O. Solving Ordinary Differential Equations I. Nonstiff 31 | Problems, Springer. 1987 32 | 33 | [2] Hairer E, Wanner G. Solving Ordinary Differential Equations II. Stiff and Differential-Algebraic 34 | Problems, Second Revision Edition. Springer. 1996 35 | 36 | ## API 37 | 38 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/ode) 39 | -------------------------------------------------------------------------------- /ode/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. ode. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /ode/data/dr1_radau5.cmp: -------------------------------------------------------------------------------- 1 | { 2 | "s":[0, 10, 11, 13, 22, 123, 124, 126, 133, 237, 238], 3 | "x":[0.00, 0.20, 0.40, 0.60, 0.80, 1.00, 1.20, 1.40, 1.60, 1.80, 2.00], 4 | "y":[ 5 | [2.000000000000000E+00, 1.858198963529348E+00, 1.693205231356631E+00, 1.484565763233088E+00, 1.083912894828041E+00, -1.863645642467316E+00, -1.699724325407345E+00, -1.493375073281585E+00, -1.120780440545200E+00, 1.869050586422796E+00, 1.706161100558755E+00], 6 | [-6.600000000000000E-01, -7.574791034460339E-01, -9.069021616884126E-01, -1.233096964774310E+00, -6.196077501342372E+00, 7.535323766548111E-01, 8.997692747327533E-01, 1.213880891197743E+00, 4.374794948980838E+00, -7.495753072329912E-01, -8.928074776823989E-01] 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /ode/data/dr2_dopri5_dense.txt: -------------------------------------------------------------------------------- 1 | istep h x y0 y1 2 | 0 1.0000000E-04 0.0000000 2.0000000E+00 0.0000000E+00 3 | 48 3.6279151E-05 0.0200000 1.9868189E+00 -6.7395077E-01 4 | 67 6.0797539E-05 0.0400000 1.9732622E+00 -6.8176416E-01 5 | 85 9.0198101E-04 0.0600000 1.9595463E+00 -6.8987886E-01 6 | 104 4.2648347E-04 0.0800000 1.9456649E+00 -6.9831496E-01 7 | 122 7.8595434E-04 0.1000000 1.9316114E+00 -7.0709511E-01 8 | 140 9.0621653E-04 0.1200000 1.9173787E+00 -7.1624350E-01 9 | 158 7.8482417E-04 0.1400000 1.9029590E+00 -7.2578703E-01 10 | 176 4.2000134E-04 0.1600000 1.8883444E+00 -7.3575568E-01 11 | 193 9.6212271E-04 0.1800000 1.8735258E+00 -7.4618282E-01 12 | 211 1.2187942E-04 0.2000000 1.8584937E+00 -7.5710495E-01 13 | -------------------------------------------------------------------------------- /ode/data/dr_dop853_dense.txt: -------------------------------------------------------------------------------- 1 | s h x y0 y1 2 | 0 1.0000000E-04 0.0000000 2.0000000E+00 0.0000000E+00 3 | 28 1.4687821E-03 0.0200000 1.9868189E+00 -6.7395067E-01 4 | 41 6.5886794E-04 0.0400000 1.9732622E+00 -6.8176408E-01 5 | 53 1.0657463E-03 0.0600000 1.9595463E+00 -6.8987877E-01 6 | 65 1.0752322E-03 0.0800000 1.9456649E+00 -6.9831503E-01 7 | 77 6.5285211E-04 0.1000000 1.9316114E+00 -7.0709510E-01 8 | 88 1.5142717E-03 0.1200000 1.9173787E+00 -7.1624339E-01 9 | 99 1.0048542E-03 0.1400000 1.9029590E+00 -7.2578707E-01 10 | 110 7.8231442E-05 0.1600000 1.8883444E+00 -7.3575574E-01 11 | 120 7.6012877E-04 0.1800000 1.8735258E+00 -7.4618276E-01 12 | 130 1.1524901E-03 0.2000000 1.8584937E+00 -7.5710487E-01 13 | -------------------------------------------------------------------------------- /ode/data/dr_dopri5_dense.txt: -------------------------------------------------------------------------------- 1 | s h x y0 y1 y2 y3 2 | 0 1.0000000E-04 0.000 9.9400000E-01 0.0000000E+00 0.0000000E+00 -2.0015851E+00 3 | 62 1.0084468E-01 2.000 -5.7987805E-01 6.0907757E-01 -4.2253007E-01 2.4422202E-01 4 | 75 1.7455232E-01 4.000 -1.9833352E-01 1.1376380E+00 4.4865247E-01 -6.6885700E-02 5 | 93 6.1730909E-02 6.000 -4.7357443E-01 2.2390680E-01 -5.6094886E-01 -9.8613720E-01 6 | 112 1.6368066E-01 8.000 -1.1745533E+00 -2.7594670E-01 -2.5317135E-01 4.4737686E-01 7 | 124 1.3880900E-01 10.000 -8.3980736E-01 4.4683009E-01 3.7374292E-01 -1.4967003E-01 8 | 147 1.0422334E-01 12.000 1.3147198E-02 -8.3857520E-01 1.7527570E-01 -4.3586688E-01 9 | 160 1.7984461E-01 14.000 -6.0311279E-01 -9.9125982E-01 -3.1049607E-01 3.4418998E-01 10 | 179 5.3927999E-02 16.000 2.4271147E-01 -3.8999470E-01 1.1188140E+00 6.0958821E-01 11 | 217 4.3950161E-04 17.065 9.9400217E-01 9.0408910E-06 1.4597583E-03 -2.0012455E+00 12 | -------------------------------------------------------------------------------- /ode/data/log_mpi.txt: -------------------------------------------------------------------------------- 1 | 2 | ========================= Test ODE 02b ========================= 3 | Hairer-Wanner VII-p5 Eq.(1.5) Van der Pol's Equation (MPI) 4 | number of F evaluations = 2233 5 | number of J evaluations = 160 6 | total number of steps = 280 7 | number of accepted steps = 241 8 | number of rejected steps = 7 9 | number of decompositions = 251 10 | number of lin solutions = 663 11 | max number of iterations = 6 12 | PASS 13 | elapsed time = 178.086209ms 14 | PASS 15 | ok code.google.com/p/gosl/ode 0.225s 16 | ok code.google.com/p/gosl/ode 0.236s 17 | 18 | ========================= Test ODE 04b (MPI) ========================= 19 | Hairer-Wanner VII-p376 Transistor Amplifier (MPI) 20 | (from E Hairer's website, not the system in the book) 21 | number of F evaluations = 2609 22 | number of J evaluations = 215 23 | total number of steps = 278 24 | number of accepted steps = 221 25 | number of rejected steps = 18 26 | number of decompositions = 276 27 | number of lin solutions = 795 28 | max number of iterations = 5 29 | elapsed time = 373.478052ms 30 | PASS 31 | PASS 32 | PASS 33 | ok code.google.com/p/gosl/ode 1.421s 34 | ok code.google.com/p/gosl/ode 1.462s 35 | ok code.google.com/p/gosl/ode 1.449s 36 | -------------------------------------------------------------------------------- /ode/data/log_numjac_mpi.txt: -------------------------------------------------------------------------------- 1 | 2 | ========================= Test ODE 02b ========================= 3 | Hairer-Wanner VII-p5 Eq.(1.5) Van der Pol's Equation (MPI) 4 | number of F evaluations = 2233 5 | number of J evaluations = 160 6 | total number of steps = 280 7 | number of accepted steps = 241 8 | number of rejected steps = 7 9 | number of decompositions = 251 10 | number of lin solutions = 663 11 | max number of iterations = 6 12 | elapsed time = 182.328788ms 13 | PASS 14 | PASS 15 | ok code.google.com/p/gosl/ode 0.220s 16 | ok code.google.com/p/gosl/ode 0.233s 17 | 18 | ========================= Test ODE 04b (MPI) ========================= 19 | Hairer-Wanner VII-p376 Transistor Amplifier (MPI) 20 | (from E Hairer's website, not the system in the book) 21 | number of F evaluations = 2633 22 | number of J evaluations = 215 23 | total number of steps = 281 24 | number of accepted steps = 221 25 | number of rejected steps = 21 26 | number of decompositions = 280 27 | number of lin solutions = 803 28 | max number of iterations = 6 29 | PASS 30 | PASS 31 | elapsed time = 263.141082ms 32 | PASS 33 | ok code.google.com/p/gosl/ode 0.306s 34 | ok code.google.com/p/gosl/ode 0.307s 35 | ok code.google.com/p/gosl/ode 0.306s 36 | -------------------------------------------------------------------------------- /ode/data/output_testode01.txt: -------------------------------------------------------------------------------- 1 | 2 | ============================== Test ODE 01 ============================ 3 | 4 | number of F evaluations = 77 5 | number of J evaluations = 1 6 | total number of steps = 17 7 | number of accepted steps = 17 8 | number of rejected steps = 0 9 | number of decompositions = 42 10 | number of lin solutions = 20 11 | max number of iterations = 2 12 | 13 | [0.1 0.01 0.001 0.0001 1e-05 1e-06 1e-07 1e-08 1e-09 1e-10 1e-11 1e-12 1e-13] 14 | tol = 1.000000e-01 => err = 1.893569e-04 => feval = 35 15 | tol = 1.000000e-02 => err = 4.708445e-05 => feval = 46 16 | tol = 1.000000e-03 => err = 1.292159e-05 => feval = 54 17 | tol = 1.000000e-04 => err = 8.496873e-06 => feval = 77 18 | tol = 1.000000e-05 => err = 1.325075e-06 => feval = 126 19 | tol = 1.000000e-06 => err = 1.236459e-07 => feval = 212 20 | tol = 1.000000e-07 => err = 8.849459e-09 => feval = 379 21 | tol = 1.000000e-08 => err = 3.832851e-10 => feval = 709 22 | tol = 1.000000e-09 => err = 1.423266e-11 => feval = 1362 23 | tol = 1.000000e-10 => err = 5.523179e-13 => feval = 2780 24 | tol = 1.000000e-11 => err = 3.417077e-14 => feval = 4667 25 | tol = 1.000000e-12 => err = 2.051160e-15 => feval = 8150 26 | tol = 1.000000e-13 => err = 8.886621e-16 => feval = 14308 27 | 28 | ============================== Test ODE 02 ============================ 29 | 30 | number of F evaluations = 3862 31 | number of J evaluations = 206 32 | total number of steps = 366 33 | number of accepted steps = 304 34 | number of rejected steps = 5 35 | number of decompositions = 972 36 | number of lin solutions = 1185 37 | max number of iterations = 6 38 | 39 | ============================== Test ODE 03 ============================ 40 | 41 | number of F evaluations = 123 42 | number of J evaluations = 9 43 | total number of steps = 18 44 | number of accepted steps = 15 45 | number of rejected steps = 0 46 | number of decompositions = 54 47 | number of lin solutions = 36 48 | max number of iterations = 3 49 | PASS 50 | ok ode 5.280s 51 | -------------------------------------------------------------------------------- /ode/data/rober.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/ode/data/rober.png -------------------------------------------------------------------------------- /ode/data/rober_radau5_cpp.dat: -------------------------------------------------------------------------------- 1 | steps x y0 y1 y2 2 | 1 0.000000000000000E+00 1.000000000000000E+00 0.000000000000000E+00 0.000000000000000E+00 3 | 2 1.000000000000000E-06 9.999999600000008E-01 3.999998320000048E-08 1.599999952000000E-14 4 | 3 9.000000000000000E-06 9.999996400000648E-01 3.599882716565646E-07 1.166354336752796E-11 5 | 4 7.300000000000000E-05 9.999970800042659E-01 2.913787382334434E-06 6.208351820680937E-09 6 | 5 3.421302105638213E-04 9.999863148909263E-01 1.307919521098008E-05 6.059138628105538E-07 7 | 6 6.112604211276427E-04 9.999755499712033E-01 2.135889538415425E-05 3.091133412644609E-06 8 | 7 8.803906316914641E-04 9.999647854413735E-01 2.724901208916896E-05 7.965546537469531E-06 9 | 8 1.303954727067043E-03 9.999478453625211E-01 3.255484201352816E-05 1.959979546541985E-05 10 | 9 1.866332914339023E-03 9.999253573022412E-01 3.530902486682289E-05 3.933367289202561E-05 11 | 10 2.428711101611004E-03 9.999028742076045E-01 3.614958793293449E-05 6.097620446265629E-05 12 | 11 3.774575940355747E-03 9.998490892191750E-01 3.647645985151893E-05 1.144343209735185E-04 13 | 12 6.085901688952521E-03 9.997567898163745E-01 3.647881802650698E-05 2.067313655991122E-04 14 | 13 1.246960885666033E-02 9.995023134527544E-01 3.643227719665359E-05 4.612542700490673E-04 15 | 14 4.516601336384846E-02 9.982091577136206E-01 3.619191783743626E-05 1.754650368542118E-03 16 | 15 1.800431604600223E-01 9.930472117084378E-01 3.525604364520644E-05 6.917532247917142E-03 17 | 16 3.000000000000000E-01 9.886740138499884E-01 3.447720471782071E-05 1.129150894529390E-02 18 | -------------------------------------------------------------------------------- /ode/data/t_erk04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/ode/data/t_erk04.png -------------------------------------------------------------------------------- /ode/data/t_erk05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/ode/data/t_erk05.png -------------------------------------------------------------------------------- /ode/fweuler.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ode 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/la" 10 | ) 11 | 12 | // FwEuler implements the (explicit) Forward Euler method 13 | type FwEuler struct { 14 | ndim int // problem dimension 15 | conf *Config // configurations 16 | work *rkwork // workspace 17 | stat *Stat // statistics 18 | fcn Func // dy/dx := f(x,y) 19 | } 20 | 21 | // add method to database 22 | func init() { 23 | rkmDB["fweuler"] = func() rkmethod { return new(FwEuler) } 24 | } 25 | 26 | // Free releases memory 27 | func (o *FwEuler) Free() {} 28 | 29 | // Info returns information about this method 30 | func (o *FwEuler) Info() (fixedOnly, implicit bool, nstages int) { 31 | return true, false, 1 32 | } 33 | 34 | // Init initializes structure 35 | func (o *FwEuler) Init(ndim int, conf *Config, work *rkwork, stat *Stat, fcn Func, jac JacF, M *la.Triplet) { 36 | if M != nil { 37 | chk.Panic("Forward-Euler solver cannot handle M matrix yet\n") 38 | } 39 | o.ndim = ndim 40 | o.conf = conf 41 | o.work = work 42 | o.stat = stat 43 | o.fcn = fcn 44 | } 45 | 46 | // Accept accepts update 47 | func (o *FwEuler) Accept(y0 la.Vector, x0 float64) (dxnew float64) { 48 | return 49 | } 50 | 51 | // Reject processes step rejection 52 | func (o *FwEuler) Reject() (dxnew float64) { 53 | return 54 | } 55 | 56 | // DenseOut produces dense output (after Accept) 57 | func (o *FwEuler) DenseOut(yout la.Vector, h, x float64, y la.Vector, xout float64) { 58 | chk.Panic("TODO") 59 | } 60 | 61 | // Step steps update 62 | func (o *FwEuler) Step(x0 float64, y0 la.Vector) { 63 | o.stat.Nfeval++ 64 | o.fcn(o.work.f[0], o.work.h, x0, y0) 65 | for i := 0; i < o.ndim; i++ { 66 | y0[i] += o.work.h * o.work.f[0][i] 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /ode/methods.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ode 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/la" 10 | ) 11 | 12 | // rkmethod defines the required functions of Runge-Kutta method 13 | type rkmethod interface { 14 | Free() // free memory 15 | Info() (fixedOnly, implicit bool, nstages int) // information 16 | Init(ndim int, conf *Config, work *rkwork, stat *Stat, fcn Func, jac JacF, M *la.Triplet) // initialize 17 | Accept(y0 la.Vector, x0 float64) (dxnew float64) // accept update (must compute rerr) 18 | Reject() (dxnew float64) // process step rejection (must compute rerr) 19 | DenseOut(yout la.Vector, h, x float64, y la.Vector, xout float64) // dense output (after Accept) 20 | Step(x0 float64, y0 la.Vector) // step update 21 | } 22 | 23 | // rkmMaker defines a function that makes rkmethods 24 | type rkmMaker func() rkmethod 25 | 26 | // rkmDB implements a database of rkmethod makers 27 | var rkmDB = make(map[string]rkmMaker) 28 | 29 | // newRKmethod finds a rkmethod in database or panic 30 | func newRKmethod(kind string) rkmethod { 31 | if maker, ok := rkmDB[kind]; ok { 32 | return maker() 33 | } 34 | chk.Panic("cannot find rkmethod named %q in database\n", kind) 35 | return nil 36 | } 37 | -------------------------------------------------------------------------------- /ode/rkwork.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ode 6 | 7 | import "github.com/cpmech/gosl/la" 8 | 9 | // rkwork holds the Runge-Kutta "workspace" variables 10 | type rkwork struct { 11 | 12 | // workspace 13 | nstg int // number of stages 14 | ndim int // dimension of y vector 15 | u la.Vector // u[stg] = x + h*c[stg] 16 | v []la.Vector // v[stg][dim] = ya[dim] + h*sum(a[stg][j]*f[j][dim], j, nstg) 17 | f []la.Vector // f[stg][dim] = f(u[stg], v[stg][dim]) 18 | 19 | // step data 20 | rs float64 // stiffness ratio ρ = ‖ k[s] - k[s-1] ‖ / ‖ v[s] - v[s-1] ‖ 21 | h float64 // current stepsize 22 | hPrev float64 // previous stepsize 23 | first bool // first step 24 | f0 la.Vector // f(x,y) before step 25 | scal la.Vector // scal = Atol + Rtol*abs(y) 26 | 27 | // step control data 28 | reuseJacAndDecOnce bool // reuse current Jacobian and current decomposition 29 | reuseJacOnce bool // reuse last Jacobian (only) 30 | jacIsOK bool // Jacobian is OK 31 | nit int // current number of iterations 32 | eta float64 // eta tolerance 33 | theta float64 // theta variable 34 | dvfac float64 // divergence factor 35 | diverg bool // flag diverging step 36 | reject bool // reject step 37 | 38 | // error control 39 | rerr float64 // relative error 40 | rerrPrev float64 // previous relative error 41 | 42 | // stiffness detection 43 | stiffYes int // counter of "stiff" steps 44 | stiffNot int // counter of not "stiff" steps 45 | } 46 | 47 | // newRKwork returns a new structure 48 | func newRKwork(nstg, ndim int) (o *rkwork) { 49 | 50 | // workspace 51 | o = new(rkwork) 52 | o.nstg = nstg 53 | o.ndim = ndim 54 | o.u = la.NewVector(o.nstg) 55 | o.v = make([]la.Vector, o.nstg) 56 | o.f = make([]la.Vector, o.nstg) 57 | for i := 0; i < o.nstg; i++ { 58 | o.v[i] = la.NewVector(o.ndim) 59 | o.f[i] = la.NewVector(o.ndim) 60 | } 61 | 62 | // step data 63 | o.f0 = la.NewVector(o.ndim) 64 | o.scal = la.NewVector(o.ndim) 65 | return 66 | } 67 | -------------------------------------------------------------------------------- /ode/t_fweuler_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ode 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func TestFwEuler01(tst *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("FwEuler01. Forward-Euler") 17 | 18 | // problem 19 | p := ProbHwEq11() 20 | 21 | // configuration 22 | conf := NewConfig("fweuler", "") 23 | conf.SetFixedH(p.Dx, p.Xf) 24 | conf.SetStepOut(true, nil) 25 | 26 | // solver 27 | sol := NewSolver(p.Ndim, conf, p.Fcn, p.Jac, nil) 28 | defer sol.Free() 29 | 30 | // solve ODE 31 | sol.Solve(p.Y, 0.0, p.Xf) 32 | 33 | // check Stat 34 | chk.Int(tst, "number of F evaluations ", sol.Stat.Nfeval, 40) 35 | chk.Int(tst, "number of J evaluations ", sol.Stat.Njeval, 0) 36 | chk.Int(tst, "total number of steps ", sol.Stat.Nsteps, 40) 37 | chk.Int(tst, "number of accepted steps", sol.Stat.Naccepted, 0) 38 | chk.Int(tst, "number of rejected steps", sol.Stat.Nrejected, 0) 39 | chk.Int(tst, "number of decompositions", sol.Stat.Ndecomp, 0) 40 | chk.Int(tst, "number of lin solutions ", sol.Stat.Nlinsol, 0) 41 | chk.Int(tst, "max number of iterations", sol.Stat.Nitmax, 0) 42 | 43 | // check results 44 | chk.Float64(tst, "yFin", 0.004753, p.Y[0], p.CalcYana(0, p.Xf)) 45 | } 46 | -------------------------------------------------------------------------------- /ode/t_highlevel_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ode 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | "github.com/cpmech/gosl/la" 13 | ) 14 | 15 | func TestHL01(tst *testing.T) { 16 | 17 | //verbose() 18 | chk.PrintTitle("HL01. Van der Pol with 1 cycle/stationary point") 19 | 20 | // constants 21 | T := 6.6632868593231301896996820305 22 | A := 2.00861986087484313650940188 23 | y := la.NewVectorSlice([]float64{A, 0}) 24 | xf := T 25 | dx := 0.1 26 | 27 | // function 28 | fcn := func(f la.Vector, dx, x float64, y la.Vector) { 29 | f[0] = y[1] 30 | f[1] = (1.0-y[0]*y[0])*y[1] - y[0] 31 | } 32 | 33 | // solve 34 | yf := y.GetCopy() 35 | atol, rtol := 1e-5, 1e-5 36 | numJac, fixedStep, saveStep, saveCont := false, false, true, false 37 | stat, _ := Solve("dopri5", fcn, nil, yf, xf, dx, atol, rtol, numJac, fixedStep, saveStep, saveCont) 38 | 39 | // results 40 | io.Pf("yf = %v\n", yf) 41 | stat.Print(false) 42 | 43 | // check 44 | chk.AnaNum(tst, "dopri5: y0", 1e-4, yf[0], y[0], chk.Verbose) 45 | chk.AnaNum(tst, "dopri5: y1", 1e-4, yf[1], y[1], chk.Verbose) 46 | 47 | // using simple version 48 | yf2 := y.GetCopy() 49 | Dopri5simple(fcn, yf2, xf, atol) 50 | chk.Array(tst, "dopri5: yf2", 1e-17, yf, yf2) 51 | 52 | // dopri8 53 | yf3 := y.GetCopy() 54 | Dopri8simple(fcn, yf3, xf, atol) 55 | chk.AnaNum(tst, "dopri8: y0", 1e-7, yf3[0], y[0], chk.Verbose) 56 | chk.AnaNum(tst, "dopri8: y1", 1e-5, yf3[1], y[1], chk.Verbose) 57 | 58 | // radau5 59 | yf4 := y.GetCopy() 60 | Radau5simple(fcn, nil, yf4, xf, atol) 61 | chk.AnaNum(tst, "radau5: y0", 1e-6, yf4[0], y[0], chk.Verbose) 62 | chk.AnaNum(tst, "radau5: y1", 1e-5, yf4[1], y[1], chk.Verbose) 63 | } 64 | -------------------------------------------------------------------------------- /ode/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ode 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /ode/t_moeuler_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package ode 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func TestMoEuler01(tst *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("MoEuler01. Modified-Euler") 17 | 18 | // problem 19 | p := ProbHwEq11() 20 | 21 | // configuration 22 | conf := NewConfig("moeuler", "") 23 | conf.SetStepOut(true, nil) 24 | 25 | // solver 26 | sol := NewSolver(p.Ndim, conf, p.Fcn, p.Jac, nil) 27 | defer sol.Free() 28 | 29 | // solve ODE 30 | sol.Solve(p.Y, 0.0, p.Xf) 31 | 32 | // check Stat 33 | chk.Int(tst, "number of F evaluations ", sol.Stat.Nfeval, 425) // 192 34 | chk.Int(tst, "number of J evaluations ", sol.Stat.Njeval, 0) 35 | chk.Int(tst, "total number of steps ", sol.Stat.Nsteps, 212) // 190 36 | chk.Int(tst, "number of accepted steps", sol.Stat.Naccepted, 212) 37 | chk.Int(tst, "number of rejected steps", sol.Stat.Nrejected, 0) 38 | chk.Int(tst, "number of decompositions", sol.Stat.Ndecomp, 0) 39 | chk.Int(tst, "number of lin solutions ", sol.Stat.Nlinsol, 0) 40 | chk.Int(tst, "max number of iterations", sol.Stat.Nitmax, 0) 41 | 42 | // check results 43 | chk.Float64(tst, "yFin", 4.294973673e-5, p.Y[0], p.CalcYana(0, p.Xf)) 44 | } 45 | -------------------------------------------------------------------------------- /opt/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. opt. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /opt/data/opt_comparison01a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/opt/data/opt_comparison01a.png -------------------------------------------------------------------------------- /opt/data/opt_comparison01b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/opt/data/opt_comparison01b.png -------------------------------------------------------------------------------- /opt/data/opt_conjgrad01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/opt/data/opt_conjgrad01.png -------------------------------------------------------------------------------- /opt/data/opt_ipm01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/opt/data/opt_ipm01.png -------------------------------------------------------------------------------- /opt/data/opt_ipm02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/opt/data/opt_ipm02.png -------------------------------------------------------------------------------- /opt/data/x_cvxopt.py: -------------------------------------------------------------------------------- 1 | # change this in from line # 922 of coneprog.py in /usr/lib/python2.7/dist-packages/cvxopt 2 | # if iters == 0: 3 | # print("%2s%16s%16s%10s%10s%10s%16s" %("it","pcost", "dcost", "gap", "pres", "dres", "k/t")) 4 | # print("%2d%16.8e%16.8e%10.3e%10.3e%10.3e%16.8e" %(iters, pcost, dcost, gap, pres, dres, kappa/tau)) 5 | from cvxopt.modeling import op 6 | lp = op() 7 | #lp.fromfile('afiro.mps') 8 | #lp.fromfile('adlittle.mps') 9 | lp.fromfile('share1b.mps') 10 | lp.solve() 11 | res = {} 12 | for i, v in enumerate(lp.variables()): 13 | #print v.value[0] 14 | res[v.name] = v.value[0] 15 | -------------------------------------------------------------------------------- /opt/nonlinsolver.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "strings" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/la" 12 | "github.com/cpmech/gosl/utl" 13 | ) 14 | 15 | // NonLinSolver solves (unconstrained) nonlinear optimization problems 16 | type NonLinSolver interface { 17 | Min(x la.Vector, params utl.Params) (fmin float64) // computes minimum and updates x @ min 18 | SetConvParams(maxIt int, ftol, gtol float64) // SetConvParams sets convergence parameters 19 | SetUseHistory(useHist bool) // SetUseHist sets use history parameter 20 | SetVerbose(verbose bool) // SetVerbose sets verbose mode 21 | AccessHistory() *History // get access to history 22 | } 23 | 24 | // nlsMaker defines a function that makes non-linear-solvers 25 | type nlsMaker func(prob *Problem) NonLinSolver 26 | 27 | // nlsMakersDB implements a database of non-linear-solver makers 28 | var nlsMakersDB = make(map[string]nlsMaker) 29 | 30 | // GetNonLinSolver finds a non-linear-solver in database or panic 31 | // kind -- e.g. conjgrad, powel, graddesc 32 | func GetNonLinSolver(kind string, prob *Problem) NonLinSolver { 33 | strKind := strings.ToLower(kind) 34 | if maker, ok := nlsMakersDB[strKind]; ok { 35 | return maker(prob) 36 | } 37 | chk.Panic("cannot find NonLinSolver named %q in database\n", kind) 38 | return nil 39 | } 40 | -------------------------------------------------------------------------------- /opt/problem.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "github.com/cpmech/gosl/fun" 9 | "github.com/cpmech/gosl/la" 10 | ) 11 | 12 | // Problem holds the functions defining an optimization problem 13 | type Problem struct { 14 | Ndim int // dimension of x == len(x) 15 | Ffcn fun.Sv // objective function f({x}) 16 | Gfcn fun.Vv // gradient function df/d{x}|(x) 17 | Hfcn fun.Mv // Hessian function d²f/d{x}d{x}|(x) 18 | Fref float64 // known solution fmin = f({x}) 19 | Xref la.Vector // known solution {x} @ min 20 | } 21 | 22 | // NewQuadraticProblem returns a quadratic optimization problem such that f(x) = xᵀ A x 23 | func NewQuadraticProblem(Amat [][]float64) (p *Problem) { 24 | 25 | // new problem 26 | p = new(Problem) 27 | p.Ndim = len(Amat) 28 | 29 | // objective function f({x}) 30 | A := la.NewMatrixDeep2(Amat) 31 | tmp := la.NewVector(A.M) 32 | p.Ffcn = func(x la.Vector) float64 { 33 | la.MatVecMul(tmp, 1, A, x) 34 | return la.VecDot(x, tmp) // xᵀ A x 35 | } 36 | 37 | // gradient function df/d{x}|(x) 38 | At := A.GetTranspose() 39 | AtPlusA := la.NewMatrix(A.M, A.M) 40 | la.MatAdd(AtPlusA, 1, At, 1, A) 41 | p.Gfcn = func(g, x la.Vector) { 42 | la.MatVecMul(g, 1, AtPlusA, x) // g := (Aᵀ+A)⋅x 43 | } 44 | 45 | // Hessian function d²f/d{x}d{x}!(x) 46 | p.Hfcn = func(h *la.Matrix, x la.Vector) { 47 | AtPlusA.CopyInto(h, 1) // H := Aᵀ + A 48 | } 49 | 50 | // solution 51 | p.Fref = 0.0 52 | p.Xref = la.NewVector(A.M) // xmin := 0.0 53 | return 54 | } 55 | -------------------------------------------------------------------------------- /opt/t_factobjs_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/la" 12 | "github.com/cpmech/gosl/utl" 13 | ) 14 | 15 | func TestFactObjs01(tst *testing.T) { 16 | 17 | //verbose() 18 | chk.PrintTitle("FactObjs01. Standard Rosenbrock function") 19 | 20 | p := Factory.Rosenbrock2d(1, 100) 21 | 22 | chk.Array(tst, "xmin", 1e-15, p.Xref, utl.Ones(2)) 23 | chk.Float64(tst, "fmin", 1e-15, p.Fref, 0.0) 24 | 25 | x := la.NewVectorSlice([]float64{-3, -4}) 26 | gAna := la.NewVector(2) 27 | p.Gfcn(gAna, x) 28 | chk.DerivScaVec(tst, "Gfcn", 1.67e-7, gAna, x, 1e-3, chk.Verbose, func(xx []float64) float64 { return p.Ffcn(xx) }) 29 | } 30 | 31 | func TestFactObjs02(tst *testing.T) { 32 | 33 | //verbose() 34 | chk.PrintTitle("FactObjs02. Multi-dimensional Rosenbrock function") 35 | 36 | N := 5 37 | p := Factory.RosenbrockMulti(N) 38 | 39 | chk.Array(tst, "xmin", 1e-15, p.Xref, utl.Ones(N)) 40 | chk.Float64(tst, "fmin", 1e-15, p.Fref, 0.0) 41 | 42 | x := la.NewVectorSlice([]float64{-1, -2, -3, -4, -5}) 43 | gAna := la.NewVector(N) 44 | p.Gfcn(gAna, x) 45 | chk.DerivScaVec(tst, "Gfcn", 1e-6, gAna, x, 1e-3, chk.Verbose, func(xx []float64) float64 { return p.Ffcn(xx) }) 46 | } 47 | -------------------------------------------------------------------------------- /opt/t_graddesc_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | "github.com/cpmech/gosl/la" 13 | ) 14 | 15 | func runGradDescTest(tst *testing.T, fnkey string, p *Problem, x0 la.Vector, tolf, tolx, α float64) (sol *GradDesc) { 16 | 17 | // solve using Gradient-Descent 18 | xmin := x0.GetCopy() 19 | sol = NewGradDesc(p) 20 | sol.Alpha = α 21 | sol.UseHist = true 22 | fmin := sol.Min(xmin, nil) 23 | 24 | // check 25 | name := "GradDesc" 26 | io.Pforan("%s: NumIter = %v\n", name, sol.NumIter) 27 | io.Pf("%s: NumFeval = %v\n", name, sol.NumFeval) 28 | io.Pf("%s: NumGeval = %v\n", name, sol.NumGeval) 29 | chk.Float64(tst, io.Sf("%s: fmin", name), tolf, fmin, p.Fref) 30 | chk.Array(tst, io.Sf("%s: xmin", name), tolx, xmin, p.Xref) 31 | io.Pl() 32 | return 33 | } 34 | 35 | func TestGradDesc01(tst *testing.T) { 36 | 37 | //verbose() 38 | chk.PrintTitle("GradDesc01. Very simple bi-dimensional optimization") 39 | 40 | // problem 41 | p := Factory.SimpleParaboloid() 42 | 43 | // initial point 44 | x0 := la.NewVectorSlice([]float64{1, 1}) 45 | 46 | // run test 47 | α := 0.6 48 | runGradDescTest(tst, "graddesc01", p, x0, 1e-10, 1e-5, α) 49 | } 50 | 51 | func TestGradDesc02(tst *testing.T) { 52 | 53 | //verbose() 54 | chk.PrintTitle("GradDesc02. quadratic optimization in 2D") 55 | 56 | // problem 57 | p := Factory.SimpleQuadratic2d() 58 | 59 | // initial point 60 | x0 := la.NewVectorSlice([]float64{1.5, -0.75}) 61 | 62 | // run test 63 | α := 0.2 64 | runGradDescTest(tst, "graddesc02", p, x0, 1e-13, 1e-6, α) 65 | } 66 | 67 | func TestGradDesc03(tst *testing.T) { 68 | 69 | //verbose() 70 | chk.PrintTitle("GradDesc02. quadratic optimization in 3D") 71 | 72 | // problem 73 | p := Factory.SimpleQuadratic3d() 74 | 75 | // initial point 76 | x0 := la.NewVectorSlice([]float64{1, 2, 3}) 77 | 78 | // run test 79 | α := 0.1 80 | runGradDescTest(tst, "graddesc03", p, x0, 1e-10, 1e-5, α) 81 | } 82 | -------------------------------------------------------------------------------- /opt/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /opt/t_linesearch_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | "github.com/cpmech/gosl/la" 13 | "github.com/cpmech/gosl/utl" 14 | ) 15 | 16 | func TestLineSearch01(tst *testing.T) { 17 | 18 | //verbose() 19 | chk.PrintTitle("LineSearch01.") 20 | 21 | // function 22 | ffcn := func(x la.Vector) float64 { 23 | return x[0]*x[0] + x[1]*x[1] - 0.5 24 | } 25 | 26 | // Jacobian 27 | Jfcn := func(dfdx, x la.Vector) { 28 | dfdx[0] = 2.0 * x[0] 29 | dfdx[1] = 2.0 * x[1] 30 | } 31 | 32 | // initial point and direction 33 | x0 := la.NewVectorSlice([]float64{-2, -2}) 34 | x := x0.GetCopy() 35 | u := la.NewVectorSlice([]float64{4, 4}) 36 | 37 | // solver 38 | line := NewLineSearch(2, ffcn, Jfcn) 39 | 40 | // set params 41 | line.SetParams(utl.NewParams( 42 | &utl.P{N: "maxitls", V: 2}, 43 | &utl.P{N: "maxitzoom", V: 2}, 44 | &utl.P{N: "maxalpha", V: 100}, 45 | &utl.P{N: "mulalpha", V: 2}, 46 | &utl.P{N: "coef1", V: 1e-4}, 47 | &utl.P{N: "coef2", V: 0.4}, 48 | &utl.P{N: "coefquad", V: 0.1}, 49 | &utl.P{N: "coefcubic", V: 0.2}, 50 | )) 51 | 52 | // solve 53 | a, f := line.Wolfe(x, u, false, 0) 54 | io.Pforan("a = %v\n", a) 55 | io.Pforan("f = %v\n", f) 56 | chk.Float64(tst, "a", 1e-15, a, 0.5) 57 | chk.Float64(tst, "f", 1e-15, a, 0.5) 58 | } 59 | -------------------------------------------------------------------------------- /opt/t_nonlinsolver_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | "github.com/cpmech/gosl/la" 13 | ) 14 | 15 | func TestNLS01(tst *testing.T) { 16 | 17 | //verbose() 18 | chk.PrintTitle("NLS01. NonLinSolver") 19 | 20 | // problem and initial point 21 | p := Factory.SimpleParaboloid() 22 | x := la.NewVectorSlice([]float64{1, 1}) 23 | 24 | for _, kind := range []string{"conjgrad", "powell", "graddesc"} { 25 | io.Pf(">>>>>>>>>>>>>>>>>>> running %q <<<<<<<<<<<<<<<<<<<<\n", kind) 26 | sol := GetNonLinSolver(kind, p) 27 | fmin := sol.Min(x, nil) 28 | io.Pf("fmin = %v (%v)\n", fmin, p.Fref) 29 | chk.Float64(tst, "fmin", 1e-10, fmin, p.Fref) 30 | chk.Array(tst, "xmin", 1e-10, x, p.Xref) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /opt/t_powell_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package opt 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | "github.com/cpmech/gosl/la" 13 | ) 14 | 15 | func runPowellTest(tst *testing.T, fnkey string, p *Problem, x0 la.Vector, tolf, tolx, α float64) (sol *Powell) { 16 | 17 | // solve using Gradient-Descent 18 | xmin := x0.GetCopy() 19 | sol = NewPowell(p) 20 | sol.UseHist = true 21 | fmin := sol.Min(xmin, nil) 22 | 23 | // check 24 | name := "Powell" 25 | io.Pforan("%s: NumIter = %v\n", name, sol.NumIter) 26 | io.Pf("%s: NumFeval = %v\n", name, sol.NumFeval) 27 | chk.Float64(tst, io.Sf("%s: fmin", name), tolf, fmin, p.Fref) 28 | chk.Array(tst, io.Sf("%s: xmin", name), tolx, xmin, p.Xref) 29 | io.Pl() 30 | return 31 | } 32 | 33 | func TestPowell01(tst *testing.T) { 34 | 35 | //verbose() 36 | chk.PrintTitle("Powell01. Very simple bi-dimensional optimization") 37 | 38 | // problem 39 | p := Factory.SimpleParaboloid() 40 | 41 | // initial point 42 | x0 := la.NewVectorSlice([]float64{1, 1}) 43 | 44 | // run test 45 | α := 0.6 46 | runPowellTest(tst, "powell01", p, x0, 1e-10, 1e-5, α) 47 | } 48 | 49 | func TestPowell02(tst *testing.T) { 50 | 51 | //verbose() 52 | chk.PrintTitle("Powell02. quadratic optimization in 2D") 53 | 54 | // problem 55 | p := Factory.SimpleQuadratic2d() 56 | 57 | // initial point 58 | x0 := la.NewVectorSlice([]float64{1.5, -0.75}) 59 | 60 | // run test 61 | α := 0.2 62 | runPowellTest(tst, "powell02", p, x0, 1e-13, 1e-6, α) 63 | } 64 | 65 | func TestPowell03(tst *testing.T) { 66 | 67 | //verbose() 68 | chk.PrintTitle("Powell02. quadratic optimization in 3D") 69 | 70 | // problem 71 | p := Factory.SimpleQuadratic3d() 72 | 73 | // initial point 74 | x0 := la.NewVectorSlice([]float64{1, 2, 3}) 75 | 76 | // run test 77 | α := 0.1 78 | runPowellTest(tst, "powell03", p, x0, 1e-10, 1e-5, α) 79 | } 80 | -------------------------------------------------------------------------------- /opt/x_cvxopt.py: -------------------------------------------------------------------------------- 1 | # change this in from line # 922 of coneprog.py in /usr/lib/python2.7/dist-packages/cvxopt 2 | # if iters == 0: 3 | # print("%2s%16s%16s%10s%10s%10s%16s" %("it","pcost", "dcost", "gap", "pres", "dres", "k/t")) 4 | # print("%2d%16.8e%16.8e%10.3e%10.3e%10.3e%16.8e" %(iters, pcost, dcost, gap, pres, dres, kappa/tau)) 5 | from cvxopt import matrix, solvers, printing 6 | A = matrix([ [-1.0, -1.0, 0.0, 1.0], [1.0, -1.0, -1.0, -2.0] ]) 7 | b = matrix([ 1.0, -2.0, 0.0, 4.0 ]) 8 | c = matrix([ 2.0, 1.0 ]) 9 | sol = solvers.lp(c,A,b) 10 | printing.options['dformat']='%.15f' 11 | print sol['x'] 12 | -------------------------------------------------------------------------------- /pde/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. pde. Partial Differential Equations 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/pde.svg)](https://pkg.go.dev/github.com/cpmech/gosl/pde) 4 | 5 | Package `pde` implements structures and algorithms for solving partial differential equations. 6 | 7 | ## TODO 8 | 9 | 1. Explain how to use the FDM (Finite Difference Method) solver 10 | 2. Explain how to use the SPC (Spectral Collocation) solver 11 | 3. Implement the FEM (Finite Element Method) solver 12 | 4. Add more tests 13 | 14 | ## API 15 | 16 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/pde) 17 | -------------------------------------------------------------------------------- /pde/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package pde 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /rnd/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. rnd. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /rnd/data/all.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | Rscript normal.R 4 | Rscript lognormal.R 5 | Rscript gumbel.R 6 | Rscript frechet.R 7 | -------------------------------------------------------------------------------- /rnd/data/frechet.R: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The Gosl Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style 3 | # license that can be found in the LICENSE file. 4 | 5 | # needs r-cran-evd 6 | 7 | library(evd) 8 | 9 | X <- seq(0, 4, 0.5) 10 | L <- c(0, 0.5) # location 11 | C <- c(1, 2) # scale 12 | A <- c(1, 2, 3) # shape 13 | Y <- matrix(ncol=5) 14 | first <- TRUE 15 | for (l in L) { 16 | for (c in C) { 17 | for (a in A) { 18 | ypdf <- dfrechet(X, l, c, a) 19 | ycdf <- pfrechet(X, l, c, a) 20 | for (i in 1:length(X)) { 21 | if (first) { 22 | Y <- rbind(c(X[i], l, c, a, ypdf[i], ycdf[i])) 23 | first <- FALSE 24 | } else { 25 | Y <- rbind(Y, c(X[i], l, c, a, ypdf[i], ycdf[i])) 26 | } 27 | } 28 | } 29 | } 30 | } 31 | write.table(Y, "/tmp/gosl-rnd-frechet.dat", row.names=FALSE, col.names=c("x","l","c","a","ypdf","ycdf"), quote=FALSE) 32 | print("file written") 33 | -------------------------------------------------------------------------------- /rnd/data/gumbel.R: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The Gosl Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style 3 | # license that can be found in the LICENSE file. 4 | 5 | # needs r-cran-evd 6 | 7 | library(evd) 8 | 9 | X <- seq(-3, 3, 0.5) 10 | U <- c(0, 0.5, 1) # location 11 | B <- c(0.5, 1, 2) # scale 12 | Y <- matrix(ncol=4) 13 | first <- TRUE 14 | for (u in U) { 15 | for (b in B) { 16 | ypdf <- dgumbel(X, u, b) 17 | ycdf <- pgumbel(X, u, b) 18 | for (i in 1:length(X)) { 19 | if (first) { 20 | Y <- rbind(c(X[i], u, b, ypdf[i], ycdf[i])) 21 | first <- FALSE 22 | } else { 23 | Y <- rbind(Y, c(X[i], u, b, ypdf[i], ycdf[i])) 24 | } 25 | } 26 | } 27 | } 28 | write.table(Y, "/tmp/gosl-rnd-gumbel.dat", row.names=FALSE, col.names=c("x","u","b","ypdf","ycdf"), quote=FALSE) 29 | print("file written") 30 | -------------------------------------------------------------------------------- /rnd/data/lognormal.R: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The Gosl Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style 3 | # license that can be found in the LICENSE file. 4 | 5 | X <- seq(0, 3, 0.25) 6 | N <- c(0, 0.5, 1) # meanlog 7 | Z <- c(0.25, 0.5, 1) # sdlog 8 | Y <- matrix(ncol=4) 9 | first <- TRUE 10 | for (n in N) { 11 | for (z in Z) { 12 | ypdf <- dlnorm(X, n, z) 13 | ycdf <- plnorm(X, n, z) 14 | for (i in 1:length(X)) { 15 | if (first) { 16 | Y <- rbind(c(X[i], n, z, ypdf[i], ycdf[i])) 17 | first <- FALSE 18 | } else { 19 | Y <- rbind(Y, c(X[i], n, z, ypdf[i], ycdf[i])) 20 | } 21 | } 22 | } 23 | } 24 | write.table(Y, "/tmp/gosl-rnd-lognormal.dat", row.names=FALSE, col.names=c("x","n","z","ypdf","ycdf"), quote=FALSE) 25 | print("file written") 26 | -------------------------------------------------------------------------------- /rnd/data/normal.R: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The Gosl Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style 3 | # license that can be found in the LICENSE file. 4 | 5 | X <- seq(-4, 4, 0.5) 6 | M <- c(-0.5, 0, 1) 7 | S <- c(0.25, 0.5, 1) 8 | Y <- matrix(ncol=4) 9 | first <- TRUE 10 | for (mu in M) { 11 | for (sig in S) { 12 | ypdf <- dnorm(X, mu, sig) 13 | ycdf <- pnorm(X, mu, sig) 14 | for (i in 1:length(X)) { 15 | if (first) { 16 | Y <- rbind(c(X[i], mu, sig, ypdf[i], ycdf[i])) 17 | first <- FALSE 18 | } else { 19 | Y <- rbind(Y, c(X[i], mu, sig, ypdf[i], ycdf[i])) 20 | } 21 | } 22 | } 23 | } 24 | write.table(Y, "/tmp/gosl-rnd-normal.dat", row.names=FALSE, col.names=c("x","mu","sig","ypdf","ycdf"), quote=FALSE) 25 | print("file written") 26 | -------------------------------------------------------------------------------- /rnd/data/rnd_haltonAndLatin01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/rnd/data/rnd_haltonAndLatin01.png -------------------------------------------------------------------------------- /rnd/data/rnd_lognormalDistribution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/rnd/data/rnd_lognormalDistribution.png -------------------------------------------------------------------------------- /rnd/data/uniform.R: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The Gosl Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style 3 | # license that can be found in the LICENSE file. 4 | 5 | a <- 1.5 # min 6 | b <- 2.5 # max 7 | 8 | X <- seq(0.5, 3.0, 0.5) 9 | Y <- matrix(ncol=5) 10 | first <- TRUE 11 | ypdf <- dunif(X, a, b) 12 | ycdf <- punif(X, a, b) 13 | for (i in 1:length(X)) { 14 | if (first) { 15 | Y <- rbind(c(X[i], a, b, ypdf[i], ycdf[i])) 16 | first <- FALSE 17 | } else { 18 | Y <- rbind(Y, c(X[i], a, b, ypdf[i], ycdf[i])) 19 | } 20 | } 21 | write.table(Y, "/tmp/gosl-rnd-uniform.dat", row.names=FALSE, col.names=c("x","a","b","ypdf","ycdf"), quote=FALSE) 22 | print("file written") 23 | -------------------------------------------------------------------------------- /rnd/data/uniform.dat: -------------------------------------------------------------------------------- 1 | x a b ypdf ycdf 2 | 0.5 1.5 2.5 0 0 3 | 1 1.5 2.5 0 0 4 | 1.5 1.5 2.5 1 0 5 | 2 1.5 2.5 1 0.5 6 | 2.5 1.5 2.5 1 1 7 | 3 1.5 2.5 0 1 8 | -------------------------------------------------------------------------------- /rnd/dist_frechet.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "math" 9 | ) 10 | 11 | // DistFrechet implements the Frechet / Type II Extreme Value Distribution (largest value) 12 | type DistFrechet struct { 13 | L float64 // location. default = 0 14 | C float64 // scale. default = 1 15 | A float64 // shape 16 | } 17 | 18 | // set factory 19 | func init() { 20 | distallocators["F"] = func() Distribution { return new(DistFrechet) } 21 | } 22 | 23 | // Name returns the name of this probability distribution 24 | func (o *DistFrechet) Name() string { return "Frechet" } 25 | 26 | // Init initializes Frechet distribution 27 | func (o *DistFrechet) Init(p *Variable) { 28 | o.L, o.C, o.A = p.L, p.C, p.A 29 | if math.Abs(o.C) < 1e-15 { 30 | o.C = 1 31 | } 32 | p.M = o.Mean() 33 | p.S = math.Sqrt(o.Variance()) 34 | } 35 | 36 | // Pdf computes the probability density function @ x 37 | func (o DistFrechet) Pdf(x float64) float64 { 38 | if x-o.L < 1e-15 { 39 | return 0 40 | } 41 | z := (x - o.L) / o.C 42 | return math.Exp(-math.Pow(z, -o.A)) * math.Pow(z, -1.0-o.A) * o.A / o.C 43 | } 44 | 45 | // Cdf computes the cumulative probability function @ x 46 | func (o DistFrechet) Cdf(x float64) float64 { 47 | if x-o.L < 1e-15 { 48 | return 0 49 | } 50 | z := (x - o.L) / o.C 51 | return math.Exp(-math.Pow(z, -o.A)) 52 | } 53 | 54 | // Mean returns the expected value 55 | func (o DistFrechet) Mean() float64 { 56 | if o.A > 1.0 { 57 | return o.L + o.C*math.Gamma(1.0-1.0/o.A) 58 | } 59 | return math.Inf(1) 60 | } 61 | 62 | // Variance returns the variance 63 | func (o DistFrechet) Variance() float64 { 64 | if o.A > 2.0 { 65 | return o.C * o.C * (math.Gamma(1.0-2.0/o.A) - math.Pow(math.Gamma(1.0-1.0/o.A), 2.0)) 66 | } 67 | return math.Inf(1) 68 | } 69 | -------------------------------------------------------------------------------- /rnd/dist_gumbel.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import "math" 8 | 9 | // DistGumbel implements the Gumbel / Type I Extreme Value Distribution (largest value) 10 | type DistGumbel struct { 11 | U float64 // location: characteristic largest value 12 | B float64 // scale: measure of dispersion of the largest value 13 | } 14 | 15 | // set factory 16 | func init() { 17 | distallocators["G"] = func() Distribution { return new(DistGumbel) } 18 | } 19 | 20 | // Name returns the name of this probability distribution 21 | func (o *DistGumbel) Name() string { return "Gumbel" } 22 | 23 | // Init initializes Gumbel distribution 24 | func (o *DistGumbel) Init(p *Variable) { 25 | euler := 0.57721566490153286060651209008240243104215 26 | μ, σ := p.M, p.S 27 | o.B = σ * math.Sqrt(6.0) / math.Pi 28 | o.U = μ - euler*o.B 29 | } 30 | 31 | // Pdf computes the probability density function @ x 32 | func (o DistGumbel) Pdf(x float64) float64 { 33 | mz := (o.U - x) / o.B 34 | return math.Exp(mz) * math.Exp(-math.Exp(mz)) / o.B 35 | } 36 | 37 | // Cdf computes the cumulative probability function @ x 38 | func (o DistGumbel) Cdf(x float64) float64 { 39 | mz := (o.U - x) / o.B 40 | return math.Exp(-math.Exp(mz)) 41 | } 42 | -------------------------------------------------------------------------------- /rnd/dist_lognormal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "math" 9 | "math/rand" 10 | ) 11 | 12 | // Lognormal returns a random number belonging to a lognormal distribution 13 | func Lognormal(μ, σ float64) float64 { 14 | δ := σ / μ 15 | v := math.Log(1.0 + δ*δ) 16 | z := math.Sqrt(v) 17 | n := math.Log(μ) - v/2.0 18 | return math.Exp(n + z*rand.NormFloat64()) 19 | } 20 | 21 | // DistLogNormal implements the lognormal distribution 22 | type DistLogNormal struct { 23 | 24 | // input 25 | N float64 // mean of log(x) 26 | Z float64 // standard deviation of log(x) 27 | 28 | // auxiliary 29 | A float64 // 1 / (z sqrt(2 π)) 30 | B float64 // -1 / (2 z²) 31 | } 32 | 33 | // set factory 34 | func init() { 35 | distallocators["L"] = func() Distribution { return new(DistLogNormal) } 36 | } 37 | 38 | // Name returns the name of this probability distribution 39 | func (o *DistLogNormal) Name() string { return "Lognormal" } 40 | 41 | // CalcDerived computes derived/auxiliary quantities 42 | func (o *DistLogNormal) CalcDerived() { 43 | o.A = 1.0 / (o.Z * math.Sqrt2 * math.SqrtPi) 44 | o.B = -1.0 / (2.0 * o.Z * o.Z) 45 | } 46 | 47 | // Init initializes lognormal distribution 48 | func (o *DistLogNormal) Init(p *Variable) { 49 | μ, σ := p.M, p.S 50 | δ := σ / μ 51 | v := math.Log(1.0 + δ*δ) 52 | o.Z = math.Sqrt(v) 53 | o.N = math.Log(μ) - v/2.0 54 | o.CalcDerived() 55 | } 56 | 57 | // Pdf computes the probability density function @ x 58 | func (o DistLogNormal) Pdf(x float64) float64 { 59 | if x < 1e-15 { 60 | return 0 61 | } 62 | return o.A * math.Exp(o.B*math.Pow(math.Log(x)-o.N, 2.0)) / x 63 | } 64 | 65 | // Cdf computes the cumulative probability function @ x 66 | func (o DistLogNormal) Cdf(x float64) float64 { 67 | if x < 1e-15 { 68 | return 0 69 | } 70 | return (1.0 + math.Erf((math.Log(x)-o.N)/(o.Z*math.Sqrt2))) / 2.0 71 | } 72 | -------------------------------------------------------------------------------- /rnd/dist_normal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "math" 9 | "math/rand" 10 | ) 11 | 12 | // Normal returns a random number belonging to a normal distribution 13 | func Normal(μ, σ float64) float64 { 14 | return μ + σ*rand.NormFloat64() 15 | } 16 | 17 | // Stdphi implements φ(x), the standard probability density function 18 | func Stdphi(x float64) float64 { 19 | return math.Exp(-x*x/2.0) / math.Sqrt2 / math.SqrtPi 20 | } 21 | 22 | // StdPhi implements Φ(x), the standard cumulative distribution function 23 | func StdPhi(x float64) float64 { 24 | return (1.0 + math.Erf(x/math.Sqrt2)) / 2.0 25 | } 26 | 27 | // StdInvPhi implements Φ⁻¹(x), the inverse standard cumulative distribution function 28 | func StdInvPhi(x float64) float64 { 29 | return ltqnorm(x) 30 | } 31 | 32 | // DistNormal implements the normal distribution 33 | type DistNormal struct { 34 | 35 | // input 36 | Mu float64 // μ: mean 37 | Sig float64 // σ: std deviation 38 | 39 | // auxiliary 40 | a float64 // 1 / (σ sqrt(2 π)) 41 | b float64 // -1 / (2 σ²) 42 | } 43 | 44 | // set factory 45 | func init() { 46 | distallocators["N"] = func() Distribution { return new(DistNormal) } 47 | } 48 | 49 | // Name returns the name of this probability distribution 50 | func (o *DistNormal) Name() string { return "Normal" } 51 | 52 | // CalcDerived compute derived/auxiliary quantities 53 | func (o *DistNormal) CalcDerived() { 54 | o.a = 1.0 / (o.Sig * math.Sqrt2 * math.SqrtPi) 55 | o.b = -1.0 / (2.0 * o.Sig * o.Sig) 56 | } 57 | 58 | // Init initializes normal distribution 59 | func (o *DistNormal) Init(p *Variable) { 60 | o.Mu, o.Sig = p.M, p.S 61 | o.CalcDerived() 62 | } 63 | 64 | // Pdf computes the probability density function @ x 65 | func (o DistNormal) Pdf(x float64) float64 { 66 | return o.a * math.Exp(o.b*math.Pow(x-o.Mu, 2.0)) 67 | } 68 | 69 | // Cdf computes the cumulative probability function @ x 70 | func (o DistNormal) Cdf(x float64) float64 { 71 | return (1.0 + math.Erf((x-o.Mu)/(o.Sig*math.Sqrt2))) / 2.0 72 | } 73 | -------------------------------------------------------------------------------- /rnd/dist_uniform.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import "math/rand" 8 | 9 | // Uniform returns a random number belonging to a uniform distribution 10 | func Uniform(min, max float64) float64 { 11 | return min + rand.Float64()*(max-min) 12 | } 13 | 14 | // DistUniform implements the normal distribution 15 | type DistUniform struct { 16 | 17 | // input 18 | A float64 // min value 19 | B float64 // max value 20 | } 21 | 22 | // set factory 23 | func init() { 24 | distallocators["U"] = func() Distribution { return new(DistUniform) } 25 | } 26 | 27 | // Name returns the name of this probability distribution 28 | func (o *DistUniform) Name() string { return "Uniform" } 29 | 30 | // Init initializes uniform distribution 31 | func (o *DistUniform) Init(p *Variable) { 32 | o.A, o.B = p.Min, p.Max 33 | } 34 | 35 | // Pdf computes the probability density function @ x 36 | func (o DistUniform) Pdf(x float64) float64 { 37 | if x < o.A { 38 | return 0.0 39 | } 40 | if x > o.B { 41 | return 0.0 42 | } 43 | return 1.0 / (o.B - o.A) 44 | } 45 | 46 | // Cdf computes the cumulative probability function @ x 47 | func (o DistUniform) Cdf(x float64) float64 { 48 | if x < o.A { 49 | return 0.0 50 | } 51 | if x > o.B { 52 | return 1.0 53 | } 54 | return (x - o.A) / (o.B - o.A) 55 | } 56 | -------------------------------------------------------------------------------- /rnd/distribution.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import "github.com/cpmech/gosl/chk" 8 | 9 | // Distribution defines a probability distribution 10 | type Distribution interface { 11 | Name() string 12 | Init(prms *Variable) 13 | Pdf(x float64) float64 14 | Cdf(x float64) float64 15 | } 16 | 17 | // factory 18 | var distallocators = make(map[string]func() Distribution) 19 | 20 | // GetDistrib returns a distribution from factory 21 | func GetDistrib(dtype string) (d Distribution) { 22 | allocator, ok := distallocators[dtype] 23 | if !ok { 24 | chk.Panic("cannot find %q distribution\n", dtype) 25 | } 26 | return allocator() 27 | } 28 | -------------------------------------------------------------------------------- /rnd/dsfmt/LICENSE-dSFMT.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2007, 2008, 2009 Mutsuo Saito, Makoto Matsumoto 2 | and Hiroshima University. 3 | Copyright (c) 2011, 2002 Mutsuo Saito, Makoto Matsumoto, Hiroshima 4 | University and The University of Tokyo. 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are 9 | met: 10 | 11 | * Redistributions of source code must retain the above copyright 12 | notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above 14 | copyright notice, this list of conditions and the following 15 | disclaimer in the documentation and/or other materials provided 16 | with the distribution. 17 | * Neither the name of the Hiroshima University nor the names of 18 | its contributors may be used to endorse or promote products 19 | derived from this software without specific prior written 20 | permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /rnd/dsfmt/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. rnd/sfmt. Light wrapper to the dSFMT library 2 | 3 | dSFMT is the [Double precision SIMD-oriented Fast Mersenne Twister](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/) developed by Mutsuo Saito and Makoto 4 | Matsumoto at the Hiroshima University. 5 | 6 | Check out the [dSFMT original license file](https://github.com/cpmech/gosl/blob/master/rnd/dsfmt/LICENSE-dSFMT.txt). 7 | 8 | **NOTE**: This sub-subpackage is supposed to be used internally only. 9 | -------------------------------------------------------------------------------- /rnd/dsfmt/connectdsfmt.c: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #include "dSFMT.h" 6 | 7 | dsfmt_t GLOBAL_DSFMT; 8 | 9 | void DsfmtInit(long seed) { 10 | dsfmt_init_gen_rand(&GLOBAL_DSFMT, seed); 11 | } 12 | 13 | double DsfmtRand(double lo, double hi) { 14 | return lo + (hi - lo) * dsfmt_genrand_close_open(&GLOBAL_DSFMT); 15 | } 16 | 17 | double DsfmtRand01() { 18 | return dsfmt_genrand_close_open(&GLOBAL_DSFMT); 19 | } 20 | -------------------------------------------------------------------------------- /rnd/dsfmt/connectdsfmt.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #ifndef GOSL_RANDOM_H 6 | #define GOSL_RANDOM_H 7 | 8 | void DsfmtInit(long seed); 9 | double DsfmtRand(double lo, double hi); 10 | double DsfmtRand01(); 11 | 12 | #endif // GOSL_RANDOM_H 13 | -------------------------------------------------------------------------------- /rnd/dsfmt/dSFMT-params19937.h: -------------------------------------------------------------------------------- 1 | #ifndef DSFMT_PARAMS19937_H 2 | #define DSFMT_PARAMS19937_H 3 | 4 | /* #define DSFMT_N 191 */ 5 | /* #define DSFMT_MAXDEGREE 19992 */ 6 | #define DSFMT_POS1 117 7 | #define DSFMT_SL1 19 8 | #define DSFMT_MSK1 UINT64_C(0x000ffafffffffb3f) 9 | #define DSFMT_MSK2 UINT64_C(0x000ffdfffc90fffd) 10 | #define DSFMT_MSK32_1 0x000ffaffU 11 | #define DSFMT_MSK32_2 0xfffffb3fU 12 | #define DSFMT_MSK32_3 0x000ffdffU 13 | #define DSFMT_MSK32_4 0xfc90fffdU 14 | #define DSFMT_FIX1 UINT64_C(0x90014964b32f4329) 15 | #define DSFMT_FIX2 UINT64_C(0x3b8d12ac548a7c7a) 16 | #define DSFMT_PCV1 UINT64_C(0x3d84e1ac0dc82880) 17 | #define DSFMT_PCV2 UINT64_C(0x0000000000000001) 18 | #define DSFMT_IDSTR "dSFMT2-19937:117-19:ffafffffffb3f-ffdfffc90fffd" 19 | 20 | 21 | /* PARAMETERS FOR ALTIVEC */ 22 | #if defined(__APPLE__) /* For OSX */ 23 | #define ALTI_SL1 (vector unsigned char)(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3) 24 | #define ALTI_SL1_PERM \ 25 | (vector unsigned char)(2,3,4,5,6,7,30,30,10,11,12,13,14,15,0,1) 26 | #define ALTI_SL1_MSK \ 27 | (vector unsigned int)(0xffffffffU,0xfff80000U,0xffffffffU,0xfff80000U) 28 | #define ALTI_MSK (vector unsigned int)(DSFMT_MSK32_1, \ 29 | DSFMT_MSK32_2, DSFMT_MSK32_3, DSFMT_MSK32_4) 30 | #else /* For OTHER OSs(Linux?) */ 31 | #define ALTI_SL1 {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3} 32 | #define ALTI_SL1_PERM \ 33 | {2,3,4,5,6,7,30,30,10,11,12,13,14,15,0,1} 34 | #define ALTI_SL1_MSK \ 35 | {0xffffffffU,0xfff80000U,0xffffffffU,0xfff80000U} 36 | #define ALTI_MSK \ 37 | {DSFMT_MSK32_1, DSFMT_MSK32_2, DSFMT_MSK32_3, DSFMT_MSK32_4} 38 | #endif 39 | 40 | #endif /* DSFMT_PARAMS19937_H */ 41 | -------------------------------------------------------------------------------- /rnd/dsfmt/dsfmt.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package dsfmt wraps the dSFMT Double precision SIMD-oriented Fast Mersenne Twister 6 | package dsfmt 7 | 8 | /* 9 | #cgo CFLAGS: -O3 -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing -std=c99 -msse2 -DHAVE_SSE2 -DDSFMT_MEXP=19937 10 | #include "connectdsfmt.h" 11 | */ 12 | import "C" 13 | 14 | import "time" 15 | 16 | // Init initializes random numbers generator 17 | // Input: 18 | // seed -- seed value; use seed <= 0 to use current time 19 | func Init(seed int) { 20 | if seed <= 0 { 21 | seed = int(time.Now().Unix()) 22 | } 23 | C.DsfmtInit(C.long(seed)) 24 | } 25 | 26 | // Rand generates pseudo random real numbers between low and high; i.e. in [low, right) 27 | // Input: 28 | // low -- lower limit (closed) 29 | // high -- upper limit (open) 30 | // Output: 31 | // random float64 32 | func Rand(low, high float64) float64 { 33 | return float64(C.DsfmtRand(C.double(low), C.double(high))) 34 | } 35 | 36 | // Rand01 generates float point random between 0 (inclusive) and 1 (exclusive) 37 | func Rand01() float64 { 38 | return float64(C.DsfmtRand01()) 39 | } 40 | -------------------------------------------------------------------------------- /rnd/dsfmt/t_dsfmt_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build !windows 6 | 7 | package dsfmt 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/cpmech/gosl/chk" 13 | "github.com/cpmech/gosl/io" 14 | ) 15 | 16 | func Test_dsfmt01(tst *testing.T) { 17 | 18 | //verbose() 19 | chk.PrintTitle("dsfmt01. floats") 20 | 21 | Init(1234) 22 | 23 | nsamples := 10 24 | for i := 0; i < nsamples; i++ { 25 | gen := Rand(10, 20) 26 | io.Pforan("gen = %v\n", gen) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /rnd/dsfmt/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build !windows 6 | 7 | package dsfmt 8 | 9 | import ( 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func init() { 15 | io.Verbose = false 16 | } 17 | 18 | func verbose() { 19 | io.Verbose = true 20 | chk.Verbose = true 21 | } 22 | -------------------------------------------------------------------------------- /rnd/halton.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/utl" 10 | ) 11 | 12 | // HaltonPoints generates randomly spaced points 13 | // x -- [dim][n] points 14 | func HaltonPoints(dim, n int) (x [][]float64) { 15 | x = utl.Alloc(dim, n) 16 | for j := 0; j < dim; j++ { 17 | for i := 0; i < n; i++ { 18 | x[j][i] = halton(i, j) 19 | } 20 | } 21 | return 22 | } 23 | 24 | // halton implements (Halton and Weller 1964) method 25 | // i -- population (point) index 26 | // j -- parameter (dimension) index 27 | func halton(i, j int) (sum float64) { 28 | if j > 999 { 29 | chk.Panic("HaltonPoints can only handle maximum dimension = 1000") 30 | } 31 | p1 := Primes1000[j] 32 | p2 := p1 33 | first := true 34 | for first || i > 0 { 35 | w := i % p1 36 | sum = sum + float64(w)/float64(p2) 37 | i = int(float64(i) / float64(p1)) 38 | p2 = p2 * p1 39 | first = false 40 | } 41 | return 42 | } 43 | -------------------------------------------------------------------------------- /rnd/rotations.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "math" 9 | "math/rand" 10 | 11 | "github.com/cpmech/gosl/utl" 12 | ) 13 | 14 | // UnitVectors generates random unit vectors in 3D 15 | func UnitVectors(n int) (U [][]float64) { 16 | U = utl.Alloc(n, 3) 17 | for i := 0; i < n; i++ { 18 | φ := 2.0 * math.Pi * rand.Float64() 19 | θ := math.Acos(1.0 - 2.0*rand.Float64()) 20 | U[i][0] = math.Sin(θ) * math.Cos(φ) 21 | U[i][1] = math.Sin(θ) * math.Sin(φ) 22 | U[i][2] = math.Cos(θ) 23 | } 24 | return 25 | } 26 | -------------------------------------------------------------------------------- /rnd/sfmt/LICENSE-SFMT.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima 2 | University. 3 | Copyright (c) 2012 Mutsuo Saito, Makoto Matsumoto, Hiroshima University 4 | and The University of Tokyo. 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are 9 | met: 10 | 11 | * Redistributions of source code must retain the above copyright 12 | notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above 14 | copyright notice, this list of conditions and the following 15 | disclaimer in the documentation and/or other materials provided 16 | with the distribution. 17 | * Neither the names of Hiroshima University, The University of 18 | Tokyo nor the names of its contributors may be used to endorse 19 | or promote products derived from this software without specific 20 | prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /rnd/sfmt/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. rnd/sfmt. Light wrapper to the SFMT library 2 | 3 | SFMT is the [SIMD-oriented Fast Mersenne 4 | Twister](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/) developed by Mutsuo Saito and Makoto 5 | Matsumoto at the Hiroshima University. 6 | 7 | Check out the [SFMT original license file](https://github.com/cpmech/gosl/blob/master/rnd/sfmt/LICENSE-SFMT.txt). 8 | 9 | **NOTE**: This sub-subpackage is supposed to be used internally only. 10 | -------------------------------------------------------------------------------- /rnd/sfmt/SFMT-params19937.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef SFMT_PARAMS19937_H 3 | #define SFMT_PARAMS19937_H 4 | 5 | #define SFMT_POS1 122 6 | #define SFMT_SL1 18 7 | #define SFMT_SL2 1 8 | #define SFMT_SR1 11 9 | #define SFMT_SR2 1 10 | #define SFMT_MSK1 0xdfffffefU 11 | #define SFMT_MSK2 0xddfecb7fU 12 | #define SFMT_MSK3 0xbffaffffU 13 | #define SFMT_MSK4 0xbffffff6U 14 | #define SFMT_PARITY1 0x00000001U 15 | #define SFMT_PARITY2 0x00000000U 16 | #define SFMT_PARITY3 0x00000000U 17 | #define SFMT_PARITY4 0x13c9e684U 18 | 19 | 20 | /* PARAMETERS FOR ALTIVEC */ 21 | #if defined(__APPLE__) /* For OSX */ 22 | #define SFMT_ALTI_SL1 \ 23 | (vector unsigned int)(SFMT_SL1, SFMT_SL1, SFMT_SL1, SFMT_SL1) 24 | #define SFMT_ALTI_SR1 \ 25 | (vector unsigned int)(SFMT_SR1, SFMT_SR1, SFMT_SR1, SFMT_SR1) 26 | #define SFMT_ALTI_MSK \ 27 | (vector unsigned int)(SFMT_MSK1, SFMT_MSK2, SFMT_MSK3, SFMT_MSK4) 28 | #define SFMT_ALTI_MSK64 \ 29 | (vector unsigned int)(SFMT_MSK2, SFMT_MSK1, SFMT_MSK4, SFMT_MSK3) 30 | #define SFMT_ALTI_SL2_PERM \ 31 | (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) 32 | #define SFMT_ALTI_SL2_PERM64 \ 33 | (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) 34 | #define SFMT_ALTI_SR2_PERM \ 35 | (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) 36 | #define SFMT_ALTI_SR2_PERM64 \ 37 | (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) 38 | #else /* For OTHER OSs(Linux?) */ 39 | #define SFMT_ALTI_SL1 {SFMT_SL1, SFMT_SL1, SFMT_SL1, SFMT_SL1} 40 | #define SFMT_ALTI_SR1 {SFMT_SR1, SFMT_SR1, SFMT_SR1, SFMT_SR1} 41 | #define SFMT_ALTI_MSK {SFMT_MSK1, SFMT_MSK2, SFMT_MSK3, SFMT_MSK4} 42 | #define SFMT_ALTI_MSK64 {SFMT_MSK2, SFMT_MSK1, SFMT_MSK4, SFMT_MSK3} 43 | #define SFMT_ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} 44 | #define SFMT_ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} 45 | #define SFMT_ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} 46 | #define SFMT_ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} 47 | #endif /* For OSX */ 48 | #define SFMT_IDSTR "SFMT-19937:122-18-1-11-1:dfffffef-ddfecb7f-bffaffff-bffffff6" 49 | 50 | #endif /* SFMT_PARAMS19937_H */ 51 | -------------------------------------------------------------------------------- /rnd/sfmt/connectsfmt.c: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #include "stdio.h" 6 | #include "SFMT.h" 7 | 8 | #ifdef WIN32 9 | #define LONG long long 10 | #else 11 | #define LONG long 12 | #endif 13 | 14 | sfmt_t GLOBAL_SFMT; 15 | 16 | void SfmtInit(LONG seed) { 17 | sfmt_init_gen_rand(&GLOBAL_SFMT, seed); 18 | } 19 | 20 | LONG SfmtRand(LONG lo, LONG hi) { 21 | return (sfmt_genrand_uint64(&GLOBAL_SFMT) % (hi-lo+1) + lo); 22 | } 23 | 24 | void SfmtShuffle(LONG *values, LONG size) { 25 | uint64_t j; 26 | LONG tmp; 27 | for (uint64_t i=size-1; i>0; i--) { 28 | j = sfmt_genrand_uint64(&GLOBAL_SFMT) % i; 29 | tmp = values[j]; 30 | values[j] = values[i]; 31 | values[i] = tmp; 32 | } 33 | } 34 | 35 | void SfmtPrintIdString() { 36 | printf("%s\n", sfmt_get_idstring(&GLOBAL_SFMT)); 37 | } 38 | -------------------------------------------------------------------------------- /rnd/sfmt/connectsfmt.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #ifndef GOSL_RANDOM_H 6 | #define GOSL_RANDOM_H 7 | 8 | #ifdef WIN32 9 | #define LONG long long 10 | #else 11 | #define LONG long 12 | #endif 13 | 14 | void SfmtInit(LONG seed); 15 | LONG SfmtRand(LONG lo, LONG hi); 16 | void SfmtShuffle(LONG *values, LONG size); 17 | void SfmtPrintIdString(); 18 | 19 | #endif // GOSL_RANDOM_H 20 | -------------------------------------------------------------------------------- /rnd/sfmt/sfmt.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package sfmt wraps the SFMT SIMD-oriented Fast Mersenne Twister 6 | package sfmt 7 | 8 | /* 9 | #cgo CFLAGS: -O3 -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing -std=c99 -msse2 -DHAVE_SSE2 -DSFMT_MEXP=19937 10 | #include "connectsfmt.h" 11 | #ifdef WIN32 12 | #define LONG long long 13 | #else 14 | #define LONG long 15 | #endif 16 | */ 17 | import "C" 18 | 19 | import ( 20 | "time" 21 | "unsafe" 22 | 23 | "github.com/cpmech/gosl/io" 24 | ) 25 | 26 | // Init initializes random numbers generator 27 | // Input: 28 | // seed -- seed value; use seed <= 0 to use current time 29 | func Init(seed int) { 30 | if seed <= 0 { 31 | seed = int(time.Now().Unix()) 32 | } 33 | C.SfmtInit(C.LONG(seed)) 34 | } 35 | 36 | // Rand generates pseudo random integer between low and high 37 | // Input: 38 | // low -- lower limit 39 | // high -- upper limit 40 | // Output: 41 | // random integer 42 | func Rand(low, high int) int { 43 | return int(C.SfmtRand(C.LONG(low), C.LONG(high))) 44 | } 45 | 46 | // Shuffle shuffles slice of integers 47 | func Shuffle(values []int) { 48 | C.SfmtShuffle((*C.LONG)(unsafe.Pointer(&values[0])), C.LONG(len(values))) 49 | } 50 | 51 | // PrintIDString prints SFMT id string 52 | func PrintIDString() { 53 | if io.Verbose { 54 | C.SfmtPrintIdString() 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /rnd/sfmt/t_b_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build !windows 6 | 7 | package sfmt 8 | 9 | import ( 10 | "math/rand" 11 | "testing" 12 | ) 13 | 14 | var benchNsamples int 15 | var benchResult int 16 | 17 | func init() { 18 | rand.Seed(4321) 19 | Init(4321) 20 | benchNsamples = 1000 21 | } 22 | 23 | func Benchmark_gornd_int(b *testing.B) { 24 | var res int 25 | lo, hi := 0, 50 26 | for i := 0; i < b.N; i++ { 27 | for j := 0; j < benchNsamples; j++ { 28 | res = rand.Int()%(hi-lo+1) + lo 29 | } 30 | } 31 | benchResult = res 32 | } 33 | 34 | func Benchmark_sfmt_int(b *testing.B) { 35 | var res int 36 | lo, hi := 0, 50 37 | for i := 0; i < b.N; i++ { 38 | for j := 0; j < benchNsamples; j++ { 39 | res = Rand(lo, hi) 40 | } 41 | } 42 | benchResult = res 43 | } 44 | -------------------------------------------------------------------------------- /rnd/sfmt/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build !windows 6 | 7 | package sfmt 8 | 9 | import ( 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func init() { 15 | io.Verbose = false 16 | } 17 | 18 | func verbose() { 19 | io.Verbose = true 20 | chk.Verbose = true 21 | } 22 | -------------------------------------------------------------------------------- /rnd/sfmt/t_sfmt_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build !windows 6 | 7 | package sfmt 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/cpmech/gosl/chk" 13 | "github.com/cpmech/gosl/io" 14 | ) 15 | 16 | func Test_sfmt01(tst *testing.T) { 17 | 18 | //verbose() 19 | chk.PrintTitle("sfmt01. integers") 20 | 21 | Init(4321) 22 | 23 | PrintIDString() 24 | //TODO: add SFMT original test here 25 | //io.Pf("64 bit generated randoms\n") 26 | //io.Pf("init_gen_rand__________\n") 27 | 28 | nsamples := 10 29 | for i := 0; i < nsamples; i++ { 30 | gen := Rand(0, 10) 31 | io.Pforan("gen = %v\n", gen) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /rnd/t_b_sfmt_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build !windows 6 | 7 | package rnd 8 | 9 | import ( 10 | "math/rand" 11 | "testing" 12 | ) 13 | 14 | var ( 15 | benchMTnsamples int 16 | benchMTints []int 17 | benchMTresult int 18 | ) 19 | 20 | func init() { 21 | rand.Seed(4321) 22 | MTinit(4321) 23 | benchMTnsamples = 1000 24 | benchMTints = make([]int, benchMTnsamples) 25 | for i := 0; i < benchMTnsamples; i++ { 26 | benchMTints[i] = i 27 | } 28 | } 29 | 30 | func Benchmark_mt_int(b *testing.B) { 31 | var res int 32 | lo, hi := 0, 50 33 | for i := 0; i < b.N; i++ { 34 | for j := 0; j < benchMTnsamples; j++ { 35 | res = MTint(lo, hi) 36 | } 37 | } 38 | benchMTresult = res 39 | } 40 | -------------------------------------------------------------------------------- /rnd/t_b_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "math/rand" 9 | "testing" 10 | ) 11 | 12 | var ( 13 | benchNsamples int 14 | benchInts []int 15 | benchResult int 16 | ) 17 | 18 | func init() { 19 | rand.Seed(4321) 20 | Init(4321) 21 | benchNsamples = 1000 22 | benchInts = make([]int, benchNsamples) 23 | for i := 0; i < benchNsamples; i++ { 24 | benchInts[i] = i 25 | } 26 | } 27 | 28 | func Benchmark_go_int(b *testing.B) { 29 | var res int 30 | lo, hi := 0, 50 31 | for i := 0; i < b.N; i++ { 32 | for j := 0; j < benchNsamples; j++ { 33 | res = Int(lo, hi) 34 | } 35 | } 36 | benchResult = res 37 | } 38 | -------------------------------------------------------------------------------- /rnd/t_dist_gumbel_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func Test_dist_gumbel_01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("dist_gumbel_01") 18 | 19 | _, dat := io.ReadTable("data/gumbel.dat") 20 | 21 | X, ok := dat["x"] 22 | if !ok { 23 | tst.Errorf("cannot get x values\n") 24 | return 25 | } 26 | U, ok := dat["u"] 27 | if !ok { 28 | tst.Errorf("cannot get u values\n") 29 | return 30 | } 31 | B, ok := dat["b"] 32 | if !ok { 33 | tst.Errorf("cannot get b values\n") 34 | return 35 | } 36 | YpdfCmp, ok := dat["ypdf"] 37 | if !ok { 38 | tst.Errorf("cannot get ypdf values\n") 39 | return 40 | } 41 | YcdfCmp, ok := dat["ycdf"] 42 | if !ok { 43 | tst.Errorf("cannot get ycdf values\n") 44 | return 45 | } 46 | 47 | var dist DistGumbel 48 | 49 | nx := len(X) 50 | for i := 0; i < nx; i++ { 51 | dist.U = U[i] 52 | dist.B = B[i] 53 | Ypdf := dist.Pdf(X[i]) 54 | Ycdf := dist.Cdf(X[i]) 55 | err := chk.PrintAnaNum("ypdf", 1e-14, YpdfCmp[i], Ypdf, chk.Verbose) 56 | if err != nil { 57 | tst.Errorf("pdf failed: %v\n", err) 58 | return 59 | } 60 | err = chk.PrintAnaNum("ycdf", 1e-15, YcdfCmp[i], Ycdf, chk.Verbose) 61 | if err != nil { 62 | tst.Errorf("cdf failed: %v\n", err) 63 | return 64 | } 65 | } 66 | } 67 | 68 | func Test_gumbel_03(tst *testing.T) { 69 | 70 | //verbose() 71 | chk.PrintTitle("dist_gumbel_03") 72 | 73 | var dist DistGumbel 74 | dist.Init(&Variable{M: 61.3, S: 7.52}) // from Haldar & Mahadevan page 90 75 | io.Pforan("dist = %+#v\n", dist) 76 | chk.Float64(tst, "u", 0.00011, dist.U, 57.9157) 77 | chk.Float64(tst, "β", 1e-4, dist.B, 1.0/0.17055) 78 | } 79 | -------------------------------------------------------------------------------- /rnd/t_dist_uniform_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func Test_dist_uniform_01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("dist_uniform_01") 18 | 19 | _, dat := io.ReadTable("data/uniform.dat") 20 | 21 | X, ok := dat["x"] 22 | if !ok { 23 | tst.Errorf("cannot get x values\n") 24 | return 25 | } 26 | A, ok := dat["a"] // min 27 | if !ok { 28 | tst.Errorf("cannot get a values\n") 29 | return 30 | } 31 | B, ok := dat["b"] // max 32 | if !ok { 33 | tst.Errorf("cannot get b values\n") 34 | return 35 | } 36 | YpdfCmp, ok := dat["ypdf"] 37 | if !ok { 38 | tst.Errorf("cannot get ypdf values\n") 39 | return 40 | } 41 | YcdfCmp, ok := dat["ycdf"] 42 | if !ok { 43 | tst.Errorf("cannot get ycdf values\n") 44 | return 45 | } 46 | 47 | var dist DistUniform 48 | 49 | nx := len(X) 50 | for i := 0; i < nx; i++ { 51 | dist.Init(&Variable{Min: A[i], Max: B[i]}) 52 | Ypdf := dist.Pdf(X[i]) 53 | Ycdf := dist.Cdf(X[i]) 54 | err := chk.PrintAnaNum("ypdf", 1e-14, YpdfCmp[i], Ypdf, chk.Verbose) 55 | if err != nil { 56 | tst.Errorf("pdf failed: %v\n", err) 57 | return 58 | } 59 | err = chk.PrintAnaNum("ycdf", 1e-15, YcdfCmp[i], Ycdf, chk.Verbose) 60 | if err != nil { 61 | tst.Errorf("cdf failed: %v\n", err) 62 | return 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /rnd/t_hypercube_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | ) 12 | 13 | func Test_hc01(tst *testing.T) { 14 | 15 | //verbose() 16 | chk.PrintTitle("hc01. hypercube") 17 | 18 | Init(111) 19 | 20 | n := 10 21 | x := LatinIHS(2, n, 5) 22 | 23 | xchk := [][]int{ 24 | {2, 9, 5, 8, 1, 4, 3, 10, 7, 6}, 25 | {3, 10, 1, 2, 7, 4, 9, 6, 5, 8}, 26 | } 27 | chk.IntDeep2(tst, "x", x, xchk) 28 | 29 | xmin := []float64{-1.0, 0.0} 30 | xmax := []float64{1.0, 2.0} 31 | dx := (xmax[0] - xmin[0]) / float64(n-1) 32 | dy := (xmax[1] - xmin[1]) / float64(n-1) 33 | X := HypercubeCoords(x, xmin, xmax) 34 | chk.Array(tst, "x0", 1e-15, X[0], []float64{-1 + dx, -1 + 8*dx, -1 + 4*dx, -1 + 7*dx, -1, -1 + 3*dx, -1 + 2*dx, -1 + 9*dx, -1 + 6*dx, -1 + 5*dx}) 35 | chk.Array(tst, "x1", 1e-15, X[1], []float64{2 * dy, 9 * dy, 0, dy, 6 * dy, 3 * dy, 8 * dy, 5 * dy, 4 * dy, 7 * dy}) 36 | } 37 | -------------------------------------------------------------------------------- /rnd/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package rnd 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /rnd/versions.txt: -------------------------------------------------------------------------------- 1 | SFMT = 1.4.1 2 | dSFMT = 2.2.3 3 | -------------------------------------------------------------------------------- /utl/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. utl. Utilities. Lists. Dictionaries. Simple Numerics 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/cpmech/gosl/utl.svg)](https://pkg.go.dev/github.com/cpmech/gosl/utl) 4 | 5 | This package implements functions for simplifying numeric calculations such as finding the maximum 6 | and minimum of lists (i.e. slices), allocation of _deep_ structures such as slices of slices, and 7 | generation of _arrays_. It also contains functions for sorting quantities and updating dictionaries 8 | (i.e. maps). 9 | 10 | This package does not aim for high performance linear algebra computations. For that purpose, we 11 | have the `la` package. Nonetheless, `utl` package is OK for _small computations_ such as for vectors 12 | in the 3D space. It also tries to use the best algorithms for sorting that are implemented in the 13 | standard Go library and others. 14 | 15 | Example of what the functions here can do: 16 | 17 | - Generate lists of integers 18 | - Generate lists of float64s 19 | - Cumulative sums 20 | - Handling tables of float64s 21 | - Pareto fronts 22 | - Slices and deep (nested) slices up to the 4th depth 23 | - Allocate deep slices 24 | - Serialization of deep slices 25 | - Sorting 26 | - Maps and _dictionaries_ 27 | - Append to maps of slices of float64 28 | - Find the _best square_ for given `size = numberOfRows * numberOfColumns` 29 | - ... 30 | 31 | ## API 32 | 33 | [Please see the documentation here](https://pkg.go.dev/github.com/cpmech/gosl/utl) 34 | -------------------------------------------------------------------------------- /utl/auxiliary.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import "math" 8 | 9 | // BestSquare finds the best square for given size=Nrows * Ncolumns 10 | func BestSquare(size int) (nrow, ncol int) { 11 | nrow = -1 // not found 12 | ncol = -1 // not found 13 | for x := 1; x <= size; x++ { 14 | if (x * x) >= size { 15 | if (x * x) == size { 16 | nrow = x 17 | ncol = x 18 | return 19 | } 20 | for y := x; y >= 1; y-- { 21 | if (x * y) == size { 22 | nrow = x 23 | ncol = y 24 | return 25 | } 26 | } 27 | } 28 | } 29 | return 30 | } 31 | 32 | // BestSquareApprox finds the best square for given size=Nrows * Ncolumns. 33 | // Approximate version; i.e. nrow*ncol may not be equal to size 34 | func BestSquareApprox(size int) (nrow, ncol int) { 35 | fsize := float64(size) 36 | ncol = int(math.Floor(math.Sqrt(fsize))) 37 | nrow = int(math.Ceil(fsize / float64(ncol))) 38 | return 39 | } 40 | 41 | // Iabs performs the absolute operation with ints 42 | func Iabs(a int) int { 43 | if a < 0 { 44 | return -a 45 | } 46 | return a 47 | } 48 | 49 | // Imin returns the minimum between two integers 50 | func Imin(a, b int) int { 51 | if a < b { 52 | return a 53 | } 54 | return b 55 | } 56 | 57 | // Imax returns the maximum between two integers 58 | func Imax(a, b int) int { 59 | if a > b { 60 | return a 61 | } 62 | return b 63 | } 64 | 65 | // Min returns the minimum between two float point numbers 66 | func Min(a, b float64) float64 { 67 | if a < b { 68 | return a 69 | } 70 | return b 71 | } 72 | 73 | // Max returns the maximum between two float point numbers 74 | func Max(a, b float64) float64 { 75 | if a > b { 76 | return a 77 | } 78 | return b 79 | } 80 | 81 | // IsPowerOfTwo checks if n is power of 2; i.e. 2⁰, 2¹, 2², 2³, 2⁴, ... 82 | func IsPowerOfTwo(n int) bool { 83 | if n < 1 { 84 | return false 85 | } 86 | return n&(n-1) == 0 87 | } 88 | 89 | // Swap swaps two float64 numbers 90 | func Swap(a, b *float64) { 91 | *a, *b = *b, *a 92 | } 93 | -------------------------------------------------------------------------------- /utl/constants.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | const ( 8 | // SQ2 = sqrt(2) [https://oeis.org/A002193] 9 | SQ2 = 1.41421356237309504880168872420969807856967187537694807317667974 10 | 11 | // SQ3 = sqrt(3) [https://oeis.org/A002194] 12 | SQ3 = 1.73205080756887729352744634150587236694280525381038062805580698 13 | 14 | // SQ5 = sqrt(5) [https://oeis.org/A002163] 15 | SQ5 = 2.23606797749978969640917366873127623544061835961152572427089724 16 | 17 | // SQ6 = sqrt(6) [https://oeis.org/A010464] 18 | SQ6 = 2.44948974278317809819728407470589139196594748065667012843269257 19 | 20 | // SQ7 = sqrt(7) [https://oeis.org/A010465] 21 | SQ7 = 2.64575131106459059050161575363926042571025918308245018036833446 22 | 23 | // SQ8 = sqrt(8) [https://oeis.org/A010466] 24 | SQ8 = 2.82842712474619009760337744841939615713934375075389614635335947 25 | ) 26 | -------------------------------------------------------------------------------- /utl/data/README.md: -------------------------------------------------------------------------------- 1 | # Gosl. utl. data subdirectory 2 | 3 | This directory contains auxiliary data files for testing and examples. 4 | -------------------------------------------------------------------------------- /utl/data/basic02.dat: -------------------------------------------------------------------------------- 1 | [1,4,5,1,2,8,3] 2 | -------------------------------------------------------------------------------- /utl/data/json01.dat: -------------------------------------------------------------------------------- 1 | { 2 | "cells" : { 3 | "-1" : [0,1,3,4,6,7,9,10,12,13], 4 | "-2" : [2,5,8,11,14] 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /utl/data/json02.dat: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "Dorival Pedroso", 3 | "id" : 666, 4 | "ok" : true, 5 | "nums" : [1, 5, 11, 21, 666.66] 6 | } 7 | -------------------------------------------------------------------------------- /utl/list.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import "github.com/cpmech/gosl/io" 8 | 9 | // List implements a tabular list with variable number of columns 10 | // Example: 11 | // Vals = [][]float64{ 12 | // {0.0}, 13 | // {1.0, 1.1, 1.2, 1.3}, 14 | // {2.0, 2.1}, 15 | // {3.0, 3.1, 3.2}, 16 | // } 17 | type List struct { 18 | Vals [][]float64 // values 19 | } 20 | 21 | // Append appends items to List 22 | func (o *List) Append(rowidx int, value float64) { 23 | size := len(o.Vals) 24 | 25 | // fill new rows if necessary 26 | if rowidx >= size { 27 | for i := size; i <= rowidx; i++ { 28 | o.Vals = append(o.Vals, []float64{}) 29 | } 30 | } 31 | 32 | // append value 33 | o.Vals[rowidx] = append(o.Vals[rowidx], value) 34 | } 35 | 36 | // SerialList implements a tabular list with variable number of columns 37 | // using a serial representation 38 | // Example: 39 | // 0.0 40 | // 1.0 1.1 1.2 1.3 41 | // 2.0 2.1 42 | // 3.0 3.1 3.2 43 | // becomes: 44 | // (0) (1) 2 3 4 (5) 6 (7) 8 9 (10) 45 | // Vals = 0.0 | 1.0 1.1 1.2 1.3 | 2.0 2.1 | 3.0 3.1 3.2 | 46 | // Ptrs = 0 1 5 7 10 47 | // Notes: 48 | // len(Ptrs) = nrows + 1 49 | // Ptrs[len(Ptrs)-1] = len(Vals) 50 | type SerialList struct { 51 | Vals []float64 // values 52 | Ptrs []int // pointers 53 | } 54 | 55 | // Append appends item to SerialList 56 | func (o *SerialList) Append(startRow bool, value float64) { 57 | if startRow { 58 | if len(o.Ptrs) == 0 { 59 | o.Ptrs = []int{0, 0} 60 | } else { 61 | start := o.Ptrs[len(o.Ptrs)-1] 62 | o.Ptrs = append(o.Ptrs, start) 63 | } 64 | } 65 | o.Vals = append(o.Vals, value) 66 | o.Ptrs[len(o.Ptrs)-1]++ 67 | } 68 | 69 | // Print prints the double-serial-list 70 | func (o SerialList) Print(fmt string) { 71 | for i := 0; i < len(o.Ptrs)-1; i++ { 72 | for j := o.Ptrs[i]; j < o.Ptrs[i+1]; j++ { 73 | io.Pf(fmt, o.Vals[j]) 74 | } 75 | io.Pf("\n") 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /utl/mapops.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | // NOTE: Like slices, maps hold references to an underlying data structure. 8 | // If you pass a map to a function that changes the contents of the map, 9 | // the changes will be visible in the caller [Effective Go]. 10 | 11 | // IntIntsMapAppend appends a new item to a map of slice. 12 | // Note: this function creates a new slice in the map if key is not found. 13 | func IntIntsMapAppend(m map[int][]int, key int, item int) { 14 | if slice, ok := m[key]; ok { 15 | slice = append(slice, item) 16 | m[key] = slice 17 | } else { 18 | m[key] = []int{item} 19 | } 20 | } 21 | 22 | // StrIntsMapAppend appends a new item to a map of slice. 23 | // Note: this function creates a new slice in the map if key is not found. 24 | func StrIntsMapAppend(m map[string][]int, key string, item int) { 25 | if slice, ok := m[key]; ok { 26 | slice = append(slice, item) 27 | m[key] = slice 28 | } else { 29 | m[key] = []int{item} 30 | } 31 | } 32 | 33 | // StrFltsMapAppend appends a new item to a map of slice. 34 | // Note: this function creates a new slice in the map if key is not found. 35 | func StrFltsMapAppend(m map[string][]float64, key string, item float64) { 36 | if slice, ok := m[key]; ok { 37 | slice = append(slice, item) 38 | m[key] = slice 39 | } else { 40 | m[key] = []float64{item} 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /utl/observer.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | // Observer is an interface to objects that need to observe something 8 | type Observer interface { 9 | Update() // the data observed by this observer is being update 10 | } 11 | 12 | // Observable indicates that an object is observable; i.e. it has a list of interested observers 13 | type Observable struct { 14 | observers []Observer // list of interested parties 15 | } 16 | 17 | // AddObserver adds an object to the list of interested observers 18 | func (o *Observable) AddObserver(obs Observer) { 19 | o.observers = append(o.observers, obs) 20 | } 21 | 22 | // NotifyUpdate notifies observers of updates 23 | func (o *Observable) NotifyUpdate() { 24 | for _, obs := range o.observers { 25 | obs.Update() 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /utl/printing.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import ( 8 | "fmt" 9 | "math" 10 | 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | // PrintDeep3 prints an array of array of array 15 | func PrintDeep3(name string, A [][][]float64) { 16 | io.Pf("%s = [\n", name) 17 | for _, a := range A { 18 | io.Pf(" %v\n", a) 19 | } 20 | io.Pf("]\n") 21 | } 22 | 23 | // PrintDeep4 prints an array of array of array 24 | func PrintDeep4(name string, A [][][][]float64, format string) { 25 | res := name + " = \n" 26 | for _, a := range A { 27 | for _, b := range a { 28 | for _, c := range b { 29 | for _, d := range c { 30 | if math.Abs(d) <= 1e-3 { 31 | d = 0 32 | } 33 | res += io.Sf(format, d) 34 | } 35 | res += "\n" 36 | } 37 | res += "\n" 38 | } 39 | } 40 | fmt.Println(res) 41 | } 42 | -------------------------------------------------------------------------------- /utl/t_b_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import ( 8 | "math/rand" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | var ( 15 | benchStrindexsmall []string 16 | benchStrindexmap map[string]int 17 | benchResult int 18 | ) 19 | 20 | func init() { 21 | rand.Seed(13) 22 | answers := []string{"66", "644", "666", "653", "10", "0", "1", "1", "1", "9", "A", ""} 23 | //n := 1000000 24 | //m := 500000 25 | //n := 100 26 | //m := 50 27 | n := 20 28 | m := 10 29 | benchStrindexsmall = make([]string, n) 30 | benchStrindexmap = make(map[string]int) 31 | for i := 0; i < n; i++ { 32 | j := rand.Intn(len(answers)) 33 | benchStrindexsmall[i] = answers[j] 34 | benchStrindexmap[io.Sf("%s_%d", answers[j], i)] = i 35 | if i == m { 36 | benchStrindexsmall[i] = "user" 37 | benchStrindexmap["user"] = i 38 | } 39 | } 40 | //io.Pforan("%v\n", ___benchmarking_strindexsmall___) 41 | //io.Pfcyan("%v\n", ___benchmarking_strindexmap___) 42 | } 43 | 44 | func BenchmarkStrIndexSmall(b *testing.B) { 45 | var idx int 46 | for i := 0; i < b.N; i++ { 47 | idx = StrIndexSmall(benchStrindexsmall, "user") 48 | } 49 | benchResult = idx 50 | } 51 | 52 | func BenchmarkStrIndexMap(b *testing.B) { 53 | var idx int 54 | for i := 0; i < b.N; i++ { 55 | idx = benchStrindexmap["user"] 56 | } 57 | benchResult = idx 58 | } 59 | -------------------------------------------------------------------------------- /utl/t_constants_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import ( 8 | "math" 9 | "testing" 10 | 11 | "github.com/cpmech/gosl/chk" 12 | "github.com/cpmech/gosl/io" 13 | ) 14 | 15 | func Test_constants(tst *testing.T) { 16 | //verbose() 17 | chk.PrintTitle("constants. sqrt(2). sqrt(3)") 18 | io.Pf("SQ2 = %.16f\n", SQ2) 19 | io.Pf("sqrt(2) = %.16f\n", math.Sqrt(2.0)) 20 | io.Pf("\n") 21 | io.Pf("SQ3 = %.16f\n", SQ3) 22 | io.Pf("sqrt(3) = %.16f\n", math.Sqrt(3.0)) 23 | io.Pf("\n") 24 | io.Pf("SQ5 = %.16f\n", SQ5) 25 | io.Pf("sqrt(5) = %.16f\n", math.Sqrt(5.0)) 26 | io.Pf("\n") 27 | io.Pf("SQ6 = %.16f\n", SQ6) 28 | io.Pf("sqrt(6) = %.16f\n", math.Sqrt(6.0)) 29 | io.Pf("\n") 30 | io.Pf("SQ7 = %.16f\n", SQ7) 31 | io.Pf("sqrt(7) = %.16f\n", math.Sqrt(7.0)) 32 | io.Pf("\n") 33 | io.Pf("SQ8 = %.16f\n", SQ8) 34 | io.Pf("sqrt(8) = %.16f\n", math.Sqrt(8.0)) 35 | io.Pf("\n") 36 | chk.Float64(tst, "sqrt(2)", 1e-15, SQ2, math.Sqrt(2.0)) 37 | chk.Float64(tst, "sqrt(3)", 1e-15, SQ3, math.Sqrt(3.0)) 38 | chk.Float64(tst, "sqrt(5)", 1e-15, SQ5, math.Sqrt(5.0)) 39 | chk.Float64(tst, "sqrt(6)", 1e-15, SQ6, math.Sqrt(6.0)) 40 | chk.Float64(tst, "sqrt(7)", 1e-15, SQ7, math.Sqrt(7.0)) 41 | chk.Float64(tst, "sqrt(8)", 1e-15, SQ8, math.Sqrt(8.0)) 42 | } 43 | -------------------------------------------------------------------------------- /utl/t_init_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import ( 8 | "github.com/cpmech/gosl/chk" 9 | "github.com/cpmech/gosl/io" 10 | ) 11 | 12 | func init() { 13 | io.Verbose = false 14 | } 15 | 16 | func verbose() { 17 | io.Verbose = true 18 | chk.Verbose = true 19 | } 20 | -------------------------------------------------------------------------------- /utl/t_observer_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | type observabledata struct { 15 | Observable 16 | x, y, z int 17 | } 18 | 19 | func (o *observabledata) Set(x, y, z int) { 20 | o.x, o.y, o.z = x, y, z 21 | o.NotifyUpdate() 22 | } 23 | 24 | type iminterested struct { 25 | desc string 26 | notification string 27 | data *observabledata 28 | } 29 | 30 | func (o *iminterested) Update() { 31 | o.notification = io.Sf("got x=%d y=%d z=%d", o.data.x, o.data.y, o.data.z) 32 | } 33 | 34 | func TestObserver01(tst *testing.T) { 35 | 36 | //verbose() 37 | chk.PrintTitle("Observer01. Observer and Observable") 38 | 39 | var data observabledata 40 | 41 | obs01 := &iminterested{"I'm interested in updates", "", &data} 42 | obs02 := &iminterested{"I'm interested in updates as well", "", &data} 43 | 44 | data.AddObserver(obs01) 45 | data.AddObserver(obs02) 46 | 47 | chk.String(tst, obs01.notification, "") 48 | chk.String(tst, obs02.notification, "") 49 | data.Set(1, 2, 3) 50 | chk.String(tst, obs01.notification, "got x=1 y=2 z=3") 51 | chk.String(tst, obs02.notification, "got x=1 y=2 z=3") 52 | } 53 | -------------------------------------------------------------------------------- /utl/t_searching_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Gosl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package utl 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/cpmech/gosl/chk" 11 | "github.com/cpmech/gosl/io" 12 | ) 13 | 14 | func Test_search01(tst *testing.T) { 15 | 16 | //verbose() 17 | chk.PrintTitle("search01") 18 | 19 | a := []string{"66", "644", "666", "653", "10", "0", "1", "1", "1"} 20 | idx := StrIndexSmall(a, "666") 21 | io.Pf("a = %v\n", a) 22 | io.Pf("idx of '666' = %v\n", idx) 23 | if idx != 2 { 24 | tst.Errorf("idx is wrong") 25 | return 26 | } 27 | idx = StrIndexSmall(a, "1") 28 | io.Pf("idx of '1' = %v\n", idx) 29 | if idx != 6 { 30 | tst.Errorf("idx is wrong") 31 | return 32 | } 33 | 34 | b := []int{66, 644, 666, 653, 10, 0, 1, 1, 1} 35 | idx = IntIndexSmall(b, 666) 36 | io.Pf("b = %v\n", b) 37 | io.Pf("idx of 666 = %v\n", idx) 38 | if idx != 2 { 39 | tst.Errorf("idx is wrong") 40 | return 41 | } 42 | idx = IntIndexSmall(b, 1) 43 | io.Pf("idx of 1 = %v\n", idx) 44 | if idx != 6 { 45 | tst.Errorf("idx is wrong") 46 | return 47 | } 48 | 49 | // TODO: finish this 50 | //jdx, bsorted := IntIndexSorted(b, 666, nil) 51 | //io.Pforan("\njdx = %v\n", jdx) 52 | //io.Pforan("bsorted = %v\n", bsorted) 53 | //chk.Ints(tst, "bsorted", bsorted, []int{0, 1, 1, 1, 10, 66, 644, 653, 666}) 54 | } 55 | -------------------------------------------------------------------------------- /zdocs/vscode-gosl-01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/zdocs/vscode-gosl-01.gif -------------------------------------------------------------------------------- /zdocs/vscode-intellisense-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/zdocs/vscode-intellisense-01.png -------------------------------------------------------------------------------- /zdocs/vscode-open-in-container.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpmech/gosl/d25acbe85e133c84d830ef6e27c699c35fba7355/zdocs/vscode-open-in-container.gif -------------------------------------------------------------------------------- /zscripts/dk-build-image.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | NAME="gosl" 4 | VERSION="latest" 5 | 6 | echo 7 | echo 8 | echo "... docker .................................................." 9 | echo "............................................................." 10 | echo 11 | 12 | docker build --no-cache -t gosl/$NAME:$VERSION . 13 | docker images -q -f "dangling=true" | xargs docker rmi 14 | -------------------------------------------------------------------------------- /zscripts/dk-publish.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | NAME="gosl" 4 | VERSION="latest" 5 | 6 | docker logout 7 | docker login 8 | docker push gosl/$NAME:$VERSION 9 | -------------------------------------------------------------------------------- /zscripts/dk-shell-into.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | NAME="gosl" 4 | VERSION="latest" 5 | 6 | docker run --rm -it gosl/$NAME:$VERSION /bin/bash 7 | -------------------------------------------------------------------------------- /zscripts/microsoft/zdownload.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SCRIPTS="\ 4 | common-debian.sh \ 5 | go-debian.sh 6 | " 7 | 8 | for s in $SCRIPTS; do 9 | curl https://raw.githubusercontent.com/microsoft/vscode-dev-containers/master/script-library/$s -o $s 10 | done --------------------------------------------------------------------------------