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:
+
+ - Compute roots of smooth real functions numerically using the Newton-Raphson algorithm.
+ - Compute definite integrals of smooth real functions numerically using Gauss-Legendre quadratures.
+
+ In addition, libinum defines the following structures:
+
+ - Variable-length arrays.
+ - Single variable and multivariate polynomials.
+
+
+
+
+ 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.
+
+
+
+ -
+
+
+
+
+
+ int root_newton_double(
+
+
+ double* out,
+ int (*func)(double*, double, void*),
+ int (*deriv_func)(double*, double, void*),
+ double init,
+ double tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+
+
+
+
+ int root_newton_ldouble(
+
+
+ long double* out,
+ int (*func)(long double*, long double, void*),
+ int (*deriv_func)(long double*, long double, void*),
+ long double init,
+ long double tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+
+
+
+
+ int root_newton_mpfr(
+
+
+ mpfr_t* out,
+ int (*func)(mpfr_t*, mpfr_t, void*),
+ int (*deriv_func)(mpfr_t*, mpfr_t, void*),
+ mpfr_t init,
+ mpfr_t tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+
+
+
+ Description: Compute a numerical approximation for the root.
+
+
+ Arguments:
+
+ -
+
out
: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an mpfr_t
), then it must be initialized.
+
+ -
+
func
: pointer to the function that computes \(f(x)\). It must be in the format specified in functions.
+
+ -
+
deriv_func
: pointer to the function that computes the derivative of \(f\). It must be in the format specified in functions.
+
+ -
+
init
: the value of \(x_0\).
+
+ -
+
tolerance
: the algorithm halts when \(|x_{n+1}-x_n|\leqslant\)tolerance
. tolerance
thus provides control over the error of the algorithm.
+
+ -
+
maxiter
: maximum number of iterations. The algorithm gives up if this number is reached and throws a LIBINUM_ERROR_MAXITER
exception.
+
+ -
+
extra_args
: pointer to the extra arguments to be passed to the functions *func
and *deriv_func
when they are evaluated.
+
+
+
+ Return value: returns 0
on success, and
+
+ LIBINUM_ERROR_MAXITER
if the maximal number of iterations was reached.
+ LIBINUM_ERROR_NAN
if any of the evaluations of *func
or *deriv_func
returned nan
or infinity
, or if any of the evaluations of *deriv_func
returned 0
.
+
+
+
+
+ -
+
+
+
+
+
+ int root_newton_inplace_double(
+
+
+ double* out,
+ int (*func)(double*, double, void*),
+ int (*deriv_func)(double*, double, void*),
+ double tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+
+
+
+
+ int root_newton_inplace_ldouble(
+
+
+ long double* out,
+ int (*func)(long double*, long double, void*),
+ int (*deriv_func)(long double*, long double, void*),
+ long double tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+
+
+
+
+ int root_newton_inplace_mpfr(
+
+
+ mpfr_t* out,
+ int (*func)(mpfr_t*, mpfr_t, void*),
+ int (*deriv_func)(mpfr_t*, mpfr_t, void*),
+ mpfr_t tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+
+
+
+ Description: Similar to root_newton_*
except that the value of \(x_0\) is passed to the function in the argument out
.
+
+
+
+ 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
+
+
+ -
+
+
+
+
+
+ int integrate_gauss_double(
+
+
+ double* out,
+ int (*func)(double*, double, void*),
+ double lower,
+ double upper,
+ array_double abcissa,
+ array_double weights,
+ void* extra_args)
+
+
+
+
+
+
+
+ int integrate_gauss_ldouble(
+
+
+ long double* out,
+ int (*func)(long double*, long double, void*),
+ long double lower,
+ long double upper,
+ array_ldouble abcissa,
+ array_ldouble weights,
+ void* extra_args)
+
+
+
+
+
+
+
+ int integrate_gauss_mpfr(
+
+
+ mpfr_t* out,
+ int (*func)(mpfr_t*, mpfr_t, void*),
+ mpfr_t lower,
+ mpfr_t upper,
+ array_mpfr abcissa,
+ array_mpfr weights,
+ void* extra_args)
+
+
+
+
+
+
+ Description: Compute a numerical approximation for the integral of a real function using Gauss quadratures.
+
+
+ Arguments:
+
+ -
+
out
: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an mpfr_t
), then it must be initialized.
+
+ -
+
func
: pointer to the function that computes the integrand \(f(x)\). It must be in the format specified in functions.
+
+ -
+
lower
: lower bound of the integration.
+
+ -
+
upper
: upper bound of the integration.
+
+ -
+
abcissa
: the abcissa used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function gauss_legendre_weights
.
+
+ -
+
weights
: the weights used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function gauss_legendre_weights
.
+
+ -
+
extra_args
: pointer to the extra arguments to be passed to the function *func
when it is evaluated (see functions).
+
+
+
+ Return value: returns 0
on success, and
+
+ LIBINUM_ERROR_NAN
if any of the evaluations of *func
returned nan
or infinity
.
+ LIBINUM_ERROR_SIZE_MISMATCH
if the lengths of the vectors abcissa
and weights
are different.
+
+
+
+
+ -
+
+
+
+
+
+ int integrate_gauss_multithread_double(
+
+
+ double* out,
+ int (*func)(double*, double, void*),
+ double lower,
+ double upper,
+ array_double abcissa,
+ array_double weights,
+ void* extra_args,
+ unsigned int threads,
+ array_pthread_t* thread_ids)
+
+
+
+
+
+
+
+ int integrate_gauss_multithread_ldouble(
+
+
+ long double* out,
+ int (*func)(long double*, long double, void*),
+ long double lower,
+ long double upper,
+ array_ldouble abcissa,
+ array_ldouble weights,
+ void* extra_args,
+ unsigned int threads,
+ array_pthread_t* thread_ids)
+
+
+
+
+
+
+
+ int integrate_gauss_multithread_mpfr(
+
+
+ mpfr_t* out,
+ int (*func)(mpfr_t*, mpfr_t, void*),
+ mpfr_t lower,
+ mpfr_t upper,
+ array_mpfr abcissa,
+ array_mpfr weights,
+ void* extra_args,
+ unsigned int threads,
+ array_pthread_t* thread_ids)
+
+
+
+
+
+ Description: Multithreaded version of integrate_gauss_*
, in which function calls are performed in parallel.
+
+
+ Arguments: Same as integrate_gauss_*
, except for
+
+ -
+
threads
: number of threads to use for the computation.
+
+ -
+
thread_ids
: pointer to an array in which the id of each thread is written to. The array must not be initialized. This array can then be used by each thread to identify themselves.
+
+
+
+ Return value: Same as integrate_gauss_*
.
+
+
+
+ -
+
+
+
+
+
+ int integrate_gauss_smarttmp_mpfr(
+
+
+ mpfr_t* out,
+ int (*func)(mpfr_t*, mpfr_t, void*),
+ mpfr_t lower,
+ mpfr_t upper,
+ array_mpfr abcissa,
+ array_mpfr weights,
+ array_mpfr* tmps,
+ void* extra_args)
+
+
+
+
+
+ Description: Similar to integrate_gauss_*
, except that, if temporary floats are needed during the computation, they are saved to an array so that they can be re-used later on. This is useful to perform repeated integrations, in which it would be costly to re-allocate memory for temporary floats. When using this function, temporary floats are allocated as needed, but they are not discarded.
+
+
+ Arguments: Same as integrate_gauss_*
, except for
+
+ -
+
tmps
: pointer to an array that is used to store temporary floats. The array must be initialized beforehand. When temporary floats are needed, the array is checked for available floats. If enough of them are already present in the array, then the routine uses them, if not, it enlarges the array and allocates as many extra floats as is needed. The floats in the array can then be re-used for other purposes.
+
+
+
+ Return value: Same as integrate_gauss_*
.
+
+
+
+ -
+
+
+
+
+
+ int integrate_gauss_smarttmp_multithread_mpfr(
+
+
+ mpfr_t* out,
+ int (*func)(mpfr_t*, mpfr_t, void*),
+ mpfr_t lower,
+ mpfr_t upper,
+ array_mpfr abcissa,
+ array_mpfr weights,
+ void* extra_args,
+ unsigned int threads,
+ array_pthread_t* thread_ids)
+
+
+
+
+
+ Description: Multithreaded version of integrate_gauss_smarttmp_*
, in which function calls are performed in parallel.
+
+
+ Arguments: Same as integrate_gauss_smarttmp_*
, except for
+
+ -
+
threads
: number of threads to use for the computation.
+
+ -
+
thread_ids
: pointer to an array in which the id of each thread is written to. The array must not be initialized. This array can then be used by each thread to identify themselves.
+
+
+
+ Return value: Same as integrate_gauss_*
.
+
+
+
+
+ -
+
+
+
+
+
+ gauss_legendre_weights_double(
+
+
+ unsigned int order,
+ double tolerance,
+ unsigned int maxiter,
+ array_double* abcissa,
+ array_double* weights)
+
+
+
+
+
+
+
+ gauss_legendre_weights_ldouble(
+
+
+ unsigned int order,
+ long double tolerance,
+ unsigned int maxiter,
+ array_ldouble* abcissa,
+ array_ldouble* weights)
+
+
+
+
+
+
+
+ gauss_legendre_weights_mpfr(
+
+
+ unsigned int order,
+ mpfr_t tolerance,
+ unsigned int maxiter,
+ array_mpfr* abcissa,
+ array_mpfr* weights)
+
+
+
+
+
+ Description: Compute the Gauss-Legendre abcissa and weights.
+
+
+ Arguments:
+
+ -
+
order
: the order \(N\) of the integration.
+
+ -
+
tolerance
: tolerance for the Newton algorithm used to compute the roots of the Legendre polynomial (see root_newton
).
+
+ -
+
maxiter
: the maximal number of steps to compute before giving up.
+
+ -
+
abcissa
: pointer to the array in which to write the abcissa. It must not be initialized.
+
+ -
+
weights
: pointer to the array in which to write the weights. It must not be initialized.
+
+
+
+ Return value: returns 0
on success, and
+
+ LIBINUM_ERROR_MAXITER
if, when computing the roots of the Legendre polynomial using a Newton iteration, the maximal number of iterations was reached.
+
+
+
+
+ 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:
+
+
+
+ -
+
array_int
:
+ { int* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_uint
:
+ { unsigned int* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_double
:
+ { double* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_ldouble
:
+ { long double* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_mpfr
:
+ { mpfr_t* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_2_mpfr
:
+ { array_mpfr* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_char
:
+ { char* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_str
:
+ { array_char* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_polynomial_double
:
+ { polynomial_double* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_polynomial_ldouble
:
+ { polynomial_ldouble* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_polynomial_mpfr
:
+ { polynomial_mpfr* values, unsigned int length, unsigned int memory }
+
+
+ -
+
array_pthread_t
:
+ { pthread_t* values, unsigned int length, unsigned int memory }
+
+
+ where the keys correspond to
+
+ values
: the array in which the objects are stored
+ length
: the number of elements in the array
+ memory
: the number of elements allocated to the array
+
+ 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
+
+ -
+
+
+ int array_int_init(array_int* array, unsigned int memory)
+
+
+
+ int array_uint_init(array_uint* array, unsigned int memory)
+
+
+
+ int array_double_init(array_double* array, unsigned int memory)
+
+
+
+ int array_ldouble_init(array_ldouble* array, unsigned int memory)
+
+
+
+ int array_mpfr_init(array_mpfr* array, unsigned int memory)
+
+
+
+ int array_2_mpfr_init(array_2_mpfr* array, unsigned int memory)
+
+
+
+ int array_char_init(array_char* array, unsigned int memory)
+
+
+
+ int array_str_init(array_str* array, unsigned int memory)
+
+
+
+ int array_polynomial_double_init(array_polynomial_double* array, unsigned int memory)
+
+
+
+ int array_polynomial_ldouble_init(array_polynomial_ldouble* array, unsigned int memory)
+
+
+
+ int array_polynomial_mpfr_init(array_polynomial_mpfr* array, unsigned int memory)
+
+
+
+ int array_pthread_t_init(array_pthread_t* array, unsigned int memory)
+
+
+ initialize the array pointed to by array
, and allocate memory
elements.
+
+
+ -
+
+
+ int array_int_free(array_int array)
+
+
+
+ int array_uint_free(array_uint array)
+
+
+
+ int array_double_free(array_double array)
+
+
+
+ int array_ldouble_free(array_ldouble array)
+
+
+
+ int array_mpfr_free(array_mpfr array)
+
+
+
+ int array_2_mpfr_free(array_2_mpfr array)
+
+
+
+ int array_char_free(array_char array)
+
+
+
+ int array_str_free(array_str array)
+
+
+
+ int array_polynomial_double_free(array_polynomial_double array)
+
+
+
+ int array_polynomial_ldouble_free(array_polynomial_ldouble array)
+
+
+
+ int array_polynomial_mpfr_free(array_polynomial_mpfr array)
+
+
+
+ int array_pthread_t_free(array_pthread_t array)
+
+
+ free the array array
.
+
+
+ -
+
+
+ int array_mpfr_free_vects(array_mpfr array)
+
+
+
+ int array_2_mpfr_free_vects(array_2_mpfr array)
+
+
+
+ int array_str_free_vects(array_str array)
+
+
+
+ int array_polynomial_double_free_vects(array_polynomial_double array)
+
+
+
+ int array_polynomial_ldouble_free_vects(array_polynomial_ldouble array)
+
+
+
+ int array_polynomial_mpfr_free_vects(array_polynomial_mpfr array)
+
+
+ free the memory pointed to by array.values
, but do not free the elements of the array.
+
+
+ -
+
+
+ int array_int_resize(array_int* array, unsigned int newsize)
+
+
+
+ int array_uint_resize(array_uint* array, unsigned int newsize)
+
+
+
+ int array_double_resize(array_double* array, unsigned int newsize)
+
+
+
+ int array_ldouble_resize(array_ldouble* array, unsigned int newsize)
+
+
+
+ int array_mpfr_resize(array_mpfr* array, unsigned int newsize)
+
+
+
+ int array_2_mpfr_resize(array_2_mpfr* array, unsigned int newsize)
+
+
+
+ int array_char_resize(array_char* array, unsigned int newsize)
+
+
+
+ int array_str_resize(array_str* array, unsigned int newsize)
+
+
+
+ int array_polynomial_double_resize(array_polynomial_double* array, unsigned int newsize)
+
+
+
+ int array_polynomial_ldouble_resize(array_polynomial_ldouble* array, unsigned int newsize)
+
+
+
+ int array_polynomial_mpfr_resize(array_polynomial_mpfr* array, unsigned int newsize)
+
+
+
+ int array_pthread_t_resize(array_pthread_t* array, unsigned int newsize)
+
+
+ copy the array pointed to by array
to another with memory newsize
.
+
+
+ -
+
+
+ int array_int_append(int value, array_int* array)
+
+
+
+ int array_uint_append(unsigned int value, array_uint* array)
+
+
+
+ int array_double_append(double value, array_double* array)
+
+
+
+ int array_ldouble_append(long double value, array_ldouble* array)
+
+
+
+ int array_mpfr_append(mpfr_t value, array_mpfr* array)
+
+
+
+ int array_2_mpfr_append(array_mpfr value, array_2_mpfr* array)
+
+
+
+ int array_char_append(char value, array_char* array)
+
+
+
+ int array_str_append(array_char value, array_str* array)
+
+
+
+ int array_polynomial_double_append(polynomial_double value, array_polynomial_double* array)
+
+
+
+ int array_polynomial_ldouble_append(polynomial_ldouble value, array_polynomial_ldouble* array)
+
+
+
+ int array_polynomial_mpfr_append(polynomial_mpfr value, array_polynomial_mpfr* array)
+
+
+
+ int array_pthread_t_append(pthread_t value, array_pthread_t* array)
+
+
+ append value
to the end of the array pointed to by array
. Resize the array if needed.
+
+
+ -
+
+
+ int array_mpfr_append_noinit(int value, array_mpfr* array)
+
+
+
+ int array_2_mpfr_append_noinit(unsigned int value, array_2_mpfr* array)
+
+
+
+ int array_str_append_noinit(double value, array_str* array)
+
+
+
+ int array_polynomial_double_append_noinit(long double value, array_polynomial_double* array)
+
+
+
+ int array_polynomial_ldouble_append_noinit(mpfr_t value, array_polynomial_ldouble* array)
+
+
+
+ int array_polynomial_mpfr_append_noinit(array_mpfr value, array_polynomial_mpfr* array)
+
+
+ append value
to the end of the array pointed to by array
. Do not initialize the new value, instead, copy value
to the end of *array
. value
must not be freed, since it will be freed when *array
is. Resize the array if needed.
+
+
+ -
+
+
+ int array_int_concat(array_int input, array_int* output)
+
+
+
+ int array_uint_concat(array_uint input, array_uint* output)
+
+
+
+ int array_double_concat(array_double input, array_double* output)
+
+
+
+ int array_ldouble_concat(array_ldouble input, array_ldouble* output)
+
+
+
+ int array_mpfr_concat(array_mpfr input, array_mpfr* output)
+
+
+
+ int array_2_mpfr_concat(array_2_mpfr input, array_2_mpfr* output)
+
+
+
+ int array_char_concat(array_char input, array_char* output)
+
+
+
+ int array_str_concat(array_str input, array_str* output)
+
+
+
+ int array_polynomial_double_concat(array_polynomial_double input, array_polynomial_double* output)
+
+
+
+ int array_polynomial_ldouble_concat(array_polynomial_ldouble input, array_polynomial_ldouble* output)
+
+
+
+ int array_polynomial_mpfr_concat(array_polynomial_mpfr input, array_polynomial_mpfr* output)
+
+
+
+ int array_pthread_t_concat(array_pthread_t input, array_pthread_t* output)
+
+
+ append the values in input
to the end of the array pointed to by output
. Resize the array if needed.
+
+
+ -
+
+
+ int array_int_cpy(array_int input, array_int* output)
+
+
+
+ int array_uint_cpy(array_uint input, array_uint* output)
+
+
+
+ int array_double_cpy(array_double input, array_double* output)
+
+
+
+ int array_ldouble_cpy(array_ldouble input, array_ldouble* output)
+
+
+
+ int array_mpfr_cpy(array_mpfr input, array_mpfr* output)
+
+
+
+ int array_2_mpfr_cpy(array_2_mpfr input, array_2_mpfr* output)
+
+
+
+ int array_char_cpy(array_char input, array_char* output)
+
+
+
+ int array_str_cpy(array_str input, array_str* output)
+
+
+
+ int array_polynomial_double_cpy(array_polynomial_double input, array_polynomial_double* output)
+
+
+
+ int array_polynomial_ldouble_cpy(array_polynomial_ldouble input, array_polynomial_ldouble* output)
+
+
+
+ int array_polynomial_mpfr_cpy(array_polynomial_mpfr input, array_polynomial_mpfr* output)
+
+
+
+ int array_pthread_t_cpy(array_pthread_t input, array_pthread_t* output)
+
+
+ copy input
to the array pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ int array_int_cpy_noinit(array_int input, array_int* output)
+
+
+
+ int array_uint_cpy_noinit(array_uint input, array_uint* output)
+
+
+
+ int array_double_cpy_noinit(array_double input, array_double* output)
+
+
+
+ int array_ldouble_cpy_noinit(array_ldouble input, array_ldouble* output)
+
+
+
+ int array_mpfr_cpy_noinit(array_mpfr input, array_mpfr* output)
+
+
+
+ int array_2_mpfr_cpy_noinit(array_2_mpfr input, array_2_mpfr* output)
+
+
+
+ int array_char_cpy_noinit(array_char input, array_char* output)
+
+
+
+ int array_str_cpy_noinit(array_str input, array_str* output)
+
+
+
+ int array_polynomial_double_cpy_noinit(array_polynomial_double input, array_polynomial_double* output)
+
+
+
+ int array_polynomial_ldouble_cpy_noinit(array_polynomial_ldouble input, array_polynomial_ldouble* output)
+
+
+
+ int array_polynomial_mpfr_cpy_noinit(array_polynomial_mpfr input, array_polynomial_mpfr* output)
+
+
+
+ int array_pthread_t_cpy_noinit(array_pthread_t input, array_pthread_t* output)
+
+
+ copy input
to the array pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
+
+
+ -
+
+
+ int array_int_subarray(array_int array, unsigned int begin, unsigned int end, array_int* subarray)
+
+
+
+ int array_uint_subarray(array_uint array, unsigned int begin, unsigned int end, array_uint* subarray)
+
+
+
+ int array_double_subarray(array_double array, unsigned int begin, unsigned int end, array_double* subarray)
+
+
+
+ int array_ldouble_subarray(array_ldouble array, unsigned int begin, unsigned int end, array_ldouble* subarray)
+
+
+
+ int array_mpfr_subarray(array_mpfr array, unsigned int begin, unsigned int end, array_mpfr* subarray)
+
+
+
+ int array_2_mpfr_subarray(array_2_mpfr array, unsigned int begin, unsigned int end, array_2_mpfr* subarray)
+
+
+
+ int array_char_subarray(array_char array, unsigned int begin, unsigned int end, array_char* subarray)
+
+
+
+ int array_str_subarray(array_str array, unsigned int begin, unsigned int end, array_str* subarray)
+
+
+
+ int array_polynomial_double_subarray(array_polynomial_double array, unsigned int begin, unsigned int end, array_polynomial_double* subarray)
+
+
+
+ int array_polynomial_ldouble_subarray(array_polynomial_ldouble array, unsigned int begin, unsigned int end, array_polynomial_ldouble* subarray)
+
+
+
+ int array_polynomial_mpfr_subarray(array_polynomial_mpfr array, unsigned int begin, unsigned int end, array_polynomial_mpfr* subarray)
+
+
+
+ int array_pthread_t_subarray(array_pthread_t array, unsigned int begin, unsigned int end, array_pthread_t* subarray)
+
+
+ extract the sub-array of array
whose indices are larger or equal to begin
and smaller or equal to end
. Returns LIBINUM_ERROR_ILLEGAL_MEMORY_ACCESS
if the subarray does not exist.
+
+
+ -
+
+
+ int array_int_print(array_int array)
+
+
+
+ int array_uint_print(array_uint array)
+
+
+
+ int array_double_print(array_double array)
+
+
+
+ int array_ldouble_print(array_ldouble array)
+
+
+
+ int array_mpfr_print(array_mpfr array)
+
+
+
+ int array_2_mpfr_print(array_2_mpfr array)
+
+
+
+ int array_char_print(array_char array)
+
+
+
+ int array_str_print(array_str array)
+
+
+
+ int array_polynomial_double_print(array_polynomial_double array)
+
+
+
+ int array_polynomial_ldouble_print(array_polynomial_ldouble array)
+
+
+
+ int array_polynomial_mpfr_print(array_polynomial_mpfr array)
+
+
+ print the elements of array
.
+
+
+
+ Extra functions for arrays of ordered objects
+ In addition, we define the following ordering for the possible objects in an array:
+
+ - the obvious ordering for
int, unsigned int, double, long double, mpfr, char
+ - lexicographical ordering for
array_char
+ - no ordering for
polynomial_*
.
+
+ If the ordering is defined, then the following functions are defined:
+
+
+ -
+
+
+ int array_int_append_unique(int value, array_int* array)
+
+
+
+ int array_uint_append_unique(unsigned int value, array_uint* array)
+
+
+
+ int array_double_append_unique(double value, array_double* array)
+
+
+
+ int array_ldouble_append_unique(long double value, array_ldouble* array)
+
+
+
+ int array_mpfr_append_unique(mpfr_t value, array_mpfr* array)
+
+
+
+ int array_2_mpfr_append_unique(array_mpfr value, array_2_mpfr* array)
+
+
+
+ int array_char_append_unique(char value, array_char* array)
+
+
+
+ int array_str_append_unique(array_char value, array_str* array)
+
+
+
+ int array_pthread_t_append_unique(pthread_t value, array_pthread_t* array)
+
+
+ if value
is not already present in the array pointed to by array
, then append it. Resize the array if needed.
+
+
+ -
+
+
+ int array_int_concat_unique(array_int input, array_int* output)
+
+
+
+ int array_uint_concat_unique(array_uint input, array_uint* output)
+
+
+
+ int array_double_concat_unique(array_double input, array_double* output)
+
+
+
+ int array_ldouble_concat_unique(array_ldouble input, array_ldouble* output)
+
+
+
+ int array_mpfr_concat_unique(array_mpfr input, array_mpfr* output)
+
+
+
+ int array_2_mpfr_concat_unique(array_2_mpfr input, array_2_mpfr* output)
+
+
+
+ int array_char_concat_unique(array_char input, array_char* output)
+
+
+
+ int array_str_concat_unique(array_str input, array_str* output)
+
+
+
+ int array_pthread_t_concat_unique(array_pthread_t input, array_pthread_t* output)
+
+
+ append the values in input
that are not already present in the array pointed to by output
. Resize the array if needed.
+
+
+ -
+
+
+ int array_int_find(int val, array_int array)
+
+
+
+ int array_uint_find(unsigned int val, array_uint array)
+
+
+
+ int array_double_find(double val, array_double array)
+
+
+
+ int array_ldouble_find(long double val, array_ldouble array)
+
+
+
+ int array_mpfr_find(mpfr_t val, array_mpfr array)
+
+
+
+ int array_2_mpfr_find(array_mpfr val, array_2_mpfr array)
+
+
+
+ int array_char_find(char val, array_char array)
+
+
+
+ int array_str_find(array_char val, array_str array)
+
+
+
+ int array_pthread_t_find(pthread_t val, array_pthread_t array)
+
+
+ search for val
in array
. Returns the index of val
in array
if it is present, and -1
if it is not.
+
+
+ -
+
+
+ int array_int_sort(array_int array)
+
+
+
+ int array_uint_sort(array_uint array)
+
+
+
+ int array_double_sort(array_double array)
+
+
+
+ int array_ldouble_sort(array_ldouble array)
+
+
+
+ int array_mpfr_sort(array_mpfr array)
+
+
+
+ int array_2_mpfr_sort(array_2_mpfr array)
+
+
+
+ int array_char_sort(array_char array)
+
+
+
+ int array_str_sort(array_str array)
+
+
+
+ int array_pthread_t_sort(array_pthread_t array)
+
+
+ sort the elements of array
from smallest to largest (for numerical values, the ordering relation is the usual one, for characters and strings, the lexicographical ordering is used). The sorting is performed in place. The quicksort algorithm is used.
+
+
+ -
+
+
+ int array_int_sort_sub(array_int array, unsigned int begin, unsigned int end)
+
+
+
+ int array_uint_sort_sub(array_uint array, unsigned int begin, unsigned int end)
+
+
+
+ int array_double_sort_sub(array_double array, unsigned int begin, unsigned int end)
+
+
+
+ int array_ldouble_sort_sub(array_ldouble array, unsigned int begin, unsigned int end)
+
+
+
+ int array_mpfr_sort_sub(array_mpfr array, unsigned int begin, unsigned int end)
+
+
+
+ int array_2_mpfr_sort_sub(array_2_mpfr array, unsigned int begin, unsigned int end)
+
+
+
+ int array_char_sort_sub(array_char array, unsigned int begin, unsigned int end)
+
+
+
+ int array_str_sort_sub(array_str array, unsigned int begin, unsigned int end)
+
+
+
+ int array_pthread_t_sort_sub(array_pthread_t array, unsigned int begin, unsigned int end)
+
+
+ sort the elements of the sub-array of array
consisting of the elements whose index is larger or equal than begin
and smaller or equal than end
. The sorting is performed in place. The quicksort algorithm is used.
+
+
+ -
+
+
+ int array_int_cmp(array_int array1, array_int array2)
+
+
+
+ int array_uint_cmp(array_uint array1, array_uint array2)
+
+
+
+ int array_double_cmp(array_double array1, array_double array2)
+
+
+
+ int array_ldouble_cmp(array_ldouble array1, array_ldouble array2)
+
+
+
+ int array_mpfr_cmp(array_mpfr array1, array_mpfr array2)
+
+
+
+ int array_2_mpfr_cmp(array_2_mpfr array1, array_2_mpfr array2)
+
+
+
+ int array_char_cmp(array_char array1, array_char array2)
+
+
+
+ int array_str_cmp(array_str array1, array_str array2)
+
+
+
+ int array_pthread_t_cmp(array_pthread_t array1, array_pthread_t array2)
+
+
+ compare array1
and array2
. Returns 0
if both arrays are equal (that is, if their elements are the same, and in the same order), -1
if array1
is smaller than array2
in lexicographical order, and 1
if array1
is smaller than array2
in lexicographical order.
+
+
+
+ Extra functions for array_char
+ In addition, the following functions are defined for array_char
.
+
+ -
+
int array_char_append_str(char* str, array_char* output)
+ append the characters in str
to the end of the array pointed to by output
. Resize the array if needed.
+
+ -
+
int array_char_to_str(array_char input, char** output)
+ copy the string in input
to the string pointed to by output
. Initializes *output
.
+
+ -
+
char* array_char_to_str(array_char* input)
+ convert the array pointed to by input
to a string, and return it. Appends '\0'
at the end of *input
. The string thus generated should not be freed, since it is actually a pointer to input->str
, which is freed when *input
is.
+
+ -
+
int str_to_array_char(char* str, array_char* output)
+ copy the string input
to the array pointed to by output
. Initializes *output
.
+
+ -
+
int array_char_cmp_str(array_char array_char, char* str)
+ compare the string in array_char
and the string str
. Returns 1
if the strings are identical, and 0
if not.
+
+ -
+
int array_char_snprintf(array_char* output, char* fmt)
+ reads the format fmt
and appends the output to the array pointed to by output
. The format should follow the specifications of the standard C function printf
.
+
+
+
+
+ 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:
+
+
+
+
+ -
+
polynomial_double
:
+ { array_double coefficients, array_uint orders }
+
+
+ -
+
polynomial_ldouble
:
+ { array_ldouble coefficients, array_uint orders }
+
+
+ -
+
polynomial_mpfr
:
+ { array_mpfr coefficients, array_uint orders }
+
+
+ where the keys correspond to
+
+ coefficients
: array of coefficients
+ orders
: array of exponents.
+
+ polynomial_*
's must be initialized before they are used, and freed when they are no longer needed.
+
+ Functions
+
+ -
+
+
+ polynomial_double_init(polynomial_double* poly, unsigned int memory)
+
+
+
+ polynomial_ldouble_init(polynomial_ldouble* poly, unsigned int memory)
+
+
+
+ polynomial_mpfr_init(polynomial_mpfr* poly, unsigned int memory)
+
+
+ initialize the polynomial pointed to by poly
, and allocate memory
terms.
+
+
+ -
+
+
+ polynomial_double_free(polynomial_double poly)
+
+
+
+ polynomial_ldouble_free(polynomial_ldouble poly)
+
+
+
+ polynomial_mpfr_free(polynomial_mpfr poly)
+
+
+ free the polynomial poly
.
+
+
+ -
+
+
+ polynomial_double_resize(polynomial_double* poly, unsigned int newsize)
+
+
+
+ polynomial_ldouble_resize(polynomial_ldouble* poly, unsigned int newsize)
+
+
+
+ polynomial_mpfr_resize(polynomial_mpfr* poly, unsigned int newsize)
+
+
+ copy the array polynomial to by poly
to another with memory newsize
.
+
+
+ -
+
+
+ polynomial_double_add_monomial(double val, unsigned int order, polynomial_double* output)
+
+
+
+ polynomial_ldouble_add_monomial(long double val, unsigned int order, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_add_monomial(mpfr_t val, unsigned int order, polynomial_mpfr* output)
+
+
+ add a term with coefficient val
and exponent order
to the polynomial pointed to by output
. Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomial_ldouble_add_monomial_dui(double val, unsigned int order, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_add_monomial_dui(double val, unsigned int order, polynomial_mpfr* output)
+
+
+ add a term with coefficient val
(converted from a double
) and exponent order
(converted from an unsigned int
) to the polynomial pointed to by output
. Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomial_double_cpy(polynomial_double input, polynomial_double* output)
+
+
+
+ polynomial_ldouble_cpy(polynomial_ldouble input, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_cpy(polynomial_mpfr input, polynomial_mpfr* output)
+
+
+ copy input
to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomial_double_cpy_noinit(polynomial_double input, polynomial_double* output)
+
+
+
+ polynomial_ldouble_cpy_noinit(polynomial_ldouble input, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_cpy_noinit(polynomial_mpfr input, polynomial_mpfr* output)
+
+
+ copy input
to the polynomial pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
+
+
+ -
+
+
+ polynomial_double_add(polynomial poly1, polynomial_double poly2, polynomial_double* output)
+
+
+
+ polynomial_ldouble_add(polynomial poly1, polynomial_ldouble poly2, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_add(polynomial poly1, polynomial_mpfr poly2, polynomial_mpfr* output)
+
+
+ add poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomial_double_add_inplace(polynomial_double poly1, polynomial_double* poly2)
+
+
+
+ polynomial_ldouble_add_inplace(polynomial_ldouble poly1, polynomial_ldouble* poly2)
+
+
+
+ polynomial_mpfr_add_inplace(polynomial_mpfr poly1, polynomial_mpfr* poly2)
+
+
+ add poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
+
+
+ -
+
+
+ polynomial_double_mul_scalar(double x, polynomial_double poly, polynomial_double* output)
+
+
+
+ polynomial_ldouble_mul_scalar(long double x, polynomial_ldouble poly, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_mul_scalar(mpfr_t x, polynomial_mpfr poly, polynomial_mpfr* output)
+
+
+ multiply x
and poly
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomial_double_mul_scalar_inplace(double x, polynomial_double* poly)
+
+
+
+ polynomial_ldouble_mul_scalar_inplace(long double x, polynomial_ldouble* poly)
+
+
+
+ polynomial_mpfr_mul_scalar_inplace(mpfr_t x, polynomial_mpfr* poly)
+
+
+ multiply x
and *poly
and write the result to the polynomial pointed to by poly
.
+
+
+ -
+
+
+ polynomial_double_mul_monomial(double x, unsigned int order, polynomial_double poly, polynomial_double* output)
+
+
+
+ polynomial_ldouble_mul_monomial(long double x, unsigned int order, polynomial_ldouble poly, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_mul_monomial(mpfr_t x, unsigned int order, polynomial_mpfr poly, polynomial_mpfr* output)
+
+
+ multiply poly
and the monomial whose coefficient and exponent are x
and order
, and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomial_double_mul_monomial_inplace(double x, unsigned int order, polynomial_double* poly)
+
+
+
+ polynomial_ldouble_mul_monomial_inplace(long double x, unsigned int order, polynomial_ldouble* poly)
+
+
+
+ polynomial_mpfr_mul_monomial_inplace(mpfr_t x, unsigned int order, polynomial_mpfr* poly)
+
+
+ multiply *poly
and the monomial whose coefficient and exponent are x
and order
, and write the result to the polynomial pointed to by poly
.
+
+
+ -
+
+
+ polynomial_double_mul(polynomial_double poly1, polynomial_double poly2, polynomial_double* output)
+
+
+
+ polynomial_ldouble_mul(polynomial_ldouble poly1, polynomial_ldouble poly2, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_mul(polynomial_mpfr poly1, polynomial_mpfr poly2, polynomial_mpfr* output)
+
+
+ multiply poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomial_double_mul_inplace(polynomial_double poly1, polynomial_double* poly2)
+
+
+
+ polynomial_ldouble_mul_inplace(polynomial_ldouble poly1, polynomial_ldouble* poly2)
+
+
+
+ polynomial_mpfr_mul_inplace(polynomial_mpfr poly1, polynomial_mpfr* poly2)
+
+
+ multiply poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
+
+
+ -
+
+
+ polynomial_double_derive(polynomial_double poly, polynomial_double* output)
+
+
+
+ polynomial_ldouble_derive(polynomial_ldouble poly, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_derive(polynomial_mpfr poly, polynomial_mpfr* output)
+
+
+ derive poly
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomial_double_derive_inplace(polynomial_double* poly)
+
+
+
+ polynomial_ldouble_derive_inplace(polynomial_ldouble* poly)
+
+
+
+ polynomial_mpfr_derive_inplace(polynomial_mpfr* poly)
+
+
+ derive *poly
and write the result to the polynomial pointed to by poly
.
+
+
+ -
+
+
+ polynomial_double_evaluate(double* out, double x, polynomial_double poly)
+
+
+
+ polynomial_ldouble_evaluate(long double* out, long double x, polynomial_ldouble poly)
+
+
+
+ polynomial_mpfr_evaluate(mpfr_t* out, mpfr_t x, polynomial_mpfr poly)
+
+
+ evaluate poly
at x
, and write the result to out
. out
must be initialized if its type requires it.
+
+
+ -
+
+
+ polynomial_double_print(polynomial_double poly)
+
+
+
+ polynomial_ldouble_print(polynomial_ldouble poly)
+
+
+
+ polynomial_mpfr_print(polynomial_mpfr poly)
+
+
+ print poly
.
+
+
+ -
+
+
+ polynomial_double_legendre(unsigned int n, polynomial_double* output)
+
+
+
+ polynomial_ldouble_legendre(unsigned int n, polynomial_ldouble* output)
+
+
+
+ polynomial_mpfr_legendre(unsigned int n, polynomial_mpfr* output)
+
+
+ compute the n
-th Legendre polynomial, and write the output to the polynomial pointed to by poly
. Initializes *poly
.
+
+
+
+ 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:
+
+
+ -
+
polynomialMV_int
:
+ { int* coefficients, array_int factors, unsigned int length, unsigned int memory }
+
+
+ -
+
polynomialMV_mpz
:
+ { mpz_t* coefficients, array_int factors, unsigned int length, unsigned int memory }
+
+
+ where the keys correspond to
+
+ coefficients
: array of coefficients
+ factors
: array of factors, i.e. arrays of indices of variables
+ length
: the number of terms in the polynomial
+ memory
: the number of terms allocated to the polynomial
+
+ 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
+
+ -
+
+
+ polynomialMV_int_init(polynomialMV_int* poly, unsigned int memory)
+
+
+
+ polynomialMV_mpz_init(polynomialMV_mpz* poly, unsigned int memory)
+
+
+ initialize the polynomial pointed to by poly
, and allocate memory
terms.
+
+
+ -
+
+
+ polynomialMV_int_free(polynomialMV_int poly)
+
+
+
+ polynomialMV_mpz_free(polynomialMV_mpz poly)
+
+
+ free the polynomial poly
.
+
+
+ -
+
+
+ polynomialMV_int_resize(polynomialMV_int* poly, unsigned int newsize)
+
+
+
+ polynomialMV_mpz_resize(polynomialMV_mpz* poly, unsigned int newsize)
+
+
+ copy the array polynomial to by poly
to another with memory newsize
.
+
+
+ -
+
+
+ polynomialMV_int_cpy(polynomialMV_int input, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_cpy(polynomialMV_mpz input, polynomialMV_mpz* output)
+
+
+ copy input
to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomialMV_int_cpy_noinit(polynomialMV_int input, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_cpy_noinit(polynomialMV_mpz input, polynomialMV_mpz* output)
+
+
+ copy input
to the polynomial pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
+
+
+ -
+
+
+ polynomialMV_int_append(array_int factor, int coef, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_append(array_int factor, mpz_t coef, polynomialMV_mpz* output)
+
+
+ add a term with coefficient coef
and factor factor
to the polynomial pointed to by output
. Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomialMV_int_append_inplace(array_int factor, int coef, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_append_inplace(array_int factor, mpz_t coef, polynomialMV_mpz* output)
+
+
+ same as polynomialMV_*_append
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomialMV_int_append_noinit(array_int factor, int coef, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_append_noinit(array_int factor, mpz_t coef, polynomialMV_mpz* output)
+
+
+ same as polynomialMV_*_append
except that the new factor and coefficient to be appended to the polynomial pointed to by output
are not allocated. Instead, pointers to factor
and coef
are appended. factor
and coef
must therefore not be freed (since they will be freed when output
is). Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomialMV_int_append_noinitfactor(array_int factor, int coef, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_append_noinitfactor(array_int factor, mpz_t coef, polynomialMV_mpz* output)
+
+
+ same as polynomialMV_*_append
except that the new factor to be appended to the polynomial pointed to by output
is not allocated. Instead, a pointer to factor
is appended. factor
must therefore not be freed (since it will be freed when output
is). coef
is, however, copied and must be freed. Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomialMV_int_append_noinit_inplace(array_int factor, int coef, polynomial* output)
+
+
+
+ polynomialMV_mpz_append_noinit_inplace(array_int factor, mpz_t coef, polynomial* output)
+
+
+ same as polynomialMV_*_append_noinit
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomialMV_int_append_noinit_inplace(array_int factor, int coef, polynomial* output)
+
+
+
+ polynomialMV_mpz_append_noinit_inplace(array_int factor, mpz_t coef, polynomial* output)
+
+
+ same as polynomialMV_*_append_noinitfactor
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
+
+
+ -
+
+
+ polynomialMV_int_add(polynomialMV_int poly1, polynomialMV_int poly2, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_add(polynomialMV_mpz poly1, polynomialMV_mpz poly2, polynomialMV_mpz* output)
+
+
+ add poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomialMV_int_add_inplace(polynomialMV_int poly1, polynomialMV_int* poly2)
+
+
+
+ polynomialMV_mpz_add_inplace(polynomialMV_mpz poly1, polynomialMV_mpz* poly2)
+
+
+ add poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
+
+
+ -
+
+
+ polynomialMV_int_multiply_scalar(polynomial poly, int num)
+
+
+
+ polynomialMV_mpz_multiply_scalar(polynomial poly, mpz_t num)
+
+
+ multiply num
and poly
and write the result to poly
.
+
+
+ -
+
+
+ polynomialMV_int_prod(polynomialMV_int poly1, polynomialMV_int poly2, polynomialMV_int* output)
+
+
+
+ polynomialMV_mpz_prod(polynomialMV_mpz poly1, polynomialMV_mpz poly2, polynomialMV_mpz* output)
+
+
+ multiply poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
+ polynomialMV_int_order(polynomialMV_int poly)
+
+
+
+ polynomialMV_mpz_order(polynomialMV_mpz poly)
+
+
+ order the factors in poly
, using array_*_sort.
+
+
+ -
+
+
+ polynomialMV_int_print(polynomialMV_int poly)
+
+
+
+ polynomialMV_mpz_print(polynomialMV_mpz poly)
+
+
+ print poly
.
+
+
+
+
+ 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
+
+
+ -
+
fprint_double(FILE* file, double x)
+ print x
to file
, with as many digits as possible.
+
+ -
+
fprint_ldouble(FILE* file, long double x)
+ print x
to file
, with as many digits as possible.
+
+ -
+
fprint_mpfr(FILE* file, mpfr_t x)
+ print x
to file
, with as many digits as allowed by the MPFR precision.
+
+ -
+
print_datatype_info()
+ print miscellaneous information about integers and floats. Namely, the number of bits used in char
, short int
, int
, long int
and long long int
; the precision and maximal and minimal exponents of double
and long double
; the type used to store the precision and emax of MPFR floats.
+
+
+
+
+ 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:
+
+ - Compute roots of smooth real functions numerically using the Newton-Raphson algorithm.
+ - Compute definite integrals of smooth real functions numerically using Gauss-Legendre quadratures.
+
+ In addition, libinum defines the following structures:
+
+ - Variable-length arrays.
+ - Single variable and multivariate polynomials.
+
+
+
+
+ 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.
+
+
+
+
+ -
+
+
+
+
+ int root_newton_${TYPENAME}(
+
+
+ ${FLOATTYPE}* out,
+ int (*func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ int (*deriv_func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ ${FLOATTYPE} init,
+ ${FLOATTYPE} tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+ ");
+ }
+ ?>
+
+
+
+ Description: Compute a numerical approximation for the root.
+
+
+ Arguments:
+
+ -
+
out
: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an mpfr_t
), then it must be initialized.
+
+ -
+
func
: pointer to the function that computes \(f(x)\). It must be in the format specified in functions.
+
+ -
+
deriv_func
: pointer to the function that computes the derivative of \(f\). It must be in the format specified in functions.
+
+ -
+
init
: the value of \(x_0\).
+
+ -
+
tolerance
: the algorithm halts when \(|x_{n+1}-x_n|\leqslant\)tolerance
. tolerance
thus provides control over the error of the algorithm.
+
+ -
+
maxiter
: maximum number of iterations. The algorithm gives up if this number is reached and throws a LIBINUM_ERROR_MAXITER
exception.
+
+ -
+
extra_args
: pointer to the extra arguments to be passed to the functions *func
and *deriv_func
when they are evaluated.
+
+
+
+ Return value: returns 0
on success, and
+
+ LIBINUM_ERROR_MAXITER
if the maximal number of iterations was reached.
+ LIBINUM_ERROR_NAN
if any of the evaluations of *func
or *deriv_func
returned nan
or infinity
, or if any of the evaluations of *deriv_func
returned 0
.
+
+
+
+
+ -
+
+
+
+
+ int root_newton_inplace_${TYPENAME}(
+
+
+ ${FLOATTYPE}* out,
+ int (*func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ int (*deriv_func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ ${FLOATTYPE} tolerance,
+ int maxiter,
+ void* extra_args)
+
+
+
+ ");
+ }
+ ?>
+
+
+
+ Description: Similar to root_newton_*
except that the value of \(x_0\) is passed to the function in the argument out
.
+
+
+
+ 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
+
+
+
+ -
+
+
+
+
+ int integrate_gauss_${TYPENAME}(
+
+
+ ${FLOATTYPE}* out,
+ int (*func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ ${FLOATTYPE} lower,
+ ${FLOATTYPE} upper,
+ array_${TYPENAME} abcissa,
+ array_${TYPENAME} weights,
+ void* extra_args)
+
+
+
+ ");
+ }
+ ?>
+
+
+
+ Description: Compute a numerical approximation for the integral of a real function using Gauss quadratures.
+
+
+ Arguments:
+
+ -
+
out
: pointer to the number to which the result will be written. If the number requires initialization (e.g. if it is an mpfr_t
), then it must be initialized.
+
+ -
+
func
: pointer to the function that computes the integrand \(f(x)\). It must be in the format specified in functions.
+
+ -
+
lower
: lower bound of the integration.
+
+ -
+
upper
: upper bound of the integration.
+
+ -
+
abcissa
: the abcissa used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function gauss_legendre_weights
.
+
+ -
+
weights
: the weights used to compute the integral using Gauss quadratures. For Gauss-Legendre integration, they can be computed by the function gauss_legendre_weights
.
+
+ -
+
extra_args
: pointer to the extra arguments to be passed to the function *func
when it is evaluated (see functions).
+
+
+
+ Return value: returns 0
on success, and
+
+ LIBINUM_ERROR_NAN
if any of the evaluations of *func
returned nan
or infinity
.
+ LIBINUM_ERROR_SIZE_MISMATCH
if the lengths of the vectors abcissa
and weights
are different.
+
+
+
+
+ -
+
+
+
+
+ int integrate_gauss_multithread_${TYPENAME}(
+
+
+ ${FLOATTYPE}* out,
+ int (*func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ ${FLOATTYPE} lower,
+ ${FLOATTYPE} upper,
+ array_${TYPENAME} abcissa,
+ array_${TYPENAME} weights,
+ void* extra_args,
+ unsigned int threads,
+ array_pthread_t* thread_ids)
+
+
+
+ ");
+ }
+ ?>
+
+
+ Description: Multithreaded version of integrate_gauss_*
, in which function calls are performed in parallel.
+
+
+ Arguments: Same as integrate_gauss_*
, except for
+
+ -
+
threads
: number of threads to use for the computation.
+
+ -
+
thread_ids
: pointer to an array in which the id of each thread is written to. The array must not be initialized. This array can then be used by each thread to identify themselves.
+
+
+
+ Return value: Same as integrate_gauss_*
.
+
+
+
+ -
+
+
+
+
+ int integrate_gauss_smarttmp_${TYPENAME}(
+
+
+ ${FLOATTYPE}* out,
+ int (*func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ ${FLOATTYPE} lower,
+ ${FLOATTYPE} upper,
+ array_${TYPENAME} abcissa,
+ array_${TYPENAME} weights,
+ array_${TYPENAME}* tmps,
+ void* extra_args)
+
+
+
+ ");
+ }
+ ?>
+
+
+ Description: Similar to integrate_gauss_*
, except that, if temporary floats are needed during the computation, they are saved to an array so that they can be re-used later on. This is useful to perform repeated integrations, in which it would be costly to re-allocate memory for temporary floats. When using this function, temporary floats are allocated as needed, but they are not discarded.
+
+
+ Arguments: Same as integrate_gauss_*
, except for
+
+ -
+
tmps
: pointer to an array that is used to store temporary floats. The array must be initialized beforehand. When temporary floats are needed, the array is checked for available floats. If enough of them are already present in the array, then the routine uses them, if not, it enlarges the array and allocates as many extra floats as is needed. The floats in the array can then be re-used for other purposes.
+
+
+
+ Return value: Same as integrate_gauss_*
.
+
+
+
+ -
+
+
+
+
+ int integrate_gauss_smarttmp_multithread_${TYPENAME}(
+
+
+ ${FLOATTYPE}* out,
+ int (*func)(${FLOATTYPE}*, ${FLOATTYPE}, void*),
+ ${FLOATTYPE} lower,
+ ${FLOATTYPE} upper,
+ array_${TYPENAME} abcissa,
+ array_${TYPENAME} weights,
+ void* extra_args,
+ unsigned int threads,
+ array_pthread_t* thread_ids)
+
+
+
+ ");
+ }
+ ?>
+
+
+ Description: Multithreaded version of integrate_gauss_smarttmp_*
, in which function calls are performed in parallel.
+
+
+ Arguments: Same as integrate_gauss_smarttmp_*
, except for
+
+ -
+
threads
: number of threads to use for the computation.
+
+ -
+
thread_ids
: pointer to an array in which the id of each thread is written to. The array must not be initialized. This array can then be used by each thread to identify themselves.
+
+
+
+ Return value: Same as integrate_gauss_*
.
+
+
+
+
+ -
+
+
+
+
+ gauss_legendre_weights_${TYPENAME}(
+
+
+ unsigned int order,
+ ${FLOATTYPE} tolerance,
+ unsigned int maxiter,
+ array_${TYPENAME}* abcissa,
+ array_${TYPENAME}* weights)
+
+
+
+ ");
+ }
+ ?>
+
+
+ Description: Compute the Gauss-Legendre abcissa and weights.
+
+
+ Arguments:
+
+ -
+
order
: the order \(N\) of the integration.
+
+ -
+
tolerance
: tolerance for the Newton algorithm used to compute the roots of the Legendre polynomial (see root_newton
).
+
+ -
+
maxiter
: the maximal number of steps to compute before giving up.
+
+ -
+
abcissa
: pointer to the array in which to write the abcissa. It must not be initialized.
+
+ -
+
weights
: pointer to the array in which to write the weights. It must not be initialized.
+
+
+
+ Return value: returns 0
on success, and
+
+ LIBINUM_ERROR_MAXITER
if, when computing the roots of the Legendre polynomial using a Newton iteration, the maximal number of iterations was reached.
+
+
+
+
+ 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:
+
+
+
+ array_${TYPENAME}
:
+ { ${TYPE}* values, unsigned int length, unsigned int memory }
+
+ ");
+ }
+ ?>
+
+ where the keys correspond to
+
+ values
: the array in which the objects are stored
+ length
: the number of elements in the array
+ memory
: the number of elements allocated to the array
+
+ 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
+
+ -
+
+
int array_${TYPENAME}_init(array_${TYPENAME}* array, unsigned int memory)
+
+ ");
+ }
+ ?>
+
+ initialize the array pointed to by array
, and allocate memory
elements.
+
+
+ -
+
+
int array_${TYPENAME}_free(array_${TYPENAME} array)
+
+ ");
+ }
+ ?>
+
+ free the array array
.
+
+
+ -
+
+
int array_${TYPENAME}_free_vects(array_${TYPENAME} array)
+
+ ");
+ }
+ ?>
+
+ free the memory pointed to by array.values
, but do not free the elements of the array.
+
+
+ -
+
+
int array_${TYPENAME}_resize(array_${TYPENAME}* array, unsigned int newsize)
+
+ ");
+ }
+ ?>
+
+ copy the array pointed to by array
to another with memory newsize
.
+
+
+ -
+
+
int array_${TYPENAME}_append(${TYPE} value, array_${TYPENAME}* array)
+
+ ");
+ }
+ ?>
+
+ append value
to the end of the array pointed to by array
. Resize the array if needed.
+
+
+ -
+
+
int array_${TYPENAME}_append_noinit(${TYPE} value, array_${TYPENAME}* array)
+
+ ");
+ }
+ ?>
+
+ append value
to the end of the array pointed to by array
. Do not initialize the new value, instead, copy value
to the end of *array
. value
must not be freed, since it will be freed when *array
is. Resize the array if needed.
+
+
+ -
+
+
int array_${TYPENAME}_concat(array_${TYPENAME} input, array_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ append the values in input
to the end of the array pointed to by output
. Resize the array if needed.
+
+
+ -
+
+
int array_${TYPENAME}_cpy(array_${TYPENAME} input, array_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ copy input
to the array pointed to by output
. Initializes *output
.
+
+
+ -
+
+
int array_${TYPENAME}_cpy_noinit(array_${TYPENAME} input, array_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ copy input
to the array pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
+
+
+ -
+
+
int array_${TYPENAME}_subarray(array_${TYPENAME} array, unsigned int begin, unsigned int end, array_${TYPENAME}* subarray)
+
+ ");
+ }
+ ?>
+
+ extract the sub-array of array
whose indices are larger or equal to begin
and smaller or equal to end
. Returns LIBINUM_ERROR_ILLEGAL_MEMORY_ACCESS
if the subarray does not exist.
+
+
+ -
+
+
int array_${TYPENAME}_print(array_${TYPENAME} array)
+
+ ");
+ }
+ ?>
+
+ print the elements of array
.
+
+
+
+ Extra functions for arrays of ordered objects
+ In addition, we define the following ordering for the possible objects in an array:
+
+ - the obvious ordering for
int, unsigned int, double, long double, mpfr, char
+ - lexicographical ordering for
array_char
+ - no ordering for
polynomial_*
.
+
+ If the ordering is defined, then the following functions are defined:
+
+
+ -
+
+
int array_${TYPENAME}_append_unique(${TYPE} value, array_${TYPENAME}* array)
+
+ ");
+ }
+ ?>
+
+ if value
is not already present in the array pointed to by array
, then append it. Resize the array if needed.
+
+
+ -
+
+
int array_${TYPENAME}_concat_unique(array_${TYPENAME} input, array_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ append the values in input
that are not already present in the array pointed to by output
. Resize the array if needed.
+
+
+ -
+
+
int array_${TYPENAME}_find(${TYPE} val, array_${TYPENAME} array)
+
+ ");
+ }
+ ?>
+
+ search for val
in array
. Returns the index of val
in array
if it is present, and -1
if it is not.
+
+
+ -
+
+
int array_${TYPENAME}_sort(array_${TYPENAME} array)
+
+ ");
+ }
+ ?>
+
+ sort the elements of array
from smallest to largest (for numerical values, the ordering relation is the usual one, for characters and strings, the lexicographical ordering is used). The sorting is performed in place. The quicksort algorithm is used.
+
+
+ -
+
+
int array_${TYPENAME}_sort_sub(array_${TYPENAME} array, unsigned int begin, unsigned int end)
+
+ ");
+ }
+ ?>
+
+ sort the elements of the sub-array of array
consisting of the elements whose index is larger or equal than begin
and smaller or equal than end
. The sorting is performed in place. The quicksort algorithm is used.
+
+
+ -
+
+
int array_${TYPENAME}_cmp(array_${TYPENAME} array1, array_${TYPENAME} array2)
+
+ ");
+ }
+ ?>
+
+ compare array1
and array2
. Returns 0
if both arrays are equal (that is, if their elements are the same, and in the same order), -1
if array1
is smaller than array2
in lexicographical order, and 1
if array1
is smaller than array2
in lexicographical order.
+
+
+
+ Extra functions for array_char
+ In addition, the following functions are defined for array_char
.
+
+ -
+
int array_char_append_str(char* str, array_char* output)
+ append the characters in str
to the end of the array pointed to by output
. Resize the array if needed.
+
+ -
+
int array_char_to_str(array_char input, char** output)
+ copy the string in input
to the string pointed to by output
. Initializes *output
.
+
+ -
+
char* array_char_to_str(array_char* input)
+ convert the array pointed to by input
to a string, and return it. Appends '\0'
at the end of *input
. The string thus generated should not be freed, since it is actually a pointer to input->str
, which is freed when *input
is.
+
+ -
+
int str_to_array_char(char* str, array_char* output)
+ copy the string input
to the array pointed to by output
. Initializes *output
.
+
+ -
+
int array_char_cmp_str(array_char array_char, char* str)
+ compare the string in array_char
and the string str
. Returns 1
if the strings are identical, and 0
if not.
+
+ -
+
int array_char_snprintf(array_char* output, char* fmt)
+ reads the format fmt
and appends the output to the array pointed to by output
. The format should follow the specifications of the standard C function printf
.
+
+
+
+
+ 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:
+
+
+
+
+
+ polynomial_${TYPENAME}
:
+ { array_${COEFTYPENAME} coefficients, array_${EXPTYPENAME} orders }
+
+ ");
+ }
+ ?>
+
+ where the keys correspond to
+
+ coefficients
: array of coefficients
+ orders
: array of exponents.
+
+ polynomial_*
's must be initialized before they are used, and freed when they are no longer needed.
+
+ Functions
+
+ -
+
+
polynomial_${TYPENAME}_init(polynomial_${TYPENAME}* poly, unsigned int memory)
+
+ ");
+ }
+ ?>
+
+ initialize the polynomial pointed to by poly
, and allocate memory
terms.
+
+
+ -
+
+
polynomial_${TYPENAME}_free(polynomial_${TYPENAME} poly)
+
+ ");
+ }
+ ?>
+
+ free the polynomial poly
.
+
+
+ -
+
+
polynomial_${TYPENAME}_resize(polynomial_${TYPENAME}* poly, unsigned int newsize)
+
+ ");
+ }
+ ?>
+
+ copy the array polynomial to by poly
to another with memory newsize
.
+
+
+ -
+
+
polynomial_${TYPENAME}_add_monomial(${COEFTYPE} val, ${EXPTYPE} order, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ add a term with coefficient val
and exponent order
to the polynomial pointed to by output
. Resize the polynomial if needed.
+
+
+ -
+
+
polynomial_${TYPENAME}_add_monomial_dui(double val, unsigned int order, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ add a term with coefficient val
(converted from a double
) and exponent order
(converted from an unsigned int
) to the polynomial pointed to by output
. Resize the polynomial if needed.
+
+
+ -
+
+
polynomial_${TYPENAME}_cpy(polynomial_${TYPENAME} input, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ copy input
to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomial_${TYPENAME}_cpy_noinit(polynomial_${TYPENAME} input, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ copy input
to the polynomial pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
+
+
+ -
+
+
polynomial_${TYPENAME}_add(polynomial poly1, polynomial_${TYPENAME} poly2, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ add poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomial_${TYPENAME}_add_inplace(polynomial_${TYPENAME} poly1, polynomial_${TYPENAME}* poly2)
+
+ ");
+ }
+ ?>
+
+ add poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
+
+
+ -
+
+
polynomial_${TYPENAME}_mul_scalar(${COEFTYPE} x, polynomial_${TYPENAME} poly, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ multiply x
and poly
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomial_${TYPENAME}_mul_scalar_inplace(${COEFTYPE} x, polynomial_${TYPENAME}* poly)
+
+ ");
+ }
+ ?>
+
+ multiply x
and *poly
and write the result to the polynomial pointed to by poly
.
+
+
+ -
+
+
polynomial_${TYPENAME}_mul_monomial(${COEFTYPE} x, ${EXPTYPE} order, polynomial_${TYPENAME} poly, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ multiply poly
and the monomial whose coefficient and exponent are x
and order
, and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomial_${TYPENAME}_mul_monomial_inplace(${COEFTYPE} x, ${EXPTYPE} order, polynomial_${TYPENAME}* poly)
+
+ ");
+ }
+ ?>
+
+ multiply *poly
and the monomial whose coefficient and exponent are x
and order
, and write the result to the polynomial pointed to by poly
.
+
+
+ -
+
+
polynomial_${TYPENAME}_mul(polynomial_${TYPENAME} poly1, polynomial_${TYPENAME} poly2, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ multiply poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomial_${TYPENAME}_mul_inplace(polynomial_${TYPENAME} poly1, polynomial_${TYPENAME}* poly2)
+
+ ");
+ }
+ ?>
+
+ multiply poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
+
+
+ -
+
+
polynomial_${TYPENAME}_derive(polynomial_${TYPENAME} poly, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ derive poly
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomial_${TYPENAME}_derive_inplace(polynomial_${TYPENAME}* poly)
+
+ ");
+ }
+ ?>
+
+ derive *poly
and write the result to the polynomial pointed to by poly
.
+
+
+ -
+
+
polynomial_${TYPENAME}_evaluate(${COEFTYPE}* out, ${COEFTYPE} x, polynomial_${TYPENAME} poly)
+
+ ");
+ }
+ ?>
+
+ evaluate poly
at x
, and write the result to out
. out
must be initialized if its type requires it.
+
+
+ -
+
+
polynomial_${TYPENAME}_print(polynomial_${TYPENAME} poly)
+
+ ");
+ }
+ ?>
+
+ print poly
.
+
+
+ -
+
+
polynomial_${TYPENAME}_legendre(unsigned int n, polynomial_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ compute the n
-th Legendre polynomial, and write the output to the polynomial pointed to by poly
. Initializes *poly
.
+
+
+
+ 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:
+
+
+ polynomialMV_${TYPENAME}
:
+ { ${COEFTYPE}* coefficients, array_${INDEXTYPENAME} factors, unsigned int length, unsigned int memory }
+
+ ");
+ }
+ ?>
+
+ where the keys correspond to
+
+ coefficients
: array of coefficients
+ factors
: array of factors, i.e. arrays of indices of variables
+ length
: the number of terms in the polynomial
+ memory
: the number of terms allocated to the polynomial
+
+ 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
+
+ -
+
+
polynomialMV_${TYPENAME}_init(polynomialMV_${TYPENAME}* poly, unsigned int memory)
+
+ ");
+ }
+ ?>
+
+ initialize the polynomial pointed to by poly
, and allocate memory
terms.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_free(polynomialMV_${TYPENAME} poly)
+
+ ");
+ }
+ ?>
+
+ free the polynomial poly
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_resize(polynomialMV_${TYPENAME}* poly, unsigned int newsize)
+
+ ");
+ }
+ ?>
+
+ copy the array polynomial to by poly
to another with memory newsize
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_cpy(polynomialMV_${TYPENAME} input, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ copy input
to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_cpy_noinit(polynomialMV_${TYPENAME} input, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ copy input
to the polynomial pointed to by output
. Does not initialize *output
, so *output
must be initialized ahead of time, and its memory must be larger or equal to the length of input
. Returns LIBINUM_ERROR_SIZE_MISMATCH
if the memory of *output
is smaller than the length of input
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_append(array_${INDEXTYPENAME} factor, ${COEFTYPE} coef, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ add a term with coefficient coef
and factor factor
to the polynomial pointed to by output
. Resize the polynomial if needed.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_append_inplace(array_${INDEXTYPENAME} factor, ${COEFTYPE} coef, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ same as polynomialMV_*_append
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_append_noinit(array_${INDEXTYPENAME} factor, ${COEFTYPE} coef, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ same as polynomialMV_*_append
except that the new factor and coefficient to be appended to the polynomial pointed to by output
are not allocated. Instead, pointers to factor
and coef
are appended. factor
and coef
must therefore not be freed (since they will be freed when output
is). Resize the polynomial if needed.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_append_noinitfactor(array_${INDEXTYPENAME} factor, ${COEFTYPE} coef, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ same as polynomialMV_*_append
except that the new factor to be appended to the polynomial pointed to by output
is not allocated. Instead, a pointer to factor
is appended. factor
must therefore not be freed (since it will be freed when output
is). coef
is, however, copied and must be freed. Resize the polynomial if needed.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_append_noinit_inplace(array_${INDEXTYPENAME} factor, ${COEFTYPE} coef, polynomial* output)
+
+ ");
+ }
+ ?>
+
+ same as polynomialMV_*_append_noinit
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_append_noinit_inplace(array_${INDEXTYPENAME} factor, ${COEFTYPE} coef, polynomial* output)
+
+ ");
+ }
+ ?>
+
+ same as polynomialMV_*_append_noinitfactor
except that if the factor is already present in the polynomial pointed to by output
, then add coef
to the coefficient of that factor, instead of appending it to the end of the polynomial. Resize the polynomial if needed.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_add(polynomialMV_${TYPENAME} poly1, polynomialMV_${TYPENAME} poly2, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ add poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_add_inplace(polynomialMV_${TYPENAME} poly1, polynomialMV_${TYPENAME}* poly2)
+
+ ");
+ }
+ ?>
+
+ add poly1
and *poly2
and write the result to the polynomial pointed to by poly2
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_multiply_scalar(polynomial poly, ${COEFTYPE} num)
+
+ ");
+ }
+ ?>
+
+ multiply num
and poly
and write the result to poly
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_prod(polynomialMV_${TYPENAME} poly1, polynomialMV_${TYPENAME} poly2, polynomialMV_${TYPENAME}* output)
+
+ ");
+ }
+ ?>
+
+ multiply poly1
and poly2
and write the result to the polynomial pointed to by output
. Initializes *output
.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_order(polynomialMV_${TYPENAME} poly)
+
+ ");
+ }
+ ?>
+
+ order the factors in poly
, using array_*_sort.
+
+
+ -
+
+
polynomialMV_${TYPENAME}_print(polynomialMV_${TYPENAME} poly)
+
+ ");
+ }
+ ?>
+
+ print poly
.
+
+
+
+
+ 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
+
+
+ -
+
fprint_double(FILE* file, double x)
+ print x
to file
, with as many digits as possible.
+
+ -
+
fprint_ldouble(FILE* file, long double x)
+ print x
to file
, with as many digits as possible.
+
+ -
+
fprint_mpfr(FILE* file, mpfr_t x)
+ print x
to file
, with as many digits as allowed by the MPFR precision.
+
+ -
+
print_datatype_info()
+ print miscellaneous information about integers and floats. Namely, the number of bits used in char
, short int
, int
, long int
and long long int
; the precision and maximal and minimal exponents of double
and long double
; the type used to store the precision and emax of MPFR floats.
+
+
+
+
+ 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-70-g09d2