From 2125f01f97abfe343fc5e0cc078bf1d081b2e441 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 20 May 2016 20:30:15 +0000 Subject: Initial commit --- INSTALL | 43 + LGPL3 | 165 ++ LICENSE | 202 ++ Makefile | 120 + NOTICE | 15 + doc/libinum-doc.html | 2332 ++++++++++++++++++++ doc/libinum-doc.php | 1835 +++++++++++++++ doc/libinum-examples/Makefile | 80 + doc/libinum-examples/build/integral_gauss-legendre | Bin 0 -> 127104 bytes doc/libinum-examples/build/root_newton | Bin 0 -> 18712 bytes .../objs/integral_gauss-legendre.o | Bin 0 -> 11072 bytes doc/libinum-examples/objs/root_newton.o | Bin 0 -> 9048 bytes doc/libinum-examples/src/integral_gauss-legendre.c | 92 + doc/libinum-examples/src/root_newton.c | 58 + include/libinum.h | 20 + include/libinum/array.h | 1 + include/libinum/array_2_mpfr.h | 1 + include/libinum/array_base.h | 1 + include/libinum/array_char.h | 1 + include/libinum/array_double.h | 1 + include/libinum/array_int.h | 1 + include/libinum/array_ldouble.h | 1 + include/libinum/array_mpfr.h | 1 + include/libinum/array_polynomial_double.h | 1 + include/libinum/array_polynomial_ldouble.h | 1 + include/libinum/array_polynomial_mpfr.h | 1 + include/libinum/array_pthread_t.h | 1 + include/libinum/array_str.h | 1 + include/libinum/array_type.h | 1 + include/libinum/array_uint.h | 1 + include/libinum/errors.h | 1 + include/libinum/integral.h | 1 + include/libinum/integral_base.h | 1 + include/libinum/integral_double.h | 1 + include/libinum/integral_ldouble.h | 1 + include/libinum/integral_mpfr.h | 1 + include/libinum/polynomial.h | 1 + include/libinum/polynomialMV.h | 1 + include/libinum/polynomialMV_base.h | 1 + include/libinum/polynomialMV_int.h | 1 + include/libinum/polynomialMV_mpz.h | 1 + include/libinum/polynomialMV_type.h | 1 + include/libinum/polynomial_base.h | 1 + include/libinum/polynomial_double.h | 1 + include/libinum/polynomial_ldouble.h | 1 + include/libinum/polynomial_mpfr.h | 1 + include/libinum/polynomial_type.h | 1 + include/libinum/root.h | 1 + include/libinum/root_base.h | 1 + include/libinum/root_double.h | 1 + include/libinum/root_ldouble.h | 1 + include/libinum/root_mpfr.h | 1 + include/libinum/types.h | 1 + include/libinum/utils.h | 1 + src/array.c | 227 ++ src/array.h | 150 ++ src/array_2_mpfr.h | 54 + src/array_base.c | 249 +++ src/array_base.h | 78 + src/array_char.h | 52 + src/array_double.h | 52 + src/array_int.h | 52 + src/array_ldouble.h | 52 + src/array_mpfr.h | 54 + src/array_polynomial_double.h | 50 + src/array_polynomial_ldouble.h | 50 + src/array_polynomial_mpfr.h | 50 + src/array_pthread_t.h | 50 + src/array_str.h | 54 + src/array_type.h | 28 + src/array_uint.h | 52 + src/errors.h | 29 + src/integral.c | 52 + src/integral.h | 53 + src/integral_base.c | 506 +++++ src/integral_base.h | 48 + src/integral_double.h | 81 + src/integral_ldouble.h | 81 + src/integral_mpfr.h | 84 + src/polynomial.c | 55 + src/polynomial.h | 55 + src/polynomialMV.c | 30 + src/polynomialMV.h | 34 + src/polynomialMV_base.c | 293 +++ src/polynomialMV_base.h | 67 + src/polynomialMV_int.h | 62 + src/polynomialMV_mpz.h | 66 + src/polynomialMV_type.h | 29 + src/polynomial_base.c | 337 +++ src/polynomial_base.h | 67 + src/polynomial_double.h | 117 + src/polynomial_ldouble.h | 117 + src/polynomial_mpfr.h | 121 + src/polynomial_type.h | 27 + src/root.c | 51 + src/root.h | 57 + src/root_base.c | 134 ++ src/root_base.h | 27 + src/root_double.h | 55 + src/root_ldouble.h | 55 + src/root_mpfr.h | 59 + src/types.h | 84 + src/utils.c | 79 + src/utils.h | 36 + 104 files changed, 9403 insertions(+) create mode 100644 INSTALL create mode 100644 LGPL3 create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 NOTICE create mode 100644 doc/libinum-doc.html create mode 100644 doc/libinum-doc.php create mode 100644 doc/libinum-examples/Makefile create mode 100755 doc/libinum-examples/build/integral_gauss-legendre create mode 100755 doc/libinum-examples/build/root_newton create mode 100644 doc/libinum-examples/objs/integral_gauss-legendre.o create mode 100644 doc/libinum-examples/objs/root_newton.o create mode 100644 doc/libinum-examples/src/integral_gauss-legendre.c create mode 100644 doc/libinum-examples/src/root_newton.c create mode 100644 include/libinum.h create mode 120000 include/libinum/array.h create mode 120000 include/libinum/array_2_mpfr.h create mode 120000 include/libinum/array_base.h create mode 120000 include/libinum/array_char.h create mode 120000 include/libinum/array_double.h create mode 120000 include/libinum/array_int.h create mode 120000 include/libinum/array_ldouble.h create mode 120000 include/libinum/array_mpfr.h create mode 120000 include/libinum/array_polynomial_double.h create mode 120000 include/libinum/array_polynomial_ldouble.h create mode 120000 include/libinum/array_polynomial_mpfr.h create mode 120000 include/libinum/array_pthread_t.h create mode 120000 include/libinum/array_str.h create mode 120000 include/libinum/array_type.h create mode 120000 include/libinum/array_uint.h create mode 120000 include/libinum/errors.h create mode 120000 include/libinum/integral.h create mode 120000 include/libinum/integral_base.h create mode 120000 include/libinum/integral_double.h create mode 120000 include/libinum/integral_ldouble.h create mode 120000 include/libinum/integral_mpfr.h create mode 120000 include/libinum/polynomial.h create mode 120000 include/libinum/polynomialMV.h create mode 120000 include/libinum/polynomialMV_base.h create mode 120000 include/libinum/polynomialMV_int.h create mode 120000 include/libinum/polynomialMV_mpz.h create mode 120000 include/libinum/polynomialMV_type.h create mode 120000 include/libinum/polynomial_base.h create mode 120000 include/libinum/polynomial_double.h create mode 120000 include/libinum/polynomial_ldouble.h create mode 120000 include/libinum/polynomial_mpfr.h create mode 120000 include/libinum/polynomial_type.h create mode 120000 include/libinum/root.h create mode 120000 include/libinum/root_base.h create mode 120000 include/libinum/root_double.h create mode 120000 include/libinum/root_ldouble.h create mode 120000 include/libinum/root_mpfr.h create mode 120000 include/libinum/types.h create mode 120000 include/libinum/utils.h create mode 100644 src/array.c create mode 100644 src/array.h create mode 100644 src/array_2_mpfr.h create mode 100644 src/array_base.c create mode 100644 src/array_base.h create mode 100644 src/array_char.h create mode 100644 src/array_double.h create mode 100644 src/array_int.h create mode 100644 src/array_ldouble.h create mode 100644 src/array_mpfr.h create mode 100644 src/array_polynomial_double.h create mode 100644 src/array_polynomial_ldouble.h create mode 100644 src/array_polynomial_mpfr.h create mode 100644 src/array_pthread_t.h create mode 100644 src/array_str.h create mode 100644 src/array_type.h create mode 100644 src/array_uint.h create mode 100644 src/errors.h create mode 100644 src/integral.c create mode 100644 src/integral.h create mode 100644 src/integral_base.c create mode 100644 src/integral_base.h create mode 100644 src/integral_double.h create mode 100644 src/integral_ldouble.h create mode 100644 src/integral_mpfr.h create mode 100644 src/polynomial.c create mode 100644 src/polynomial.h create mode 100644 src/polynomialMV.c create mode 100644 src/polynomialMV.h create mode 100644 src/polynomialMV_base.c create mode 100644 src/polynomialMV_base.h create mode 100644 src/polynomialMV_int.h create mode 100644 src/polynomialMV_mpz.h create mode 100644 src/polynomialMV_type.h create mode 100644 src/polynomial_base.c create mode 100644 src/polynomial_base.h create mode 100644 src/polynomial_double.h create mode 100644 src/polynomial_ldouble.h create mode 100644 src/polynomial_mpfr.h create mode 100644 src/polynomial_type.h create mode 100644 src/root.c create mode 100644 src/root.h create mode 100644 src/root_base.c create mode 100644 src/root_base.h create mode 100644 src/root_double.h create mode 100644 src/root_ldouble.h create mode 100644 src/root_mpfr.h create mode 100644 src/types.h create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000..bc9fa86 --- /dev/null +++ b/INSTALL @@ -0,0 +1,43 @@ +####################################################################### +## ## +## Compiling and installing libinum. ## +## ## +####################################################################### + +* libinum should work on any POSIX compliant system, such as GNU/Linux or OSX. + +* libinum is linked against the GNU MPFR, GNU GMP and pthread libraries. + +* Compiling: + Run + make + + The default paths can be modified by passing the appropriate arguments to + make, as specified in the following table + compiler : CC=/usr/bin/gcc + linker : LD=/usr/bin/gcc + archive : AR=/usr/bin/ar + include : INCLUDE= + lib paths : LIB= + optimize : OPT=-O3 + + For example, + make CC=/usr/local/bin/gcc INCLUDE=/usr/local/include LIBDIR=-L/usr/local/lib + + In addition, libinum can be compiled as dynamic or static library, which is + controlled by the STATIC option of the makefile. + If STATIC=0 (default) then compile both + If STATIC=1 then compile the static library + If STATIC=2 then compile the dynamic library + + +* Installing: + Run + make install + + The default install prefix (/usr) can be changed by changing the PREFIX + variable. + + For example + make install PREFIX=/usr/local + diff --git a/LGPL3 b/LGPL3 new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/LGPL3 @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d73250a --- /dev/null +++ b/Makefile @@ -0,0 +1,120 @@ +## Copyright 2016 Ian Jauslin +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. + +# whether to compile only the static library, only the dynamic library, or both +# if static=0 then compile both +# if static=2 then compile the dynamic library +# if static=1 then compile the static library +STATIC=0 + +VERSION=1.0 + +# products of the compilation +PROJECT_SO=libinum.so.$(VERSION) +PROJECT_LIBS=libinum.a +PROJECT_DOCS=libinum-doc.html + +# debug and optimization flags +#DB= -ggdb +OPT= -O3 + +# warning flags +WARNINGS= -Wall -Wextra -Wno-strict-overflow -std=c99 -pedantic + +# installation dirs +PREFIX=/usr +LIBDIR=$(PREFIX)/lib +INCLUDEDIR=$(PREFIX)/include +DOCDIR=$(PREFIX)/share/doc/libinum + +# compiler +CC=/usr/bin/gcc +LD=$(CC) +AR=/usr/bin/ar + +# directories +INCLUDE = +LIB = + +# flags +override LDFLAGS +=$(LIB) +override CFLAGS +=$(INCLUDE)$(DB) $(OPT) $(WARNINGS) + +# build directories +BUILDDIR=./build +SRCDIR=./src +OBJDIR=./objs + +# objects +LIBINUM_OBJS = $(addprefix $(OBJDIR)/, array.o integral.o utils.o polynomial.o polynomialMV.o root.o) + + +ifeq ($(STATIC),1) + # compile libinum.a + PREREQ=static + # install static lib + INSTALLLIB=install-static +else ifeq ($(STATIC),2) + # compile libinum.so.$(VERSION) + PREREQ=shared + # install shared lib + INSTALLLIB=install-shared +else + # compile both + PREREQ=shared static + # install both + INSTALLLIB=install-shared install-static +endif + + +all: init $(PREREQ) + +# create dirs +init: + @[ -d $(OBJDIR) ] || /bin/mkdir $(OBJDIR) + @[ -d $(BUILDDIR) ] || /bin/mkdir $(BUILDDIR) + +# static library +static: $(PROJECT_LIBS) +# shared library +shared: $(PROJECT_SO) + +libinum.a: $(LIBINUM_OBJS) + $(AR) -rc $(BUILDDIR)/$@ $^ + +libinum.so.$(VERSION): $(LIBINUM_OBJS) + $(LD) -shared -lm -lmpfr -lgmp -lpthread $(LDFLAGS) -o $(BUILDDIR)/$@ $^ + ln -fs ./libinum.so.$(VERSION) $(BUILDDIR)/libinum.so + +%.o : ../$(SRCDIR)/%.c + $(CC) -c $(CFLAGS) -fPIC $< -o $@ + +install: $(INSTALLLIB) all + mkdir -p $(MANDIR) $(DOCDIR) $(INCLUDEDIR)/libinum + cp -rL include/libinum/* $(INCLUDEDIR)/libinum/ + install -Dm644 include/libinum.h $(INCLUDEDIR)/ + cp -r doc/libinum-examples $(DOCDIR)/ + install -Dm644 $(addprefix doc/,$(PROJECT_DOCS)) $(DOCDIR)/ + +install-static: all + mkdir -p $(LIBDIR) + install -Dm755 $(addprefix $(BUILDDIR)/,$(PROJECT_LIBS)) $(LIBDIR)/ +install-shared: all + mkdir -p $(LIBDIR) + install -Dm755 $(addprefix $(BUILDDIR)/,$(PROJECT_SO)) $(LIBDIR)/ + for lib in $(PROJECT_SO); do ln -fs ./$$lib $(LIBDIR)/$${lib%.so.*}.so; done + +clean: + @rm -rf $(OBJDIR) + @rm -rf $(BUILDDIR) diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..ce0ac9d --- /dev/null +++ b/NOTICE @@ -0,0 +1,15 @@ +libinum +Copyright 2016 Ian Jauslin + +Numerical values may be represented as multi-precision floats using +the GNU MPFR library, which is licensed under the GNU Lesser General +Public License (LGPL) version 3 (see LGPL3 for a copy of the license). +See + http://www.mpfr.org/ +for details. + +The GNU MPFR library is based on the GNU GMP library, which is licensed +under the GNU Lesser General Public License (LGPL) version 3 (see LGPL3 +for a copy of the license). See + http://www.gmplib.org/ +for details. diff --git a/doc/libinum-doc.html b/doc/libinum-doc.html new file mode 100644 index 0000000..e50e8f7 --- /dev/null +++ b/doc/libinum-doc.html @@ -0,0 +1,2332 @@ + + + + + + + + + + +

libinum v1.0

+ +

+ This is the official documentation for libinum, version 1.0. +

+ +

Table of contents

+ + +

Description

+

+ libinum is a C library that implements several algorithms, intended for applications in numerical and symbolic computations. +

+

+ libinum defines functions to perform the following tasks: +

+ In addition, libinum defines the following structures: + +

+ +

+ As a general rule, the functions and structures can be used with several different data types. For instance, the root finding and integration functions can manipulate double precision, extended precision, or multi-precision floating point numbers. See Data types for details. +

+ +

Data types

+

+ One of the guiding principles of libinum is that it should should not push a specific data type on users. If, hypothetically, the computation being carried out requires the precision of the floating point numbers to be large, then multi-precision floating point numbers might be preferable to double precision numbers. If, instead, computation time is more important than precision, then extended precision floats may be preferred. The structures and functions defined in libinum, therefore, support various data types. +

+ +

+ We will now briefly discuss the data types that may be used to represent numbers. +

+ + +

Integers

+

Fixed-precision integers

+

+ The standard integer types of the C language are char, short int, int, long int and long long int, as well as their unsigned counterparts. The number of bits, and, in consequence, the available range of integers, of each of these types, is platform-dependent. In cases where this could be problematic, fixed-precision integers can be used: with 8 bits: int8_t, 16: int16_t, 32: int32_t and 64: int64_t. +

+

+ libinum provides a function, print_datatype_info(), that prints the equivalence table between char, short int, int, long int and long long int and int8_t, int16_t, int32_t and int64_t for the implementation of the C library used to compile libinum. +

+ +

Multi-precision integers

+

+ In cases where the required number of bits of integers exceeds 64, one can use multi-precision integers, which can be as large as will fit in memory. However, computation times can greatly increase when using multi-precision integers. The implementation used in libinum is provided by the GNU GMP library. +

+ + + +

Floating point numbers

+

+ Let us first start with a description of floating point numbers. A real number \(x\) is approximated, with a precision of \(m\) bits, as a collection of three numbers: the sign \(s\in\{-1,1\}\); mantissa, whose binary expansion is denoted by \(a_1\cdots a_m\); and exponent \(e\in\mathbb Z\). The approximate value of the number \(x\) is obtained from its sign, mantissa and exponent by + $$ + x=s\times a_1.a_2\cdots a_m\times2^e. + $$ + The precision of a floating point number is the number of bits allocated to its sign and mantissa (since \(a_1\) is necessarily equal to \(1\), it is not stored, so the precision of \(x\) is \(m\) instead of \(m+1\)). +

+

In libinum, floating point number may either be double, extended or multi-precision numbers. + +

Double precision floating point numbers

+

+ Double precision floats are represented using the double type. +

+

+ The precision and maximal and minimal values of the exponent of double floats depends on the compiler. Their values can be printed using the libinum function print_datatype_info(). +

+

+ For example, using version 5.3.0 of the GNU GCC compiler on the x86-64 architecture, the precision is of 53 bits (i.e. 15 decimal digits), and the maximal and minimal values of the exponent are 1024 and -1021. +

+ +

Extended precision floating point numbers

+

+ Extended precision floats are represented using the long double type. They require more memory than double precision floats, and, as a consequence, slightly more computation time. +

+

+ The precision and maximal and minimal values of the exponent of long double floats depends on the compiler. Their values can be printed using the libinum function print_datatype_info(). +

+

+ For example, using version 5.3.0 of the GNU GCC compiler on the x86-64 architecture, the precision is of 64 bits (i.e. 18 decimal digits), and the maximal and minimal values of the exponent are 16384 and -16381. +

+ +

Multi-precision floating point numbers

+

+ A multi-precision floating point number is a floating point number whose precision can be set to an arbitrary value (until the number fills up the entire memory of the computer). In libinum, multi-precision floats are implemented using the GNU MPFR library, and will be called MPFR floats. +

+

+ The precision of MPFR floats can be set using the function mpfr_set_default_prec (see the MPFR library documentation for details). The default value of the precision is 53 bits (as of version 3.1.4 of the MPFR library). +

+

+ In addition, the maximal value (in absolute value) of the exponent can be controlled by setting emax using the function mpfr_set_emax (see the MPFR library documentation for details). +

+

+ Depending on the MPFR implementation, the precision and emax can either be an int or a long int, so its maximal and minimal values are platform dependent. The libinum function print_datatype_info() prints which it is. +

+ + +

Algorithms

+

+ What follows is a detailed description of the functions that implement the algorithms supported by libinum. +

+ +

Root finding

+

+ libinum can compute the root of smooth real functions using the Newton-Raphson algorithm. The algorithm converges quickly, provided an adequate approximation of the root in provided, and that the root is not located at a minimum of the function. +

+ +

Description

+

+ Given a function \(f\) and a real number \(x_0\), the algorithm produces a sequence \((x_n)\) which, provided the algorithm converges, tends to a root of \(f\). The sequence is defined as + $$ + x_{n+1}=x_n-\frac{f(x_{n})}{f'(x_{n})} + $$ + where \(f'\) denotes the derivative of \(f\). The following estimate holds: + $$ + |x_{n+1}-x_n|\leqslant \frac12|x_n-x_{n-1}|^2\sup_{\xi\in[x_{n+1},x_n]}\frac{f''(\xi)}{f'(\xi)} + $$ + so that, provided \(f\) is smooth and its derivative does not vanish in the intervals \([x_{n+1},x_n]\), the algorithm converges quadratically. +

+ +

Implementation

+

+ This algorithm has been implemented using double, extended and multi-precision floats. +

+ + + +

Integrals

+

+ libinum can compute definite integrals of smooth real functions using Gauss-Legendre quadratures. +

+ +

Description

+

+ The main idea of Gauss-Legendre quadratures is to find a set of abcissa \(\{t_1,\cdots,t_N\}\in[-1,1]^N\) and weights \(\{w_1,\cdots,w_N\}\in\mathbb R^N\) such that, if \(f\) were a polynomial of degree \(\leqslant2N-1\), then the integral would be equal to a discrete sum: + $$ + \int_{a}^bdx\ f(x)=\sum_{i=1}^Nw_if\left(\frac{a+b}2+t_i\frac{b-a}2\right). + $$ + In the general case, if \(f\) is \(\mathcal C^{2N}\), then + $$ + \left|\int_{a}^bdx\ f(x)-\sum_{i=1}^Nw_if\left(\frac{a+b}2+t_i\frac{b-a}2\right)\right| + \leqslant \frac{N!^4}{(2N+1)(2N)!^3}\sup_{x\in[a,b]}\frac{d^{2N}f(x)}{dx^{2N}}. + $$ + The number \(N\) is called the order of the integration. +

+ +

+ As it turns out, the abcissa are the roots of the \(N\)-th Legendre polynomial \(L_N\), defined by the recursive equation + $$ + (N+1)L_{N+1}(x)=(2N+1)xL_N(x)-NL_{N-1}(x),\quad + L_0(x)=1,\quad L_1(x)=x + $$ + and the weights are + $$ + w_i=\frac2{(1-x_i^2)L_N'(t_i)}. + $$ +

+ +

Implementation

+

+ This algorithm has been implemented using double, extended and multi-precision floats +

+ +

Types

+

+ What follows is a detailed description of the C types defined in libinum. +

+ +

array

+

Array that can be dynamically resized.

+ + +

Structure

+ Arrays of several types of objects are defined. They are structures, with the following keys: + + + where the keys correspond to + + array_*'s must be initialized before they are used, and freed when they are no longer needed. khen the array is freed, the objects it contains are freed as well. + +

Functions

+ + +

Extra functions for arrays of ordered objects

+ In addition, we define the following ordering for the possible objects in an array: + + If the ordering is defined, then the following functions are defined: + + + +

Extra functions for array_char

+ In addition, the following functions are defined for array_char. + + + +

polynomial

+

A polynomial with real coefficients. A polynomial is represented as an array of coefficients and an array of exponents. For example \(1+x+2x^2\) is represented as ({1.,1.,2.},{0,1,2}).

+ +

Structure

+ The coefficients of polynomials can be double, extended, or multi-precision floats, and their exponents are unsigned integers. Polynomials are represented as structures, with the following keys: + + + + where the keys correspond to + + polynomial_*'s must be initialized before they are used, and freed when they are no longer needed. + +

Functions

+ + +

polynomialMV

+

Multi-variable polynomials. A polynomial is represented as an array of coefficients and an array of arrays of indices, each of which represents a variable. For example, \(3x_1^2+x_1x_2\) is represented as ({3,1},{{1,1},{1,2}}).

+ + +

Structure

+ The coefficients of multi-variable polynomials can be integers or multiprecision integers, and the indices are integers. Multi-variable polynomials are represented as structures, with the following keys: + + where the keys correspond to + + polynomialMV_*'s must be initialized before they are used, and freed when they are no longer needed. When the polynomial is freed, its coefficients and factors are freed as well. + +

Functions

+ + + +

Functions

+

+ Functions are not, strictly speaking, a C type. Instead, in this section, we describe how functions whose pointers are to be passed to the various algorithms of libinum, should be formatted. +

+ +

+ Functions must take 3 arguments, and return an integer (a return code): + int f(TYPE* out, TYPE in, void* extra_args) + where TYPE is the appropriate data type, e.g. double or mpfr_t. in is the argument of the function, out is made to point to \(f(\)in\()\), and extra_args is a pointer to void, which may contain extra arguments to be passed to the function, for example a parameter that the function depends on. For example, to implement the function \(f_\alpha(x)=x^\alpha\) using MPFR floats, + +

+int f(mpfr_t out, mpfr_t in, void* extra_args){
+  int alpha=*((*int)extra_args);
+  mpfr_pow_ui(out, in, alpha, MPFR_RNDN);
+  return(0);
+}
+
+ +

+ +

Utilities

+

+ In addition, the following functions are defined +

+ + + +

Examples

+

+ Examples of programs using libinum are provided with the source code, in the doc/libinum-examples directory. If libinum is installed on the filesystem, then the examples can also be found at /usr/share/doc/libinum/libinum-examples. +

+ + +

Authors

+

+ libinum was written by Ian Jauslin. +

+ + diff --git a/doc/libinum-doc.php b/doc/libinum-doc.php new file mode 100644 index 0000000..2daf429 --- /dev/null +++ b/doc/libinum-doc.php @@ -0,0 +1,1835 @@ + + + + + + + + + + +

libinum v1.0

+ +

+ This is the official documentation for libinum, version 1.0. +

+ +

Table of contents

+ + +

Description

+

+ libinum is a C library that implements several algorithms, intended for applications in numerical and symbolic computations. +

+

+ libinum defines functions to perform the following tasks: +

+ In addition, libinum defines the following structures: + +

+ +

+ As a general rule, the functions and structures can be used with several different data types. For instance, the root finding and integration functions can manipulate double precision, extended precision, or multi-precision floating point numbers. See Data types for details. +

+ +

Data types

+

+ One of the guiding principles of libinum is that it should should not push a specific data type on users. If, hypothetically, the computation being carried out requires the precision of the floating point numbers to be large, then multi-precision floating point numbers might be preferable to double precision numbers. If, instead, computation time is more important than precision, then extended precision floats may be preferred. The structures and functions defined in libinum, therefore, support various data types. +

+ +

+ We will now briefly discuss the data types that may be used to represent numbers. +

+ + +

Integers

+

Fixed-precision integers

+

+ The standard integer types of the C language are char, short int, int, long int and long long int, as well as their unsigned counterparts. The number of bits, and, in consequence, the available range of integers, of each of these types, is platform-dependent. In cases where this could be problematic, fixed-precision integers can be used: with 8 bits: int8_t, 16: int16_t, 32: int32_t and 64: int64_t. +

+

+ libinum provides a function, print_datatype_info(), that prints the equivalence table between char, short int, int, long int and long long int and int8_t, int16_t, int32_t and int64_t for the implementation of the C library used to compile libinum. +

+ +

Multi-precision integers

+

+ In cases where the required number of bits of integers exceeds 64, one can use multi-precision integers, which can be as large as will fit in memory. However, computation times can greatly increase when using multi-precision integers. The implementation used in libinum is provided by the GNU GMP library. +

+ + + +

Floating point numbers

+

+ Let us first start with a description of floating point numbers. A real number \(x\) is approximated, with a precision of \(m\) bits, as a collection of three numbers: the sign \(s\in\{-1,1\}\); mantissa, whose binary expansion is denoted by \(a_1\cdots a_m\); and exponent \(e\in\mathbb Z\). The approximate value of the number \(x\) is obtained from its sign, mantissa and exponent by + $$ + x=s\times a_1.a_2\cdots a_m\times2^e. + $$ + The precision of a floating point number is the number of bits allocated to its sign and mantissa (since \(a_1\) is necessarily equal to \(1\), it is not stored, so the precision of \(x\) is \(m\) instead of \(m+1\)). +

+

In libinum, floating point number may either be double, extended or multi-precision numbers. + +

Double precision floating point numbers

+

+ Double precision floats are represented using the double type. +

+

+ The precision and maximal and minimal values of the exponent of double floats depends on the compiler. Their values can be printed using the libinum function print_datatype_info(). +

+

+ For example, using version 5.3.0 of the GNU GCC compiler on the x86-64 architecture, the precision is of 53 bits (i.e. 15 decimal digits), and the maximal and minimal values of the exponent are 1024 and -1021. +

+ +

Extended precision floating point numbers

+

+ Extended precision floats are represented using the long double type. They require more memory than double precision floats, and, as a consequence, slightly more computation time. +

+

+ The precision and maximal and minimal values of the exponent of long double floats depends on the compiler. Their values can be printed using the libinum function print_datatype_info(). +

+

+ For example, using version 5.3.0 of the GNU GCC compiler on the x86-64 architecture, the precision is of 64 bits (i.e. 18 decimal digits), and the maximal and minimal values of the exponent are 16384 and -16381. +

+ +

Multi-precision floating point numbers

+

+ A multi-precision floating point number is a floating point number whose precision can be set to an arbitrary value (until the number fills up the entire memory of the computer). In libinum, multi-precision floats are implemented using the GNU MPFR library, and will be called MPFR floats. +

+

+ The precision of MPFR floats can be set using the function mpfr_set_default_prec (see the MPFR library documentation for details). The default value of the precision is 53 bits (as of version 3.1.4 of the MPFR library). +

+

+ In addition, the maximal value (in absolute value) of the exponent can be controlled by setting emax using the function mpfr_set_emax (see the MPFR library documentation for details). +

+

+ Depending on the MPFR implementation, the precision and emax can either be an int or a long int, so its maximal and minimal values are platform dependent. The libinum function print_datatype_info() prints which it is. +

+ + +

Algorithms

+

+ What follows is a detailed description of the functions that implement the algorithms supported by libinum. +

+ +

Root finding

+

+ libinum can compute the root of smooth real functions using the Newton-Raphson algorithm. The algorithm converges quickly, provided an adequate approximation of the root in provided, and that the root is not located at a minimum of the function. +

+ +

Description

+

+ Given a function \(f\) and a real number \(x_0\), the algorithm produces a sequence \((x_n)\) which, provided the algorithm converges, tends to a root of \(f\). The sequence is defined as + $$ + x_{n+1}=x_n-\frac{f(x_{n})}{f'(x_{n})} + $$ + where \(f'\) denotes the derivative of \(f\). The following estimate holds: + $$ + |x_{n+1}-x_n|\leqslant \frac12|x_n-x_{n-1}|^2\sup_{\xi\in[x_{n+1},x_n]}\frac{f''(\xi)}{f'(\xi)} + $$ + so that, provided \(f\) is smooth and its derivative does not vanish in the intervals \([x_{n+1},x_n]\), the algorithm converges quadratically. +

+ +

Implementation

+

+ This algorithm has been implemented using double, extended and multi-precision floats. + +

+ + + +

Integrals

+

+ libinum can compute definite integrals of smooth real functions using Gauss-Legendre quadratures. +

+ +

Description

+

+ The main idea of Gauss-Legendre quadratures is to find a set of abcissa \(\{t_1,\cdots,t_N\}\in[-1,1]^N\) and weights \(\{w_1,\cdots,w_N\}\in\mathbb R^N\) such that, if \(f\) were a polynomial of degree \(\leqslant2N-1\), then the integral would be equal to a discrete sum: + $$ + \int_{a}^bdx\ f(x)=\sum_{i=1}^Nw_if\left(\frac{a+b}2+t_i\frac{b-a}2\right). + $$ + In the general case, if \(f\) is \(\mathcal C^{2N}\), then + $$ + \left|\int_{a}^bdx\ f(x)-\sum_{i=1}^Nw_if\left(\frac{a+b}2+t_i\frac{b-a}2\right)\right| + \leqslant \frac{N!^4}{(2N+1)(2N)!^3}\sup_{x\in[a,b]}\frac{d^{2N}f(x)}{dx^{2N}}. + $$ + The number \(N\) is called the order of the integration. +

+ +

+ As it turns out, the abcissa are the roots of the \(N\)-th Legendre polynomial \(L_N\), defined by the recursive equation + $$ + (N+1)L_{N+1}(x)=(2N+1)xL_N(x)-NL_{N-1}(x),\quad + L_0(x)=1,\quad L_1(x)=x + $$ + and the weights are + $$ + w_i=\frac2{(1-x_i^2)L_N'(t_i)}. + $$ +

+ +

Implementation

+

+ This algorithm has been implemented using double, extended and multi-precision floats + +

+ +

Types

+

+ What follows is a detailed description of the C types defined in libinum. +

+ +

array

+

Array that can be dynamically resized.

+ + + +

Structure

+ Arrays of several types of objects are defined. They are structures, with the following keys: + + + where the keys correspond to + + array_*'s must be initialized before they are used, and freed when they are no longer needed. khen the array is freed, the objects it contains are freed as well. + +

Functions

+ + +

Extra functions for arrays of ordered objects

+ In addition, we define the following ordering for the possible objects in an array: + + If the ordering is defined, then the following functions are defined: + + + +

Extra functions for array_char

+ In addition, the following functions are defined for array_char. + + + +

polynomial

+

A polynomial with real coefficients. A polynomial is represented as an array of coefficients and an array of exponents. For example \(1+x+2x^2\) is represented as ({1.,1.,2.},{0,1,2}).

+ +

Structure

+ The coefficients of polynomials can be double, extended, or multi-precision floats, and their exponents are unsigned integers. Polynomials are represented as structures, with the following keys: + + + + + where the keys correspond to + + polynomial_*'s must be initialized before they are used, and freed when they are no longer needed. + +

Functions

+ + +

polynomialMV

+

Multi-variable polynomials. A polynomial is represented as an array of coefficients and an array of arrays of indices, each of which represents a variable. For example, \(3x_1^2+x_1x_2\) is represented as ({3,1},{{1,1},{1,2}}).

+ + + +

Structure

+ The coefficients of multi-variable polynomials can be integers or multiprecision integers, and the indices are integers. Multi-variable polynomials are represented as structures, with the following keys: + + where the keys correspond to + + polynomialMV_*'s must be initialized before they are used, and freed when they are no longer needed. When the polynomial is freed, its coefficients and factors are freed as well. + +

Functions

+ + + +

Functions

+

+ Functions are not, strictly speaking, a C type. Instead, in this section, we describe how functions whose pointers are to be passed to the various algorithms of libinum, should be formatted. +

+ +

+ Functions must take 3 arguments, and return an integer (a return code): + int f(TYPE* out, TYPE in, void* extra_args) + where TYPE is the appropriate data type, e.g. double or mpfr_t. in is the argument of the function, out is made to point to \(f(\)in\()\), and extra_args is a pointer to void, which may contain extra arguments to be passed to the function, for example a parameter that the function depends on. For example, to implement the function \(f_\alpha(x)=x^\alpha\) using MPFR floats, + +

+int f(mpfr_t out, mpfr_t in, void* extra_args){
+  int alpha=*((*int)extra_args);
+  mpfr_pow_ui(out, in, alpha, MPFR_RNDN);
+  return(0);
+}
+
+ +

+ +

Utilities

+

+ In addition, the following functions are defined +

+ + + +

Examples

+

+ Examples of programs using libinum are provided with the source code, in the doc/libinum-examples directory. If libinum is installed on the filesystem, then the examples can also be found at /usr/share/doc/libinum/libinum-examples. +

+ + +

Authors

+

+ libinum was written by Ian Jauslin. +

+ + diff --git a/doc/libinum-examples/Makefile b/doc/libinum-examples/Makefile new file mode 100644 index 0000000..5c7ccc1 --- /dev/null +++ b/doc/libinum-examples/Makefile @@ -0,0 +1,80 @@ +# whether to link dynamically +# if static=0 then link dynamically +# if static=2 then link statically +# if static=1 then link libkondo statically but other libraries dynamically +STATIC=1 + +VERSION=0.3.1 + +# products of the compilation +PROJECT_BINS= root_newton integral_gauss-legendre + +# debug and optimization flags +#DB= -ggdb +OPT= -O3 + +# warning flags +WARNINGS= -Wall -Wextra -Wno-strict-overflow -std=c99 -pedantic -Wno-unused-parameter + +# compiler +CC=/usr/bin/gcc +LD=$(CC) + +# directories +INCLUDE = +LIB = +#INCLUDE = -I../../include +#LIB = -L../../build + +# flags +override LDFLAGS +=$(LIB) +override CFLAGS +=$(INCLUDE) $(DB) $(OPT) $(WARNINGS) + +# build directories +BUILDDIR=./build +SRCDIR=./src +OBJDIR=./objs + +# objects +OBJS = $(addprefix $(OBJDIR)/, root_newton.o integral_gauss-legendre.o) + +# flags which depend on whether to link statically or dynamically +# lib flag +LIBINUM_FLAG=-linum +# additional library required for static linking +XTRA_LIBS= + +ifeq ($(STATIC),1) + # libinum is linked against libm, libmpfr, libgmp and libpthread + XTRA_LIBS=-lm -lmpfr -lgmp -lpthread + # link binaries using the static library + LIBINUM_FLAG=-l:libinum.a +else ifeq ($(STATIC),2) + # libinum is linked against libm, libmpfr, libgmp and libpthread + XTRA_LIBS=-lm -lmpfr -lgmp -lpthread + # link binaries statically + override LDFLAGS += -static +else + # required flag for subsequent dynamic linking + override CFLAGS += -fPIC +endif + + +all: init $(PROJECT_BINS) + +# create dirs +init: + @[ -d $(OBJDIR) ] || /bin/mkdir $(OBJDIR) + @[ -d $(BUILDDIR) ] || /bin/mkdir $(BUILDDIR) + +root_newton: + $(CC) -c $(CFLAGS) src/$@.c -o objs/$@.o + $(LD) $(LDFLAGS) -o $(BUILDDIR)/$@ objs/$@.o $(LIBINUM_FLAG) $(XTRA_LIBS) + +integral_gauss-legendre: + $(CC) -c $(CFLAGS) src/$@.c -o objs/$@.o + $(LD) $(LDFLAGS) -o $(BUILDDIR)/$@ objs/$@.o $(LIBINUM_FLAG) $(XTRA_LIBS) + +clean: + @rm -rf $(OBJDIR) + @rm -rf $(BUILDDIR) diff --git a/doc/libinum-examples/build/integral_gauss-legendre b/doc/libinum-examples/build/integral_gauss-legendre new file mode 100755 index 0000000..b86cfe7 Binary files /dev/null and b/doc/libinum-examples/build/integral_gauss-legendre differ diff --git a/doc/libinum-examples/build/root_newton b/doc/libinum-examples/build/root_newton new file mode 100755 index 0000000..68a2e50 Binary files /dev/null and b/doc/libinum-examples/build/root_newton differ diff --git a/doc/libinum-examples/objs/integral_gauss-legendre.o b/doc/libinum-examples/objs/integral_gauss-legendre.o new file mode 100644 index 0000000..83841c9 Binary files /dev/null and b/doc/libinum-examples/objs/integral_gauss-legendre.o differ diff --git a/doc/libinum-examples/objs/root_newton.o b/doc/libinum-examples/objs/root_newton.o new file mode 100644 index 0000000..1e567f7 Binary files /dev/null and b/doc/libinum-examples/objs/root_newton.o differ diff --git a/doc/libinum-examples/src/integral_gauss-legendre.c b/doc/libinum-examples/src/integral_gauss-legendre.c new file mode 100644 index 0000000..f65465f --- /dev/null +++ b/doc/libinum-examples/src/integral_gauss-legendre.c @@ -0,0 +1,92 @@ +#include +#include +// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears +#define MPFR_USE_VA_LIST +#include +#include + +int func(mpfr_t* out, mpfr_t in, void* extra_args); + +int main(int argc, const char* argv[]){ + int ret; + mpfr_t tolerance, val, lower, upper; + array_mpfr abcissa, weights; + + // precision of MPFR floats + mpfr_set_default_prec(53); + + // tolerance for computing weights + mpfr_init(tolerance); + mpfr_set_d(tolerance, 1.e-11, MPFR_RNDN); + + // compute weights + ret=gauss_legendre_weights_mpfr(10, tolerance, 10000, &abcissa, &weights); + // return codes + if(ret==LIBINUM_ERROR_MAXITER){ + printf("error: maximum number of iterations reached when computing the integration abcissa\n"); + mpfr_clear(tolerance); + return(ret); + } + else if(ret==LIBINUM_ERROR_NAN){ + printf("error: infinity encountered when computing the integration abcissa\n"); + mpfr_clear(tolerance); + return(ret); + } + else{ + printf("abcissa:\n"); + array_mpfr_print(abcissa); + printf("\nweights:\n"); + array_mpfr_print(weights); + printf("\n"); + } + + mpfr_clear(tolerance); + + mpfr_inits(val, lower, upper, NULL); + + mpfr_set_ui(lower, 0, MPFR_RNDN); + mpfr_set_ui(upper, 2, MPFR_RNDN); + + ret=integrate_gauss_mpfr(&val, &func, lower, upper, abcissa, weights, NULL); + // return codes + if(ret==LIBINUM_ERROR_SIZE_MISMATCH){ + printf("error: the mpfr_arrays 'abcissa' and 'weights' must have the same length\n"); + mpfr_clears(val, upper, lower, NULL); + array_mpfr_free(abcissa); + array_mpfr_free(weights); + return(ret); + } + else if(ret==LIBINUM_ERROR_NAN){ + printf("error: the integrand is singular\n"); + array_mpfr_free(abcissa); + array_mpfr_free(weights); + mpfr_clears(val, upper, lower, NULL); + return(ret); + } + else{ + printf("\\int_0^2 x/sin(x) dx = "); + fprint_mpfr(stdout, val); + printf("\n"); + } + + mpfr_clears(val, upper, lower, NULL); + array_mpfr_free(abcissa); + array_mpfr_free(weights); + + return(0); +} + +/* +// e^x +int func(mpfr_t* out, mpfr_t in, void* extra_args){ + mpfr_exp(*out, in, MPFR_RNDN); + return(0); +} +*/ + +// x/sin(x) +int func(mpfr_t* out, mpfr_t in, void* extra_args){ + mpfr_sin(*out, in, MPFR_RNDN); + mpfr_div(*out, in, *out, MPFR_RNDN); + return(0); +} diff --git a/doc/libinum-examples/src/root_newton.c b/doc/libinum-examples/src/root_newton.c new file mode 100644 index 0000000..f307c58 --- /dev/null +++ b/doc/libinum-examples/src/root_newton.c @@ -0,0 +1,58 @@ +#include +#include +// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears +#define MPFR_USE_VA_LIST +#include +#include + +int func(mpfr_t* out, mpfr_t in, void* extra_args); +int dfunc(mpfr_t* out, mpfr_t in, void* extra_args); + +int main(int argc, const char* argv[]){ + int ret; + mpfr_t init, prec, x; + unsigned int maxiter; + int extra_arg; + + mpfr_inits(init, prec, x, NULL); + + // start at 2 + mpfr_set_ui(init, 2, MPFR_RNDN); + // precision + mpfr_set_d(prec, 1.e-30, MPFR_RNDN); + // maximum number of iterations before error + maxiter=10000; + // extra argument + extra_arg=4; + + // compute root + ret=root_newton_mpfr(&x, &func, &dfunc, init, prec, maxiter, &extra_arg); + + // return codes + if(ret==LIBINUM_ERROR_MAXITER){ + printf("error: maximum number of iterations reached\n"); + } + else if(ret==LIBINUM_ERROR_NAN){ + printf("error: infinity encountered\n"); + } + else{ + mpfr_printf("% 14.7Re\n", x); + } + + mpfr_clears(init, prec, x, NULL); + + return(0); +} + +// x^2-1 +int func(mpfr_t* out, mpfr_t in, void* extra_args){ + mpfr_pow_ui(*out, in, 2, MPFR_RNDN); + mpfr_add_d(*out, *out, -1./ *((int*)extra_args), MPFR_RNDN); + return(0); +} + +// 2*x +int dfunc(mpfr_t* out, mpfr_t in, void* extra_args){ + mpfr_mul_ui(*out, in, 2, MPFR_RNDN); + return(0); +} diff --git a/include/libinum.h b/include/libinum.h new file mode 100644 index 0000000..bd510cc --- /dev/null +++ b/include/libinum.h @@ -0,0 +1,20 @@ +#ifndef LIBINUM_H +#define LIBINUM_H + +#define LIBINUM_VERSION "1.0" + +#include + +#include + +#include +#include + +#include + +#include +#include +#include + + +#endif diff --git a/include/libinum/array.h b/include/libinum/array.h new file mode 120000 index 0000000..5071e5e --- /dev/null +++ b/include/libinum/array.h @@ -0,0 +1 @@ +../../src/array.h \ No newline at end of file diff --git a/include/libinum/array_2_mpfr.h b/include/libinum/array_2_mpfr.h new file mode 120000 index 0000000..1538fe7 --- /dev/null +++ b/include/libinum/array_2_mpfr.h @@ -0,0 +1 @@ +../../src/array_2_mpfr.h \ No newline at end of file diff --git a/include/libinum/array_base.h b/include/libinum/array_base.h new file mode 120000 index 0000000..263b8eb --- /dev/null +++ b/include/libinum/array_base.h @@ -0,0 +1 @@ +../../src/array_base.h \ No newline at end of file diff --git a/include/libinum/array_char.h b/include/libinum/array_char.h new file mode 120000 index 0000000..5023391 --- /dev/null +++ b/include/libinum/array_char.h @@ -0,0 +1 @@ +../../src/array_char.h \ No newline at end of file diff --git a/include/libinum/array_double.h b/include/libinum/array_double.h new file mode 120000 index 0000000..c53088c --- /dev/null +++ b/include/libinum/array_double.h @@ -0,0 +1 @@ +../../src/array_double.h \ No newline at end of file diff --git a/include/libinum/array_int.h b/include/libinum/array_int.h new file mode 120000 index 0000000..3ebf9b3 --- /dev/null +++ b/include/libinum/array_int.h @@ -0,0 +1 @@ +../../src/array_int.h \ No newline at end of file diff --git a/include/libinum/array_ldouble.h b/include/libinum/array_ldouble.h new file mode 120000 index 0000000..64db9f3 --- /dev/null +++ b/include/libinum/array_ldouble.h @@ -0,0 +1 @@ +../../src/array_ldouble.h \ No newline at end of file diff --git a/include/libinum/array_mpfr.h b/include/libinum/array_mpfr.h new file mode 120000 index 0000000..ab0c709 --- /dev/null +++ b/include/libinum/array_mpfr.h @@ -0,0 +1 @@ +../../src/array_mpfr.h \ No newline at end of file diff --git a/include/libinum/array_polynomial_double.h b/include/libinum/array_polynomial_double.h new file mode 120000 index 0000000..fc99294 --- /dev/null +++ b/include/libinum/array_polynomial_double.h @@ -0,0 +1 @@ +../../src/array_polynomial_double.h \ No newline at end of file diff --git a/include/libinum/array_polynomial_ldouble.h b/include/libinum/array_polynomial_ldouble.h new file mode 120000 index 0000000..90eb96e --- /dev/null +++ b/include/libinum/array_polynomial_ldouble.h @@ -0,0 +1 @@ +../../src/array_polynomial_ldouble.h \ No newline at end of file diff --git a/include/libinum/array_polynomial_mpfr.h b/include/libinum/array_polynomial_mpfr.h new file mode 120000 index 0000000..8529b91 --- /dev/null +++ b/include/libinum/array_polynomial_mpfr.h @@ -0,0 +1 @@ +../../src/array_polynomial_mpfr.h \ No newline at end of file diff --git a/include/libinum/array_pthread_t.h b/include/libinum/array_pthread_t.h new file mode 120000 index 0000000..5cd5570 --- /dev/null +++ b/include/libinum/array_pthread_t.h @@ -0,0 +1 @@ +../../src/array_pthread_t.h \ No newline at end of file diff --git a/include/libinum/array_str.h b/include/libinum/array_str.h new file mode 120000 index 0000000..894bc38 --- /dev/null +++ b/include/libinum/array_str.h @@ -0,0 +1 @@ +../../src/array_str.h \ No newline at end of file diff --git a/include/libinum/array_type.h b/include/libinum/array_type.h new file mode 120000 index 0000000..d33d613 --- /dev/null +++ b/include/libinum/array_type.h @@ -0,0 +1 @@ +../../src/array_type.h \ No newline at end of file diff --git a/include/libinum/array_uint.h b/include/libinum/array_uint.h new file mode 120000 index 0000000..8702167 --- /dev/null +++ b/include/libinum/array_uint.h @@ -0,0 +1 @@ +../../src/array_uint.h \ No newline at end of file diff --git a/include/libinum/errors.h b/include/libinum/errors.h new file mode 120000 index 0000000..813a959 --- /dev/null +++ b/include/libinum/errors.h @@ -0,0 +1 @@ +../../src/errors.h \ No newline at end of file diff --git a/include/libinum/integral.h b/include/libinum/integral.h new file mode 120000 index 0000000..8649ee4 --- /dev/null +++ b/include/libinum/integral.h @@ -0,0 +1 @@ +../../src/integral.h \ No newline at end of file diff --git a/include/libinum/integral_base.h b/include/libinum/integral_base.h new file mode 120000 index 0000000..b1e8d7e --- /dev/null +++ b/include/libinum/integral_base.h @@ -0,0 +1 @@ +../../src/integral_base.h \ No newline at end of file diff --git a/include/libinum/integral_double.h b/include/libinum/integral_double.h new file mode 120000 index 0000000..4b6bf23 --- /dev/null +++ b/include/libinum/integral_double.h @@ -0,0 +1 @@ +../../src/integral_double.h \ No newline at end of file diff --git a/include/libinum/integral_ldouble.h b/include/libinum/integral_ldouble.h new file mode 120000 index 0000000..e2b597e --- /dev/null +++ b/include/libinum/integral_ldouble.h @@ -0,0 +1 @@ +../../src/integral_ldouble.h \ No newline at end of file diff --git a/include/libinum/integral_mpfr.h b/include/libinum/integral_mpfr.h new file mode 120000 index 0000000..8b709df --- /dev/null +++ b/include/libinum/integral_mpfr.h @@ -0,0 +1 @@ +../../src/integral_mpfr.h \ No newline at end of file diff --git a/include/libinum/polynomial.h b/include/libinum/polynomial.h new file mode 120000 index 0000000..b1969f9 --- /dev/null +++ b/include/libinum/polynomial.h @@ -0,0 +1 @@ +../../src/polynomial.h \ No newline at end of file diff --git a/include/libinum/polynomialMV.h b/include/libinum/polynomialMV.h new file mode 120000 index 0000000..ab198e6 --- /dev/null +++ b/include/libinum/polynomialMV.h @@ -0,0 +1 @@ +../../src/polynomialMV.h \ No newline at end of file diff --git a/include/libinum/polynomialMV_base.h b/include/libinum/polynomialMV_base.h new file mode 120000 index 0000000..f81b0d2 --- /dev/null +++ b/include/libinum/polynomialMV_base.h @@ -0,0 +1 @@ +../../src/polynomialMV_base.h \ No newline at end of file diff --git a/include/libinum/polynomialMV_int.h b/include/libinum/polynomialMV_int.h new file mode 120000 index 0000000..4ff8e55 --- /dev/null +++ b/include/libinum/polynomialMV_int.h @@ -0,0 +1 @@ +../../src/polynomialMV_int.h \ No newline at end of file diff --git a/include/libinum/polynomialMV_mpz.h b/include/libinum/polynomialMV_mpz.h new file mode 120000 index 0000000..dad4fb1 --- /dev/null +++ b/include/libinum/polynomialMV_mpz.h @@ -0,0 +1 @@ +../../src/polynomialMV_mpz.h \ No newline at end of file diff --git a/include/libinum/polynomialMV_type.h b/include/libinum/polynomialMV_type.h new file mode 120000 index 0000000..6a1e262 --- /dev/null +++ b/include/libinum/polynomialMV_type.h @@ -0,0 +1 @@ +../../src/polynomialMV_type.h \ No newline at end of file diff --git a/include/libinum/polynomial_base.h b/include/libinum/polynomial_base.h new file mode 120000 index 0000000..dbb75ea --- /dev/null +++ b/include/libinum/polynomial_base.h @@ -0,0 +1 @@ +../../src/polynomial_base.h \ No newline at end of file diff --git a/include/libinum/polynomial_double.h b/include/libinum/polynomial_double.h new file mode 120000 index 0000000..43f3379 --- /dev/null +++ b/include/libinum/polynomial_double.h @@ -0,0 +1 @@ +../../src/polynomial_double.h \ No newline at end of file diff --git a/include/libinum/polynomial_ldouble.h b/include/libinum/polynomial_ldouble.h new file mode 120000 index 0000000..4d5e231 --- /dev/null +++ b/include/libinum/polynomial_ldouble.h @@ -0,0 +1 @@ +../../src/polynomial_ldouble.h \ No newline at end of file diff --git a/include/libinum/polynomial_mpfr.h b/include/libinum/polynomial_mpfr.h new file mode 120000 index 0000000..ab26be9 --- /dev/null +++ b/include/libinum/polynomial_mpfr.h @@ -0,0 +1 @@ +../../src/polynomial_mpfr.h \ No newline at end of file diff --git a/include/libinum/polynomial_type.h b/include/libinum/polynomial_type.h new file mode 120000 index 0000000..22cd70d --- /dev/null +++ b/include/libinum/polynomial_type.h @@ -0,0 +1 @@ +../../src/polynomial_type.h \ No newline at end of file diff --git a/include/libinum/root.h b/include/libinum/root.h new file mode 120000 index 0000000..4bf0b8d --- /dev/null +++ b/include/libinum/root.h @@ -0,0 +1 @@ +../../src/root.h \ No newline at end of file diff --git a/include/libinum/root_base.h b/include/libinum/root_base.h new file mode 120000 index 0000000..e66cb8e --- /dev/null +++ b/include/libinum/root_base.h @@ -0,0 +1 @@ +../../src/root_base.h \ No newline at end of file diff --git a/include/libinum/root_double.h b/include/libinum/root_double.h new file mode 120000 index 0000000..338967e --- /dev/null +++ b/include/libinum/root_double.h @@ -0,0 +1 @@ +../../src/root_double.h \ No newline at end of file diff --git a/include/libinum/root_ldouble.h b/include/libinum/root_ldouble.h new file mode 120000 index 0000000..59c64a4 --- /dev/null +++ b/include/libinum/root_ldouble.h @@ -0,0 +1 @@ +../../src/root_ldouble.h \ No newline at end of file diff --git a/include/libinum/root_mpfr.h b/include/libinum/root_mpfr.h new file mode 120000 index 0000000..bfd6182 --- /dev/null +++ b/include/libinum/root_mpfr.h @@ -0,0 +1 @@ +../../src/root_mpfr.h \ No newline at end of file diff --git a/include/libinum/types.h b/include/libinum/types.h new file mode 120000 index 0000000..6ca5d5d --- /dev/null +++ b/include/libinum/types.h @@ -0,0 +1 @@ +../../src/types.h \ No newline at end of file diff --git a/include/libinum/utils.h b/include/libinum/utils.h new file mode 120000 index 0000000..64817bb --- /dev/null +++ b/include/libinum/utils.h @@ -0,0 +1 @@ +../../src/utils.h \ No newline at end of file diff --git a/src/array.c b/src/array.c new file mode 100644 index 0000000..772cb39 --- /dev/null +++ b/src/array.c @@ -0,0 +1,227 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "array.h" +#include +#include +#include +#include "errors.h" +#include "polynomial.h" +#include "utils.h" + +//-------------------------------------------------- +// +// integer array (array_int) +// +//-------------------------------------------------- +#include "array_int.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// unsigned integer array (array_uint) +// +//-------------------------------------------------- +#include "array_uint.h" +#include "array_base.c" + +//-------------------------------------------------- +// +// double array (array_double) +// +//-------------------------------------------------- +#include "array_double.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// long double array (array_ldouble) +// +//-------------------------------------------------- +#include "array_ldouble.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// MPFR array (array_mpfr) +// +//-------------------------------------------------- +#include "array_mpfr.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// array of MPFR arrays (array_2_mpfr) +// +//-------------------------------------------------- +#include "array_2_mpfr.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// character array (array_char) +// +//-------------------------------------------------- +#include "array_char.h" +#include "array_base.c" + +// append a char* +int array_char_append_str(char* str, array_char* output){ + char* ptr; + for (ptr=str;*ptr!='\0';ptr++){ + array_char_append(*ptr, output); + } + return(0); +} + +// convert to char* +int array_char_to_str(array_char input, char** output){ + unsigned int i; + (*output)=calloc(input.length+1,sizeof(char)); + for(i=0;ivalues[input->length-1]!='\0'){ + if(input->length==input->memory){ + array_char_resize(input,input->length+1); + } + // add final '\0' + input->values[input->length]='\0'; + } + return(input->values); +} + +// convert from char* +int str_to_array_char(char* str, array_char* output){ + char* ptr; + unsigned int str_len=0; + for(ptr=str;*ptr!='\0';ptr++){ + str_len++; + } + array_char_init(output, str_len); + for(ptr=str;*ptr!='\0';ptr++){ + array_char_append(*ptr,output); + } + return(0); +} + +// compare an array_char and a char* +int array_char_cmp_str(array_char array_char, char* str){ + unsigned int j; + for(j=0;jsize){ + // resize + free(out_str); + // +1 for '\0' + size=extra_size+1; + out_str=calloc(size,sizeof(char)); + // read format again + va_start(vaptr, fmt); + vsnprintf(out_str,size,fmt,vaptr); + va_end(vaptr); + } + + // write to char array + for(ptr=out_str;*ptr!='\0';ptr++){ + array_char_append(*ptr, output); + } + + free(out_str); + + return(0); +} + + +//-------------------------------------------------- +// +// string array (array_str) +// +//-------------------------------------------------- +#include "array_str.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// mpfr polynomial array (array_polynomial_mpfr) +// +//-------------------------------------------------- +#include "array_polynomial_mpfr.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// double polynomial array (array_polynomial_double) +// +//-------------------------------------------------- +#include "array_polynomial_double.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// long double polynomial array (array_polynomial_ldouble) +// +//-------------------------------------------------- +#include "array_polynomial_ldouble.h" +#include "array_base.c" + + +//-------------------------------------------------- +// +// pthread_t array (array_pthread_t) +// +//-------------------------------------------------- +#include "array_pthread_t.h" +#include "array_base.c" diff --git a/src/array.h b/src/array.h new file mode 100644 index 0000000..63639a7 --- /dev/null +++ b/src/array.h @@ -0,0 +1,150 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Mutable arrays +*/ + +#ifndef LIBINUM_ARRAY_H +#define LIBINUM_ARRAY_H + +#include "types.h" + +//-------------------------------------------------- +// +// integer array (array_int) +// +//-------------------------------------------------- +#include "array_int.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// unsigned integer array (array_uint) +// +//-------------------------------------------------- +#include "array_uint.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// double array (array_double) +// +//-------------------------------------------------- +#include "array_double.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// long double array (array_ldouble) +// +//-------------------------------------------------- +#include "array_ldouble.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// MPFR array (array_mpfr) +// +//-------------------------------------------------- +#include "array_mpfr.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// array of MPFR arrays (array_2_mpfr) +// +//-------------------------------------------------- +#include "array_2_mpfr.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// character array (array_char) +// +//-------------------------------------------------- +#include "array_char.h" +#include "array_base.h" + +// append a char* +int array_char_append_str(char* str, array_char* output); + +// convert to char* +int array_char_to_str(array_char input, char** output); +// noinit (changes the size of input if needed) +char* array_char_to_str_noinit(array_char* input); + +// convert from char* +int str_to_array_char(char* str, array_char* output); + +// compare an array_char and a char* +int array_char_cmp_str(array_char array_char, char* str); + +// format strings +int array_char_snprintf(array_char* output, char* fmt, ...); + + +//-------------------------------------------------- +// +// string array (array_str) +// +//-------------------------------------------------- +#include "array_str.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// mpfr polynomial array (array_polynomial_mpfr) +// +//-------------------------------------------------- +#include "array_polynomial_mpfr.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// double polynomial array (array_polynomial_double) +// +//-------------------------------------------------- +#include "array_polynomial_double.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// long double polynomial array (array_polynomial_ldouble) +// +//-------------------------------------------------- +#include "array_polynomial_ldouble.h" +#include "array_base.h" + + +//-------------------------------------------------- +// +// pthread_t array (array_pthread_t) +// +//-------------------------------------------------- +#include "array_pthread_t.h" +#include "array_base.h" + +#endif diff --git a/src/array_2_mpfr.h b/src/array_2_mpfr.h new file mode 100644 index 0000000..afd99d9 --- /dev/null +++ b/src/array_2_mpfr.h @@ -0,0 +1,54 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for arrays of MPFR float arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_2_mpfr +// prefix of function names +#define ARRAY_FUNC(NAME) array_2_mpfr_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE array_mpfr +// free +#define ARRAY_VAL_FREE(VAL) array_mpfr_free(VAL) +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL=NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) array_mpfr_cpy(FROMVAL, &TOVAL); +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) array_mpfr_cmp(VAL1, VAL2)==0 +#define ARRAY_VAL_IFLT(VAL1, VAL2) array_mpfr_cmp(VAL1, VAL2)<0 +#define ARRAY_VAL_IFGT(VAL1, VAL2) array_mpfr_cmp(VAL1, VAL2)>0 +// print values +#define ARRAY_VAL_PRINT(VAL) array_mpfr_print(VAL) + diff --git a/src/array_base.c b/src/array_base.c new file mode 100644 index 0000000..a70bd86 --- /dev/null +++ b/src/array_base.c @@ -0,0 +1,249 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Base functions for arrays + + see polynomial_*.h for the values taken by ARRAY_FUNC, etc... +*/ + + +// init +int ARRAY_FUNC(init) (ARRAY_TYPENAME* array, unsigned int memory){ + array->values=calloc(memory,sizeof(ARRAY_VAL_TYPE)); + array->memory=memory; + array->length=0; + return(0); +} +int ARRAY_FUNC(free) (ARRAY_TYPENAME array){ + #ifdef ARRAY_VAL_FREE + unsigned int i; + for(i=0;ilength;i++){ + ARRAY_VAL_SET(new_array.values[i], array->values[i]); + } + free(array->values); + *array=new_array; + return(0); +} + +// add a value +int ARRAY_FUNC(append) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME* output){ + if(output->length >= output->memory){ + ARRAY_FUNC(resize) (output,2*output->memory+1); + } + ARRAY_VAL_CPY(val, output->values[output->length]); + output->length++; + return(0); +} +// do not copy the value, instead let the last element of the array point to 'val' +// only if the value requires initializing +#ifdef ARRAY_VAL_FREE +int ARRAY_FUNC(append_noinit) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME* output){ + if(output->length >= output->memory){ + ARRAY_FUNC(resize) (output,2*output->memory+1); + } + ARRAY_VAL_SET(output->values[output->length], val); + output->length++; + return(0); +} +#endif +// add a value only if it is not already present +#ifdef ARRAY_VAL_IFEQ +int ARRAY_FUNC(append_unique) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME* output){ + if(ARRAY_FUNC(find) (val, *output)<0){ + ARRAY_FUNC(append) (val, output); + } + return(0); +} +#endif + +// copy +int ARRAY_FUNC(cpy) (ARRAY_TYPENAME input, ARRAY_TYPENAME* output){ + ARRAY_FUNC(init) (output, input.length); + ARRAY_FUNC(cpy_noinit) (input, output); + return(0); +} +// do not init output array +int ARRAY_FUNC(cpy_noinit) (ARRAY_TYPENAME input, ARRAY_TYPENAME* output){ + unsigned int i; + if(output->memoryvalues[i]); + } + output->length=input.length; + return(0); +} + + +// concatenate +int ARRAY_FUNC(concat) (ARRAY_TYPENAME input, ARRAY_TYPENAME* output){ + unsigned int i; + for(i=0;iend || end>=array.length){ + return(LIBINUM_ERROR_ILLEGAL_MEMORY_ACCESS); + } + ARRAY_FUNC(init) (subarray,end-begin); + for(i=begin;i<=end;i++){ + ARRAY_FUNC(append) (array.values[i], subarray); + } + return(0); +} + +// find (does not assume the array is sorted) +#ifdef ARRAY_VAL_IFEQ +int ARRAY_FUNC(find) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME array){ + unsigned int i; + for(i=0;i1){ + ARRAY_FUNC(sort_sub) (array, 0, array.length-1); + } + return(0); +} +// sort a sub-array +int ARRAY_FUNC(sort_sub) (ARRAY_TYPENAME array, unsigned int begin, unsigned int end){ + unsigned int i; + unsigned int index; + ARRAY_VAL_TYPE tmp; + // the pivot: middle of the array + unsigned int pivot=(begin+end)/2; + + // if the array is non trivial + if(begin0){ + ARRAY_FUNC(sort_sub) (array, begin, index-1); + } + ARRAY_FUNC(sort_sub) (array, index+1, end); + } + return(0); +} +#endif + +// compare arrays +#if defined ARRAY_VAL_IFLT && defined ARRAY_VAL_IFGT +int ARRAY_FUNC(cmp) (ARRAY_TYPENAME array1, ARRAY_TYPENAME array2){ + unsigned int i; + + // compare lengths + if(array1.lengtharray2.length){ + return(1); + } + + // compare terms + for(i=0;iVAL2 +// print values +#define ARRAY_VAL_PRINT(VAL) printf("%c",VAL) + diff --git a/src/array_double.h b/src/array_double.h new file mode 100644 index 0000000..8d3c187 --- /dev/null +++ b/src/array_double.h @@ -0,0 +1,52 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for double arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_double +// prefix of function names +#define ARRAY_FUNC(NAME) array_double_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE double +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) TOVAL = FROMVAL +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) VAL1==VAL2 +#define ARRAY_VAL_IFLT(VAL1, VAL2) VAL1VAL2 +// print values +#define ARRAY_VAL_PRINT(VAL) fprint_double(stdout, VAL) + diff --git a/src/array_int.h b/src/array_int.h new file mode 100644 index 0000000..9496b89 --- /dev/null +++ b/src/array_int.h @@ -0,0 +1,52 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for integer arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_int +// prefix of function names +#define ARRAY_FUNC(NAME) array_int_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE int +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) TOVAL = FROMVAL +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) VAL1==VAL2 +#define ARRAY_VAL_IFLT(VAL1, VAL2) VAL1VAL2 +// print values +#define ARRAY_VAL_PRINT(VAL) printf("%d",VAL) + diff --git a/src/array_ldouble.h b/src/array_ldouble.h new file mode 100644 index 0000000..d314906 --- /dev/null +++ b/src/array_ldouble.h @@ -0,0 +1,52 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for long double arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_ldouble +// prefix of function names +#define ARRAY_FUNC(NAME) array_ldouble_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE long double +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) TOVAL = FROMVAL +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) VAL1==VAL2 +#define ARRAY_VAL_IFLT(VAL1, VAL2) VAL1VAL2 +// print values +#define ARRAY_VAL_PRINT(VAL) fprint_ldouble(stdout, VAL) + diff --git a/src/array_mpfr.h b/src/array_mpfr.h new file mode 100644 index 0000000..46bc54b --- /dev/null +++ b/src/array_mpfr.h @@ -0,0 +1,54 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for MPFR float arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_mpfr +// prefix of function names +#define ARRAY_FUNC(NAME) array_mpfr_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE mpfr_t +// free +#define ARRAY_VAL_FREE(VAL) mpfr_clear(VAL) +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL[0]=NEWVAL[0] +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) mpfr_init(TOVAL); mpfr_set(TOVAL, FROMVAL, MPFR_RNDN) +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) mpfr_cmp(VAL1, VAL2)==0 +#define ARRAY_VAL_IFLT(VAL1, VAL2) mpfr_cmp(VAL1, VAL2)<0 +#define ARRAY_VAL_IFGT(VAL1, VAL2) mpfr_cmp(VAL1, VAL2)>0 +// print values +#define ARRAY_VAL_PRINT(VAL) fprint_mpfr(stdout, VAL) + diff --git a/src/array_polynomial_double.h b/src/array_polynomial_double.h new file mode 100644 index 0000000..d25e689 --- /dev/null +++ b/src/array_polynomial_double.h @@ -0,0 +1,50 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for arrays of polynomials with double coefficients +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_polynomial_double +// prefix of function names +#define ARRAY_FUNC(NAME) array_polynomial_double_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE polynomial_double +// free +#define ARRAY_VAL_FREE(VAL) polynomial_double_free(VAL) +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) polynomial_double_cpy(FROMVAL, &TOVAL) +// print values +#define ARRAY_VAL_PRINT(VAL) polynomial_double_print(VAL) + diff --git a/src/array_polynomial_ldouble.h b/src/array_polynomial_ldouble.h new file mode 100644 index 0000000..49c81cc --- /dev/null +++ b/src/array_polynomial_ldouble.h @@ -0,0 +1,50 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for arrays of polynomials with long double coefficients +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_polynomial_ldouble +// prefix of function names +#define ARRAY_FUNC(NAME) array_polynomial_ldouble_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE polynomial_ldouble +// free +#define ARRAY_VAL_FREE(VAL) polynomial_ldouble_free(VAL) +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) polynomial_ldouble_cpy(FROMVAL, &TOVAL) +// print values +#define ARRAY_VAL_PRINT(VAL) polynomial_ldouble_print(VAL) + diff --git a/src/array_polynomial_mpfr.h b/src/array_polynomial_mpfr.h new file mode 100644 index 0000000..8cfe6e8 --- /dev/null +++ b/src/array_polynomial_mpfr.h @@ -0,0 +1,50 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for arrays of polynomials with mpfr coefficients +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_polynomial_mpfr +// prefix of function names +#define ARRAY_FUNC(NAME) array_polynomial_mpfr_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE polynomial_mpfr +// free +#define ARRAY_VAL_FREE(VAL) polynomial_mpfr_free(VAL) +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) polynomial_mpfr_cpy(FROMVAL, &TOVAL) +// print values +#define ARRAY_VAL_PRINT(VAL) polynomial_mpfr_print(VAL) + diff --git a/src/array_pthread_t.h b/src/array_pthread_t.h new file mode 100644 index 0000000..e200c5d --- /dev/null +++ b/src/array_pthread_t.h @@ -0,0 +1,50 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for pthread_t arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_pthread_t +// prefix of function names +#define ARRAY_FUNC(NAME) array_pthread_t_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE pthread_t +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) TOVAL = FROMVAL +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) VAL1==VAL2 +#define ARRAY_VAL_IFLT(VAL1, VAL2) VAL1VAL2 + diff --git a/src/array_str.h b/src/array_str.h new file mode 100644 index 0000000..2d35d1b --- /dev/null +++ b/src/array_str.h @@ -0,0 +1,54 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for string arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_str +// prefix of function names +#define ARRAY_FUNC(NAME) array_str_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE array_char +// free +#define ARRAY_VAL_FREE(VAL) array_char_free(VAL) +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) array_char_cpy(FROMVAL, &TOVAL) +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) array_char_cmp(VAL1, VAL2)==0 +#define ARRAY_VAL_IFLT(VAL1, VAL2) array_char_cmp(VAL1, VAL2)<0 +#define ARRAY_VAL_IFGT(VAL1, VAL2) array_char_cmp(VAL1, VAL2)>0 +// print values +#define ARRAY_VAL_PRINT(VAL) array_char_print(VAL) + diff --git a/src/array_type.h b/src/array_type.h new file mode 100644 index 0000000..4aa63fc --- /dev/null +++ b/src/array_type.h @@ -0,0 +1,28 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Structure definition for arrays + + see array_*.h for the values taken by ARRAY_TYPENAME, etc... +*/ + +typedef struct ARRAY_TYPENAME { + ARRAY_VAL_TYPE* values; + unsigned int length; + unsigned int memory; +} ARRAY_TYPENAME; + diff --git a/src/array_uint.h b/src/array_uint.h new file mode 100644 index 0000000..b3cec85 --- /dev/null +++ b/src/array_uint.h @@ -0,0 +1,52 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for unsigned int arrays +*/ + + +// reset CPP macros +#undef ARRAY_TYPENAME +#undef ARRAY_FUNC +#undef ARRAY_VAL_TYPE +#undef ARRAY_VAL_FREE +#undef ARRAY_VAL_SET +#undef ARRAY_VAL_CPY +#undef ARRAY_VAL_IFEQ +#undef ARRAY_VAL_IFLT +#undef ARRAY_VAL_IFGT +#undef ARRAY_VAL_PRINT + + +// name of the polynomial type +#define ARRAY_TYPENAME array_uint +// prefix of function names +#define ARRAY_FUNC(NAME) array_uint_ ## NAME + +// type of values +#define ARRAY_VAL_TYPE unsigned int +// set values +#define ARRAY_VAL_SET(VAL, NEWVAL) VAL = NEWVAL +// copy values +#define ARRAY_VAL_CPY(FROMVAL, TOVAL) TOVAL = FROMVAL +// compare values +#define ARRAY_VAL_IFEQ(VAL1, VAL2) VAL1==VAL2 +#define ARRAY_VAL_IFLT(VAL1, VAL2) VAL1VAL2 +// print values +#define ARRAY_VAL_PRINT(VAL) printf("%u",VAL) + diff --git a/src/errors.h b/src/errors.h new file mode 100644 index 0000000..ee26dd2 --- /dev/null +++ b/src/errors.h @@ -0,0 +1,29 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#ifndef LIBINUM_ERRORS_H +#define LIBINUM_ERRORS_H + +// maximal number of iterations reached +#define LIBINUM_ERROR_MAXITER -1 +// illegal memoru access +#define LIBINUM_ERROR_ILLEGAL_MEMORY_ACCESS -2 +// size incompatibilities +#define LIBINUM_ERROR_SIZE_MISMATCH -3 +// NaN or Inf +#define LIBINUM_ERROR_NAN -4 + +#endif diff --git a/src/integral.c b/src/integral.c new file mode 100644 index 0000000..43ccd82 --- /dev/null +++ b/src/integral.c @@ -0,0 +1,52 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "integral.h" + +#include +#include +#include +#include "root.h" +#include "array.h" +#include "polynomial.h" +#include "errors.h" + + +//-------------------------------------------------- +// +// using doubles +// +//-------------------------------------------------- +#include "integral_double.h" +#include "integral_base.c" + + +//-------------------------------------------------- +// +// using long doubles +// +//-------------------------------------------------- +#include "integral_ldouble.h" +#include "integral_base.c" + + +//-------------------------------------------------- +// +// using mpfr floats +// +//-------------------------------------------------- +#include "integral_mpfr.h" +#include "integral_base.c" diff --git a/src/integral.h b/src/integral.h new file mode 100644 index 0000000..01c1fe4 --- /dev/null +++ b/src/integral.h @@ -0,0 +1,53 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Integration of real functions +*/ + +#ifndef LIBINUM_INTEGRAL_H +#define LIBINUM_INTEGRAL_H + +#include "types.h" + + +//-------------------------------------------------- +// +// using doubles +// +//-------------------------------------------------- +#include "integral_double.h" +#include "integral_base.h" + + +//-------------------------------------------------- +// +// using long doubles +// +//-------------------------------------------------- +#include "integral_ldouble.h" +#include "integral_base.h" + +//-------------------------------------------------- +// +// using mpfr floats +// +//-------------------------------------------------- + +#include "integral_mpfr.h" +#include "integral_base.h" + +#endif diff --git a/src/integral_base.c b/src/integral_base.c new file mode 100644 index 0000000..da3f05a --- /dev/null +++ b/src/integral_base.c @@ -0,0 +1,506 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Base functions for integration + + see integral_*.h for the values taken by INTEGRAL_FUNC, etc... +*/ + + +// compute the integral of a real function of 1 variable using the Gauss-Legendre scheme +int INTEGRAL_FUNC(integrate_gauss) (INTEGRAL_FLOAT_TYPE* out, int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*), INTEGRAL_FLOAT_TYPE lower, INTEGRAL_FLOAT_TYPE upper, INTEGRAL_FLOATARRAY_TYPE abcissa, INTEGRAL_FLOATARRAY_TYPE weights, void* extra_args){ + unsigned int i; + INTEGRAL_FLOAT_TYPE valf, delta, avg, x; + int ret; + + // check arguments + if(abcissa.length != weights.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + + #ifdef INTEGRAL_FLOAT_INIT + INTEGRAL_FLOAT_INIT(valf); + INTEGRAL_FLOAT_INIT(delta); + INTEGRAL_FLOAT_INIT(avg); + INTEGRAL_FLOAT_INIT(x); + #endif + + // init to 0 + INTEGRAL_FLOAT_SET_UI(*out, 0); + + // half length of interval + INTEGRAL_FLOAT_SUB(delta, upper, lower); + INTEGRAL_FLOAT_DIV_UI(delta, delta, 2); + // average of interval + INTEGRAL_FLOAT_ADD(avg, upper, lower); + INTEGRAL_FLOAT_DIV_UI(avg, avg, 2); + + for(i=0;ilength=threads; + + // check arguments + if(abcissa.length != weights.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + + #ifdef INTEGRAL_FLOAT_INIT + INTEGRAL_FLOAT_INIT(delta); + INTEGRAL_FLOAT_INIT(avg); + INTEGRAL_FLOAT_INIT(x); + #endif + + // init to 0 + INTEGRAL_FLOAT_SET_UI(*out, 0); + + // half length of interval + INTEGRAL_FLOAT_SUB(delta, upper, lower); + INTEGRAL_FLOAT_DIV_UI(delta, delta, 2); + // average of interval + INTEGRAL_FLOAT_ADD(avg, upper, lower); + INTEGRAL_FLOAT_DIV_UI(avg, avg, 2); + + // inits + for(thread_nr=0;thread_nrvalues+thread_nr, NULL, INTEGRAL_FUNC(integrate_gauss_thread), (void*)(args+thread_nr)); + } + + // wait for completion and join + for(thread_nr=0;thread_nrvalues[thread_nr], NULL); + + if(args[thread_nr].ret<0){ + ret=args[thread_nr].ret; + } + else{ + INTEGRAL_FLOAT_ADD(*out, *out, args[thread_nr].out); + } + } + + // multiply by size of interval + INTEGRAL_FLOAT_MUL(*out, *out, delta); + + for(thread_nr=0;thread_nrout, 0); + + for(i=0;ix.length;i++){ + // evaluate + ret=(*(argument->func))(&valf, argument->x.values[i], argument->extra_args); + + if(ret<0){ + #ifdef INTEGRAL_FLOAT_FREE + INTEGRAL_FLOAT_FREE(valf); + #endif + argument->ret=ret; + return(NULL); + } + // check whether valf is a number + if(! INTEGRAL_FLOAT_ISNUMBER(valf)){ + #ifdef INTEGRAL_FLOAT_FREE + INTEGRAL_FLOAT_FREE(valf); + #endif + argument->ret=LIBINUM_ERROR_NAN; + return(NULL); + } + + INTEGRAL_FLOAT_MUL(valf, valf, argument->weights.values[i]); + INTEGRAL_FLOAT_ADD(argument->out, argument->out, valf); + } + #ifdef INTEGRAL_FLOAT_FREE + INTEGRAL_FLOAT_FREE(valf); + #endif + + return(NULL); +} + + + +// smart management of temporary variables: initialize as many as needed but allow them to be re-used instead of freeing them +#ifdef INTEGRAL_FLOAT_INIT +int INTEGRAL_FUNC(integrate_gauss_smarttmp) (INTEGRAL_FLOAT_TYPE* out, int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*), INTEGRAL_FLOAT_TYPE lower, INTEGRAL_FLOAT_TYPE upper, INTEGRAL_FLOATARRAY_TYPE abcissa, INTEGRAL_FLOATARRAY_TYPE weights, INTEGRAL_FLOATARRAY_TYPE* tmps, void* extra_args){ + unsigned int i; + int ret; + + // check arguments + if(abcissa.length != weights.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + + // allocate tmps if needed + if(tmps->memory<4){ + // no need to resize since the values should not be kept + INTEGRAL_FLOATARRAY_FUNC(free)(*tmps); + INTEGRAL_FLOATARRAY_FUNC(init)(tmps, 4); + } + for (i=tmps->length;i<4;i++){ + INTEGRAL_FLOAT_INIT(tmps->values[i]); + (tmps->length)++; + } + + // init to 0 + INTEGRAL_FLOAT_SET_UI(*out, 0); + + // half length of interval + INTEGRAL_FLOAT_SUB(tmps->values[0], upper, lower); + INTEGRAL_FLOAT_DIV_UI(tmps->values[0], tmps->values[0], 2); + // average of interval + INTEGRAL_FLOAT_ADD(tmps->values[1], upper, lower); + INTEGRAL_FLOAT_DIV_UI(tmps->values[1], tmps->values[1], 2); + + for(i=0;ivalues[2], tmps->values[0], abcissa.values[i]); + INTEGRAL_FLOAT_ADD(tmps->values[2], tmps->values[2], tmps->values[1]); + + ret=(*func)(tmps->values+3, tmps->values[2], extra_args); + if(ret<0){ + return(ret); + } + // check whether tmps->values[3] is a number + if(! INTEGRAL_FLOAT_ISNUMBER(tmps->values[3])){ + return(LIBINUM_ERROR_NAN); + } + + INTEGRAL_FLOAT_MUL(tmps->values[3], tmps->values[3], weights.values[i]); + INTEGRAL_FLOAT_ADD(*out, *out, tmps->values[3]); + } + + INTEGRAL_FLOAT_MUL(*out, *out, tmps->values[0]); + + return(0); +} +#endif + +// multithreaded version with smart management of temporary variables (only for datatypes that need to be initialized) +#ifdef INTEGRAL_FLOAT_INIT +// arguments to pass to each thread +struct INTEGRAL_FUNC(pthread_integrate_gauss_smarttmp_args) { + // partial sum of the values assigned to the thread + INTEGRAL_FLOAT_TYPE* out; + // abcissa + INTEGRAL_FLOATARRAY_TYPE x; + // weights + INTEGRAL_FLOATARRAY_TYPE weights; + // tmp + INTEGRAL_FLOAT_TYPE* tmp; + // pointer to the function to evaluate + int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*); + // extra arguments passed to func + void* extra_args; + // return value + int ret; +}; +int INTEGRAL_FUNC(integrate_gauss_smarttmp_multithread) (INTEGRAL_FLOAT_TYPE* out, int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*), INTEGRAL_FLOAT_TYPE lower, INTEGRAL_FLOAT_TYPE upper, INTEGRAL_FLOATARRAY_TYPE abcissa, INTEGRAL_FLOATARRAY_TYPE weights, INTEGRAL_FLOATARRAY_TYPE* tmps, void* extra_args, unsigned int threads, array_pthread_t* thread_ids){ + unsigned int i; + struct INTEGRAL_FUNC(pthread_integrate_gauss_smarttmp_args) args[threads]; + int ret=0; + unsigned int thread_nr; + + array_pthread_t_init(thread_ids, threads); + thread_ids->length=threads; + + // check arguments + if(abcissa.length != weights.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + + // allocate tmps if needed + if(tmps->memory<2+2*threads+abcissa.length){ + // no need to resize since the values should not be kept + INTEGRAL_FLOATARRAY_FUNC(free)(*tmps); + INTEGRAL_FLOATARRAY_FUNC(init)(tmps, 2+2*threads+abcissa.length); + } + for (i=tmps->length;i<2+2*threads+abcissa.length;i++){ + INTEGRAL_FLOAT_INIT(tmps->values[i]); + (tmps->length)++; + } + + + // init to 0 + INTEGRAL_FLOAT_SET_UI(*out, 0); + + // half length of interval + INTEGRAL_FLOAT_SUB(tmps->values[0], upper, lower); + INTEGRAL_FLOAT_DIV_UI(tmps->values[0], tmps->values[0], 2); + // average of interval + INTEGRAL_FLOAT_ADD(tmps->values[1], upper, lower); + INTEGRAL_FLOAT_DIV_UI(tmps->values[1], tmps->values[1], 2); + + // inits + for(thread_nr=0;thread_nrvalues+2+thread_nr; + args[thread_nr].tmp=tmps->values+2+threads+thread_nr; + } + + // set abcissa and weights + for(i=0,thread_nr=0;ivalues[2+2*threads+i], tmps->values[0], abcissa.values[i]); + INTEGRAL_FLOAT_ADD(tmps->values[2+2*threads+i], tmps->values[2+2*threads+i], tmps->values[1]); + + INTEGRAL_FLOATARRAY_FUNC(append_noinit) (tmps->values[2+2*threads+i], &(args[thread_nr].x)); + INTEGRAL_FLOATARRAY_FUNC(append_noinit) (weights.values[i], &(args[thread_nr].weights)); + } + + for(thread_nr=0;thread_nrvalues+thread_nr, NULL, INTEGRAL_FUNC(integrate_gauss_smarttmp_thread), (void*)(args+thread_nr)); + } + + // wait for completion and join + for(thread_nr=0;thread_nrvalues[thread_nr], NULL); + + if(args[thread_nr].ret<0){ + ret=args[thread_nr].ret; + } + else{ + INTEGRAL_FLOAT_ADD(*out, *out, *(args[thread_nr].out)); + } + } + + // multiply by size of interval + INTEGRAL_FLOAT_MUL(*out, *out, tmps->values[0]); + + // free x and weights + for(thread_nr=0;thread_nrout), 0); + + for(i=0;ix.length;i++){ + // evaluate + ret=(*(argument->func))(argument->tmp, argument->x.values[i], argument->extra_args); + + if(ret<0){ + argument->ret=ret; + return(NULL); + } + // check whether argument->tmp is a number + if(! INTEGRAL_FLOAT_ISNUMBER(*(argument->tmp))){ + argument->ret=LIBINUM_ERROR_NAN; + return(NULL); + } + + INTEGRAL_FLOAT_MUL(*(argument->tmp), *(argument->tmp), argument->weights.values[i]); + INTEGRAL_FLOAT_ADD(*(argument->out), *(argument->out), *(argument->tmp)); + } + + return(NULL); +} +#endif + + +// compute the abcissa and weights for the Gauss-Legendre numerical integration scheme +int INTEGRAL_FUNC(gauss_legendre_weights) (unsigned int order, INTEGRAL_FLOAT_TYPE tolerance, unsigned int maxiter, INTEGRAL_FLOATARRAY_TYPE* abcissa, INTEGRAL_FLOATARRAY_TYPE* weights){ + unsigned int i; + INTEGRAL_FLOAT_TYPE x, tmp; + INTEGRAL_POLYNOMIALARRAY_TYPE legendre; + int ret; + + INTEGRAL_FLOATARRAY_FUNC(init) (abcissa, order); + INTEGRAL_FLOATARRAY_FUNC(init) (weights, order); + + #ifdef INTEGRAL_FLOAT_INIT + INTEGRAL_FLOAT_INIT(x); + INTEGRAL_FLOAT_INIT(tmp); + #endif + + INTEGRAL_POLYNOMIALARRAY_FUNC(init) (&legendre, 2); + // compute the roots of the 'order'-th Legendre polynomial + INTEGRAL_POLYNOMIAL_FUNC(legendre) (order, legendre.values); + INTEGRAL_POLYNOMIAL_FUNC(derive) (legendre.values[0], legendre.values+1); + legendre.length=2; + + for(i=0;ivalues[0]); + return(0); +} +int INTEGRAL_FUNC(deriv_legendre_wrapper) (INTEGRAL_FLOAT_TYPE* out, INTEGRAL_FLOAT_TYPE in, void* legendre){ + INTEGRAL_POLYNOMIAL_FUNC(evaluate) (out, in, ((INTEGRAL_POLYNOMIALARRAY_TYPE*)legendre)->values[1]); + return(0); +} diff --git a/src/integral_base.h b/src/integral_base.h new file mode 100644 index 0000000..4440a52 --- /dev/null +++ b/src/integral_base.h @@ -0,0 +1,48 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Base functions for integration + + see integral_*.h for the values taken by INTEGRAL_FUNC, etc... +*/ + +#include + +// compute the integral of a real function of 1 variable using the Gauss-Legendre scheme +int INTEGRAL_FUNC(integrate_gauss) (INTEGRAL_FLOAT_TYPE* out, int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*), INTEGRAL_FLOAT_TYPE lower, INTEGRAL_FLOAT_TYPE upper, INTEGRAL_FLOATARRAY_TYPE abcissa, INTEGRAL_FLOATARRAY_TYPE weights, void* extra_args); + +// multithreaded version +int INTEGRAL_FUNC(integrate_gauss_multithread) (INTEGRAL_FLOAT_TYPE* out, int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*), INTEGRAL_FLOAT_TYPE lower, INTEGRAL_FLOAT_TYPE upper, INTEGRAL_FLOATARRAY_TYPE abcissa, INTEGRAL_FLOATARRAY_TYPE weights, void* extra_args, unsigned int threads, array_pthread_t* thread_ids); +// per-thread function +void* INTEGRAL_FUNC(integrate_gauss_thread) (void* args); + +// smart management of temporary variables: initialize as many as needed but allow them to be re-used instead of freeing them +#ifdef INTEGRAL_FLOAT_INIT +int INTEGRAL_FUNC(integrate_gauss_smarttmp) (INTEGRAL_FLOAT_TYPE* out, int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*), INTEGRAL_FLOAT_TYPE lower, INTEGRAL_FLOAT_TYPE upper, INTEGRAL_FLOATARRAY_TYPE abcissa, INTEGRAL_FLOATARRAY_TYPE weights, INTEGRAL_FLOATARRAY_TYPE* tmps, void* extra_args); +// multithreaded version with smart management of temporary variables (only for datatypes that need to be initialized) +int INTEGRAL_FUNC(integrate_gauss_smarttmp_multithread) (INTEGRAL_FLOAT_TYPE* out, int (*func)(INTEGRAL_FLOAT_TYPE*, INTEGRAL_FLOAT_TYPE, void*), INTEGRAL_FLOAT_TYPE lower, INTEGRAL_FLOAT_TYPE upper, INTEGRAL_FLOATARRAY_TYPE abcissa, INTEGRAL_FLOATARRAY_TYPE weights, INTEGRAL_FLOATARRAY_TYPE* tmps, void* extra_args, unsigned int threads, array_pthread_t* thread_ids); +// per-thread function +void* INTEGRAL_FUNC(integrate_gauss_smarttmp_thread) (void* args); +#endif + +// compute the abcissa and weights for the Gauss-Legendre numerical integration scheme +int INTEGRAL_FUNC(gauss_legendre_weights) (unsigned int order, INTEGRAL_FLOAT_TYPE tolerance, unsigned int maxiter, INTEGRAL_FLOATARRAY_TYPE* abcissa, INTEGRAL_FLOATARRAY_TYPE* weights); + +// wrapper functions to evaluate the legendre polynomial and its derivative +int INTEGRAL_FUNC(legendre_wrapper) (INTEGRAL_FLOAT_TYPE* out, INTEGRAL_FLOAT_TYPE in, void* legendre); +int INTEGRAL_FUNC(deriv_legendre_wrapper) (INTEGRAL_FLOAT_TYPE* out, INTEGRAL_FLOAT_TYPE in, void* legendre); + diff --git a/src/integral_double.h b/src/integral_double.h new file mode 100644 index 0000000..de179fb --- /dev/null +++ b/src/integral_double.h @@ -0,0 +1,81 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for integration using with doubles +*/ + + +// reset CPP macros +#undef INTEGRAL_FUNC +#undef INTEGRAL_FLOAT_TYPE +#undef INTEGRAL_FLOAT_INIT +#undef INTEGRAL_FLOAT_FREE +#undef INTEGRAL_FLOAT_SET_D +#undef INTEGRAL_FLOAT_SET_UI +#undef INTEGRAL_FLOAT_ADD +#undef INTEGRAL_FLOAT_SUB +#undef INTEGRAL_FLOAT_UI_SUB +#undef INTEGRAL_FLOAT_MUL +#undef INTEGRAL_FLOAT_POW_UI +#undef INTEGRAL_FLOAT_DIV +#undef INTEGRAL_FLOAT_DIV_UI +#undef INTEGRAL_FLOAT_ISNUMBER +#undef INTEGRAL_FLOATARRAY_TYPE +#undef INTEGRAL_FLOATARRAY_FUNC +#undef INTEGRAL_POLYNOMIAL_FUNC +#undef INTEGRAL_POLYNOMIALARRAY_TYPE +#undef INTEGRAL_POLYNOMIALARRAY_FUNC + +// suffix of function names +#define INTEGRAL_FUNC(NAME) NAME ## _double + +// type of floats +#define INTEGRAL_FLOAT_TYPE double +// set float from double +#define INTEGRAL_FLOAT_SET_D(FLOAT, VAL) FLOAT=VAL +// set float from unsigned int +#define INTEGRAL_FLOAT_SET_UI(FLOAT, VAL) FLOAT=(double)VAL +// add floats +#define INTEGRAL_FLOAT_ADD(FLOAT, VAL1, VAL2) FLOAT=VAL1+VAL2 +// subtract floats +#define INTEGRAL_FLOAT_SUB(FLOAT, VAL1, VAL2) FLOAT=VAL1-VAL2 +// subtract floats in which the first is specified as an unsigned int +#define INTEGRAL_FLOAT_UI_SUB(FLOAT, VAL1, VAL2) FLOAT=(double)VAL1-VAL2 +// multiply floats +#define INTEGRAL_FLOAT_MUL(FLOAT, VAL1, VAL2) FLOAT=VAL1*VAL2 +// power of a float, specified as an unsigned int +#define INTEGRAL_FLOAT_POW_UI(FLOAT, VAL1, VAL2) FLOAT=pow(VAL1, (double)VAL2) +// divide floats +#define INTEGRAL_FLOAT_DIV(FLOAT, VAL1, VAL2) FLOAT=VAL1/VAL2 +// divide floats, one of which is specified as an unsigned int +#define INTEGRAL_FLOAT_DIV_UI(FLOAT, VAL1, VAL2) FLOAT=VAL1/(double)VAL2 +// check whether a float is a regular number +#define INTEGRAL_FLOAT_ISNUMBER(FLOAT) (fpclassify(FLOAT)>=FP_ZERO) + +// type of float arrays +#define INTEGRAL_FLOATARRAY_TYPE array_double +// prefix of float array function names +#define INTEGRAL_FLOATARRAY_FUNC(NAME) array_double_ ## NAME + +// prefix of polynomial function names +#define INTEGRAL_POLYNOMIAL_FUNC(NAME) polynomial_double_ ## NAME + +// type of polynomial arrays +#define INTEGRAL_POLYNOMIALARRAY_TYPE array_polynomial_double +// prefix of polynomial array function names +#define INTEGRAL_POLYNOMIALARRAY_FUNC(NAME) array_polynomial_double_ ## NAME + diff --git a/src/integral_ldouble.h b/src/integral_ldouble.h new file mode 100644 index 0000000..5b1d26f --- /dev/null +++ b/src/integral_ldouble.h @@ -0,0 +1,81 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for integration using with long doubles +*/ + + +// reset CPP macros +#undef INTEGRAL_FUNC +#undef INTEGRAL_FLOAT_TYPE +#undef INTEGRAL_FLOAT_INIT +#undef INTEGRAL_FLOAT_FREE +#undef INTEGRAL_FLOAT_SET_D +#undef INTEGRAL_FLOAT_SET_UI +#undef INTEGRAL_FLOAT_ADD +#undef INTEGRAL_FLOAT_SUB +#undef INTEGRAL_FLOAT_UI_SUB +#undef INTEGRAL_FLOAT_MUL +#undef INTEGRAL_FLOAT_POW_UI +#undef INTEGRAL_FLOAT_DIV +#undef INTEGRAL_FLOAT_DIV_UI +#undef INTEGRAL_FLOAT_ISNUMBER +#undef INTEGRAL_FLOATARRAY_TYPE +#undef INTEGRAL_FLOATARRAY_FUNC +#undef INTEGRAL_POLYNOMIAL_FUNC +#undef INTEGRAL_POLYNOMIALARRAY_TYPE +#undef INTEGRAL_POLYNOMIALARRAY_FUNC + +// suffix of function names +#define INTEGRAL_FUNC(NAME) NAME ## _ldouble + +// type of floats +#define INTEGRAL_FLOAT_TYPE long double +// set float from double +#define INTEGRAL_FLOAT_SET_D(FLOAT, VAL) FLOAT=(long double)VAL +// set float from unsigned int +#define INTEGRAL_FLOAT_SET_UI(FLOAT, VAL) FLOAT=(long double)VAL +// add floats +#define INTEGRAL_FLOAT_ADD(FLOAT, VAL1, VAL2) FLOAT=VAL1+VAL2 +// subtract floats +#define INTEGRAL_FLOAT_SUB(FLOAT, VAL1, VAL2) FLOAT=VAL1-VAL2 +// subtract floats in which the first is specified as an unsigned int +#define INTEGRAL_FLOAT_UI_SUB(FLOAT, VAL1, VAL2) FLOAT=(long double)VAL1-VAL2 +// multiply floats +#define INTEGRAL_FLOAT_MUL(FLOAT, VAL1, VAL2) FLOAT=VAL1*VAL2 +// power of a float, specified as an unsigned int +#define INTEGRAL_FLOAT_POW_UI(FLOAT, VAL1, VAL2) FLOAT=powl(VAL1, (long double)VAL2) +// divide floats +#define INTEGRAL_FLOAT_DIV(FLOAT, VAL1, VAL2) FLOAT=VAL1/VAL2 +// divide floats, one of which is specified as an unsigned int +#define INTEGRAL_FLOAT_DIV_UI(FLOAT, VAL1, VAL2) FLOAT=VAL1/(long double)VAL2 +// check whether a float is a regular number +#define INTEGRAL_FLOAT_ISNUMBER(FLOAT) (fpclassify(FLOAT)>=FP_ZERO) + +// type of float arrays +#define INTEGRAL_FLOATARRAY_TYPE array_ldouble +// prefix of float array function names +#define INTEGRAL_FLOATARRAY_FUNC(NAME) array_ldouble_ ## NAME + +// prefix of polynomial function names +#define INTEGRAL_POLYNOMIAL_FUNC(NAME) polynomial_ldouble_ ## NAME + +// type of polynomial arrays +#define INTEGRAL_POLYNOMIALARRAY_TYPE array_polynomial_ldouble +// prefix of polynomial array function names +#define INTEGRAL_POLYNOMIALARRAY_FUNC(NAME) array_polynomial_ldouble_ ## NAME + diff --git a/src/integral_mpfr.h b/src/integral_mpfr.h new file mode 100644 index 0000000..30c9b1a --- /dev/null +++ b/src/integral_mpfr.h @@ -0,0 +1,84 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for integration using with multi-precision floats (mpfr) +*/ + + +// reset CPP macros +#undef INTEGRAL_FUNC +#undef INTEGRAL_FLOAT_TYPE +#undef INTEGRAL_FLOAT_INIT +#undef INTEGRAL_FLOAT_FREE +#undef INTEGRAL_FLOAT_SET_D +#undef INTEGRAL_FLOAT_SET_UI +#undef INTEGRAL_FLOAT_ADD +#undef INTEGRAL_FLOAT_SUB +#undef INTEGRAL_FLOAT_UI_SUB +#undef INTEGRAL_FLOAT_MUL +#undef INTEGRAL_FLOAT_POW_UI +#undef INTEGRAL_FLOAT_DIV +#undef INTEGRAL_FLOAT_DIV_UI +#undef INTEGRAL_FLOAT_ISNUMBER +#undef INTEGRAL_FLOATARRAY_TYPE +#undef INTEGRAL_FLOATARRAY_FUNC +#undef INTEGRAL_POLYNOMIAL_FUNC +#undef INTEGRAL_POLYNOMIALARRAY_TYPE +#undef INTEGRAL_POLYNOMIALARRAY_FUNC + +// suffix of function names +#define INTEGRAL_FUNC(NAME) NAME ## _mpfr + +// type of floats +#define INTEGRAL_FLOAT_TYPE mpfr_t +// init float +#define INTEGRAL_FLOAT_INIT(VAR) mpfr_init(VAR) +// free float +#define INTEGRAL_FLOAT_FREE(VAR) mpfr_clear(VAR) +// set float from double +#define INTEGRAL_FLOAT_SET_D(FLOAT, VAL) mpfr_set_d(FLOAT, VAL, MPFR_RNDN) +// set float from unsigned int +#define INTEGRAL_FLOAT_SET_UI(FLOAT, VAL) mpfr_set_ui(FLOAT, VAL, MPFR_RNDN) +// add floats +#define INTEGRAL_FLOAT_ADD(FLOAT, VAL1, VAL2) mpfr_add(FLOAT, VAL1, VAL2, MPFR_RNDN) +// subtract floats +#define INTEGRAL_FLOAT_SUB(FLOAT, VAL1, VAL2) mpfr_sub(FLOAT, VAL1, VAL2, MPFR_RNDN) +// subtract floats in which the first is specified as an unsigned int +#define INTEGRAL_FLOAT_UI_SUB(FLOAT, VAL1, VAL2) mpfr_ui_sub(FLOAT, VAL1, VAL2, MPFR_RNDN) +// multiply floats +#define INTEGRAL_FLOAT_MUL(FLOAT, VAL1, VAL2) mpfr_mul(FLOAT, VAL1, VAL2, MPFR_RNDN) +// power of a float, specified as an unsigned int +#define INTEGRAL_FLOAT_POW_UI(FLOAT, VAL1, VAL2) mpfr_pow_ui(FLOAT, VAL1, VAL2, MPFR_RNDN) +// divide floats +#define INTEGRAL_FLOAT_DIV(FLOAT, VAL1, VAL2) mpfr_div(FLOAT, VAL1, VAL2, MPFR_RNDN) +// divide floats, one of which is specified as an unsigned int +#define INTEGRAL_FLOAT_DIV_UI(FLOAT, VAL1, VAL2) mpfr_div_ui(FLOAT, VAL1, VAL2, MPFR_RNDN) +// check whether a float is a regular number +#define INTEGRAL_FLOAT_ISNUMBER(FLOAT) mpfr_number_p(FLOAT)!=0 + +// type of float arrays +#define INTEGRAL_FLOATARRAY_TYPE array_mpfr +// prefix of float array function names +#define INTEGRAL_FLOATARRAY_FUNC(NAME) array_mpfr_ ## NAME + +// prefix of polynomial function names +#define INTEGRAL_POLYNOMIAL_FUNC(NAME) polynomial_mpfr_ ## NAME + +// type of polynomial arrays +#define INTEGRAL_POLYNOMIALARRAY_TYPE array_polynomial_mpfr +// prefix of polynomial array function names +#define INTEGRAL_POLYNOMIALARRAY_FUNC(NAME) array_polynomial_mpfr_ ## NAME diff --git a/src/polynomial.c b/src/polynomial.c new file mode 100644 index 0000000..9221241 --- /dev/null +++ b/src/polynomial.c @@ -0,0 +1,55 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "polynomial.h" + +#include +#include +#include +#include +#include "array.h" +#include "utils.h" +#include "errors.h" + + +//-------------------------------------------------- +// +// mpfr coefficients, unsigned int order (polynomial_mpfr) +// +//-------------------------------------------------- + +#include "polynomial_mpfr.h" +#include "polynomial_base.c" + + +//-------------------------------------------------- +// +// double coefficients, unsigned int order (polynomial_mpfr) +// +//-------------------------------------------------- + +#include "polynomial_double.h" +#include "polynomial_base.c" + + +//-------------------------------------------------- +// +// long double coefficients, unsigned int order (polynomial_mpfr) +// +//-------------------------------------------------- + +#include "polynomial_ldouble.h" +#include "polynomial_base.c" diff --git a/src/polynomial.h b/src/polynomial.h new file mode 100644 index 0000000..376149b --- /dev/null +++ b/src/polynomial.h @@ -0,0 +1,55 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Real polynomial +*/ + + +#ifndef LIBINUM_POLYNOMIAL_H +#define LIBINUM_POLYNOMIAL_H + +#include "types.h" + +//-------------------------------------------------- +// +// mpfr coefficients, unsigned int order (polynomial_mpfr) +// +//-------------------------------------------------- +#include "polynomial_mpfr.h" +#include "polynomial_base.h" + + +//-------------------------------------------------- +// +// double coefficients, unsigned int order (polynomial_mpfr) +// +//-------------------------------------------------- + +#include "polynomial_double.h" +#include "polynomial_base.h" + + +//-------------------------------------------------- +// +// long double coefficients, unsigned int order (polynomial_mpfr) +// +//-------------------------------------------------- + +#include "polynomial_ldouble.h" +#include "polynomial_base.h" + +#endif diff --git a/src/polynomialMV.c b/src/polynomialMV.c new file mode 100644 index 0000000..2cafcf6 --- /dev/null +++ b/src/polynomialMV.c @@ -0,0 +1,30 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + + +#include "polynomialMV.h" +#include +#include +#include "array.h" +#include "errors.h" + +// multi-precision integer coefficients +#include "polynomialMV_mpz.h" +#include "polynomialMV_base.c" + +// integer coefficients +#include "polynomialMV_int.h" +#include "polynomialMV_base.c" diff --git a/src/polynomialMV.h b/src/polynomialMV.h new file mode 100644 index 0000000..e368d5b --- /dev/null +++ b/src/polynomialMV.h @@ -0,0 +1,34 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Multi-variable polynomials +*/ + +#ifndef LIBINUM_POLYNOMIAL_MV_H +#define LIBINUM_POLYNOMIAL_MV_H + +#include "types.h" + +// multi-precision integer coefficients +#include "polynomialMV_mpz.h" +#include "polynomialMV_base.h" + +// integer coefficients +#include "polynomialMV_int.h" +#include "polynomialMV_base.h" + +#endif diff --git a/src/polynomialMV_base.c b/src/polynomialMV_base.c new file mode 100644 index 0000000..814c499 --- /dev/null +++ b/src/polynomialMV_base.c @@ -0,0 +1,293 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Base functions for multivariable polynomials + + see polynomialMV_*.h for the values taken by POLYNOMIALMV_FUNC, etc... +*/ + +// init +int POLYNOMIALMV_FUNC(init) (POLYNOMIALMV_TYPENAME* polynomial, int size){ + polynomial->factors=calloc(size, sizeof(POLYNOMIALMV_FACTOR_TYPE)); + polynomial->coefficients=calloc(size, sizeof(POLYNOMIALMV_COEF_TYPE)); + polynomial->length=0; + polynomial->memory=size; + return(0); +} + +// free memory +int POLYNOMIALMV_FUNC(free) (POLYNOMIALMV_TYPENAME polynomial){ + unsigned int i; + for(i=0;ilength;i++){ + new_poly.factors[i]=polynomial->factors[i]; + POLYNOMIALMV_COEF_SET(new_poly.coefficients[i], polynomial->coefficients[i]); + } + new_poly.length=polynomial->length; + + free(polynomial->factors); + free(polynomial->coefficients); + + *polynomial=new_poly; + return(0); +} + +// copy a polynomial +int POLYNOMIALMV_FUNC(cpy) (POLYNOMIALMV_TYPENAME input, POLYNOMIALMV_TYPENAME* output){ + POLYNOMIALMV_FUNC(init) (output, input.length); + POLYNOMIALMV_FUNC(cpy_noinit) (input, output); + return(0); +} +int POLYNOMIALMV_FUNC(cpy_noinit) (POLYNOMIALMV_TYPENAME input, POLYNOMIALMV_TYPENAME* output){ + unsigned int i; + if(output->memoryfactors+i); + POLYNOMIALMV_COEF_CPY(output->coefficients[i], input.coefficients[i]); + } + output->length=input.length; + + return(0); +} + +// append an element to a polynomial +int POLYNOMIALMV_FUNC(append) (POLYNOMIALMV_FACTOR_TYPE factor, POLYNOMIALMV_COEF_TYPE coef, POLYNOMIALMV_TYPENAME* output){ + unsigned int offset=output->length; + + if(output->length>=output->memory){ + POLYNOMIALMV_FUNC(resize) (output,2*output->memory+1); + } + + // copy and allocate + POLYNOMIALMV_FACTOR_FUNC(cpy) (factor, output->factors+offset); + POLYNOMIALMV_COEF_CPY(output->coefficients[offset], coef); + //increment length + output->length++; + + return(0); +} +// if there already exists an element with the same factor, then just add coefficients +// requires the factors to be ordered +int POLYNOMIALMV_FUNC(append_inplace) (POLYNOMIALMV_FACTOR_TYPE factor, POLYNOMIALMV_COEF_TYPE coef, POLYNOMIALMV_TYPENAME* output){ + unsigned int i; + unsigned short int foundit=0; + for(i=0;ilength;i++){ + if(POLYNOMIALMV_FACTOR_FUNC(cmp) (factor, output->factors[i])==0){ + POLYNOMIALMV_COEF_ADD(output->coefficients[i], output->coefficients[i], coef); + foundit=1; + break; + } + } + if(foundit==0){ + POLYNOMIALMV_FUNC(append) (factor, coef, output); + } + return(0); +} +// do not allocate memory for the factor or the coefficient +int POLYNOMIALMV_FUNC(append_noinit) (POLYNOMIALMV_FACTOR_TYPE factor, POLYNOMIALMV_COEF_TYPE coef, POLYNOMIALMV_TYPENAME* output){ + int offset=output->length; + + if(output->length>=output->memory){ + POLYNOMIALMV_FUNC(resize) (output,2*output->memory+1); + } + + // copy without allocating + output->factors[offset]=factor; + POLYNOMIALMV_COEF_SET(output->coefficients[offset], coef); + // increment length + output->length++; + return(0); +} +// noinit factor but init coefficient +int POLYNOMIALMV_FUNC(append_noinitfactor) (POLYNOMIALMV_FACTOR_TYPE factor, POLYNOMIALMV_COEF_TYPE coef, POLYNOMIALMV_TYPENAME* output){ + int offset=output->length; + + if(output->length>=output->memory){ + POLYNOMIALMV_FUNC(resize) (output,2*output->memory+1); + } + + // copy without allocating + output->factors[offset]=factor; + POLYNOMIALMV_COEF_CPY(output->coefficients[offset], coef); + // increment length + output->length++; + return(0); +} +// noinit +// if there already exists an element with the same factor, then just add coefficients +// requires the factors to be ordered +int POLYNOMIALMV_FUNC(append_noinit_inplace) (POLYNOMIALMV_FACTOR_TYPE factor, POLYNOMIALMV_COEF_TYPE coef, POLYNOMIALMV_TYPENAME* output){ + unsigned int i; + unsigned short int foundit=0; + for(i=0;ilength;i++){ + if(POLYNOMIALMV_FACTOR_FUNC(cmp) (factor, output->factors[i])==0){ + POLYNOMIALMV_COEF_ADD(output->coefficients[i], output->coefficients[i], coef); + foundit=1; + // free + POLYNOMIALMV_FACTOR_FUNC(free) (factor); + #ifdef POLYNOMIALMV_COEF_FREE + POLYNOMIALMV_COEF_FREE(coef); + #endif + break; + } + } + if(foundit==0){ + POLYNOMIALMV_FUNC(append_noinit) (factor, coef, output); + } + return(0); +} +// noinit factor but init coefficient +// if there already exists an element with the same factor, then just add coefficients +// requires the factors to be ordered +int POLYNOMIALMV_FUNC(append_noinitfactor_inplace) (POLYNOMIALMV_FACTOR_TYPE factor, POLYNOMIALMV_COEF_TYPE coef, POLYNOMIALMV_TYPENAME* output){ + unsigned int i; + unsigned short int foundit=0; + for(i=0;ilength;i++){ + if(POLYNOMIALMV_FACTOR_FUNC(cmp) (factor, output->factors[i])==0){ + POLYNOMIALMV_COEF_ADD(output->coefficients[i], output->coefficients[i], coef); + foundit=1; + // free factor + POLYNOMIALMV_FACTOR_FUNC(free) (factor); + break; + } + } + if(foundit==0){ + POLYNOMIALMV_FUNC(append_noinitfactor) (factor, coef, output); + } + return(0); +} + +// add polynomials (inplace) +int POLYNOMIALMV_FUNC(add_inplace) (POLYNOMIALMV_TYPENAME input, POLYNOMIALMV_TYPENAME* inout){ + unsigned int i; + for(i=0;i polynomial.factors[i].values[j+1]){ + POLYNOMIALMV_FACTOR_FUNC(sort) (polynomial.factors[i]); + break; + } + } + } + return(0); +} + +// print +int POLYNOMIALMV_FUNC(print) (POLYNOMIALMV_TYPENAME polynomial){ + unsigned int i,j; + + // for each monomial + for(i=0;icoefficients), memory); + POLYNOMIAL_ORDERSARRAY_FUNC(init) (&(poly->orders), memory); + return(0); +} +int POLYNOMIAL_FUNC(free) (POLYNOMIAL_TYPENAME poly){ + POLYNOMIAL_COEFSARRAY_FUNC(free) (poly.coefficients); + POLYNOMIAL_ORDERSARRAY_FUNC(free) (poly.orders); + return(0); +} + +// resize memory +int POLYNOMIAL_FUNC(resize) (POLYNOMIAL_TYPENAME* poly, unsigned int newsize){ + POLYNOMIAL_COEFSARRAY_FUNC(resize) (&(poly->coefficients), newsize); + POLYNOMIAL_ORDERSARRAY_FUNC(resize) (&(poly->orders), newsize); + return(0); +} + +// add a monomial +int POLYNOMIAL_FUNC(add_monomial) (POLYNOMIAL_COEF_TYPE val, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME* output){ + unsigned int i; + if((output->coefficients.length != output->orders.length) || (output->coefficients.memory != output->orders.memory)){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + // check whether the order already exists in the polynomial + for(i=0;i< output->coefficients.length;i++){ + if(POLYNOMIAL_ORDER_CMP(order, output->orders.values[i])==0){ + POLYNOMIAL_COEF_ADD(output->coefficients.values[i], output->coefficients.values[i], val); + return(0); + } + } + // if the order does not already exist + if(output->coefficients.length >= output->coefficients.memory){ + POLYNOMIAL_FUNC(resize) (output,2*output->coefficients.memory+1); + } + POLYNOMIAL_COEF_CPY(val, output->coefficients.values[output->coefficients.length]); + POLYNOMIAL_ORDER_CPY(order, output->orders.values[output->coefficients.length]); + output->coefficients.length++; + output->orders.length++; + return(0); +} +// from a double-valued coefficient and an unsigned int order +int POLYNOMIAL_FUNC(add_monomial_dui) (double val, unsigned int order, POLYNOMIAL_TYPENAME* output){ + unsigned int i; + if((output->coefficients.length != output->orders.length) || (output->coefficients.memory != output->orders.memory)){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + // check whether the order already exists in the polynomial + for(i=0;i< output->coefficients.length;i++){ + if(POLYNOMIAL_ORDER_CMP_UI(output->orders.values[i], order)==0){ + POLYNOMIAL_COEF_ADD_D(output->coefficients.values[i], output->coefficients.values[i], val); + return(0); + } + } + // if the order does not already exist + if(output->coefficients.length >= output->coefficients.memory){ + POLYNOMIAL_FUNC(resize) (output,2*output->coefficients.memory+1); + } + POLYNOMIAL_COEF_CPY_D(val, output->coefficients.values[output->coefficients.length]); + POLYNOMIAL_ORDER_CPY_UI(order, output->orders.values[output->coefficients.length]); + output->coefficients.length++; + output->orders.length++; + return(0); +} + +// copy +int POLYNOMIAL_FUNC(cpy) (POLYNOMIAL_TYPENAME input, POLYNOMIAL_TYPENAME* output){ + POLYNOMIAL_COEFSARRAY_FUNC(cpy) (input.coefficients, &(output->coefficients)); + POLYNOMIAL_ORDERSARRAY_FUNC(cpy) (input.orders, &(output->orders)); + return(0); +} +int POLYNOMIAL_FUNC(cpy_noinit) (POLYNOMIAL_TYPENAME input, POLYNOMIAL_TYPENAME* output){ + int ret; + ret=POLYNOMIAL_COEFSARRAY_FUNC(cpy_noinit) (input.coefficients, &(output->coefficients)); + if(ret<0){ + return(ret); + } + POLYNOMIAL_ORDERSARRAY_FUNC(cpy_noinit) (input.orders, &(output->orders)); + return(ret); +} + +// add +int POLYNOMIAL_FUNC(add_inplace) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME* poly2){ + unsigned int i; + if(poly1.coefficients.length != poly1.orders.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + for(i=0;icoefficients.length != poly->orders.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + for(i=0;icoefficients.length;i++){ + POLYNOMIAL_COEF_MUL(poly->coefficients.values[i], poly->coefficients.values[i], x); + } + return(0); +} +int POLYNOMIAL_FUNC(mul_scalar) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output){ + int ret; + POLYNOMIAL_FUNC(cpy) (poly, output); + ret=POLYNOMIAL_FUNC(mul_scalar_inplace) (x, output); + return(ret); +} + +// multiply by a monomial +int POLYNOMIAL_FUNC(mul_monomial_inplace) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME* poly){ + unsigned int i; + if(poly->coefficients.length != poly->orders.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + for(i=0;icoefficients.length;i++){ + POLYNOMIAL_COEF_MUL(poly->coefficients.values[i], poly->coefficients.values[i], x); + POLYNOMIAL_ORDER_ADD(poly->orders.values[i], poly->orders.values[i], order); + } + return(0); +} +int POLYNOMIAL_FUNC(mul_monomial) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output){ + int ret; + POLYNOMIAL_FUNC(cpy) (poly,output); + ret=POLYNOMIAL_FUNC(mul_monomial_inplace) (x, order, output); + return(ret); +} + +// multiply two polynomials +int POLYNOMIAL_FUNC(mul_inplace) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME* poly2){ + unsigned int i; + int ret; + if(poly1.coefficients.length != poly1.orders.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + for(i=0;icoefficients.length != poly->orders.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + for(i=0;icoefficients.length;i++){ + if(POLYNOMIAL_ORDER_CMP_UI(poly->orders.values[i], 0)>0){ + POLYNOMIAL_COEF_MUL_ORDER(poly->coefficients.values[i], poly->coefficients.values[i], poly->orders.values[i]); + POLYNOMIAL_ORDER_SUB_UI(poly->orders.values[i], poly->orders.values[i], 1); + } + else{ + // remove the term by setting it to the last term and reducing the length + #ifdef POLYNOMIAL_COEF_FREE + POLYNOMIAL_COEF_FREE(poly->coefficients.values[i]); + #endif + #ifdef POLYNOMIAL_ORDER_FREE + POLYNOMIAL_ORDER_FREE(poly->orders.values[i]); + #endif + if(icoefficients.length-1){ + POLYNOMIAL_COEF_SET(poly->coefficients.values[i], poly->coefficients.values[poly->coefficients.length-1]); + POLYNOMIAL_ORDER_SET(poly->orders.values[i], poly->orders.values[poly->coefficients.length-1]); + i--; + } + poly->coefficients.length--; + poly->orders.length--; + } + } + return(0); +} +int POLYNOMIAL_FUNC(derive) (POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output){ + int ret; + POLYNOMIAL_FUNC(cpy) (poly, output); + ret=POLYNOMIAL_FUNC(derive_inplace) (output); + return(ret); +} + +// evaluate +int POLYNOMIAL_FUNC(evaluate) (POLYNOMIAL_COEF_TYPE* out, POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME poly){ + unsigned int i; + POLYNOMIAL_COEF_TYPE tmp; + + #ifdef POLYNOMIAL_COEF_INIT + POLYNOMIAL_COEF_INIT(tmp); + #endif + + if(poly.coefficients.length != poly.orders.length){ + return(LIBINUM_ERROR_SIZE_MISMATCH); + } + + POLYNOMIAL_COEF_SET_UI(*out, 0); + for(i=0;i0){ + printf("+ "); + } + POLYNOMIAL_COEF_PRINT(poly.coefficients.values[j]); + printf(" x^"); + POLYNOMIAL_ORDER_PRINT(poly.orders.values[j]); + printf("\n"); + } + + return(0); +} + + +// n-th Legendre polynomial +int POLYNOMIAL_FUNC(legendre) (unsigned int n, POLYNOMIAL_TYPENAME* output){ + POLYNOMIAL_TYPENAME prevpoly, tmppoly; + unsigned int i; + int j; + POLYNOMIAL_COEF_TYPE tmp; + POLYNOMIAL_ORDER_TYPE tmp_o; + + if(n==0){ + POLYNOMIAL_FUNC(init) (output, 1); + POLYNOMIAL_FUNC(add_monomial_dui) (1., 0, output); + return(0); + } + else if(n==1){ + POLYNOMIAL_FUNC(init) (output, n); + POLYNOMIAL_FUNC(add_monomial_dui) (1., 1, output); + return(0); + } + + #ifdef POLYNOMIAL_COEF_INIT + POLYNOMIAL_COEF_INIT(tmp); + #endif + #ifdef POLYNOMIAL_ORDER_INIT + POLYNOMIAL_ORDER_INIT(tmp_o); + #endif + + // init: prevpoly=1 + POLYNOMIAL_FUNC(init) (&prevpoly, n-1); + POLYNOMIAL_FUNC(add_monomial_dui) (1., 0, &prevpoly); + // init: output=x + POLYNOMIAL_FUNC(init) (output, n); + POLYNOMIAL_FUNC(add_monomial_dui) (1., 1, output); + + // implement i*p_i=(2*i-1)*p_{i-1}-(i-1)*p_{i-2} + for(i=2;i<=n;i++){ + // save current poly to copy it to prevpoly at the end of the loop + POLYNOMIAL_FUNC(cpy) (*output, &tmppoly); + + // (2*i-1)/i*p_{i-1} + POLYNOMIAL_COEF_SET_UI(tmp, 2*i-1); + POLYNOMIAL_COEF_DIV_UI(tmp, tmp, i); + POLYNOMIAL_ORDER_SET_UI(tmp_o, 1); + POLYNOMIAL_FUNC(mul_monomial_inplace) (tmp, tmp_o, output); + + // -(i-1)/i*p_{i-2} + // copy i to a signed int + j=i; + POLYNOMIAL_COEF_SET_SI(tmp, -j+1); + POLYNOMIAL_COEF_DIV_UI(tmp, tmp, i); + POLYNOMIAL_FUNC(mul_scalar_inplace) (tmp, &prevpoly); + + // add it to p_n + POLYNOMIAL_FUNC(add_inplace) (prevpoly, output); + + // replace prevpoly + POLYNOMIAL_FUNC(free) (prevpoly); + prevpoly=tmppoly; + } + + POLYNOMIAL_FUNC(free) (prevpoly); + #ifdef POLYNOMIAL_COEF_FREE + POLYNOMIAL_COEF_FREE(tmp); + #endif + #ifdef POLYNOMIAL_ORDER_FREE + POLYNOMIAL_ORDER_FREE(tmp_o); + #endif + + return(0); +} + diff --git a/src/polynomial_base.h b/src/polynomial_base.h new file mode 100644 index 0000000..3165a48 --- /dev/null +++ b/src/polynomial_base.h @@ -0,0 +1,67 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Base functions for real polynomials + + see polynomial_*.h for the values taken by POLYNOMIAL_FUNC, etc... +*/ + +// init +int POLYNOMIAL_FUNC(init) (POLYNOMIAL_TYPENAME* poly, unsigned int memory); +int POLYNOMIAL_FUNC(free) (POLYNOMIAL_TYPENAME poly); + +// resize memory +int POLYNOMIAL_FUNC(resize) (POLYNOMIAL_TYPENAME* poly, unsigned int newsize); + +// add a monomial +int POLYNOMIAL_FUNC(add_monomial) (POLYNOMIAL_COEF_TYPE val, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME* output); +// from a double-valued coefficient and an unsigned int order +int POLYNOMIAL_FUNC(add_monomial_dui) (double val, unsigned int order, POLYNOMIAL_TYPENAME* output); + +// copy +int POLYNOMIAL_FUNC(cpy) (POLYNOMIAL_TYPENAME input, POLYNOMIAL_TYPENAME* output); +int POLYNOMIAL_FUNC(cpy_noinit) (POLYNOMIAL_TYPENAME input, POLYNOMIAL_TYPENAME* output); + +// add +int POLYNOMIAL_FUNC(add_inplace) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME* poly2); +int POLYNOMIAL_FUNC(add) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME poly2, POLYNOMIAL_TYPENAME* output); + +// multiply by a scalar +int POLYNOMIAL_FUNC(mul_scalar_inplace) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME* poly); +int POLYNOMIAL_FUNC(mul_scalar) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output); + +// multiply by a monomial +int POLYNOMIAL_FUNC(mul_monomial_inplace) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME* poly); +int POLYNOMIAL_FUNC(mul_monomial) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output); + +// multiply two polynomials +int POLYNOMIAL_FUNC(mul_inplace) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME* poly2); +int POLYNOMIAL_FUNC(mul) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME poly2, POLYNOMIAL_TYPENAME* output); + +// derive +int POLYNOMIAL_FUNC(derive_inplace) (POLYNOMIAL_TYPENAME* poly); +int POLYNOMIAL_FUNC(derive) (POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output); + +// evaluate +int POLYNOMIAL_FUNC(evaluate) (POLYNOMIAL_COEF_TYPE* out, POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME poly); + +// print +int POLYNOMIAL_FUNC(print) (POLYNOMIAL_TYPENAME poly); + + +// n-th Legendre polynomial +int POLYNOMIAL_FUNC(legendre) (unsigned int n, POLYNOMIAL_TYPENAME* output); diff --git a/src/polynomial_double.h b/src/polynomial_double.h new file mode 100644 index 0000000..f876119 --- /dev/null +++ b/src/polynomial_double.h @@ -0,0 +1,117 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for real polynomials with double coefficients and unsigned integer powers +*/ + + +// reset CPP macros +#undef POLYNOMIAL_TYPENAME +#undef POLYNOMIAL_FUNC +#undef POLYNOMIAL_COEF_TYPE +#undef POLYNOMIAL_COEFSARRAY_TYPE +#undef POLYNOMIAL_COEFSARRAY_FUNC +#undef POLYNOMIAL_COEF_INIT +#undef POLYNOMIAL_COEF_FREE +#undef POLYNOMIAL_COEF_SET +#undef POLYNOMIAL_COEF_SET_SI +#undef POLYNOMIAL_COEF_SET_UI +#undef POLYNOMIAL_COEF_CPY +#undef POLYNOMIAL_COEF_CPY_D +#undef POLYNOMIAL_COEF_ADD +#undef POLYNOMIAL_COEF_ADD_D +#undef POLYNOMIAL_COEF_MUL +#undef POLYNOMIAL_COEF_MUL_ORDER +#undef POLYNOMIAL_COEF_DIV_UI +#undef POLYNOMIAL_COEF_POW_UI +#undef POLYNOMIAL_COEF_PRINT +#undef POLYNOMIAL_ORDER_TYPE +#undef POLYNOMIAL_ORDERSARRAY_TYPE +#undef POLYNOMIAL_ORDERSARRAY_FUNC +#undef POLYNOMIAL_ORDER_INIT +#undef POLYNOMIAL_ORDER_FREE +#undef POLYNOMIAL_ORDER_SET +#undef POLYNOMIAL_ORDER_SET_UI +#undef POLYNOMIAL_ORDER_CPY +#undef POLYNOMIAL_ORDER_CPY_UI +#undef POLYNOMIAL_ORDER_ADD +#undef POLYNOMIAL_ORDER_SUB_UI +#undef POLYNOMIAL_ORDER_CMP +#undef POLYNOMIAL_ORDER_CMP_UI +#undef POLYNOMIAL_ORDER_PRINT + +// name of the polynomial type +#define POLYNOMIAL_TYPENAME polynomial_double +// prefix of function names +#define POLYNOMIAL_FUNC(NAME) polynomial_double_ ## NAME + +// type of the coefficient +#define POLYNOMIAL_COEF_TYPE double +// type of coefficient arrays +#define POLYNOMIAL_COEFSARRAY_TYPE array_double +// prefix of coefficient array function names +#define POLYNOMIAL_COEFSARRAY_FUNC(NAME) array_double_ ## NAME +// set coefficient +#define POLYNOMIAL_COEF_SET(COEF, VAL) COEF=VAL +// set coefficient from signed int +#define POLYNOMIAL_COEF_SET_SI(COEF, VAL) COEF=(double)VAL +// set coefficient from unsigned int +#define POLYNOMIAL_COEF_SET_UI(COEF, VAL) COEF=(double)VAL +// copy coefficient +#define POLYNOMIAL_COEF_CPY(VAL, COEF) COEF=VAL +// copy coefficient from double +#define POLYNOMIAL_COEF_CPY_D(VAL, COEF) COEF=VAL +// add coefficients +#define POLYNOMIAL_COEF_ADD(COEF, VAL1, VAL2) COEF=VAL1+VAL2 +// add coefficients, one of which is specified as a double +#define POLYNOMIAL_COEF_ADD_D(COEF, VAL1, VAL2) COEF=VAL1+VAL2 +// multiply coefficients +#define POLYNOMIAL_COEF_MUL(COEF, VAL1, VAL2) COEF=VAL1*VAL2 +// multiply coefficients, one of which is specified as a POLYNOMIAL_ORDER_TYPE +#define POLYNOMIAL_COEF_MUL_ORDER(COEF, VAL1, VAL2) COEF=VAL1*(double)VAL2 +// divide coefficients, one of which is specified as an unsigned int +#define POLYNOMIAL_COEF_DIV_UI(COEF, VAL1, VAL2) COEF=VAL1/(double)VAL2 +// power of a coefficient, specified as an unsigned int +#define POLYNOMIAL_COEF_POW_UI(COEF, VAL1, VAL2) COEF=pow(VAL1, (double)VAL2) +// print a coefficient +#define POLYNOMIAL_COEF_PRINT(COEF) fprint_double(stdout, COEF) + +// type of the order +#define POLYNOMIAL_ORDER_TYPE unsigned int +// type of order arrays +#define POLYNOMIAL_ORDERSARRAY_TYPE array_uint +// prefix of order array function names +#define POLYNOMIAL_ORDERSARRAY_FUNC(NAME) array_uint_ ## NAME +// set order +#define POLYNOMIAL_ORDER_SET(ORDER, VAL) ORDER=VAL +// set order from unsigned int +#define POLYNOMIAL_ORDER_SET_UI(ORDER, VAL) ORDER=VAL +// copy order +#define POLYNOMIAL_ORDER_CPY(VAL, ORDER) ORDER=VAL +// copy order from unsigned int +#define POLYNOMIAL_ORDER_CPY_UI(VAL, ORDER) ORDER=VAL +// add orders +#define POLYNOMIAL_ORDER_ADD(ORDER, VAL1, VAL2) ORDER=VAL1+VAL2 +// subtract an order and an unsigned integer +#define POLYNOMIAL_ORDER_SUB_UI(ORDER, VAL1, VAL2) ORDER=VAL1-VAL2 +// compare orders (0 if equal, -1 if <, +1 if >) +#define POLYNOMIAL_ORDER_CMP(VAL1, VAL2) (int)VAL1-(int)VAL2 +// compare orders, one of which is specified as an unsigned int (0 if equal, -1 if <, +1 if >) +#define POLYNOMIAL_ORDER_CMP_UI(VAL1, VAL2) (int)VAL1-(int)VAL2 +// print orders +#define POLYNOMIAL_ORDER_PRINT(ORDER) printf("%u",ORDER) + diff --git a/src/polynomial_ldouble.h b/src/polynomial_ldouble.h new file mode 100644 index 0000000..54e5501 --- /dev/null +++ b/src/polynomial_ldouble.h @@ -0,0 +1,117 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for real polynomials with long double coefficients and unsigned integer powers +*/ + + +// reset CPP macros +#undef POLYNOMIAL_TYPENAME +#undef POLYNOMIAL_FUNC +#undef POLYNOMIAL_COEF_TYPE +#undef POLYNOMIAL_COEFSARRAY_TYPE +#undef POLYNOMIAL_COEFSARRAY_FUNC +#undef POLYNOMIAL_COEF_INIT +#undef POLYNOMIAL_COEF_FREE +#undef POLYNOMIAL_COEF_SET +#undef POLYNOMIAL_COEF_SET_SI +#undef POLYNOMIAL_COEF_SET_UI +#undef POLYNOMIAL_COEF_CPY +#undef POLYNOMIAL_COEF_CPY_D +#undef POLYNOMIAL_COEF_ADD +#undef POLYNOMIAL_COEF_ADD_D +#undef POLYNOMIAL_COEF_MUL +#undef POLYNOMIAL_COEF_MUL_ORDER +#undef POLYNOMIAL_COEF_DIV_UI +#undef POLYNOMIAL_COEF_POW_UI +#undef POLYNOMIAL_COEF_PRINT +#undef POLYNOMIAL_ORDER_TYPE +#undef POLYNOMIAL_ORDERSARRAY_TYPE +#undef POLYNOMIAL_ORDERSARRAY_FUNC +#undef POLYNOMIAL_ORDER_INIT +#undef POLYNOMIAL_ORDER_FREE +#undef POLYNOMIAL_ORDER_SET +#undef POLYNOMIAL_ORDER_SET_UI +#undef POLYNOMIAL_ORDER_CPY +#undef POLYNOMIAL_ORDER_CPY_UI +#undef POLYNOMIAL_ORDER_ADD +#undef POLYNOMIAL_ORDER_SUB_UI +#undef POLYNOMIAL_ORDER_CMP +#undef POLYNOMIAL_ORDER_CMP_UI +#undef POLYNOMIAL_ORDER_PRINT + +// name of the polynomial type +#define POLYNOMIAL_TYPENAME polynomial_ldouble +// prefix of function names +#define POLYNOMIAL_FUNC(NAME) polynomial_ldouble_ ## NAME + +// type of the coefficient +#define POLYNOMIAL_COEF_TYPE long double +// type of coefficient arrays +#define POLYNOMIAL_COEFSARRAY_TYPE array_ldouble +// prefix of coefficient array function names +#define POLYNOMIAL_COEFSARRAY_FUNC(NAME) array_ldouble_ ## NAME +// set coefficient +#define POLYNOMIAL_COEF_SET(COEF, VAL) COEF=VAL +// set coefficient from signed int +#define POLYNOMIAL_COEF_SET_SI(COEF, VAL) COEF=(long double)VAL +// set coefficient from unsigned int +#define POLYNOMIAL_COEF_SET_UI(COEF, VAL) COEF=(long double)VAL +// copy coefficient +#define POLYNOMIAL_COEF_CPY(VAL, COEF) COEF=VAL +// copy coefficient from double +#define POLYNOMIAL_COEF_CPY_D(VAL, COEF) COEF=(long double)VAL +// add coefficients +#define POLYNOMIAL_COEF_ADD(COEF, VAL1, VAL2) COEF=VAL1+VAL2 +// add coefficients, one of which is specified as a double +#define POLYNOMIAL_COEF_ADD_D(COEF, VAL1, VAL2) COEF=VAL1+(long double)VAL2 +// multiply coefficients +#define POLYNOMIAL_COEF_MUL(COEF, VAL1, VAL2) COEF=VAL1*VAL2 +// multiply coefficients, one of which is specified as a POLYNOMIAL_ORDER_TYPE +#define POLYNOMIAL_COEF_MUL_ORDER(COEF, VAL1, VAL2) COEF=VAL1*(long double)VAL2 +// divide coefficients, one of which is specified as an unsigned int +#define POLYNOMIAL_COEF_DIV_UI(COEF, VAL1, VAL2) COEF=VAL1/(long double)VAL2 +// power of a coefficient, specified as an unsigned int +#define POLYNOMIAL_COEF_POW_UI(COEF, VAL1, VAL2) COEF=powl(VAL1, (long double)VAL2) +// print a coefficient +#define POLYNOMIAL_COEF_PRINT(COEF) fprint_ldouble(stdout, COEF) + +// type of the order +#define POLYNOMIAL_ORDER_TYPE unsigned int +// type of order arrays +#define POLYNOMIAL_ORDERSARRAY_TYPE array_uint +// prefix of order array function names +#define POLYNOMIAL_ORDERSARRAY_FUNC(NAME) array_uint_ ## NAME +// set order +#define POLYNOMIAL_ORDER_SET(ORDER, VAL) ORDER=VAL +// set order from unsigned int +#define POLYNOMIAL_ORDER_SET_UI(ORDER, VAL) ORDER=VAL +// copy order +#define POLYNOMIAL_ORDER_CPY(VAL, ORDER) ORDER=VAL +// copy order from unsigned int +#define POLYNOMIAL_ORDER_CPY_UI(VAL, ORDER) ORDER=VAL +// add orders +#define POLYNOMIAL_ORDER_ADD(ORDER, VAL1, VAL2) ORDER=VAL1+VAL2 +// subtract an order and an unsigned integer +#define POLYNOMIAL_ORDER_SUB_UI(ORDER, VAL1, VAL2) ORDER=VAL1-VAL2 +// compare orders (0 if equal, -1 if <, +1 if >) +#define POLYNOMIAL_ORDER_CMP(VAL1, VAL2) (int)VAL1-(int)VAL2 +// compare orders, one of which is specified as an unsigned int (0 if equal, -1 if <, +1 if >) +#define POLYNOMIAL_ORDER_CMP_UI(VAL1, VAL2) (int)VAL1-(int)VAL2 +// print orders +#define POLYNOMIAL_ORDER_PRINT(ORDER) printf("%u",ORDER) + diff --git a/src/polynomial_mpfr.h b/src/polynomial_mpfr.h new file mode 100644 index 0000000..f8bc983 --- /dev/null +++ b/src/polynomial_mpfr.h @@ -0,0 +1,121 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for real polynomials with multi-precision float (mpfr) coefficients and unsigned integer powers +*/ + + +// reset CPP macros +#undef POLYNOMIAL_TYPENAME +#undef POLYNOMIAL_FUNC +#undef POLYNOMIAL_COEF_TYPE +#undef POLYNOMIAL_COEFSARRAY_TYPE +#undef POLYNOMIAL_COEFSARRAY_FUNC +#undef POLYNOMIAL_COEF_INIT +#undef POLYNOMIAL_COEF_FREE +#undef POLYNOMIAL_COEF_SET +#undef POLYNOMIAL_COEF_SET_SI +#undef POLYNOMIAL_COEF_SET_UI +#undef POLYNOMIAL_COEF_CPY +#undef POLYNOMIAL_COEF_CPY_D +#undef POLYNOMIAL_COEF_ADD +#undef POLYNOMIAL_COEF_ADD_D +#undef POLYNOMIAL_COEF_MUL +#undef POLYNOMIAL_COEF_MUL_ORDER +#undef POLYNOMIAL_COEF_DIV_UI +#undef POLYNOMIAL_COEF_POW_UI +#undef POLYNOMIAL_COEF_PRINT +#undef POLYNOMIAL_ORDER_TYPE +#undef POLYNOMIAL_ORDERSARRAY_TYPE +#undef POLYNOMIAL_ORDERSARRAY_FUNC +#undef POLYNOMIAL_ORDER_INIT +#undef POLYNOMIAL_ORDER_FREE +#undef POLYNOMIAL_ORDER_SET +#undef POLYNOMIAL_ORDER_SET_UI +#undef POLYNOMIAL_ORDER_CPY +#undef POLYNOMIAL_ORDER_CPY_UI +#undef POLYNOMIAL_ORDER_ADD +#undef POLYNOMIAL_ORDER_SUB_UI +#undef POLYNOMIAL_ORDER_CMP +#undef POLYNOMIAL_ORDER_CMP_UI +#undef POLYNOMIAL_ORDER_PRINT + +// name of the polynomial type +#define POLYNOMIAL_TYPENAME polynomial_mpfr +// prefix of function names +#define POLYNOMIAL_FUNC(NAME) polynomial_mpfr_ ## NAME + +// type of the coefficient +#define POLYNOMIAL_COEF_TYPE mpfr_t +// type of coefficient arrays +#define POLYNOMIAL_COEFSARRAY_TYPE array_mpfr +// prefix of coefficient array function names +#define POLYNOMIAL_COEFSARRAY_FUNC(NAME) array_mpfr_ ## NAME +// init coefficient +#define POLYNOMIAL_COEF_INIT(VAR) mpfr_init(VAR) +// free coefficient +#define POLYNOMIAL_COEF_FREE(VAR) mpfr_clear(VAR) +// set coefficient +#define POLYNOMIAL_COEF_SET(COEF, VAL) COEF[0]=VAL[0] +// set coefficient from signed int +#define POLYNOMIAL_COEF_SET_SI(COEF, VAL) mpfr_set_si(COEF, VAL, MPFR_RNDN) +// set coefficient from unsigned int +#define POLYNOMIAL_COEF_SET_UI(COEF, VAL) mpfr_set_ui(COEF, VAL, MPFR_RNDN) +// copy coefficient +#define POLYNOMIAL_COEF_CPY(VAL, COEF) mpfr_init(COEF); mpfr_set(COEF, VAL, MPFR_RNDN) +// copy coefficient from double +#define POLYNOMIAL_COEF_CPY_D(VAL, COEF) mpfr_init(COEF); mpfr_set_d(COEF, VAL, MPFR_RNDN) +// add coefficients +#define POLYNOMIAL_COEF_ADD(COEF, VAL1, VAL2) mpfr_add(COEF, VAL1, VAL2, MPFR_RNDN) +// add coefficients, one of which is specified as a double +#define POLYNOMIAL_COEF_ADD_D(COEF, VAL1, VAL2) mpfr_add_d(COEF, VAL1, VAL2, MPFR_RNDN) +// multiply coefficients +#define POLYNOMIAL_COEF_MUL(COEF, VAL1, VAL2) mpfr_mul(COEF, VAL1, VAL2, MPFR_RNDN) +// multiply coefficients, one of which is specified as a POLYNOMIAL_ORDER_TYPE +#define POLYNOMIAL_COEF_MUL_ORDER(COEF, VAL1, VAL2) mpfr_mul_ui(COEF, VAL1, VAL2, MPFR_RNDN) +// divide coefficients, one of which is specified as an unsigned int +#define POLYNOMIAL_COEF_DIV_UI(COEF, VAL1, VAL2) mpfr_div_ui(COEF, VAL1, VAL2, MPFR_RNDN) +// power of a coefficient, specified as an unsigned int +#define POLYNOMIAL_COEF_POW_UI(COEF, VAL1, VAL2) mpfr_pow_ui(COEF, VAL1, VAL2, MPFR_RNDN) +// print a coefficient +#define POLYNOMIAL_COEF_PRINT(COEF) fprint_mpfr(stdout, COEF) + +// type of the order +#define POLYNOMIAL_ORDER_TYPE unsigned int +// type of order arrays +#define POLYNOMIAL_ORDERSARRAY_TYPE array_uint +// prefix of order array function names +#define POLYNOMIAL_ORDERSARRAY_FUNC(NAME) array_uint_ ## NAME +// set order +#define POLYNOMIAL_ORDER_SET(ORDER, VAL) ORDER=VAL +// set order from unsigned int +#define POLYNOMIAL_ORDER_SET_UI(ORDER, VAL) ORDER=VAL +// copy order +#define POLYNOMIAL_ORDER_CPY(VAL, ORDER) ORDER=VAL +// copy order from unsigned int +#define POLYNOMIAL_ORDER_CPY_UI(VAL, ORDER) ORDER=VAL +// add orders +#define POLYNOMIAL_ORDER_ADD(ORDER, VAL1, VAL2) ORDER=VAL1+VAL2 +// subtract an order and an unsigned integer +#define POLYNOMIAL_ORDER_SUB_UI(ORDER, VAL1, VAL2) ORDER=VAL1-VAL2 +// compare orders (0 if equal, -1 if <, +1 if >) +#define POLYNOMIAL_ORDER_CMP(VAL1, VAL2) (int)VAL1-(int)VAL2 +// compare orders, one of which is specified as an unsigned int (0 if equal, -1 if <, +1 if >) +#define POLYNOMIAL_ORDER_CMP_UI(VAL1, VAL2) (int)VAL1-(int)VAL2 +// print orders +#define POLYNOMIAL_ORDER_PRINT(ORDER) printf("%u",ORDER) + diff --git a/src/polynomial_type.h b/src/polynomial_type.h new file mode 100644 index 0000000..3361d67 --- /dev/null +++ b/src/polynomial_type.h @@ -0,0 +1,27 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Structure definition for real polynomials + + see polynomial_*.h for the values taken by POLYNOMIAL_TYPENAME, etc... +*/ + +typedef struct POLYNOMIAL_TYPENAME { + POLYNOMIAL_COEFSARRAY_TYPE coefficients; + POLYNOMIAL_ORDERSARRAY_TYPE orders; +} POLYNOMIAL_TYPENAME; + diff --git a/src/root.c b/src/root.c new file mode 100644 index 0000000..c79745f --- /dev/null +++ b/src/root.c @@ -0,0 +1,51 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "root.h" + +#include +#include +#include "errors.h" + + +//-------------------------------------------------- +// +// using doubles +// +//-------------------------------------------------- + +#include "root_double.h" +#include "root_base.c" + + +//-------------------------------------------------- +// +// using long doubles +// +//-------------------------------------------------- + +#include "root_ldouble.h" +#include "root_base.c" + + +//-------------------------------------------------- +// +// using mpfr floats +// +//-------------------------------------------------- + +#include "root_mpfr.h" +#include "root_base.c" diff --git a/src/root.h b/src/root.h new file mode 100644 index 0000000..7106389 --- /dev/null +++ b/src/root.h @@ -0,0 +1,57 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Root finding +*/ + +#ifndef LIBINUM_ROOT_H +#define LIBINUM_ROOT_H + +#include "types.h" + + +//-------------------------------------------------- +// +// using doubles +// +//-------------------------------------------------- + +#include "root_double.h" +#include "root_base.h" + + +//-------------------------------------------------- +// +// using long doubles +// +//-------------------------------------------------- + +#include "root_ldouble.h" +#include "root_base.h" + + +//-------------------------------------------------- +// +// using mpfr floats +// +//-------------------------------------------------- + +#include "root_mpfr.h" +#include "root_base.h" + +#endif + diff --git a/src/root_base.c b/src/root_base.c new file mode 100644 index 0000000..b13d9db --- /dev/null +++ b/src/root_base.c @@ -0,0 +1,134 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Base functions for root finding + + see integral_*.h for the values taken by ROOT_FUNC, etc... +*/ + + +// compute the root of a real function of 1 variable using the Newton method +int ROOT_FUNC(root_newton_inplace) (ROOT_FLOAT_TYPE* out, int (*func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), int (*deriv_func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), ROOT_FLOAT_TYPE tolerance, unsigned int maxiter, void* extra_args){ + unsigned int count=0; + ROOT_FLOAT_TYPE valf, valdf, delta, tmp; + int ret; + + #ifdef ROOT_FLOAT_INIT + ROOT_FLOAT_INIT(valf); + ROOT_FLOAT_INIT(valdf); + ROOT_FLOAT_INIT(delta); + ROOT_FLOAT_INIT(tmp); + #endif + + // evaluate at guess + ret=(*func)(&valf, *out, extra_args); + if(ret<0){ + #ifdef ROOT_FLOAT_FREE + ROOT_FLOAT_FREE(valf); + ROOT_FLOAT_FREE(valdf); + ROOT_FLOAT_FREE(delta); + ROOT_FLOAT_FREE(tmp); + #endif + return(ret); + } + // check that valf is a number + if(! ROOT_FLOAT_ISNUMBER(valf)){ + #ifdef ROOT_FLOAT_FREE + ROOT_FLOAT_FREE(valf); + ROOT_FLOAT_FREE(valdf); + ROOT_FLOAT_FREE(delta); + ROOT_FLOAT_FREE(tmp); + #endif + return(LIBINUM_ERROR_NAN); + } + + ROOT_FLOAT_ABS(delta, valf); + + // loop until tolerance is reached + while(ROOT_FLOAT_CMP(delta, tolerance) > 0 && count < maxiter){ + // new guess + (*deriv_func)(&valdf, *out, extra_args); + if(ret<0){ + #ifdef ROOT_FLOAT_FREE + ROOT_FLOAT_FREE(valf); + ROOT_FLOAT_FREE(valdf); + ROOT_FLOAT_FREE(delta); + ROOT_FLOAT_FREE(tmp); + #endif + return(ret); + } + // check that valdf is a number that is not 0 + if(! ROOT_FLOAT_ISNUMBER(valdf) || ROOT_FLOAT_ISZERO(valdf)){ + #ifdef ROOT_FLOAT_FREE + ROOT_FLOAT_FREE(valf); + ROOT_FLOAT_FREE(valdf); + ROOT_FLOAT_FREE(delta); + ROOT_FLOAT_FREE(tmp); + #endif + return(LIBINUM_ERROR_NAN); + } + + ROOT_FLOAT_DIV(tmp, valf, valdf); + ROOT_FLOAT_SUB(*out, *out, tmp); + + // evaluate at new guess + (*func)(&valf, *out, extra_args); + if(ret<0){ + #ifdef ROOT_FLOAT_FREE + ROOT_FLOAT_FREE(valf); + ROOT_FLOAT_FREE(valdf); + ROOT_FLOAT_FREE(delta); + ROOT_FLOAT_FREE(tmp); + #endif + return(ret); + } + // check that valf is a number + if(! ROOT_FLOAT_ISNUMBER(valf)){ + #ifdef ROOT_FLOAT_FREE + ROOT_FLOAT_FREE(valf); + ROOT_FLOAT_FREE(valdf); + ROOT_FLOAT_FREE(delta); + ROOT_FLOAT_FREE(tmp); + #endif + return(LIBINUM_ERROR_NAN); + } + + ROOT_FLOAT_ABS(delta, valf); + + // increase counter + count++; + } + + // free variables + #ifdef ROOT_FLOAT_FREE + ROOT_FLOAT_FREE(valf); + ROOT_FLOAT_FREE(valdf); + ROOT_FLOAT_FREE(delta); + ROOT_FLOAT_FREE(tmp); + #endif + + // fail if maxiter was reached + if(count==maxiter){ + return(LIBINUM_ERROR_MAXITER); + } + return(0); +} +int ROOT_FUNC(root_newton) (ROOT_FLOAT_TYPE* out, int (*func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), int (*deriv_func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), ROOT_FLOAT_TYPE init, ROOT_FLOAT_TYPE tolerance, unsigned int maxiter, void* extra_args){ + ROOT_FLOAT_SET(*out, init); + ROOT_FUNC(root_newton_inplace) (out, func, deriv_func, tolerance, maxiter, extra_args); + return(0); +} diff --git a/src/root_base.h b/src/root_base.h new file mode 100644 index 0000000..7a69616 --- /dev/null +++ b/src/root_base.h @@ -0,0 +1,27 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Base functions for root finding + + see integral_*.h for the values taken by ROOT_FUNC, etc... +*/ + + +// compute the root of a real function of 1 variable using the Newton method +int ROOT_FUNC(root_newton_inplace) (ROOT_FLOAT_TYPE* out, int (*func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), int (*deriv_func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), ROOT_FLOAT_TYPE tolerance, unsigned int maxiter, void* extra_args); +int ROOT_FUNC(root_newton) (ROOT_FLOAT_TYPE* out, int (*func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), int (*deriv_func)(ROOT_FLOAT_TYPE*, ROOT_FLOAT_TYPE, void*), ROOT_FLOAT_TYPE init, ROOT_FLOAT_TYPE tolerance, unsigned int maxiter, void* extra_args); + diff --git a/src/root_double.h b/src/root_double.h new file mode 100644 index 0000000..f564998 --- /dev/null +++ b/src/root_double.h @@ -0,0 +1,55 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for root finding using doubles +*/ + + +// reset CPP macros +#undef ROOT_FUNC +#undef ROOT_FLOAT_TYPE +#undef ROOT_FLOAT_INIT +#undef ROOT_FLOAT_FREE +#undef ROOT_FLOAT_SET +#undef ROOT_FLOAT_SUB +#undef ROOT_FLOAT_DIV +#undef ROOT_FLOAT_CMP +#undef ROOT_FLOAT_ABS +#undef ROOT_FLOAT_ISNUMBER +#undef ROOT_FLOAT_ISZERO + + +// suffix of function names +#define ROOT_FUNC(NAME) NAME ## _double + +// type of floats +#define ROOT_FLOAT_TYPE double +// set float +#define ROOT_FLOAT_SET(FLOAT, VAL) FLOAT=VAL +// subtract floats +#define ROOT_FLOAT_SUB(FLOAT, VAL1, VAL2) FLOAT=VAL1-VAL2 +// divide floats +#define ROOT_FLOAT_DIV(FLOAT, VAL1, VAL2) FLOAT=VAL1/VAL2 +// compare floats (0 if equal, -1 if <, +1 if >) +#define ROOT_FLOAT_CMP(VAL1, VAL2) VAL1-VAL2 +// abs of float +#define ROOT_FLOAT_ABS(FLOAT, VAL) FLOAT=fabs(VAL) +// check whether a float is a regular number +#define ROOT_FLOAT_ISNUMBER(FLOAT) (fpclassify(FLOAT)>=FP_ZERO) +// whether float is 0 +#define ROOT_FLOAT_ISZERO(VAL) (VAL==0.) + diff --git a/src/root_ldouble.h b/src/root_ldouble.h new file mode 100644 index 0000000..a20643b --- /dev/null +++ b/src/root_ldouble.h @@ -0,0 +1,55 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for root finding using long doubles +*/ + + +// reset CPP macros +#undef ROOT_FUNC +#undef ROOT_FLOAT_TYPE +#undef ROOT_FLOAT_INIT +#undef ROOT_FLOAT_FREE +#undef ROOT_FLOAT_SET +#undef ROOT_FLOAT_SUB +#undef ROOT_FLOAT_DIV +#undef ROOT_FLOAT_CMP +#undef ROOT_FLOAT_ABS +#undef ROOT_FLOAT_ISNUMBER +#undef ROOT_FLOAT_ISZERO + + +// suffix of function names +#define ROOT_FUNC(NAME) NAME ## _ldouble + +// type of floats +#define ROOT_FLOAT_TYPE long double +// set float +#define ROOT_FLOAT_SET(FLOAT, VAL) FLOAT=VAL +// subtract floats +#define ROOT_FLOAT_SUB(FLOAT, VAL1, VAL2) FLOAT=VAL1-VAL2 +// divide floats +#define ROOT_FLOAT_DIV(FLOAT, VAL1, VAL2) FLOAT=VAL1/VAL2 +// compare floats (0 if equal, -1 if <, +1 if >) +#define ROOT_FLOAT_CMP(VAL1, VAL2) VAL1-VAL2 +// abs of float +#define ROOT_FLOAT_ABS(FLOAT, VAL) FLOAT=fabsl(VAL) +// check whether a float is a regular number +#define ROOT_FLOAT_ISNUMBER(FLOAT) (fpclassify(FLOAT)>=FP_ZERO) +// whether float is 0 +#define ROOT_FLOAT_ISZERO(VAL) (VAL==0.) + diff --git a/src/root_mpfr.h b/src/root_mpfr.h new file mode 100644 index 0000000..6535e0b --- /dev/null +++ b/src/root_mpfr.h @@ -0,0 +1,59 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Preprocessor macros for root finding using multi-precision floats (mpfr) +*/ + + +// reset CPP macros +#undef ROOT_FUNC +#undef ROOT_FLOAT_TYPE +#undef ROOT_FLOAT_INIT +#undef ROOT_FLOAT_FREE +#undef ROOT_FLOAT_SET +#undef ROOT_FLOAT_SUB +#undef ROOT_FLOAT_DIV +#undef ROOT_FLOAT_CMP +#undef ROOT_FLOAT_ABS +#undef ROOT_FLOAT_ISNUMBER +#undef ROOT_FLOAT_ISZERO + + +// suffix of function names +#define ROOT_FUNC(NAME) NAME ## _mpfr + +// type of floats +#define ROOT_FLOAT_TYPE mpfr_t +// init float +#define ROOT_FLOAT_INIT(VAR) mpfr_init(VAR) +// free float +#define ROOT_FLOAT_FREE(VAR) mpfr_clear(VAR) +// set float +#define ROOT_FLOAT_SET(FLOAT, VAL) mpfr_set(FLOAT, VAL, MPFR_RNDN) +// subtract floats +#define ROOT_FLOAT_SUB(FLOAT, VAL1, VAL2) mpfr_sub(FLOAT, VAL1, VAL2, MPFR_RNDN) +// divide floats +#define ROOT_FLOAT_DIV(FLOAT, VAL1, VAL2) mpfr_div(FLOAT, VAL1, VAL2, MPFR_RNDN) +// compare floats (0 if equal, -1 if <, +1 if >) +#define ROOT_FLOAT_CMP(VAL1, VAL2) mpfr_cmp(VAL1, VAL2) +// abs of float +#define ROOT_FLOAT_ABS(FLOAT, VAL) mpfr_abs(FLOAT, VAL, MPFR_RNDN) +// check whether a float is a regular number +#define ROOT_FLOAT_ISNUMBER(FLOAT) mpfr_number_p(FLOAT)!=0 +// whether float is 0 +#define ROOT_FLOAT_ISZERO(VAL) mpfr_zero_p(VAL)!=0 + diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..e438133 --- /dev/null +++ b/src/types.h @@ -0,0 +1,84 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Types and structures +*/ + +#ifndef LIBINUM_TYPES_H +#define LIBINUM_TYPES_H + +#include +#include + +// arrays +#include "array_int.h" +#include "array_type.h" + +#include "array_uint.h" +#include "array_type.h" + +#include "array_double.h" +#include "array_type.h" + +#include "array_ldouble.h" +#include "array_type.h" + +#include "array_mpfr.h" +#include "array_type.h" + +#include "array_2_mpfr.h" +#include "array_type.h" + +#include "array_char.h" +#include "array_type.h" + +#include "array_str.h" +#include "array_type.h" + +#include "array_pthread_t.h" +#include "array_type.h" + + +// 1-variable polynomial +#include "polynomial_mpfr.h" +#include "polynomial_type.h" + +#include "polynomial_double.h" +#include "polynomial_type.h" + +#include "polynomial_ldouble.h" +#include "polynomial_type.h" + +// polynomial array +#include "array_polynomial_mpfr.h" +#include "array_type.h" + +#include "array_polynomial_double.h" +#include "array_type.h" + +#include "array_polynomial_ldouble.h" +#include "array_type.h" + + +// multivariable polynomials +#include "polynomialMV_mpz.h" +#include "polynomialMV_type.h" + +#include "polynomialMV_int.h" +#include "polynomialMV_type.h" + +#endif diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..d1fad7e --- /dev/null +++ b/src/utils.c @@ -0,0 +1,79 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "utils.h" + +#include +#include +// define MPFR_USE_FILE to enable the use of mpfr_printf +#define MPFR_USE_FILE +#include +#include +#include "array.h" + +// stringify macros +#define STR(S) XSTR(S) +#define XSTR(S) #S + +// print a double at maximal precision +int fprint_double(FILE* file, double x){ + fprintf(file, "% ." STR(__DBL_DIG__) "le",x); + return(0); +} + +// print a long double at maximal precision +int fprint_ldouble(FILE* file, long double x){ + fprintf(file, "% ." STR(__LDBL_DIG__) "Le",x); + return(0); +} + +// log10(2) +#define LOG2 0.3010299956639812 +// print an mpfr at maximal precision +int fprint_mpfr(FILE* file, mpfr_t x){ + // the printf format + array_char printf_format; + array_char_init(&printf_format,12); + array_char_snprintf(&printf_format,"%% .%dRe\0", (int)((mpfr_get_default_prec()-1)*LOG2)); + mpfr_fprintf(file, printf_format.values, x); + array_char_free(printf_format); + return(0); +} + + +// print information about data types +int print_datatype_info(FILE* file){ + + fprintf(file, " int8_t = " STR(__INT8_TYPE__) "\n"); + fprintf(file, " int16_t = " STR(__INT16_TYPE__) "\n"); + fprintf(file, " int32_t = " STR(__INT32_TYPE__) "\n"); + fprintf(file, " int64_t = " STR(__INT64_TYPE__) "\n"); + + fprintf(file, "\n"); + + fprintf(file, " double: precision: " STR(__DBL_MANT_DIG__) ", emax: " STR(__DBL_MAX_EXP__) ", emin: " STR(__DBL_MIN_EXP__) "\n"); + fprintf(file, "long double: precision: " STR(__LDBL_MANT_DIG__) ", emax: " STR(__LDBL_MAX_EXP__) ", emin: " STR(__LDBL_MIN_EXP__) "\n"); + + fprintf(file, "\n"); + + #if _MPFR_PREC_FORMAT == 2 + fprintf(file, "mpfr precision and emax: int\n"); + #elif _MPFR_PREC_FORMAT == 3 + fprintf(file, "mpfr precision and emax: long int\n"); + #endif + + return(0); +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..c3954b4 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,36 @@ +/* +Copyright 2016 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/* + Useful functions +*/ + +#ifndef LIBINUM_UTILS_H +#define LIBINUM_UTILS_H + +#include +#include + +// print a double at maximal precision +int fprint_double(FILE* file, double x); +// print a long double at maximal precision +int fprint_ldouble(FILE* file, long double x); +// print an mpfr at maximal precision +int fprint_mpfr(FILE* file, mpfr_t x); + +// print information about data types +int print_datatype_info(); +#endif -- cgit v1.2.3-54-g00ecf